developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (C) 2023 MediaTek Inc. |
| 4 | * |
| 5 | * Author: Chris.Chou <chris.chou@mediatek.com> |
| 6 | * Ren-Ting Wang <ren-ting.wang@mediatek.com> |
| 7 | */ |
| 8 | |
| 9 | #include <crypto/aes.h> |
| 10 | #include <crypto/hash.h> |
| 11 | #include <crypto/hmac.h> |
| 12 | #include <crypto/md5.h> |
| 13 | #include <linux/delay.h> |
developer | d690dc9 | 2024-08-19 14:31:18 +0800 | [diff] [blame] | 14 | #include <linux/jiffies.h> |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 15 | #include <crypto/internal/hash.h> |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 16 | |
| 17 | #include <crypto-eip/ddk/slad/api_pcl.h> |
| 18 | #include <crypto-eip/ddk/slad/api_pcl_dtl.h> |
| 19 | #include <crypto-eip/ddk/slad/api_pec.h> |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 20 | |
| 21 | #include "crypto-eip/crypto-eip.h" |
| 22 | #include "crypto-eip/ddk-wrapper.h" |
| 23 | #include "crypto-eip/internal.h" |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 24 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 25 | void crypto_free_sa(void *sa_pointer, int ring) |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 26 | { |
| 27 | DMABuf_Handle_t SAHandle = {0}; |
| 28 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 29 | if (ring < 0) |
| 30 | return; |
| 31 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 32 | SAHandle.p = sa_pointer; |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 33 | PEC_SA_UnRegister(ring, SAHandle, DMABuf_NULLHandle, |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 34 | DMABuf_NULLHandle); |
| 35 | DMABuf_Release(SAHandle); |
| 36 | } |
| 37 | |
| 38 | void crypto_free_token(void *token) |
| 39 | { |
| 40 | DMABuf_Handle_t TokenHandle = {0}; |
| 41 | |
| 42 | TokenHandle.p = token; |
| 43 | DMABuf_Release(TokenHandle); |
| 44 | } |
| 45 | |
| 46 | /* TODO: to be remove*/ |
| 47 | void crypto_free_pkt(void *pkt) |
| 48 | { |
| 49 | DMABuf_Handle_t PktHandle = {0}; |
| 50 | |
| 51 | PktHandle.p = pkt; |
| 52 | DMABuf_Release(PktHandle); |
| 53 | } |
| 54 | |
| 55 | void crypto_free_sglist(void *sglist) |
| 56 | { |
| 57 | PEC_Status_t res; |
| 58 | unsigned int count; |
| 59 | unsigned int size; |
| 60 | DMABuf_Handle_t SGListHandle = {0}; |
| 61 | DMABuf_Handle_t ParticleHandle = {0}; |
| 62 | int i; |
| 63 | uint8_t *Particle_p; |
| 64 | |
| 65 | SGListHandle.p = sglist; |
| 66 | res = PEC_SGList_GetCapacity(SGListHandle, &count); |
| 67 | if (res != PEC_STATUS_OK) |
| 68 | return; |
| 69 | for (i = 0; i < count; i++) { |
| 70 | PEC_SGList_Read(SGListHandle, |
| 71 | i, |
| 72 | &ParticleHandle, |
| 73 | &size, |
| 74 | &Particle_p); |
| 75 | DMABuf_Particle_Release(ParticleHandle); |
| 76 | } |
| 77 | |
| 78 | PEC_SGList_Destroy(SGListHandle); |
| 79 | } |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 80 | |
| 81 | static bool crypto_iotoken_create(IOToken_Input_Dscr_t * const dscr_p, |
| 82 | void * const ext_p, u32 *data_p, |
| 83 | PEC_CommandDescriptor_t * const pec_cmd_dscr) |
| 84 | { |
| 85 | int IOTokenRc; |
| 86 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 87 | dscr_p->InPacket_ByteCount = pec_cmd_dscr->SrcPkt_ByteCount; |
| 88 | dscr_p->Ext_p = ext_p; |
| 89 | |
| 90 | IOTokenRc = IOToken_Create(dscr_p, data_p); |
| 91 | if (IOTokenRc < 0) { |
| 92 | CRYPTO_ERR("IOToken_Create error %d\n", IOTokenRc); |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | pec_cmd_dscr->InputToken_p = data_p; |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | unsigned int crypto_pe_busy_get_one(IOToken_Output_Dscr_t *const OutTokenDscr_p, |
| 102 | u32 *OutTokenData_p, |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 103 | PEC_ResultDescriptor_t *RD_p, int ring) |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 104 | { |
| 105 | int LoopCounter = MTK_EIP197_INLINE_NOF_TRIES; |
| 106 | int IOToken_Rc; |
| 107 | PEC_Status_t pecres; |
| 108 | |
| 109 | ZEROINIT(*OutTokenDscr_p); |
| 110 | ZEROINIT(*RD_p); |
| 111 | |
| 112 | /* Link data structures */ |
| 113 | RD_p->OutputToken_p = OutTokenData_p; |
| 114 | |
| 115 | while (LoopCounter > 0) { |
| 116 | /* Try to get the processed packet from the driver */ |
| 117 | unsigned int Counter = 0; |
| 118 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 119 | pecres = PEC_Packet_Get(ring, RD_p, 1, &Counter); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 120 | if (pecres != PEC_STATUS_OK) { |
| 121 | /* IO error */ |
| 122 | CRYPTO_ERR("PEC_Packet_Get error %d\n", pecres); |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | if (Counter) { |
| 127 | IOToken_Rc = IOToken_Parse(OutTokenData_p, OutTokenDscr_p); |
| 128 | if (IOToken_Rc < 0) { |
| 129 | /* IO error */ |
| 130 | CRYPTO_ERR("IOToken_Parse error %d\n", IOToken_Rc); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | if (OutTokenDscr_p->ErrorCode != 0) { |
| 135 | /* Packet process error */ |
| 136 | CRYPTO_ERR("Result descriptor error 0x%x\n", |
| 137 | OutTokenDscr_p->ErrorCode); |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | /* packet received */ |
| 142 | return Counter; |
| 143 | } |
| 144 | |
| 145 | /* Wait for MTK_EIP197_PKT_GET_TIMEOUT_MS milliseconds */ |
| 146 | udelay(MTK_EIP197_PKT_GET_TIMEOUT_MS); |
| 147 | LoopCounter--; |
| 148 | } |
| 149 | |
| 150 | /* IO error (timeout, not result packet received) */ |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | unsigned int crypto_pe_get_one(IOToken_Output_Dscr_t *const OutTokenDscr_p, |
| 155 | u32 *OutTokenData_p, |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 156 | PEC_ResultDescriptor_t *RD_p, int ring) |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 157 | { |
| 158 | int IOToken_Rc; |
| 159 | unsigned int Counter = 0; |
| 160 | PEC_Status_t pecres; |
| 161 | |
| 162 | ZEROINIT(*OutTokenDscr_p); |
| 163 | ZEROINIT(*RD_p); |
| 164 | |
| 165 | RD_p->OutputToken_p = OutTokenData_p; |
| 166 | |
| 167 | /* Try to get the processed packet from the driver */ |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 168 | pecres = PEC_Packet_Get(ring, RD_p, 1, &Counter); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 169 | if (pecres != PEC_STATUS_OK) { |
| 170 | /* IO error */ |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 171 | CRYPTO_ERR("PEC_Packet_Get error %d, ring id %d\n", pecres, ring); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | if (Counter) { |
| 176 | IOToken_Rc = IOToken_Parse(OutTokenData_p, OutTokenDscr_p); |
| 177 | if (IOToken_Rc < 0) { |
| 178 | /* IO error */ |
| 179 | CRYPTO_ERR("IOToken_Parse error %d\n", IOToken_Rc); |
| 180 | return 0; |
| 181 | } |
| 182 | if (OutTokenDscr_p->ErrorCode != 0) { |
| 183 | /* Packet process error */ |
| 184 | CRYPTO_ERR("Result descriptor error 0x%x\n", |
| 185 | OutTokenDscr_p->ErrorCode); |
| 186 | return 0; |
| 187 | } |
| 188 | /* packet received */ |
| 189 | return Counter; |
| 190 | } |
| 191 | |
| 192 | /* IO error (timeout, not result packet received) */ |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | SABuilder_Crypto_Mode_t lookaside_match_alg_mode(enum mtk_crypto_cipher_mode mode) |
| 197 | { |
| 198 | switch (mode) { |
| 199 | case MTK_CRYPTO_MODE_CBC: |
| 200 | return SAB_CRYPTO_MODE_CBC; |
| 201 | case MTK_CRYPTO_MODE_ECB: |
| 202 | return SAB_CRYPTO_MODE_ECB; |
| 203 | case MTK_CRYPTO_MODE_OFB: |
| 204 | return SAB_CRYPTO_MODE_OFB; |
| 205 | case MTK_CRYPTO_MODE_CFB: |
| 206 | return SAB_CRYPTO_MODE_CFB; |
| 207 | case MTK_CRYPTO_MODE_CTR: |
| 208 | return SAB_CRYPTO_MODE_CTR; |
| 209 | case MTK_CRYPTO_MODE_GCM: |
| 210 | return SAB_CRYPTO_MODE_GCM; |
| 211 | case MTK_CRYPTO_MODE_GMAC: |
| 212 | return SAB_CRYPTO_MODE_GMAC; |
| 213 | case MTK_CRYPTO_MODE_CCM: |
| 214 | return SAB_CRYPTO_MODE_CCM; |
| 215 | default: |
| 216 | return SAB_CRYPTO_MODE_BASIC; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | SABuilder_Crypto_t lookaside_match_alg_name(enum mtk_crypto_alg alg) |
| 221 | { |
| 222 | switch (alg) { |
| 223 | case MTK_CRYPTO_AES: |
| 224 | return SAB_CRYPTO_AES; |
| 225 | case MTK_CRYPTO_DES: |
| 226 | return SAB_CRYPTO_DES; |
| 227 | case MTK_CRYPTO_3DES: |
| 228 | return SAB_CRYPTO_3DES; |
| 229 | default: |
| 230 | return SAB_CRYPTO_NULL; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | SABuilder_Auth_t aead_hash_match(enum mtk_crypto_alg alg) |
| 235 | { |
| 236 | switch (alg) { |
| 237 | case MTK_CRYPTO_ALG_SHA1: |
| 238 | return SAB_AUTH_HMAC_SHA1; |
| 239 | case MTK_CRYPTO_ALG_SHA224: |
| 240 | return SAB_AUTH_HMAC_SHA2_224; |
| 241 | case MTK_CRYPTO_ALG_SHA256: |
| 242 | return SAB_AUTH_HMAC_SHA2_256; |
| 243 | case MTK_CRYPTO_ALG_SHA384: |
| 244 | return SAB_AUTH_HMAC_SHA2_384; |
| 245 | case MTK_CRYPTO_ALG_SHA512: |
| 246 | return SAB_AUTH_HMAC_SHA2_512; |
| 247 | case MTK_CRYPTO_ALG_MD5: |
| 248 | return SAB_AUTH_HMAC_MD5; |
| 249 | case MTK_CRYPTO_ALG_GCM: |
| 250 | return SAB_AUTH_AES_GCM; |
| 251 | case MTK_CRYPTO_ALG_GMAC: |
| 252 | return SAB_AUTH_AES_GMAC; |
| 253 | case MTK_CRYPTO_ALG_CCM: |
| 254 | return SAB_AUTH_AES_CCM; |
| 255 | default: |
| 256 | return SAB_AUTH_NULL; |
| 257 | } |
| 258 | } |
| 259 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 260 | void mtk_crypto_ring3_handler(void) |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 261 | { |
| 262 | struct mtk_crypto_result *rd; |
| 263 | struct mtk_crypto_context *ctx; |
| 264 | IOToken_Output_Dscr_t OutTokenDscr; |
| 265 | PEC_ResultDescriptor_t Res; |
| 266 | uint32_t OutputToken[IOTOKEN_OUT_WORD_COUNT]; |
| 267 | int ret = 0; |
| 268 | |
| 269 | while (true) { |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 270 | spin_lock_bh(&priv->mtk_eip_ring[3].ring_lock); |
| 271 | if (list_empty(&priv->mtk_eip_ring[3].list)) { |
| 272 | spin_unlock_bh(&priv->mtk_eip_ring[3].ring_lock); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 273 | return; |
developer | 0bed96a | 2024-04-01 19:03:52 +0800 | [diff] [blame] | 274 | } |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 275 | rd = list_first_entry(&priv->mtk_eip_ring[3].list, struct mtk_crypto_result, list); |
| 276 | spin_unlock_bh(&priv->mtk_eip_ring[3].ring_lock); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 277 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 278 | ctx = crypto_tfm_ctx(rd->async->tfm); |
| 279 | if (crypto_pe_get_one(&OutTokenDscr, OutputToken, &Res, ctx->ring) < 1) { |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 280 | PEC_NotifyFunction_t CBFunc; |
| 281 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 282 | CBFunc = mtk_crypto_ring3_handler; |
| 283 | if (OutTokenDscr.ErrorCode == 0) |
| 284 | continue; |
| 285 | else if (OutTokenDscr.ErrorCode & BIT(9)) |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 286 | ret = -EBADMSG; |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 287 | else if (OutTokenDscr.ErrorCode == 0x4003) |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 288 | ret = 0; |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 289 | else |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 290 | ret = 1; |
| 291 | |
| 292 | CRYPTO_ERR("error from crypto_pe_get_one: %d\n", ret); |
| 293 | } |
| 294 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 295 | ret = ctx->handle_result(rd, ret); |
| 296 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 297 | spin_lock_bh(&priv->mtk_eip_ring[3].ring_lock); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 298 | list_del(&rd->list); |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 299 | spin_unlock_bh(&priv->mtk_eip_ring[3].ring_lock); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 300 | kfree(rd); |
| 301 | } |
| 302 | } |
| 303 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 304 | void mtk_crypto_ring2_handler(void) |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 305 | { |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 306 | struct mtk_crypto_result *rd; |
| 307 | struct mtk_crypto_context *ctx; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 308 | IOToken_Output_Dscr_t OutTokenDscr; |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 309 | PEC_ResultDescriptor_t Res; |
| 310 | uint32_t OutputToken[IOTOKEN_OUT_WORD_COUNT]; |
| 311 | int ret = 0; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 312 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 313 | while (true) { |
| 314 | spin_lock_bh(&priv->mtk_eip_ring[2].ring_lock); |
| 315 | if (list_empty(&priv->mtk_eip_ring[2].list)) { |
| 316 | spin_unlock_bh(&priv->mtk_eip_ring[2].ring_lock); |
| 317 | return; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 318 | } |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 319 | rd = list_first_entry(&priv->mtk_eip_ring[2].list, struct mtk_crypto_result, list); |
| 320 | spin_unlock_bh(&priv->mtk_eip_ring[2].ring_lock); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 321 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 322 | ctx = crypto_tfm_ctx(rd->async->tfm); |
| 323 | if (crypto_pe_get_one(&OutTokenDscr, OutputToken, &Res, ctx->ring) < 1) { |
| 324 | PEC_NotifyFunction_t CBFunc; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 325 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 326 | CBFunc = mtk_crypto_ring2_handler; |
| 327 | if (OutTokenDscr.ErrorCode == 0) |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 328 | continue; |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 329 | else if (OutTokenDscr.ErrorCode & BIT(9)) |
| 330 | ret = -EBADMSG; |
| 331 | else if (OutTokenDscr.ErrorCode == 0x4003) |
| 332 | ret = 0; |
| 333 | else |
| 334 | ret = 1; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 335 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 336 | CRYPTO_ERR("error from crypto_pe_get_one: %d\n", ret); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 337 | } |
| 338 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 339 | ret = ctx->handle_result(rd, ret); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 340 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 341 | spin_lock_bh(&priv->mtk_eip_ring[2].ring_lock); |
| 342 | list_del(&rd->list); |
| 343 | spin_unlock_bh(&priv->mtk_eip_ring[2].ring_lock); |
| 344 | kfree(rd); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 345 | } |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 346 | } |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 347 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 348 | void mtk_crypto_ring1_handler(void) |
| 349 | { |
| 350 | struct mtk_crypto_result *rd; |
| 351 | struct mtk_crypto_context *ctx; |
| 352 | IOToken_Output_Dscr_t OutTokenDscr; |
| 353 | PEC_ResultDescriptor_t Res; |
| 354 | uint32_t OutputToken[IOTOKEN_OUT_WORD_COUNT]; |
| 355 | int ret = 0; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 356 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 357 | while (true) { |
| 358 | spin_lock_bh(&priv->mtk_eip_ring[1].ring_lock); |
| 359 | if (list_empty(&priv->mtk_eip_ring[1].list)) { |
| 360 | spin_unlock_bh(&priv->mtk_eip_ring[1].ring_lock); |
| 361 | return; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 362 | } |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 363 | rd = list_first_entry(&priv->mtk_eip_ring[1].list, struct mtk_crypto_result, list); |
| 364 | spin_unlock_bh(&priv->mtk_eip_ring[1].ring_lock); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 365 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 366 | ctx = crypto_tfm_ctx(rd->async->tfm); |
| 367 | if (crypto_pe_get_one(&OutTokenDscr, OutputToken, &Res, ctx->ring) < 1) { |
| 368 | PEC_NotifyFunction_t CBFunc; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 369 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 370 | CBFunc = mtk_crypto_ring1_handler; |
| 371 | if (OutTokenDscr.ErrorCode == 0) |
| 372 | continue; |
| 373 | else if (OutTokenDscr.ErrorCode & BIT(9)) |
| 374 | ret = -EBADMSG; |
| 375 | else if (OutTokenDscr.ErrorCode == 0x4003) |
| 376 | ret = 0; |
| 377 | else |
| 378 | ret = 1; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 379 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 380 | CRYPTO_ERR("error from crypto_pe_get_one: %d\n", ret); |
| 381 | } |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 382 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 383 | ret = ctx->handle_result(rd, ret); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 384 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 385 | spin_lock_bh(&priv->mtk_eip_ring[1].ring_lock); |
| 386 | list_del(&rd->list); |
| 387 | spin_unlock_bh(&priv->mtk_eip_ring[1].ring_lock); |
| 388 | kfree(rd); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 389 | } |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 390 | } |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 391 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 392 | void mtk_crypto_ring0_handler(void) |
| 393 | { |
| 394 | struct mtk_crypto_result *rd; |
| 395 | struct mtk_crypto_context *ctx; |
| 396 | IOToken_Output_Dscr_t OutTokenDscr; |
| 397 | PEC_ResultDescriptor_t Res; |
| 398 | uint32_t OutputToken[IOTOKEN_OUT_WORD_COUNT]; |
| 399 | int ret = 0; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 400 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 401 | while (true) { |
| 402 | spin_lock_bh(&priv->mtk_eip_ring[0].ring_lock); |
| 403 | if (list_empty(&priv->mtk_eip_ring[0].list)) { |
| 404 | spin_unlock_bh(&priv->mtk_eip_ring[0].ring_lock); |
| 405 | return; |
| 406 | } |
| 407 | rd = list_first_entry(&priv->mtk_eip_ring[0].list, struct mtk_crypto_result, list); |
| 408 | spin_unlock_bh(&priv->mtk_eip_ring[0].ring_lock); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 409 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 410 | ctx = crypto_tfm_ctx(rd->async->tfm); |
| 411 | if (crypto_pe_get_one(&OutTokenDscr, OutputToken, &Res, ctx->ring) < 1) { |
| 412 | PEC_NotifyFunction_t CBFunc; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 413 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 414 | CBFunc = mtk_crypto_ring0_handler; |
| 415 | if (OutTokenDscr.ErrorCode == 0) |
| 416 | continue; |
| 417 | else if (OutTokenDscr.ErrorCode & BIT(9)) |
| 418 | ret = -EBADMSG; |
| 419 | else if (OutTokenDscr.ErrorCode == 0x4003) |
| 420 | ret = 0; |
| 421 | else |
| 422 | ret = 1; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 423 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 424 | CRYPTO_ERR("error from crypto_pe_get_one: %d\n", ret); |
| 425 | } |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 426 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 427 | ret = ctx->handle_result(rd, ret); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 428 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 429 | spin_lock_bh(&priv->mtk_eip_ring[0].ring_lock); |
| 430 | list_del(&rd->list); |
| 431 | spin_unlock_bh(&priv->mtk_eip_ring[0].ring_lock); |
| 432 | kfree(rd); |
| 433 | } |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 434 | } |
| 435 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 436 | void (*mtk_crypto_interrupt_handler[])(void) = { |
| 437 | mtk_crypto_ring0_handler, |
| 438 | mtk_crypto_ring1_handler, |
| 439 | mtk_crypto_ring2_handler, |
| 440 | mtk_crypto_ring3_handler |
| 441 | }; |
| 442 | |
developer | d690dc9 | 2024-08-19 14:31:18 +0800 | [diff] [blame] | 443 | void mtk_crypto_req_expired_timer(struct timer_list *t) |
| 444 | { |
| 445 | struct mtk_crypto_cipher_ctx *ctx = from_timer(ctx, t, poll_timer); |
| 446 | PEC_NotifyFunction_t CBFunc; |
| 447 | int ring = ctx->base.ring; |
| 448 | int rc; |
| 449 | |
| 450 | del_timer(&ctx->poll_timer); |
| 451 | ctx->poll_timer.expires = 0; |
| 452 | |
| 453 | CBFunc = mtk_crypto_interrupt_handler[ring]; |
| 454 | rc = PEC_ResultNotify_Request(ring, CBFunc, 1); |
| 455 | if (rc != PEC_STATUS_OK) |
| 456 | CRYPTO_ERR("PEC_ResultNotify_Request failed with rc = %d\n", rc); |
| 457 | } |
| 458 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 459 | int mtk_crypto_ddk_alloc_buff(struct mtk_crypto_cipher_ctx *ctx, int dir, unsigned int digestsize, |
| 460 | struct mtk_crypto_engine_data *data) |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 461 | { |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 462 | SABuilder_Params_t params; |
| 463 | SABuilder_Params_Basic_t ProtocolParams; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 464 | |
| 465 | DMABuf_Status_t DMAStatus; |
| 466 | DMABuf_Properties_t DMAProperties = {0, 0, 0, 0}; |
| 467 | DMABuf_HostAddress_t SAHostAddress; |
| 468 | DMABuf_HostAddress_t TokenHostAddress; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 469 | |
| 470 | DMABuf_Handle_t SAHandle = {0}; |
| 471 | DMABuf_Handle_t TokenHandle = {0}; |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 472 | unsigned int SAWords = 0; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 473 | |
| 474 | unsigned int TCRWords = 0; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 475 | unsigned int TokenMaxWords = 0; |
| 476 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 477 | IOToken_Input_Dscr_t InTokenDscr; |
| 478 | IOToken_Output_Dscr_t OutTokenDscr; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 479 | void *InTokenDscrExt_p = NULL; |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 480 | int rc; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 481 | |
| 482 | #ifdef CRYPTO_IOTOKEN_EXT |
| 483 | IOToken_Input_Dscr_Ext_t InTokenDscrExt; |
| 484 | |
| 485 | ZEROINIT(InTokenDscrExt); |
| 486 | InTokenDscrExt_p = &InTokenDscrExt; |
| 487 | #endif |
| 488 | ZEROINIT(InTokenDscr); |
| 489 | ZEROINIT(OutTokenDscr); |
| 490 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 491 | if (dir == MTK_CRYPTO_ENCRYPT) |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 492 | rc = SABuilder_Init_Basic(¶ms, &ProtocolParams, SAB_DIRECTION_OUTBOUND); |
| 493 | else |
| 494 | rc = SABuilder_Init_Basic(¶ms, &ProtocolParams, SAB_DIRECTION_INBOUND); |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 495 | |
| 496 | if (data->sa_handle) { |
| 497 | crypto_free_sa(data->sa_handle, ctx->base.ring); |
| 498 | crypto_free_token(data->token_handle); |
| 499 | kfree(data->token_context); |
| 500 | } |
| 501 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 502 | if (rc) { |
| 503 | CRYPTO_ERR("SABuilder_Init_Basic failed: %d\n", rc); |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 504 | return rc; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /* Build SA */ |
| 508 | params.CryptoAlgo = lookaside_match_alg_name(ctx->alg); |
| 509 | params.CryptoMode = lookaside_match_alg_mode(ctx->mode); |
| 510 | params.KeyByteCount = ctx->key_len; |
| 511 | params.Key_p = (uint8_t *) ctx->key; |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 512 | if (params.CryptoMode == SAB_CRYPTO_MODE_GCM && ctx->aead == EIP197_AEAD_TYPE_IPSEC_ESP) { |
| 513 | params.Nonce_p = (uint8_t *) &ctx->nonce; |
| 514 | params.IVSrc = SAB_IV_SRC_TOKEN; |
| 515 | params.flags |= SAB_FLAG_COPY_IV; |
| 516 | } else if (params.CryptoMode == SAB_CRYPTO_MODE_GMAC) { |
| 517 | params.Nonce_p = (uint8_t *) &ctx->nonce; |
| 518 | params.IVSrc = SAB_IV_SRC_TOKEN; |
| 519 | } else if (params.CryptoMode == SAB_CRYPTO_MODE_GCM) { |
| 520 | params.IVSrc = SAB_IV_SRC_TOKEN; |
| 521 | } else if (params.CryptoMode == SAB_CRYPTO_MODE_CCM) { /* Todo, use token for ccm */ |
| 522 | params.IVSrc = SAB_IV_SRC_TOKEN; |
| 523 | params.Nonce_p = (uint8_t *) &ctx->nonce + 1; |
| 524 | } else { |
| 525 | params.IVSrc = SAB_IV_SRC_TOKEN; |
| 526 | } |
| 527 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 528 | if (params.CryptoMode == SAB_CRYPTO_MODE_CTR) |
| 529 | params.Nonce_p = (uint8_t *) &ctx->nonce; |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 530 | |
| 531 | params.AuthAlgo = aead_hash_match(ctx->hash_alg); |
| 532 | params.AuthKey1_p = (uint8_t *) ctx->ipad; |
| 533 | params.AuthKey2_p = (uint8_t *) ctx->opad; |
| 534 | |
| 535 | ProtocolParams.ICVByteCount = digestsize; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 536 | |
| 537 | rc = SABuilder_GetSizes(¶ms, &SAWords, NULL, NULL); |
| 538 | if (rc) { |
| 539 | CRYPTO_ERR("SA not created because of size errors: %d\n", rc); |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 540 | return rc; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 541 | } |
| 542 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 543 | data->sa_size = SAWords; |
| 544 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 545 | DMAProperties.fCached = true; |
| 546 | DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 547 | DMAProperties.Bank = MTK_EIP197_INLINE_BANK_TRANSFORM; |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 548 | DMAProperties.Size = MAX(4 * SAWords, 256); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 549 | |
| 550 | DMAStatus = DMABuf_Alloc(DMAProperties, &SAHostAddress, &SAHandle); |
| 551 | if (DMAStatus != DMABUF_STATUS_OK) { |
| 552 | rc = 1; |
| 553 | CRYPTO_ERR("Allocation of SA failed: %d\n", DMAStatus); |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 554 | return rc; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 555 | } |
| 556 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 557 | rc = SABuilder_BuildSA(¶ms, (u32 *) SAHostAddress.p, NULL, NULL); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 558 | if (rc) { |
| 559 | CRYPTO_ERR("SA not created because of errors: %d\n", rc); |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 560 | goto release_sa; |
| 561 | } |
| 562 | |
| 563 | rc = PEC_SA_Register(ctx->base.ring, SAHandle, DMABuf_NULLHandle, |
| 564 | DMABuf_NULLHandle); |
| 565 | if (rc != PEC_STATUS_OK) { |
| 566 | CRYPTO_ERR("PEC_SA_Register failed: %d\n", rc); |
| 567 | goto release_sa; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 568 | } |
| 569 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 570 | data->sa_addr = SAHostAddress.p; |
| 571 | data->sa_handle = SAHandle.p; |
| 572 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 573 | /* Build Token */ |
| 574 | rc = TokenBuilder_GetContextSize(¶ms, &TCRWords); |
| 575 | if (rc) { |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 576 | CRYPTO_ERR("TokenBuilder_GetContextSize returned errors: f%d\n", rc); |
| 577 | goto release_sa; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 578 | } |
| 579 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 580 | data->token_context = kmalloc(4 * TCRWords, GFP_KERNEL); |
| 581 | if (!data->token_context) { |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 582 | rc = 1; |
| 583 | CRYPTO_ERR("Allocation of TCR failed\n"); |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 584 | goto release_sa; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 585 | } |
| 586 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 587 | rc = TokenBuilder_BuildContext(¶ms, data->token_context); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 588 | if (rc) { |
| 589 | CRYPTO_ERR("TokenBuilder_BuildContext failed: %d\n", rc); |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 590 | goto release_context; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 591 | } |
| 592 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 593 | rc = TokenBuilder_GetSize(data->token_context, &TokenMaxWords); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 594 | if (rc) { |
| 595 | CRYPTO_ERR("TokenBuilder_GetSize failed: %d\n", rc); |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 596 | goto release_context; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 597 | } |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 598 | data->token_size = TokenMaxWords; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 599 | |
| 600 | DMAProperties.fCached = true; |
| 601 | DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 602 | DMAProperties.Bank = MTK_EIP197_INLINE_BANK_TOKEN; |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 603 | DMAProperties.Size = 4 * TokenMaxWords; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 604 | |
| 605 | DMAStatus = DMABuf_Alloc(DMAProperties, &TokenHostAddress, &TokenHandle); |
| 606 | if (DMAStatus != DMABUF_STATUS_OK) { |
| 607 | rc = 1; |
| 608 | CRYPTO_ERR("Allocation of token builder failed: %d\n", DMAStatus); |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 609 | goto release_context; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 610 | } |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 611 | data->token_addr = TokenHostAddress.p; |
| 612 | data->token_handle = TokenHandle.p; |
| 613 | data->valid = 1; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 614 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 615 | return rc; |
| 616 | |
| 617 | release_context: |
| 618 | kfree(data->token_context); |
| 619 | release_sa: |
| 620 | DMABuf_Release(SAHandle); |
| 621 | |
| 622 | return rc; |
| 623 | } |
| 624 | |
| 625 | int mtk_crypto_basic_cipher(struct crypto_async_request *async, |
| 626 | struct mtk_crypto_cipher_req *mtk_req, struct scatterlist *src, |
| 627 | struct scatterlist *dst, unsigned int cryptlen, |
| 628 | unsigned int assoclen, unsigned int digestsize, u8 *iv, |
| 629 | unsigned int ivsize) |
| 630 | { |
| 631 | struct mtk_crypto_cipher_ctx *ctx = crypto_tfm_ctx(async->tfm); |
| 632 | struct mtk_crypto_engine_data *data; |
| 633 | struct mtk_crypto_result *result; |
| 634 | struct scatterlist *sg; |
| 635 | unsigned int totlen_src; |
| 636 | unsigned int totlen_dst; |
| 637 | unsigned int src_pkt = cryptlen + assoclen; |
| 638 | unsigned int pass_assoc = 0; |
| 639 | int pass_id; |
| 640 | int rc; |
| 641 | int i; |
| 642 | int ring = ctx->base.ring; |
| 643 | |
| 644 | DMABuf_Properties_t DMAProperties = {0, 0, 0, 0}; |
| 645 | |
| 646 | DMABuf_Handle_t SAHandle = {0}; |
| 647 | DMABuf_Handle_t TokenHandle = {0}; |
| 648 | DMABuf_Handle_t SrcSGListHandle = {0}; |
| 649 | DMABuf_Handle_t DstSGListHandle = {0}; |
| 650 | |
| 651 | unsigned int TokenWords = 0; |
| 652 | unsigned int TokenHeaderWord; |
| 653 | |
| 654 | TokenBuilder_Params_t TokenParams; |
| 655 | PEC_CommandDescriptor_t Cmd; |
| 656 | PEC_NotifyFunction_t CBFunc; |
| 657 | unsigned int count; |
| 658 | |
| 659 | IOToken_Input_Dscr_t InTokenDscr; |
| 660 | IOToken_Output_Dscr_t OutTokenDscr; |
| 661 | uint32_t InputToken[IOTOKEN_IN_WORD_COUNT]; |
| 662 | void *InTokenDscrExt_p = NULL; |
| 663 | uint8_t token_iv[16] = {0}; |
| 664 | uint8_t *aad = NULL; |
| 665 | |
| 666 | #ifdef CRYPTO_IOTOKEN_EXT |
| 667 | IOToken_Input_Dscr_Ext_t InTokenDscrExt; |
| 668 | |
| 669 | ZEROINIT(InTokenDscrExt); |
| 670 | InTokenDscrExt_p = &InTokenDscrExt; |
| 671 | #endif |
| 672 | ZEROINIT(InTokenDscr); |
| 673 | ZEROINIT(OutTokenDscr); |
| 674 | |
| 675 | /* Init SA */ |
| 676 | if (mtk_req->direction == MTK_CRYPTO_ENCRYPT) { |
| 677 | totlen_src = cryptlen + assoclen; |
| 678 | totlen_dst = totlen_src + digestsize; |
| 679 | data = &ctx->enc; |
| 680 | } else { |
| 681 | totlen_src = cryptlen + assoclen; |
| 682 | totlen_dst = totlen_src - digestsize; |
| 683 | data = &ctx->dec; |
| 684 | } |
| 685 | |
| 686 | SAHandle.p = data->sa_handle; |
| 687 | TokenHandle.p = data->token_handle; |
| 688 | |
| 689 | if ((ctx->mode == MTK_CRYPTO_MODE_GCM && ctx->aead == EIP197_AEAD_TYPE_IPSEC_ESP) |
| 690 | || ctx->mode == MTK_CRYPTO_MODE_GMAC) { |
| 691 | memcpy(token_iv, &ctx->nonce, 4); |
| 692 | memcpy(token_iv + 4, iv, ivsize); |
| 693 | token_iv[15] = 1; |
| 694 | } else if (ctx->mode == MTK_CRYPTO_MODE_GCM) { |
| 695 | memcpy(token_iv, iv, ivsize); |
| 696 | token_iv[15] = 1; |
| 697 | } else if (ctx->mode == MTK_CRYPTO_MODE_CCM) { |
| 698 | memcpy(token_iv, (uint8_t *) &ctx->nonce, 4); |
| 699 | memcpy(token_iv + 4, iv, ivsize); |
| 700 | token_iv[15] = 0; |
| 701 | } else if (ctx->mode == MTK_CRYPTO_MODE_CTR) { |
| 702 | memcpy(token_iv, &ctx->nonce, 4); |
| 703 | memcpy(token_iv + 4, iv, ivsize); |
| 704 | token_iv[15] = 1; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 705 | } |
| 706 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 707 | /* Check dst buffer has enough size */ |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 708 | mtk_req->nr_src = sg_nents_for_len(src, totlen_src); |
| 709 | mtk_req->nr_dst = sg_nents_for_len(dst, totlen_dst); |
| 710 | |
| 711 | if (src == dst) { |
| 712 | mtk_req->nr_src = max(mtk_req->nr_src, mtk_req->nr_dst); |
| 713 | mtk_req->nr_dst = mtk_req->nr_src; |
| 714 | if (unlikely((totlen_src || totlen_dst) && (mtk_req->nr_src <= 0))) { |
| 715 | CRYPTO_ERR("In-place buffer not large enough\n"); |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 716 | goto error_remove_sg; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 717 | } |
| 718 | dma_map_sg(crypto_dev, src, mtk_req->nr_src, DMA_BIDIRECTIONAL); |
| 719 | } else { |
| 720 | if (unlikely(totlen_src && (mtk_req->nr_src <= 0))) { |
| 721 | CRYPTO_ERR("Source buffer not large enough\n"); |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 722 | goto error_remove_sg; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 723 | } |
| 724 | dma_map_sg(crypto_dev, src, mtk_req->nr_src, DMA_TO_DEVICE); |
| 725 | |
| 726 | if (unlikely(totlen_dst && (mtk_req->nr_dst <= 0))) { |
| 727 | CRYPTO_ERR("Dest buffer not large enough\n"); |
| 728 | dma_unmap_sg(crypto_dev, src, mtk_req->nr_src, DMA_TO_DEVICE); |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 729 | goto error_remove_sg; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 730 | } |
| 731 | dma_map_sg(crypto_dev, dst, mtk_req->nr_dst, DMA_FROM_DEVICE); |
| 732 | } |
| 733 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 734 | if (ctx->mode == MTK_CRYPTO_MODE_CCM || |
| 735 | (ctx->mode == MTK_CRYPTO_MODE_GCM && ctx->aead == EIP197_AEAD_TYPE_IPSEC_ESP)) { |
| 736 | |
| 737 | aad = kmalloc(assoclen, GFP_KERNEL); |
| 738 | if (!aad) |
| 739 | goto error_remove_sg; |
| 740 | sg_copy_to_buffer(src, mtk_req->nr_src, aad, assoclen); |
| 741 | src_pkt -= assoclen; |
| 742 | pass_assoc = assoclen; |
| 743 | } |
| 744 | |
| 745 | /* Assign sg list */ |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 746 | rc = PEC_SGList_Create(MAX(mtk_req->nr_src, 1), &SrcSGListHandle); |
| 747 | if (rc != PEC_STATUS_OK) { |
| 748 | CRYPTO_ERR("PEC_SGList_Create src failed with rc = %d\n", rc); |
| 749 | goto error_remove_sg; |
| 750 | } |
| 751 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 752 | pass_id = 0; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 753 | DMAProperties.fCached = true; |
| 754 | DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 755 | DMAProperties.Bank = MTK_EIP197_INLINE_BANK_PACKET; |
| 756 | for_each_sg(src, sg, mtk_req->nr_src, i) { |
| 757 | int len = sg_dma_len(sg); |
| 758 | DMABuf_Handle_t sg_handle; |
| 759 | DMABuf_HostAddress_t host; |
| 760 | |
| 761 | if (totlen_src < len) |
| 762 | len = totlen_src; |
| 763 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 764 | if (pass_assoc) { |
| 765 | if (pass_assoc >= len) { |
| 766 | pass_assoc -= len; |
| 767 | pass_id++; |
| 768 | continue; |
| 769 | } |
| 770 | DMAProperties.Size = MAX(len - pass_assoc, 1); |
| 771 | rc = DMABuf_Particle_Alloc(DMAProperties, sg_dma_address(sg) + pass_assoc, |
| 772 | &host, &sg_handle); |
| 773 | if (rc != DMABUF_STATUS_OK) { |
| 774 | CRYPTO_ERR("DMABuf_Particle_Alloc failed rc = %d\n", rc); |
| 775 | goto error_remove_sg; |
| 776 | } |
| 777 | rc = PEC_SGList_Write(SrcSGListHandle, i - pass_id, sg_handle, |
| 778 | len - pass_assoc); |
| 779 | if (rc != PEC_STATUS_OK) |
| 780 | pr_notice("PEC_SGList_Write failed rc = %d\n", rc); |
| 781 | pass_assoc = 0; |
| 782 | } else { |
| 783 | DMAProperties.Size = MAX(len, 1); |
| 784 | rc = DMABuf_Particle_Alloc(DMAProperties, sg_dma_address(sg), |
| 785 | &host, &sg_handle); |
| 786 | if (rc != DMABUF_STATUS_OK) { |
| 787 | CRYPTO_ERR("DMABuf_Particle_Alloc failed rc = %d\n", rc); |
| 788 | goto error_remove_sg; |
| 789 | } |
| 790 | |
| 791 | rc = PEC_SGList_Write(SrcSGListHandle, i - pass_id, sg_handle, len); |
| 792 | if (rc != PEC_STATUS_OK) |
| 793 | pr_notice("PEC_SGList_Write failed rc = %d\n", rc); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 794 | } |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 795 | |
| 796 | totlen_src -= len; |
| 797 | if (!totlen_src) |
| 798 | break; |
| 799 | } |
| 800 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 801 | /* Alloc sg list for result */ |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 802 | rc = PEC_SGList_Create(MAX(mtk_req->nr_dst, 1), &DstSGListHandle); |
| 803 | if (rc != PEC_STATUS_OK) { |
| 804 | CRYPTO_ERR("PEC_SGList_Create dst failed with rc = %d\n", rc); |
| 805 | goto error_remove_sg; |
| 806 | } |
| 807 | |
| 808 | for_each_sg(dst, sg, mtk_req->nr_dst, i) { |
| 809 | int len = sg_dma_len(sg); |
| 810 | DMABuf_Handle_t sg_handle; |
| 811 | DMABuf_HostAddress_t host; |
| 812 | |
| 813 | if (len > totlen_dst) |
| 814 | len = totlen_dst; |
| 815 | |
| 816 | DMAProperties.Size = MAX(len, 1); |
| 817 | rc = DMABuf_Particle_Alloc(DMAProperties, sg_dma_address(sg), &host, &sg_handle); |
| 818 | if (rc != DMABUF_STATUS_OK) { |
| 819 | CRYPTO_ERR("DMABuf_Particle_Alloc failed rc = %d\n", rc); |
| 820 | goto error_remove_sg; |
| 821 | } |
| 822 | rc = PEC_SGList_Write(DstSGListHandle, i, sg_handle, len); |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 823 | if (rc != PEC_STATUS_OK) |
| 824 | pr_notice("PEC_SGList_Write failed rc = %d\n", rc); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 825 | |
| 826 | if (unlikely(!len)) |
| 827 | break; |
| 828 | totlen_dst -= len; |
| 829 | } |
| 830 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 831 | /* Build Token */ |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 832 | ZEROINIT(TokenParams); |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 833 | |
| 834 | if (ctx->mode == MTK_CRYPTO_MODE_GCM || ctx->mode == MTK_CRYPTO_MODE_GMAC || |
| 835 | ctx->mode == MTK_CRYPTO_MODE_CCM || ctx->mode == MTK_CRYPTO_MODE_CTR) |
| 836 | TokenParams.IV_p = token_iv; |
| 837 | else |
| 838 | TokenParams.IV_p = iv; |
| 839 | |
| 840 | if ((ctx->mode == MTK_CRYPTO_MODE_GCM && ctx->aead == EIP197_AEAD_TYPE_IPSEC_ESP) || |
| 841 | ctx->mode == MTK_CRYPTO_MODE_CCM) { |
| 842 | TokenParams.AdditionalValue = assoclen - ivsize; |
| 843 | TokenParams.AAD_p = aad; |
| 844 | } else if (ctx->mode != MTK_CRYPTO_MODE_GMAC) |
| 845 | TokenParams.AdditionalValue = assoclen; |
| 846 | |
| 847 | rc = TokenBuilder_BuildToken(data->token_context, aad, src_pkt, |
| 848 | &TokenParams, (uint32_t *) data->token_addr, |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 849 | &TokenWords, &TokenHeaderWord); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 850 | if (rc != TKB_STATUS_OK) { |
| 851 | CRYPTO_ERR("Token builder failed: %d\n", rc); |
| 852 | goto error_remove_sg; |
| 853 | } |
| 854 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 855 | if (ctx->mode == MTK_CRYPTO_MODE_CBC && |
| 856 | mtk_req->direction == MTK_CRYPTO_DECRYPT) |
| 857 | sg_pcopy_to_buffer(src, mtk_req->nr_src, iv, ivsize, cryptlen - ivsize); |
| 858 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 859 | ZEROINIT(Cmd); |
| 860 | Cmd.Token_Handle = TokenHandle; |
| 861 | Cmd.Token_WordCount = TokenWords; |
| 862 | Cmd.SrcPkt_Handle = SrcSGListHandle; |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 863 | Cmd.SrcPkt_ByteCount = src_pkt; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 864 | Cmd.DstPkt_Handle = DstSGListHandle; |
| 865 | Cmd.SA_Handle1 = SAHandle; |
| 866 | Cmd.SA_Handle2 = DMABuf_NULLHandle; |
| 867 | |
| 868 | #if defined(CRYPTO_IOTOKEN_EXT) |
| 869 | InTokenDscrExt.HW_Services = IOTOKEN_CMD_PKT_LAC; |
| 870 | #endif |
| 871 | InTokenDscr.TknHdrWordInit = TokenHeaderWord; |
| 872 | |
| 873 | if (!crypto_iotoken_create(&InTokenDscr, |
| 874 | InTokenDscrExt_p, |
| 875 | InputToken, |
| 876 | &Cmd)) { |
| 877 | rc = 1; |
| 878 | goto error_remove_sg; |
| 879 | } |
| 880 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 881 | rc = PEC_Packet_Put(ring, &Cmd, 1, &count); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 882 | if (rc != PEC_STATUS_OK && count != 1) { |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 883 | goto error_remove_sg; |
| 884 | } |
| 885 | |
| 886 | result = kmalloc(sizeof(struct mtk_crypto_result), GFP_KERNEL); |
| 887 | if (!result) { |
| 888 | rc = 1; |
| 889 | CRYPTO_ERR("No memory for result\n"); |
| 890 | goto error_remove_sg; |
| 891 | } |
| 892 | INIT_LIST_HEAD(&result->list); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 893 | result->eip.pkt_handle = SrcSGListHandle.p; |
| 894 | result->async = async; |
| 895 | result->dst = DstSGListHandle.p; |
developer | d690dc9 | 2024-08-19 14:31:18 +0800 | [diff] [blame] | 896 | CBFunc = mtk_crypto_interrupt_handler[ring]; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 897 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 898 | spin_lock_bh(&priv->mtk_eip_ring[ring].ring_lock); |
| 899 | list_add_tail(&result->list, &priv->mtk_eip_ring[ring].list); |
| 900 | spin_unlock_bh(&priv->mtk_eip_ring[ring].ring_lock); |
developer | d690dc9 | 2024-08-19 14:31:18 +0800 | [diff] [blame] | 901 | |
| 902 | if (ctx->poll_timer.expires == 0) { |
| 903 | ctx->poll_timer.expires = jiffies + (2 * HZ / 1000); |
| 904 | add_timer(&ctx->poll_timer); |
| 905 | } |
| 906 | |
| 907 | if ((atomic_inc_return(&ctx->base.req_count) % REQ_PKT_NUM) == 0) { |
| 908 | del_timer(&ctx->poll_timer); |
| 909 | ctx->poll_timer.expires = 0; |
| 910 | rc = PEC_ResultNotify_Request(ring, CBFunc, 1); |
| 911 | if (rc != PEC_STATUS_OK) { |
| 912 | CRYPTO_ERR("PEC_ResultNotify_Request failed with rc = %d\n", rc); |
| 913 | goto error_remove_sg; |
| 914 | } |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 915 | } |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 916 | |
| 917 | return rc; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 918 | |
| 919 | error_remove_sg: |
| 920 | if (src == dst) { |
| 921 | dma_unmap_sg(crypto_dev, src, mtk_req->nr_src, DMA_BIDIRECTIONAL); |
| 922 | } else { |
| 923 | dma_unmap_sg(crypto_dev, src, mtk_req->nr_src, DMA_TO_DEVICE); |
| 924 | dma_unmap_sg(crypto_dev, dst, mtk_req->nr_dst, DMA_FROM_DEVICE); |
| 925 | } |
| 926 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 927 | if (aad != NULL) |
| 928 | kfree(aad); |
| 929 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 930 | crypto_free_sglist(SrcSGListHandle.p); |
| 931 | crypto_free_sglist(DstSGListHandle.p); |
| 932 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 933 | return rc; |
| 934 | } |
| 935 | |
| 936 | SABuilder_Auth_t lookaside_match_hash(enum mtk_crypto_alg alg) |
| 937 | { |
| 938 | switch (alg) { |
| 939 | case MTK_CRYPTO_ALG_SHA1: |
| 940 | return SAB_AUTH_HASH_SHA1; |
| 941 | case MTK_CRYPTO_ALG_SHA224: |
| 942 | return SAB_AUTH_HASH_SHA2_224; |
| 943 | case MTK_CRYPTO_ALG_SHA256: |
| 944 | return SAB_AUTH_HASH_SHA2_256; |
| 945 | case MTK_CRYPTO_ALG_SHA384: |
| 946 | return SAB_AUTH_HASH_SHA2_384; |
| 947 | case MTK_CRYPTO_ALG_SHA512: |
| 948 | return SAB_AUTH_HASH_SHA2_512; |
| 949 | case MTK_CRYPTO_ALG_MD5: |
| 950 | return SAB_AUTH_HASH_MD5; |
| 951 | case MTK_CRYPTO_ALG_XCBC: |
| 952 | return SAB_AUTH_AES_XCBC_MAC; |
| 953 | case MTK_CRYPTO_ALG_CMAC_128: |
| 954 | return SAB_AUTH_AES_CMAC_128; |
| 955 | case MTK_CRYPTO_ALG_CMAC_192: |
| 956 | return SAB_AUTH_AES_CMAC_192; |
| 957 | case MTK_CRYPTO_ALG_CMAC_256: |
| 958 | return SAB_AUTH_AES_CMAC_256; |
| 959 | default: |
| 960 | return SAB_AUTH_NULL; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | int crypto_ahash_token_req(struct crypto_async_request *async, struct mtk_crypto_ahash_req *mtk_req, |
| 965 | uint8_t *Input_p, unsigned int InputByteCount, bool finish) |
| 966 | { |
| 967 | struct mtk_crypto_result *result; |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 968 | struct ahash_request *areq = ahash_request_cast(async); |
| 969 | struct mtk_crypto_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 970 | |
| 971 | DMABuf_Properties_t DMAProperties = {0, 0, 0, 0}; |
| 972 | DMABuf_HostAddress_t TokenHostAddress; |
| 973 | DMABuf_HostAddress_t PktHostAddress; |
| 974 | DMABuf_Status_t DMAStatus; |
| 975 | |
| 976 | DMABuf_Handle_t TokenHandle = {0}; |
| 977 | DMABuf_Handle_t PktHandle = {0}; |
| 978 | DMABuf_Handle_t SAHandle = {0}; |
| 979 | |
| 980 | unsigned int TokenMaxWords = 0; |
| 981 | unsigned int TokenHeaderWord; |
| 982 | unsigned int TokenWords = 0; |
| 983 | void *TCRData = 0; |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 984 | int ring = ctx->base.ring; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 985 | |
| 986 | TokenBuilder_Params_t TokenParams; |
| 987 | PEC_CommandDescriptor_t Cmd; |
| 988 | PEC_NotifyFunction_t CBFunc; |
| 989 | |
| 990 | unsigned int count; |
| 991 | int rc; |
| 992 | |
| 993 | u32 InputToken[IOTOKEN_IN_WORD_COUNT]; |
| 994 | IOToken_Output_Dscr_t OutTokenDscr; |
| 995 | IOToken_Input_Dscr_t InTokenDscr; |
| 996 | void *InTokenDscrExt_p = NULL; |
| 997 | |
| 998 | #ifdef CRYPTO_IOTOKEN_EXT |
| 999 | IOToken_Input_Dscr_Ext_t InTokenDscrExt; |
| 1000 | |
| 1001 | ZEROINIT(InTokenDscrExt); |
| 1002 | InTokenDscrExt_p = &InTokenDscrExt; |
| 1003 | #endif |
| 1004 | ZEROINIT(InTokenDscr); |
| 1005 | ZEROINIT(OutTokenDscr); |
| 1006 | |
| 1007 | TCRData = mtk_req->token_context; |
| 1008 | rc = TokenBuilder_GetSize(TCRData, &TokenMaxWords); |
| 1009 | if (rc) { |
| 1010 | CRYPTO_ERR("TokenBuilder_GetSize failed: %d\n", rc); |
| 1011 | goto error_exit; |
| 1012 | } |
| 1013 | |
| 1014 | DMAProperties.fCached = true; |
| 1015 | DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 1016 | DMAProperties.Bank = MTK_EIP197_INLINE_BANK_TOKEN; |
| 1017 | DMAProperties.Size = 4*TokenMaxWords; |
| 1018 | |
| 1019 | DMAStatus = DMABuf_Alloc(DMAProperties, &TokenHostAddress, &TokenHandle); |
| 1020 | if (DMAStatus != DMABUF_STATUS_OK) { |
| 1021 | rc = 1; |
| 1022 | CRYPTO_ERR("Allocation of token builder failed: %d\n", DMAStatus); |
| 1023 | goto error_exit; |
| 1024 | } |
| 1025 | |
| 1026 | DMAProperties.fCached = true; |
| 1027 | DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 1028 | DMAProperties.Bank = MTK_EIP197_INLINE_BANK_PACKET; |
| 1029 | DMAProperties.Size = MAX(InputByteCount, mtk_req->digest_sz); |
| 1030 | |
| 1031 | DMAStatus = DMABuf_Alloc(DMAProperties, &PktHostAddress, &PktHandle); |
| 1032 | if (DMAStatus != DMABUF_STATUS_OK) { |
| 1033 | rc = 1; |
| 1034 | CRYPTO_ERR("Allocation of source packet buffer failed: %d\n", |
| 1035 | DMAStatus); |
| 1036 | goto error_exit; |
| 1037 | } |
| 1038 | memcpy(PktHostAddress.p, Input_p, InputByteCount); |
| 1039 | |
| 1040 | ZEROINIT(TokenParams); |
| 1041 | TokenParams.PacketFlags |= TKB_PACKET_FLAG_HASHAPPEND; |
| 1042 | if (finish) |
| 1043 | TokenParams.PacketFlags |= TKB_PACKET_FLAG_HASHFINAL; |
| 1044 | |
| 1045 | rc = TokenBuilder_BuildToken(TCRData, (u8 *) PktHostAddress.p, |
| 1046 | InputByteCount, &TokenParams, |
| 1047 | (u32 *) TokenHostAddress.p, |
| 1048 | &TokenWords, &TokenHeaderWord); |
| 1049 | if (rc != TKB_STATUS_OK) { |
| 1050 | CRYPTO_ERR("Token builder failed: %d\n", rc); |
| 1051 | goto error_exit_unregister; |
| 1052 | } |
| 1053 | |
| 1054 | SAHandle.p = mtk_req->sa_pointer; |
| 1055 | ZEROINIT(Cmd); |
| 1056 | Cmd.Token_Handle = TokenHandle; |
| 1057 | Cmd.Token_WordCount = TokenWords; |
| 1058 | Cmd.SrcPkt_Handle = PktHandle; |
| 1059 | Cmd.SrcPkt_ByteCount = InputByteCount; |
| 1060 | Cmd.DstPkt_Handle = PktHandle; |
| 1061 | Cmd.SA_Handle1 = SAHandle; |
| 1062 | Cmd.SA_Handle2 = DMABuf_NULLHandle; |
| 1063 | |
| 1064 | #if defined(CRYPTO_IOTOKEN_EXT) |
| 1065 | InTokenDscrExt.HW_Services = IOTOKEN_CMD_PKT_LAC; |
| 1066 | #endif |
| 1067 | InTokenDscr.TknHdrWordInit = TokenHeaderWord; |
| 1068 | |
| 1069 | if (!crypto_iotoken_create(&InTokenDscr, |
| 1070 | InTokenDscrExt_p, |
| 1071 | InputToken, |
| 1072 | &Cmd)) { |
| 1073 | rc = 1; |
| 1074 | goto error_exit_unregister; |
| 1075 | } |
| 1076 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 1077 | rc = PEC_Packet_Put(ring, &Cmd, 1, &count); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1078 | if (rc != PEC_STATUS_OK && count != 1) { |
| 1079 | rc = 1; |
| 1080 | CRYPTO_ERR("PEC_Packet_Put error: %d\n", rc); |
| 1081 | goto error_exit_unregister; |
| 1082 | } |
| 1083 | |
| 1084 | result = kmalloc(sizeof(struct mtk_crypto_result), GFP_KERNEL); |
| 1085 | if (!result) { |
| 1086 | rc = 1; |
| 1087 | CRYPTO_ERR("No memory for result\n"); |
| 1088 | goto error_exit_unregister; |
| 1089 | } |
| 1090 | INIT_LIST_HEAD(&result->list); |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 1091 | result->eip.token_handle = TokenHandle.p; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1092 | result->eip.pkt_handle = PktHandle.p; |
| 1093 | result->async = async; |
| 1094 | result->dst = PktHostAddress.p; |
| 1095 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 1096 | spin_lock_bh(&priv->mtk_eip_ring[ring].ring_lock); |
| 1097 | list_add_tail(&result->list, &priv->mtk_eip_ring[ring].list); |
| 1098 | spin_unlock_bh(&priv->mtk_eip_ring[ring].ring_lock); |
| 1099 | CBFunc = mtk_crypto_interrupt_handler[ring]; |
| 1100 | rc = PEC_ResultNotify_Request(ring, CBFunc, 1); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1101 | |
| 1102 | return rc; |
| 1103 | |
| 1104 | error_exit_unregister: |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 1105 | PEC_SA_UnRegister(ring, SAHandle, DMABuf_NULLHandle, |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1106 | DMABuf_NULLHandle); |
| 1107 | |
| 1108 | error_exit: |
| 1109 | DMABuf_Release(SAHandle); |
| 1110 | DMABuf_Release(TokenHandle); |
| 1111 | DMABuf_Release(PktHandle); |
| 1112 | |
| 1113 | if (TCRData != NULL) |
| 1114 | kfree(TCRData); |
| 1115 | |
| 1116 | return rc; |
| 1117 | } |
| 1118 | |
| 1119 | int crypto_ahash_aes_cbc(struct crypto_async_request *async, struct mtk_crypto_ahash_req *mtk_req, |
| 1120 | uint8_t *Input_p, unsigned int InputByteCount) |
| 1121 | { |
| 1122 | struct mtk_crypto_ahash_ctx *ctx = crypto_tfm_ctx(async->tfm); |
| 1123 | struct mtk_crypto_result *result; |
| 1124 | SABuilder_Params_Basic_t ProtocolParams; |
| 1125 | SABuilder_Params_t params; |
| 1126 | unsigned int SAWords = 0; |
| 1127 | int rc; |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 1128 | int ring = ctx->base.ring; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1129 | |
| 1130 | DMABuf_Properties_t DMAProperties = {0, 0, 0, 0}; |
| 1131 | DMABuf_HostAddress_t TokenHostAddress; |
| 1132 | DMABuf_HostAddress_t PktHostAddress; |
| 1133 | DMABuf_HostAddress_t SAHostAddress; |
| 1134 | DMABuf_Status_t DMAStatus; |
| 1135 | |
| 1136 | DMABuf_Handle_t TokenHandle = {0}; |
| 1137 | DMABuf_Handle_t PktHandle = {0}; |
| 1138 | DMABuf_Handle_t SAHandle = {0}; |
| 1139 | |
| 1140 | unsigned int TokenMaxWords = 0; |
| 1141 | unsigned int TokenHeaderWord; |
| 1142 | unsigned int TokenWords = 0; |
| 1143 | unsigned int TCRWords = 0; |
| 1144 | void *TCRData = 0; |
| 1145 | |
| 1146 | TokenBuilder_Params_t TokenParams; |
| 1147 | PEC_CommandDescriptor_t Cmd; |
| 1148 | PEC_NotifyFunction_t CBFunc; |
| 1149 | |
| 1150 | unsigned int count; |
| 1151 | int i; |
| 1152 | |
| 1153 | u32 InputToken[IOTOKEN_IN_WORD_COUNT]; |
| 1154 | IOToken_Output_Dscr_t OutTokenDscr; |
| 1155 | IOToken_Input_Dscr_t InTokenDscr; |
| 1156 | void *InTokenDscrExt_p = NULL; |
| 1157 | |
| 1158 | #ifdef CRYPTO_IOTOKEN_EXT |
| 1159 | IOToken_Input_Dscr_Ext_t InTokenDscrExt; |
| 1160 | |
| 1161 | ZEROINIT(InTokenDscrExt); |
| 1162 | InTokenDscrExt_p = &InTokenDscrExt; |
| 1163 | #endif |
| 1164 | ZEROINIT(InTokenDscr); |
| 1165 | ZEROINIT(OutTokenDscr); |
| 1166 | |
| 1167 | if (!IS_ALIGNED(InputByteCount, 16)) { |
| 1168 | pr_notice("not aligned: %d\n", InputByteCount); |
| 1169 | return -EINVAL; |
| 1170 | } |
| 1171 | rc = SABuilder_Init_Basic(¶ms, &ProtocolParams, SAB_DIRECTION_OUTBOUND); |
| 1172 | if (rc) { |
| 1173 | CRYPTO_ERR("SABuilder_Init_Basic failed: %d\n", rc); |
| 1174 | goto error_exit; |
| 1175 | } |
| 1176 | |
| 1177 | params.CryptoAlgo = SAB_CRYPTO_AES; |
| 1178 | params.CryptoMode = SAB_CRYPTO_MODE_CBC; |
| 1179 | params.KeyByteCount = ctx->key_sz - 2 * AES_BLOCK_SIZE; |
| 1180 | params.Key_p = (uint8_t *) ctx->ipad + 2 * AES_BLOCK_SIZE; |
| 1181 | params.IVSrc = SAB_IV_SRC_SA; |
| 1182 | params.IV_p = (uint8_t *) mtk_req->state; |
| 1183 | |
| 1184 | if (ctx->alg == MTK_CRYPTO_ALG_XCBC) { |
| 1185 | for (i = 0; i < params.KeyByteCount; i = i + 4) { |
| 1186 | swap(params.Key_p[i], params.Key_p[i+3]); |
| 1187 | swap(params.Key_p[i+1], params.Key_p[i+2]); |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | rc = SABuilder_GetSizes(¶ms, &SAWords, NULL, NULL); |
| 1192 | if (rc) { |
| 1193 | CRYPTO_ERR("SA not created because of size errors: %d\n", rc); |
| 1194 | goto error_exit; |
| 1195 | } |
| 1196 | |
| 1197 | DMAProperties.fCached = true; |
| 1198 | DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 1199 | DMAProperties.Bank = MTK_EIP197_INLINE_BANK_TRANSFORM; |
| 1200 | DMAProperties.Size = MAX(4*SAWords, 256); |
| 1201 | |
| 1202 | DMAStatus = DMABuf_Alloc(DMAProperties, &SAHostAddress, &SAHandle); |
| 1203 | if (DMAStatus != DMABUF_STATUS_OK) { |
| 1204 | rc = 1; |
| 1205 | CRYPTO_ERR("Allocation of SA failed: %d\n", DMAStatus); |
| 1206 | goto error_exit; |
| 1207 | } |
| 1208 | |
| 1209 | rc = SABuilder_BuildSA(¶ms, (u32 *)SAHostAddress.p, NULL, NULL); |
| 1210 | if (rc) { |
| 1211 | CRYPTO_ERR("SA not created because of errors: %d\n", rc); |
| 1212 | goto error_exit; |
| 1213 | } |
| 1214 | |
| 1215 | rc = TokenBuilder_GetContextSize(¶ms, &TCRWords); |
| 1216 | if (rc) { |
| 1217 | CRYPTO_ERR("TokenBuilder_GetContextSize returned errors: %d\n", rc); |
| 1218 | goto error_exit; |
| 1219 | } |
| 1220 | |
| 1221 | TCRData = kmalloc(4 * TCRWords, GFP_KERNEL); |
| 1222 | if (!TCRData) { |
| 1223 | rc = 1; |
| 1224 | CRYPTO_ERR("Allocation of TCR failed\n"); |
| 1225 | goto error_exit; |
| 1226 | } |
| 1227 | |
| 1228 | rc = TokenBuilder_BuildContext(¶ms, TCRData); |
| 1229 | if (rc) { |
| 1230 | CRYPTO_ERR("TokenBuilder_BuildContext failed: %d\n", rc); |
| 1231 | goto error_exit; |
| 1232 | } |
| 1233 | |
| 1234 | rc = TokenBuilder_GetSize(TCRData, &TokenMaxWords); |
| 1235 | if (rc) { |
| 1236 | CRYPTO_ERR("TokenBuilder_GetSize failed: %d\n", rc); |
| 1237 | goto error_exit; |
| 1238 | } |
| 1239 | |
| 1240 | DMAProperties.fCached = true; |
| 1241 | DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 1242 | DMAProperties.Bank = MTK_EIP197_INLINE_BANK_TOKEN; |
| 1243 | DMAProperties.Size = 4*TokenMaxWords; |
| 1244 | |
| 1245 | DMAStatus = DMABuf_Alloc(DMAProperties, &TokenHostAddress, &TokenHandle); |
| 1246 | if (DMAStatus != DMABUF_STATUS_OK) { |
| 1247 | rc = 1; |
| 1248 | CRYPTO_ERR("Allocation of token builder failed: %d\n", DMAStatus); |
| 1249 | goto error_exit; |
| 1250 | } |
| 1251 | |
| 1252 | DMAProperties.fCached = true; |
| 1253 | DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 1254 | DMAProperties.Bank = MTK_EIP197_INLINE_BANK_PACKET; |
| 1255 | DMAProperties.Size = MAX(InputByteCount, 1); |
| 1256 | |
| 1257 | DMAStatus = DMABuf_Alloc(DMAProperties, &PktHostAddress, &PktHandle); |
| 1258 | if (DMAStatus != DMABUF_STATUS_OK) { |
| 1259 | rc = 1; |
| 1260 | CRYPTO_ERR("Allocation of source packet buffer failed: %d\n", DMAStatus); |
| 1261 | goto error_exit; |
| 1262 | } |
| 1263 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 1264 | rc = PEC_SA_Register(ring, SAHandle, DMABuf_NULLHandle, |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1265 | DMABuf_NULLHandle); |
| 1266 | if (rc != PEC_STATUS_OK) { |
| 1267 | CRYPTO_ERR("PEC_SA_Register failed: %d\n", rc); |
| 1268 | goto error_exit; |
| 1269 | } |
| 1270 | |
| 1271 | memcpy(PktHostAddress.p, Input_p, InputByteCount); |
| 1272 | |
| 1273 | ZEROINIT(TokenParams); |
| 1274 | rc = TokenBuilder_BuildToken(TCRData, (uint8_t *) PktHostAddress.p, InputByteCount, |
| 1275 | &TokenParams, (uint32_t *) TokenHostAddress.p, |
| 1276 | &TokenWords, &TokenHeaderWord); |
| 1277 | if (rc != TKB_STATUS_OK) { |
| 1278 | CRYPTO_ERR("Token builder failed: %d\n", rc); |
| 1279 | goto error_exit_unregister; |
| 1280 | } |
| 1281 | |
| 1282 | ZEROINIT(Cmd); |
| 1283 | Cmd.Token_Handle = TokenHandle; |
| 1284 | Cmd.Token_WordCount = TokenWords; |
| 1285 | Cmd.SrcPkt_Handle = PktHandle; |
| 1286 | Cmd.SrcPkt_ByteCount = InputByteCount; |
| 1287 | Cmd.DstPkt_Handle = PktHandle; |
| 1288 | Cmd.SA_Handle1 = SAHandle; |
| 1289 | Cmd.SA_Handle2 = DMABuf_NULLHandle; |
| 1290 | |
| 1291 | #if defined(CRYPTO_IOTOKEN_EXT) |
| 1292 | InTokenDscrExt.HW_Services = IOTOKEN_CMD_PKT_LAC; |
| 1293 | #endif |
| 1294 | InTokenDscr.TknHdrWordInit = TokenHeaderWord; |
| 1295 | |
| 1296 | if (!crypto_iotoken_create(&InTokenDscr, |
| 1297 | InTokenDscrExt_p, |
| 1298 | InputToken, |
| 1299 | &Cmd)) { |
| 1300 | rc = 1; |
| 1301 | goto error_exit_unregister; |
| 1302 | } |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1303 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 1304 | rc = PEC_Packet_Put(ring, &Cmd, 1, &count); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1305 | if (rc != PEC_STATUS_OK && count != 1) { |
| 1306 | rc = 1; |
| 1307 | CRYPTO_ERR("PEC_Packet_Put error: %d\n", rc); |
| 1308 | goto error_exit_unregister; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1309 | } |
| 1310 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1311 | result = kmalloc(sizeof(struct mtk_crypto_result), GFP_KERNEL); |
| 1312 | if (!result) { |
| 1313 | rc = 1; |
| 1314 | CRYPTO_ERR("No memory for result\n"); |
| 1315 | goto error_exit_unregister; |
| 1316 | } |
| 1317 | INIT_LIST_HEAD(&result->list); |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 1318 | result->eip.sa_handle = SAHandle.p; |
| 1319 | result->eip.token_handle = TokenHandle.p; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1320 | result->eip.token_context = TCRData; |
| 1321 | result->eip.pkt_handle = PktHandle.p; |
| 1322 | result->async = async; |
| 1323 | result->dst = PktHostAddress.p; |
| 1324 | result->size = InputByteCount; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1325 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 1326 | spin_lock_bh(&priv->mtk_eip_ring[ring].ring_lock); |
| 1327 | list_add_tail(&result->list, &priv->mtk_eip_ring[ring].list); |
| 1328 | spin_unlock_bh(&priv->mtk_eip_ring[ring].ring_lock); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1329 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 1330 | CBFunc = mtk_crypto_interrupt_handler[ring]; |
| 1331 | rc = PEC_ResultNotify_Request(ring, CBFunc, 1); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1332 | if (rc != PEC_STATUS_OK) { |
| 1333 | CRYPTO_ERR("PEC_ResultNotify_Request failed with rc = %d\n", rc); |
| 1334 | goto error_exit_unregister; |
| 1335 | } |
| 1336 | return 0; |
| 1337 | |
| 1338 | error_exit_unregister: |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 1339 | PEC_SA_UnRegister(ring, SAHandle, DMABuf_NULLHandle, |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1340 | DMABuf_NULLHandle); |
| 1341 | |
| 1342 | error_exit: |
| 1343 | DMABuf_Release(SAHandle); |
| 1344 | DMABuf_Release(TokenHandle); |
| 1345 | DMABuf_Release(PktHandle); |
| 1346 | |
| 1347 | if (TCRData != NULL) |
| 1348 | kfree(TCRData); |
| 1349 | |
| 1350 | return rc; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1351 | } |
| 1352 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1353 | int crypto_first_ahash_req(struct crypto_async_request *async, |
| 1354 | struct mtk_crypto_ahash_req *mtk_req, uint8_t *Input_p, |
| 1355 | unsigned int InputByteCount, bool finish) |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1356 | { |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1357 | struct mtk_crypto_ahash_ctx *ctx = crypto_tfm_ctx(async->tfm); |
| 1358 | struct mtk_crypto_result *result; |
| 1359 | SABuilder_Params_Basic_t ProtocolParams; |
| 1360 | SABuilder_Params_t params; |
| 1361 | unsigned int SAWords = 0; |
| 1362 | static uint8_t DummyAuthKey[64]; |
| 1363 | int rc; |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 1364 | int ring = ctx->base.ring; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1365 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1366 | DMABuf_Properties_t DMAProperties = {0, 0, 0, 0}; |
| 1367 | DMABuf_HostAddress_t TokenHostAddress; |
| 1368 | DMABuf_HostAddress_t PktHostAddress; |
| 1369 | DMABuf_HostAddress_t SAHostAddress; |
| 1370 | DMABuf_Status_t DMAStatus; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1371 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1372 | DMABuf_Handle_t TokenHandle = {0}; |
| 1373 | DMABuf_Handle_t PktHandle = {0}; |
| 1374 | DMABuf_Handle_t SAHandle = {0}; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1375 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1376 | unsigned int TokenMaxWords = 0; |
| 1377 | unsigned int TokenHeaderWord; |
| 1378 | unsigned int TokenWords = 0; |
| 1379 | unsigned int TCRWords = 0; |
| 1380 | void *TCRData = 0; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1381 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1382 | TokenBuilder_Params_t TokenParams; |
| 1383 | PEC_CommandDescriptor_t Cmd; |
| 1384 | PEC_NotifyFunction_t CBFunc; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1385 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1386 | unsigned int count; |
| 1387 | int i; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1388 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1389 | u32 InputToken[IOTOKEN_IN_WORD_COUNT]; |
| 1390 | IOToken_Output_Dscr_t OutTokenDscr; |
| 1391 | IOToken_Input_Dscr_t InTokenDscr; |
| 1392 | void *InTokenDscrExt_p = NULL; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1393 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1394 | #ifdef CRYPTO_IOTOKEN_EXT |
| 1395 | IOToken_Input_Dscr_Ext_t InTokenDscrExt; |
| 1396 | |
| 1397 | ZEROINIT(InTokenDscrExt); |
| 1398 | InTokenDscrExt_p = &InTokenDscrExt; |
| 1399 | #endif |
| 1400 | ZEROINIT(InTokenDscr); |
| 1401 | ZEROINIT(OutTokenDscr); |
| 1402 | |
| 1403 | rc = SABuilder_Init_Basic(¶ms, &ProtocolParams, SAB_DIRECTION_OUTBOUND); |
| 1404 | if (rc) { |
| 1405 | CRYPTO_ERR("SABuilder_Init_Basic failed: %d\n", rc); |
| 1406 | goto error_exit; |
| 1407 | } |
| 1408 | |
| 1409 | params.IV_p = (uint8_t *) ctx->ipad; |
| 1410 | params.AuthAlgo = lookaside_match_hash(ctx->alg); |
| 1411 | params.AuthKey1_p = DummyAuthKey; |
| 1412 | if (params.AuthAlgo == SAB_AUTH_AES_XCBC_MAC) { |
| 1413 | params.AuthKey1_p = (uint8_t *) ctx->ipad + 2 * AES_BLOCK_SIZE; |
| 1414 | params.AuthKey2_p = (uint8_t *) ctx->ipad; |
| 1415 | params.AuthKey3_p = (uint8_t *) ctx->ipad + AES_BLOCK_SIZE; |
| 1416 | |
| 1417 | for (i = 0; i < AES_BLOCK_SIZE; i = i + 4) { |
| 1418 | swap(params.AuthKey1_p[i], params.AuthKey1_p[i+3]); |
| 1419 | swap(params.AuthKey1_p[i+1], params.AuthKey1_p[i+2]); |
| 1420 | |
| 1421 | swap(params.AuthKey2_p[i], params.AuthKey2_p[i+3]); |
| 1422 | swap(params.AuthKey2_p[i+1], params.AuthKey2_p[i+2]); |
| 1423 | |
| 1424 | swap(params.AuthKey3_p[i], params.AuthKey3_p[i+3]); |
| 1425 | swap(params.AuthKey3_p[i+1], params.AuthKey3_p[i+2]); |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1426 | } |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1427 | } |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1428 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1429 | if (!finish) |
| 1430 | params.flags |= SAB_FLAG_HASH_SAVE | SAB_FLAG_HASH_INTERMEDIATE; |
| 1431 | |
| 1432 | params.flags |= SAB_FLAG_SUPPRESS_PAYLOAD; |
| 1433 | ProtocolParams.ICVByteCount = mtk_req->digest_sz; |
| 1434 | |
| 1435 | rc = SABuilder_GetSizes(¶ms, &SAWords, NULL, NULL); |
| 1436 | if (rc) { |
| 1437 | CRYPTO_ERR("SA not created because of size errors: %d\n", rc); |
| 1438 | goto error_exit; |
| 1439 | } |
| 1440 | |
| 1441 | DMAProperties.fCached = true; |
| 1442 | DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 1443 | DMAProperties.Bank = MTK_EIP197_INLINE_BANK_TRANSFORM; |
| 1444 | DMAProperties.Size = MAX(4*SAWords, 256); |
| 1445 | |
| 1446 | DMAStatus = DMABuf_Alloc(DMAProperties, &SAHostAddress, &SAHandle); |
| 1447 | if (DMAStatus != DMABUF_STATUS_OK) { |
| 1448 | rc = 1; |
| 1449 | CRYPTO_ERR("Allocation of SA failed: %d\n", DMAStatus); |
| 1450 | goto error_exit; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1451 | } |
| 1452 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1453 | rc = SABuilder_BuildSA(¶ms, (u32 *)SAHostAddress.p, NULL, NULL); |
| 1454 | if (rc) { |
| 1455 | CRYPTO_ERR("SA not created because of errors: %d\n", rc); |
| 1456 | goto error_exit; |
| 1457 | } |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1458 | |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1459 | rc = TokenBuilder_GetContextSize(¶ms, &TCRWords); |
| 1460 | if (rc) { |
| 1461 | CRYPTO_ERR("TokenBuilder_GetContextSize returned errors: %d\n", rc); |
| 1462 | goto error_exit; |
| 1463 | } |
| 1464 | |
| 1465 | TCRData = kmalloc(4 * TCRWords, GFP_KERNEL); |
| 1466 | if (!TCRData) { |
| 1467 | rc = 1; |
| 1468 | CRYPTO_ERR("Allocation of TCR failed\n"); |
| 1469 | goto error_exit; |
| 1470 | } |
| 1471 | |
| 1472 | rc = TokenBuilder_BuildContext(¶ms, TCRData); |
| 1473 | if (rc) { |
| 1474 | CRYPTO_ERR("TokenBuilder_BuildContext failed: %d\n", rc); |
| 1475 | goto error_exit; |
| 1476 | } |
| 1477 | mtk_req->token_context = TCRData; |
| 1478 | |
| 1479 | rc = TokenBuilder_GetSize(TCRData, &TokenMaxWords); |
| 1480 | if (rc) { |
| 1481 | CRYPTO_ERR("TokenBuilder_GetSize failed: %d\n", rc); |
| 1482 | goto error_exit; |
| 1483 | } |
| 1484 | |
| 1485 | DMAProperties.fCached = true; |
| 1486 | DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 1487 | DMAProperties.Bank = MTK_EIP197_INLINE_BANK_TOKEN; |
| 1488 | DMAProperties.Size = 4*TokenMaxWords; |
| 1489 | |
| 1490 | DMAStatus = DMABuf_Alloc(DMAProperties, &TokenHostAddress, &TokenHandle); |
| 1491 | if (DMAStatus != DMABUF_STATUS_OK) { |
| 1492 | rc = 1; |
| 1493 | CRYPTO_ERR("Allocation of token builder failed: %d\n", DMAStatus); |
| 1494 | goto error_exit; |
| 1495 | } |
| 1496 | |
| 1497 | DMAProperties.fCached = true; |
| 1498 | DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 1499 | DMAProperties.Bank = MTK_EIP197_INLINE_BANK_PACKET; |
| 1500 | DMAProperties.Size = MAX(InputByteCount, mtk_req->digest_sz); |
| 1501 | |
| 1502 | DMAStatus = DMABuf_Alloc(DMAProperties, &PktHostAddress, &PktHandle); |
| 1503 | if (DMAStatus != DMABUF_STATUS_OK) { |
| 1504 | rc = 1; |
| 1505 | CRYPTO_ERR("Allocation of source packet buffer failed: %d\n", |
| 1506 | DMAStatus); |
| 1507 | goto error_exit; |
| 1508 | } |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1509 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 1510 | rc = PEC_SA_Register(ring, SAHandle, DMABuf_NULLHandle, |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1511 | DMABuf_NULLHandle); |
| 1512 | if (rc != PEC_STATUS_OK) { |
| 1513 | CRYPTO_ERR("PEC_SA_Register failed: %d\n", rc); |
| 1514 | goto error_exit; |
| 1515 | } |
| 1516 | memcpy(PktHostAddress.p, Input_p, InputByteCount); |
| 1517 | |
| 1518 | ZEROINIT(TokenParams); |
| 1519 | TokenParams.PacketFlags |= (TKB_PACKET_FLAG_HASHFIRST |
| 1520 | | TKB_PACKET_FLAG_HASHAPPEND); |
| 1521 | if (finish) |
| 1522 | TokenParams.PacketFlags |= TKB_PACKET_FLAG_HASHFINAL; |
| 1523 | |
| 1524 | rc = TokenBuilder_BuildToken(TCRData, (u8 *) PktHostAddress.p, |
| 1525 | InputByteCount, &TokenParams, |
| 1526 | (u32 *) TokenHostAddress.p, |
| 1527 | &TokenWords, &TokenHeaderWord); |
| 1528 | if (rc != TKB_STATUS_OK) { |
| 1529 | CRYPTO_ERR("Token builder failed: %d\n", rc); |
| 1530 | goto error_exit_unregister; |
| 1531 | } |
| 1532 | |
| 1533 | ZEROINIT(Cmd); |
| 1534 | Cmd.Token_Handle = TokenHandle; |
| 1535 | Cmd.Token_WordCount = TokenWords; |
| 1536 | Cmd.SrcPkt_Handle = PktHandle; |
| 1537 | Cmd.SrcPkt_ByteCount = InputByteCount; |
| 1538 | Cmd.DstPkt_Handle = PktHandle; |
| 1539 | Cmd.SA_Handle1 = SAHandle; |
| 1540 | Cmd.SA_Handle2 = DMABuf_NULLHandle; |
| 1541 | |
| 1542 | mtk_req->sa_pointer = SAHandle.p; |
| 1543 | |
| 1544 | #if defined(CRYPTO_IOTOKEN_EXT) |
| 1545 | InTokenDscrExt.HW_Services = IOTOKEN_CMD_PKT_LAC; |
| 1546 | #endif |
| 1547 | InTokenDscr.TknHdrWordInit = TokenHeaderWord; |
| 1548 | |
| 1549 | if (!crypto_iotoken_create(&InTokenDscr, |
| 1550 | InTokenDscrExt_p, |
| 1551 | InputToken, |
| 1552 | &Cmd)) { |
| 1553 | rc = 1; |
| 1554 | goto error_exit_unregister; |
| 1555 | } |
| 1556 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 1557 | rc = PEC_Packet_Put(ring, &Cmd, 1, &count); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1558 | if (rc != PEC_STATUS_OK && count != 1) { |
| 1559 | rc = 1; |
| 1560 | CRYPTO_ERR("PEC_Packet_Put error: %d\n", rc); |
| 1561 | goto error_exit_unregister; |
| 1562 | } |
| 1563 | |
| 1564 | result = kmalloc(sizeof(struct mtk_crypto_result), GFP_KERNEL); |
| 1565 | if (!result) { |
| 1566 | rc = 1; |
| 1567 | CRYPTO_ERR("No memory for result\n"); |
| 1568 | goto error_exit_unregister; |
| 1569 | } |
| 1570 | INIT_LIST_HEAD(&result->list); |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 1571 | result->eip.token_handle = TokenHandle.p; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1572 | result->eip.pkt_handle = PktHandle.p; |
| 1573 | result->async = async; |
| 1574 | result->dst = PktHostAddress.p; |
| 1575 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 1576 | spin_lock_bh(&priv->mtk_eip_ring[ring].ring_lock); |
| 1577 | list_add_tail(&result->list, &priv->mtk_eip_ring[ring].list); |
| 1578 | spin_unlock_bh(&priv->mtk_eip_ring[ring].ring_lock); |
| 1579 | CBFunc = mtk_crypto_interrupt_handler[ring]; |
| 1580 | rc = PEC_ResultNotify_Request(ring, CBFunc, 1); |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1581 | |
| 1582 | return rc; |
| 1583 | |
| 1584 | error_exit_unregister: |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 1585 | PEC_SA_UnRegister(ring, SAHandle, DMABuf_NULLHandle, |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1586 | DMABuf_NULLHandle); |
| 1587 | |
| 1588 | error_exit: |
| 1589 | DMABuf_Release(SAHandle); |
| 1590 | DMABuf_Release(TokenHandle); |
| 1591 | DMABuf_Release(PktHandle); |
| 1592 | |
| 1593 | if (TCRData != NULL) |
| 1594 | kfree(TCRData); |
| 1595 | |
| 1596 | return rc; |
| 1597 | } |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1598 | |
| 1599 | bool crypto_basic_hash(SABuilder_Auth_t HashAlgo, uint8_t *Input_p, |
| 1600 | unsigned int InputByteCount, uint8_t *Output_p, |
| 1601 | unsigned int OutputByteCount, bool fFinalize) |
| 1602 | { |
| 1603 | SABuilder_Params_Basic_t ProtocolParams; |
| 1604 | SABuilder_Params_t params; |
| 1605 | unsigned int SAWords = 0; |
| 1606 | static uint8_t DummyAuthKey[64]; |
| 1607 | int rc; |
| 1608 | |
| 1609 | DMABuf_Properties_t DMAProperties = {0, 0, 0, 0}; |
| 1610 | DMABuf_HostAddress_t TokenHostAddress; |
| 1611 | DMABuf_HostAddress_t PktHostAddress; |
| 1612 | DMABuf_HostAddress_t SAHostAddress; |
| 1613 | DMABuf_Status_t DMAStatus; |
| 1614 | |
| 1615 | DMABuf_Handle_t TokenHandle = {0}; |
| 1616 | DMABuf_Handle_t PktHandle = {0}; |
| 1617 | DMABuf_Handle_t SAHandle = {0}; |
| 1618 | |
| 1619 | unsigned int TokenMaxWords = 0; |
| 1620 | unsigned int TokenHeaderWord; |
| 1621 | unsigned int TokenWords = 0; |
| 1622 | unsigned int TCRWords = 0; |
| 1623 | void *TCRData = 0; |
| 1624 | |
| 1625 | TokenBuilder_Params_t TokenParams; |
| 1626 | PEC_CommandDescriptor_t Cmd; |
| 1627 | PEC_ResultDescriptor_t Res; |
| 1628 | unsigned int count; |
| 1629 | |
| 1630 | u32 OutputToken[IOTOKEN_IN_WORD_COUNT]; |
| 1631 | u32 InputToken[IOTOKEN_IN_WORD_COUNT]; |
| 1632 | IOToken_Output_Dscr_t OutTokenDscr; |
| 1633 | IOToken_Input_Dscr_t InTokenDscr; |
| 1634 | void *InTokenDscrExt_p = NULL; |
| 1635 | |
| 1636 | #ifdef CRYPTO_IOTOKEN_EXT |
| 1637 | IOToken_Input_Dscr_Ext_t InTokenDscrExt; |
| 1638 | |
| 1639 | ZEROINIT(InTokenDscrExt); |
| 1640 | InTokenDscrExt_p = &InTokenDscrExt; |
| 1641 | #endif |
| 1642 | ZEROINIT(InTokenDscr); |
| 1643 | ZEROINIT(OutTokenDscr); |
| 1644 | |
| 1645 | rc = SABuilder_Init_Basic(¶ms, &ProtocolParams, SAB_DIRECTION_OUTBOUND); |
| 1646 | if (rc) { |
| 1647 | CRYPTO_ERR("SABuilder_Init_Basic failed: %d\n", rc); |
| 1648 | goto error_exit; |
| 1649 | } |
| 1650 | |
| 1651 | params.AuthAlgo = HashAlgo; |
| 1652 | params.AuthKey1_p = DummyAuthKey; |
| 1653 | |
| 1654 | if (!fFinalize) |
| 1655 | params.flags |= SAB_FLAG_HASH_SAVE | SAB_FLAG_HASH_INTERMEDIATE; |
| 1656 | params.flags |= SAB_FLAG_SUPPRESS_PAYLOAD; |
| 1657 | ProtocolParams.ICVByteCount = OutputByteCount; |
| 1658 | |
| 1659 | rc = SABuilder_GetSizes(¶ms, &SAWords, NULL, NULL); |
| 1660 | if (rc) { |
| 1661 | CRYPTO_ERR("SA not created because of size errors: %d\n", rc); |
| 1662 | goto error_exit; |
| 1663 | } |
| 1664 | |
| 1665 | DMAProperties.fCached = true; |
| 1666 | DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 1667 | DMAProperties.Bank = MTK_EIP197_INLINE_BANK_TRANSFORM; |
| 1668 | DMAProperties.Size = MAX(4*SAWords, 256); |
| 1669 | |
| 1670 | DMAStatus = DMABuf_Alloc(DMAProperties, &SAHostAddress, &SAHandle); |
| 1671 | if (DMAStatus != DMABUF_STATUS_OK) { |
| 1672 | rc = 1; |
| 1673 | CRYPTO_ERR("Allocation of SA failed: %d\n", DMAStatus); |
| 1674 | goto error_exit; |
| 1675 | } |
| 1676 | |
| 1677 | rc = SABuilder_BuildSA(¶ms, (u32 *)SAHostAddress.p, NULL, NULL); |
| 1678 | if (rc) { |
| 1679 | CRYPTO_ERR("SA not created because of errors: %d\n", rc); |
| 1680 | goto error_exit; |
| 1681 | } |
| 1682 | |
| 1683 | rc = TokenBuilder_GetContextSize(¶ms, &TCRWords); |
| 1684 | if (rc) { |
| 1685 | CRYPTO_ERR("TokenBuilder_GetContextSize returned errors: %d\n", rc); |
| 1686 | goto error_exit; |
| 1687 | } |
| 1688 | |
| 1689 | TCRData = kmalloc(4 * TCRWords, GFP_KERNEL); |
| 1690 | if (!TCRData) { |
| 1691 | rc = 1; |
| 1692 | CRYPTO_ERR("Allocation of TCR failed\n"); |
| 1693 | goto error_exit; |
| 1694 | } |
| 1695 | |
| 1696 | rc = TokenBuilder_BuildContext(¶ms, TCRData); |
| 1697 | if (rc) { |
| 1698 | CRYPTO_ERR("TokenBuilder_BuildContext failed: %d\n", rc); |
| 1699 | goto error_exit; |
| 1700 | } |
| 1701 | |
| 1702 | rc = TokenBuilder_GetSize(TCRData, &TokenMaxWords); |
| 1703 | if (rc) { |
| 1704 | CRYPTO_ERR("TokenBuilder_GetSize failed: %d\n", rc); |
| 1705 | goto error_exit; |
| 1706 | } |
| 1707 | |
| 1708 | DMAProperties.fCached = true; |
| 1709 | DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 1710 | DMAProperties.Bank = MTK_EIP197_INLINE_BANK_TOKEN; |
| 1711 | DMAProperties.Size = 4*TokenMaxWords; |
| 1712 | |
| 1713 | DMAStatus = DMABuf_Alloc(DMAProperties, &TokenHostAddress, &TokenHandle); |
| 1714 | if (DMAStatus != DMABUF_STATUS_OK) { |
| 1715 | rc = 1; |
| 1716 | CRYPTO_ERR("Allocation of token builder failed: %d\n", DMAStatus); |
| 1717 | goto error_exit; |
| 1718 | } |
| 1719 | |
| 1720 | DMAProperties.fCached = true; |
| 1721 | DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 1722 | DMAProperties.Bank = MTK_EIP197_INLINE_BANK_PACKET; |
| 1723 | DMAProperties.Size = MAX(InputByteCount, OutputByteCount); |
| 1724 | |
| 1725 | DMAStatus = DMABuf_Alloc(DMAProperties, &PktHostAddress, &PktHandle); |
| 1726 | if (DMAStatus != DMABUF_STATUS_OK) { |
| 1727 | rc = 1; |
| 1728 | CRYPTO_ERR("Allocation of source packet buffer failed: %d\n", |
| 1729 | DMAStatus); |
| 1730 | goto error_exit; |
| 1731 | } |
| 1732 | |
| 1733 | rc = PEC_SA_Register(PEC_INTERFACE_ID, SAHandle, DMABuf_NULLHandle, |
| 1734 | DMABuf_NULLHandle); |
| 1735 | if (rc != PEC_STATUS_OK) { |
| 1736 | CRYPTO_ERR("PEC_SA_Register failed: %d\n", rc); |
| 1737 | goto error_exit; |
| 1738 | } |
| 1739 | |
| 1740 | memcpy(PktHostAddress.p, Input_p, InputByteCount); |
| 1741 | |
| 1742 | ZEROINIT(TokenParams); |
| 1743 | TokenParams.PacketFlags |= (TKB_PACKET_FLAG_HASHFIRST |
| 1744 | | TKB_PACKET_FLAG_HASHAPPEND); |
| 1745 | if (fFinalize) |
| 1746 | TokenParams.PacketFlags |= TKB_PACKET_FLAG_HASHFINAL; |
| 1747 | |
| 1748 | rc = TokenBuilder_BuildToken(TCRData, (u8 *) PktHostAddress.p, |
| 1749 | InputByteCount, &TokenParams, |
| 1750 | (u32 *) TokenHostAddress.p, |
| 1751 | &TokenWords, &TokenHeaderWord); |
| 1752 | if (rc != TKB_STATUS_OK) { |
| 1753 | CRYPTO_ERR("Token builder failed: %d\n", rc); |
| 1754 | goto error_exit_unregister; |
| 1755 | } |
| 1756 | |
| 1757 | ZEROINIT(Cmd); |
| 1758 | Cmd.Token_Handle = TokenHandle; |
| 1759 | Cmd.Token_WordCount = TokenWords; |
| 1760 | Cmd.SrcPkt_Handle = PktHandle; |
| 1761 | Cmd.SrcPkt_ByteCount = InputByteCount; |
| 1762 | Cmd.DstPkt_Handle = PktHandle; |
| 1763 | Cmd.SA_Handle1 = SAHandle; |
| 1764 | Cmd.SA_Handle2 = DMABuf_NULLHandle; |
| 1765 | |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1766 | #if defined(CRYPTO_IOTOKEN_EXT) |
| 1767 | InTokenDscrExt.HW_Services = IOTOKEN_CMD_PKT_LAC; |
| 1768 | #endif |
| 1769 | InTokenDscr.TknHdrWordInit = TokenHeaderWord; |
| 1770 | |
| 1771 | if (!crypto_iotoken_create(&InTokenDscr, |
| 1772 | InTokenDscrExt_p, |
| 1773 | InputToken, |
| 1774 | &Cmd)) { |
| 1775 | rc = 1; |
| 1776 | goto error_exit_unregister; |
| 1777 | } |
| 1778 | |
| 1779 | rc = PEC_Packet_Put(PEC_INTERFACE_ID, &Cmd, 1, &count); |
| 1780 | if (rc != PEC_STATUS_OK && count != 1) { |
| 1781 | rc = 1; |
| 1782 | CRYPTO_ERR("PEC_Packet_Put error: %d\n", rc); |
| 1783 | goto error_exit_unregister; |
| 1784 | } |
| 1785 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 1786 | if (crypto_pe_busy_get_one(&OutTokenDscr, OutputToken, &Res, PEC_INTERFACE_ID) < 1) { |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1787 | rc = 1; |
developer | ebe38fd | 2024-03-22 14:36:06 +0800 | [diff] [blame] | 1788 | CRYPTO_ERR("error from crypto_pe_busy_get_one\n"); |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1789 | goto error_exit_unregister; |
| 1790 | } |
| 1791 | memcpy(Output_p, PktHostAddress.p, OutputByteCount); |
| 1792 | |
| 1793 | error_exit_unregister: |
| 1794 | PEC_SA_UnRegister(PEC_INTERFACE_ID, SAHandle, DMABuf_NULLHandle, |
| 1795 | DMABuf_NULLHandle); |
| 1796 | |
| 1797 | error_exit: |
| 1798 | DMABuf_Release(SAHandle); |
| 1799 | DMABuf_Release(TokenHandle); |
| 1800 | DMABuf_Release(PktHandle); |
| 1801 | |
| 1802 | if (TCRData != NULL) |
| 1803 | kfree(TCRData); |
| 1804 | |
| 1805 | return rc == 0; |
| 1806 | } |
| 1807 | |
| 1808 | bool crypto_hmac_precompute(SABuilder_Auth_t AuthAlgo, |
| 1809 | uint8_t *AuthKey_p, |
| 1810 | unsigned int AuthKeyByteCount, |
| 1811 | uint8_t *Inner_p, |
| 1812 | uint8_t *Outer_p) |
| 1813 | { |
| 1814 | SABuilder_Auth_t HashAlgo; |
| 1815 | unsigned int blocksize, hashsize, digestsize; |
| 1816 | static uint8_t pad_block[128], hashed_key[128]; |
| 1817 | unsigned int i; |
| 1818 | |
| 1819 | switch (AuthAlgo) { |
| 1820 | case SAB_AUTH_HMAC_MD5: |
| 1821 | HashAlgo = SAB_AUTH_HASH_MD5; |
| 1822 | blocksize = 64; |
| 1823 | hashsize = 16; |
| 1824 | digestsize = 16; |
| 1825 | break; |
| 1826 | case SAB_AUTH_HMAC_SHA1: |
| 1827 | HashAlgo = SAB_AUTH_HASH_SHA1; |
| 1828 | blocksize = 64; |
| 1829 | hashsize = 20; |
| 1830 | digestsize = 20; |
| 1831 | break; |
| 1832 | case SAB_AUTH_HMAC_SHA2_224: |
| 1833 | HashAlgo = SAB_AUTH_HASH_SHA2_224; |
| 1834 | blocksize = 64; |
| 1835 | hashsize = 28; |
| 1836 | digestsize = 32; |
| 1837 | break; |
| 1838 | case SAB_AUTH_HMAC_SHA2_256: |
| 1839 | HashAlgo = SAB_AUTH_HASH_SHA2_256; |
| 1840 | blocksize = 64; |
| 1841 | hashsize = 32; |
| 1842 | digestsize = 32; |
| 1843 | break; |
| 1844 | case SAB_AUTH_HMAC_SHA2_384: |
| 1845 | HashAlgo = SAB_AUTH_HASH_SHA2_384; |
| 1846 | blocksize = 128; |
| 1847 | hashsize = 48; |
| 1848 | digestsize = 64; |
| 1849 | break; |
| 1850 | case SAB_AUTH_HMAC_SHA2_512: |
| 1851 | HashAlgo = SAB_AUTH_HASH_SHA2_512; |
| 1852 | blocksize = 128; |
| 1853 | hashsize = 64; |
| 1854 | digestsize = 64; |
| 1855 | break; |
| 1856 | default: |
| 1857 | CRYPTO_ERR("Unknown HMAC algorithm\n"); |
| 1858 | return false; |
| 1859 | } |
| 1860 | |
| 1861 | memset(hashed_key, 0, blocksize); |
| 1862 | if (AuthKeyByteCount <= blocksize) { |
| 1863 | memcpy(hashed_key, AuthKey_p, AuthKeyByteCount); |
| 1864 | } else { |
| 1865 | if (!crypto_basic_hash(HashAlgo, AuthKey_p, AuthKeyByteCount, |
| 1866 | hashed_key, hashsize, true)) |
| 1867 | return false; |
| 1868 | } |
| 1869 | |
| 1870 | for (i = 0; i < blocksize; i++) |
| 1871 | pad_block[i] = hashed_key[i] ^ 0x36; |
| 1872 | |
| 1873 | if (!crypto_basic_hash(HashAlgo, pad_block, blocksize, |
| 1874 | Inner_p, digestsize, false)) |
| 1875 | return false; |
| 1876 | |
| 1877 | for (i = 0; i < blocksize; i++) |
| 1878 | pad_block[i] = hashed_key[i] ^ 0x5c; |
| 1879 | |
| 1880 | if (!crypto_basic_hash(HashAlgo, pad_block, blocksize, |
| 1881 | Outer_p, digestsize, false)) |
| 1882 | return false; |
| 1883 | |
| 1884 | return true; |
| 1885 | } |
| 1886 | |
| 1887 | static SABuilder_Crypto_t set_crypto_algo(struct xfrm_algo *ealg) |
| 1888 | { |
| 1889 | if (strcmp(ealg->alg_name, "cbc(des)") == 0) |
| 1890 | return SAB_CRYPTO_DES; |
| 1891 | else if (strcmp(ealg->alg_name, "cbc(aes)") == 0) |
| 1892 | return SAB_CRYPTO_AES; |
| 1893 | else if (strcmp(ealg->alg_name, "cbc(des3_ede)") == 0) |
| 1894 | return SAB_CRYPTO_3DES; |
| 1895 | |
| 1896 | return SAB_CRYPTO_NULL; |
| 1897 | } |
| 1898 | |
| 1899 | static bool set_auth_algo(struct xfrm_algo_auth *aalg, SABuilder_Params_t *params, |
| 1900 | uint8_t *inner, uint8_t *outer) |
| 1901 | { |
| 1902 | if (strcmp(aalg->alg_name, "hmac(sha1)") == 0) { |
| 1903 | params->AuthAlgo = SAB_AUTH_HMAC_SHA1; |
| 1904 | inner = kcalloc(SHA1_DIGEST_SIZE, sizeof(uint8_t), GFP_KERNEL); |
| 1905 | outer = kcalloc(SHA1_DIGEST_SIZE, sizeof(uint8_t), GFP_KERNEL); |
| 1906 | crypto_hmac_precompute(SAB_AUTH_HMAC_SHA1, &aalg->alg_key[0], |
| 1907 | aalg->alg_key_len / 8, inner, outer); |
| 1908 | |
| 1909 | params->AuthKey1_p = inner; |
| 1910 | params->AuthKey2_p = outer; |
| 1911 | } else if (strcmp(aalg->alg_name, "hmac(sha256)") == 0) { |
| 1912 | params->AuthAlgo = SAB_AUTH_HMAC_SHA2_256; |
| 1913 | inner = kcalloc(SHA256_DIGEST_SIZE, sizeof(uint8_t), GFP_KERNEL); |
| 1914 | outer = kcalloc(SHA256_DIGEST_SIZE, sizeof(uint8_t), GFP_KERNEL); |
| 1915 | crypto_hmac_precompute(SAB_AUTH_HMAC_SHA2_256, &aalg->alg_key[0], |
| 1916 | aalg->alg_key_len / 8, inner, outer); |
| 1917 | params->AuthKey1_p = inner; |
| 1918 | params->AuthKey2_p = outer; |
| 1919 | } else if (strcmp(aalg->alg_name, "hmac(sha384)") == 0) { |
| 1920 | params->AuthAlgo = SAB_AUTH_HMAC_SHA2_384; |
| 1921 | inner = kcalloc(SHA384_DIGEST_SIZE, sizeof(uint8_t), GFP_KERNEL); |
| 1922 | outer = kcalloc(SHA384_DIGEST_SIZE, sizeof(uint8_t), GFP_KERNEL); |
| 1923 | crypto_hmac_precompute(SAB_AUTH_HMAC_SHA2_384, &aalg->alg_key[0], |
| 1924 | aalg->alg_key_len / 8, inner, outer); |
| 1925 | params->AuthKey1_p = inner; |
| 1926 | params->AuthKey2_p = outer; |
| 1927 | } else if (strcmp(aalg->alg_name, "hmac(sha512)") == 0) { |
| 1928 | params->AuthAlgo = SAB_AUTH_HMAC_SHA2_512; |
| 1929 | inner = kcalloc(SHA512_DIGEST_SIZE, sizeof(uint8_t), GFP_KERNEL); |
| 1930 | outer = kcalloc(SHA512_DIGEST_SIZE, sizeof(uint8_t), GFP_KERNEL); |
| 1931 | crypto_hmac_precompute(SAB_AUTH_HMAC_SHA2_512, &aalg->alg_key[0], |
| 1932 | aalg->alg_key_len / 8, inner, outer); |
| 1933 | params->AuthKey1_p = inner; |
| 1934 | params->AuthKey2_p = outer; |
| 1935 | } else if (strcmp(aalg->alg_name, "hmac(md5)") == 0) { |
| 1936 | params->AuthAlgo = SAB_AUTH_HMAC_MD5; |
| 1937 | inner = kcalloc(MD5_DIGEST_SIZE, sizeof(uint8_t), GFP_KERNEL); |
| 1938 | outer = kcalloc(MD5_DIGEST_SIZE, sizeof(uint8_t), GFP_KERNEL); |
| 1939 | crypto_hmac_precompute(SAB_AUTH_HMAC_MD5, &aalg->alg_key[0], |
| 1940 | aalg->alg_key_len / 8, inner, outer); |
| 1941 | params->AuthKey1_p = inner; |
| 1942 | params->AuthKey2_p = outer; |
| 1943 | } else { |
| 1944 | return false; |
| 1945 | } |
| 1946 | |
| 1947 | return true; |
| 1948 | } |
| 1949 | |
| 1950 | u32 *mtk_ddk_tr_ipsec_build(struct mtk_xfrm_params *xfrm_params, u32 ipsec_mode) |
| 1951 | { |
| 1952 | struct xfrm_state *xs = xfrm_params->xs; |
| 1953 | SABuilder_Params_IPsec_t ipsec_params; |
| 1954 | SABuilder_Status_t sa_status; |
| 1955 | SABuilder_Params_t params; |
| 1956 | bool set_auth_success = false; |
| 1957 | unsigned int SAWords = 0; |
developer | 6ad3f36 | 2024-10-28 11:19:49 +0800 | [diff] [blame^] | 1958 | uint8_t *inner = NULL; |
| 1959 | uint8_t *outer = NULL; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1960 | |
| 1961 | DMABuf_Status_t dma_status; |
| 1962 | DMABuf_Properties_t dma_properties = {0, 0, 0, 0}; |
| 1963 | DMABuf_HostAddress_t sa_host_addr; |
| 1964 | |
| 1965 | DMABuf_Handle_t sa_handle = {0}; |
| 1966 | |
developer | 6ad3f36 | 2024-10-28 11:19:49 +0800 | [diff] [blame^] | 1967 | PCL_Status_t pcl_status; |
| 1968 | |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1969 | sa_status = SABuilder_Init_ESP(¶ms, |
| 1970 | &ipsec_params, |
| 1971 | be32_to_cpu(xs->id.spi), |
| 1972 | ipsec_mode, |
| 1973 | SAB_IPSEC_IPV4, |
| 1974 | xfrm_params->dir); |
| 1975 | |
| 1976 | if (sa_status != SAB_STATUS_OK) { |
| 1977 | pr_err("SABuilder_Init_ESP failed\n"); |
developer | 6ad3f36 | 2024-10-28 11:19:49 +0800 | [diff] [blame^] | 1978 | sa_host_addr.p = NULL; |
| 1979 | goto error_ret; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1980 | } |
| 1981 | |
developer | 584d7e5 | 2023-12-13 19:28:49 +0800 | [diff] [blame] | 1982 | /* No support for aead now */ |
| 1983 | if (xs->aead) { |
| 1984 | CRYPTO_ERR("AEAD not supported\n"); |
developer | 6ad3f36 | 2024-10-28 11:19:49 +0800 | [diff] [blame^] | 1985 | sa_host_addr.p = NULL; |
| 1986 | goto error_ret; |
developer | 584d7e5 | 2023-12-13 19:28:49 +0800 | [diff] [blame] | 1987 | } |
| 1988 | |
developer | 9d17aad | 2024-08-01 17:36:15 +0800 | [diff] [blame] | 1989 | /* Check algorithm exist in xfrm state*/ |
| 1990 | if (!xs->ealg || !xs->aalg) { |
| 1991 | CRYPTO_ERR("NULL algorithm in xfrm state\n"); |
developer | 6ad3f36 | 2024-10-28 11:19:49 +0800 | [diff] [blame^] | 1992 | sa_host_addr.p = NULL; |
| 1993 | goto error_ret; |
developer | 9d17aad | 2024-08-01 17:36:15 +0800 | [diff] [blame] | 1994 | } |
| 1995 | |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1996 | /* Add crypto key and parameters */ |
| 1997 | params.CryptoAlgo = set_crypto_algo(xs->ealg); |
| 1998 | params.CryptoMode = SAB_CRYPTO_MODE_CBC; |
| 1999 | params.KeyByteCount = xs->ealg->alg_key_len / 8; |
| 2000 | params.Key_p = xs->ealg->alg_key; |
| 2001 | |
| 2002 | /* Add authentication key and parameters */ |
| 2003 | set_auth_success = set_auth_algo(xs->aalg, ¶ms, inner, outer); |
| 2004 | if (set_auth_success != true) { |
| 2005 | CRYPTO_ERR("Set Auth Algo failed\n"); |
developer | 6ad3f36 | 2024-10-28 11:19:49 +0800 | [diff] [blame^] | 2006 | sa_host_addr.p = NULL; |
| 2007 | goto error_ret; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | ipsec_params.IPsecFlags |= (SAB_IPSEC_PROCESS_IP_HEADERS |
| 2011 | | SAB_IPSEC_EXT_PROCESSING); |
| 2012 | if (ipsec_mode == SAB_IPSEC_TUNNEL) { |
| 2013 | ipsec_params.SrcIPAddr_p = (uint8_t *) &xs->props.saddr.a4; |
| 2014 | ipsec_params.DestIPAddr_p = (uint8_t *) &xs->id.daddr.a4; |
| 2015 | } |
| 2016 | |
| 2017 | sa_status = SABuilder_GetSizes(¶ms, &SAWords, NULL, NULL); |
| 2018 | if (sa_status != SAB_STATUS_OK) { |
| 2019 | CRYPTO_ERR("SA not created because of size errors\n"); |
developer | 6ad3f36 | 2024-10-28 11:19:49 +0800 | [diff] [blame^] | 2020 | sa_host_addr.p = NULL; |
| 2021 | goto error_ret; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | dma_properties.fCached = true; |
| 2025 | dma_properties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 2026 | dma_properties.Bank = MTK_EIP197_INLINE_BANK_TRANSFORM; |
| 2027 | dma_properties.Size = SAWords * sizeof(u32); |
| 2028 | |
| 2029 | dma_status = DMABuf_Alloc(dma_properties, &sa_host_addr, &sa_handle); |
| 2030 | if (dma_status != DMABUF_STATUS_OK) { |
| 2031 | CRYPTO_ERR("Allocation of SA failed\n"); |
developer | 6ad3f36 | 2024-10-28 11:19:49 +0800 | [diff] [blame^] | 2032 | sa_host_addr.p = NULL; |
| 2033 | goto error_ret; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | sa_status = SABuilder_BuildSA(¶ms, (u32 *) sa_host_addr.p, NULL, NULL); |
| 2037 | if (sa_status != SAB_STATUS_OK) { |
| 2038 | CRYPTO_ERR("SA not created because of errors\n"); |
developer | 6ad3f36 | 2024-10-28 11:19:49 +0800 | [diff] [blame^] | 2039 | DMABuf_Release(sa_handle); |
| 2040 | sa_host_addr.p = NULL; |
| 2041 | goto error_ret; |
| 2042 | } |
| 2043 | |
| 2044 | pcl_status = PCL_Transform_Register(sa_handle); |
| 2045 | if (pcl_status != PCL_STATUS_OK) { |
| 2046 | CRYPTO_ERR("%s: PCL_Transform_Register failed\n", __func__); |
| 2047 | DMABuf_Release(sa_handle); |
| 2048 | sa_host_addr.p = NULL; |
| 2049 | goto error_ret; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 2050 | } |
| 2051 | |
developer | 6ad3f36 | 2024-10-28 11:19:49 +0800 | [diff] [blame^] | 2052 | xfrm_params->p_handle = (u32 *) sa_handle.p; |
| 2053 | |
| 2054 | error_ret: |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 2055 | kfree(inner); |
| 2056 | kfree(outer); |
| 2057 | return (u32 *) sa_host_addr.p; |
| 2058 | } |
| 2059 | |
| 2060 | int mtk_ddk_pec_init(void) |
| 2061 | { |
| 2062 | PEC_InitBlock_t pec_init_blk = {0, 0, false}; |
| 2063 | PEC_Capabilities_t pec_cap; |
| 2064 | PEC_Status_t pec_sta; |
| 2065 | u32 i = MTK_EIP197_INLINE_NOF_TRIES; |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 2066 | u32 j; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2067 | #ifdef PEC_PCL_EIP197 |
| 2068 | PCL_Status_t pcl_sta; |
| 2069 | #endif |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 2070 | |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2071 | #ifdef PEC_PCL_EIP197 |
| 2072 | pcl_sta = PCL_Init(PCL_INTERFACE_ID, 1); |
| 2073 | if (pcl_sta != PCL_STATUS_OK) { |
| 2074 | CRYPTO_ERR("PCL could not be initialized, error=%d\n", pcl_sta); |
| 2075 | return 0; |
| 2076 | } |
| 2077 | |
| 2078 | pcl_sta = PCL_DTL_Init(PCL_INTERFACE_ID); |
| 2079 | if (pcl_sta != PCL_STATUS_OK) { |
| 2080 | CRYPTO_ERR("PCL-DTL could not be initialized, error=%d\n", pcl_sta); |
| 2081 | return -1; |
| 2082 | } |
| 2083 | #endif |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 2084 | for (j = 0; j < PEC_MAX_INTERFACE_NUM; j++) { |
| 2085 | while (i) { |
| 2086 | pec_sta = PEC_Init(j, &pec_init_blk); |
| 2087 | if (pec_sta == PEC_STATUS_OK) { |
| 2088 | CRYPTO_INFO("PEC_INIT interface %d ok!\n", j); |
| 2089 | break; |
| 2090 | } else if (pec_sta != PEC_STATUS_OK && pec_sta != PEC_STATUS_BUSY) { |
| 2091 | return pec_sta; |
| 2092 | } |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 2093 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 2094 | mdelay(MTK_EIP197_INLINE_RETRY_DELAY_MS); |
| 2095 | i--; |
| 2096 | } |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | if (!i) { |
| 2100 | CRYPTO_ERR("PEC could not be initialized: %d\n", pec_sta); |
| 2101 | return pec_sta; |
| 2102 | } |
| 2103 | |
| 2104 | pec_sta = PEC_Capabilities_Get(&pec_cap); |
| 2105 | if (pec_sta != PEC_STATUS_OK) { |
| 2106 | CRYPTO_ERR("PEC capability could not be obtained: %d\n", pec_sta); |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2107 | #ifdef PEC_PCL_EIP197 |
| 2108 | PCL_UnInit(PCL_INTERFACE_ID); |
| 2109 | #endif |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 2110 | return pec_sta; |
| 2111 | } |
| 2112 | |
| 2113 | CRYPTO_INFO("PEC Capabilities: %s\n", pec_cap.szTextDescription); |
| 2114 | |
| 2115 | return 0; |
| 2116 | } |
| 2117 | |
| 2118 | void mtk_ddk_pec_deinit(void) |
| 2119 | { |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2120 | unsigned int LoopCounter = MTK_EIP197_INLINE_NOF_TRIES; |
| 2121 | PEC_Status_t PEC_Status; |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 2122 | int j; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2123 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 2124 | for (j = 0; j < PEC_MAX_INTERFACE_NUM; j++) { |
| 2125 | while (LoopCounter > 0) { |
| 2126 | PEC_Status = PEC_UnInit(j); |
| 2127 | if (PEC_Status == PEC_STATUS_OK) |
| 2128 | break; |
| 2129 | else if (PEC_Status != PEC_STATUS_OK && PEC_Status != PEC_STATUS_BUSY) { |
| 2130 | CRYPTO_ERR("PEC could not deinit, error=%d\n", PEC_Status); |
| 2131 | return; |
| 2132 | } |
| 2133 | // Wait for MTK_EIP197_INLINE_RETRY_DELAY_MS milliseconds |
| 2134 | udelay(MTK_EIP197_INLINE_RETRY_DELAY_MS * 1000); |
| 2135 | LoopCounter--; |
| 2136 | } |
| 2137 | // Check for timeout |
| 2138 | if (LoopCounter == 0) { |
| 2139 | CRYPTO_ERR("PEC could not be un-initialized, timeout\n"); |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2140 | return; |
| 2141 | } |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | #ifdef PEC_PCL_EIP197 |
| 2145 | PCL_DTL_UnInit(PCL_INTERFACE_ID); |
| 2146 | PCL_UnInit(PCL_INTERFACE_ID); |
| 2147 | #endif |
| 2148 | } |
| 2149 | |
| 2150 | bool |
| 2151 | mtk_ddk_aes_block_encrypt(uint8_t *Key_p, |
| 2152 | unsigned int KeyByteCount, |
| 2153 | uint8_t *InData_p, |
| 2154 | uint8_t *OutData_p) |
| 2155 | { |
| 2156 | int rc; |
| 2157 | SABuilder_Params_t params; |
| 2158 | SABuilder_Params_Basic_t ProtocolParams; |
| 2159 | unsigned int SAWords = 0; |
| 2160 | |
| 2161 | DMABuf_Status_t DMAStatus; |
| 2162 | DMABuf_Properties_t DMAProperties = {0, 0, 0, 0}; |
| 2163 | DMABuf_HostAddress_t SAHostAddress; |
| 2164 | DMABuf_HostAddress_t TokenHostAddress; |
| 2165 | DMABuf_HostAddress_t PktHostAddress; |
| 2166 | |
| 2167 | DMABuf_Handle_t SAHandle = {0}; |
| 2168 | DMABuf_Handle_t TokenHandle = {0}; |
| 2169 | DMABuf_Handle_t PktHandle = {0}; |
| 2170 | |
| 2171 | unsigned int TCRWords = 0; |
| 2172 | void *TCRData = 0; |
| 2173 | unsigned int TokenWords = 0; |
| 2174 | unsigned int TokenHeaderWord; |
| 2175 | unsigned int TokenMaxWords = 0; |
| 2176 | |
| 2177 | TokenBuilder_Params_t TokenParams; |
| 2178 | PEC_CommandDescriptor_t Cmd; |
| 2179 | PEC_ResultDescriptor_t Res; |
| 2180 | unsigned int count; |
| 2181 | |
| 2182 | IOToken_Input_Dscr_t InTokenDscr; |
| 2183 | IOToken_Output_Dscr_t OutTokenDscr; |
| 2184 | uint32_t InputToken[IOTOKEN_IN_WORD_COUNT]; |
| 2185 | uint32_t OutputToken[IOTOKEN_OUT_WORD_COUNT]; |
| 2186 | IOToken_Output_Dscr_Ext_t OutTokenDscrExt; |
| 2187 | IOToken_Input_Dscr_Ext_t InTokenDscrExt; |
| 2188 | |
| 2189 | ZEROINIT(InTokenDscrExt); |
| 2190 | ZEROINIT(OutTokenDscrExt); |
| 2191 | |
| 2192 | ZEROINIT(InTokenDscr); |
| 2193 | ZEROINIT(OutTokenDscr); |
| 2194 | |
| 2195 | rc = SABuilder_Init_Basic(¶ms, &ProtocolParams, SAB_DIRECTION_OUTBOUND); |
| 2196 | if (rc != 0) { |
| 2197 | CRYPTO_ERR("SABuilder_Init_Basic failed\n"); |
| 2198 | goto error_exit; |
| 2199 | } |
| 2200 | // Add crypto key and parameters. |
| 2201 | params.CryptoAlgo = SAB_CRYPTO_AES; |
| 2202 | params.CryptoMode = SAB_CRYPTO_MODE_ECB; |
| 2203 | params.KeyByteCount = KeyByteCount; |
| 2204 | params.Key_p = Key_p; |
| 2205 | |
| 2206 | rc = SABuilder_GetSizes(¶ms, &SAWords, NULL, NULL); |
| 2207 | |
| 2208 | if (rc != 0) { |
| 2209 | CRYPTO_ERR("SA not created because of errors\n"); |
| 2210 | goto error_exit; |
| 2211 | } |
| 2212 | |
| 2213 | DMAProperties.fCached = true; |
| 2214 | DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 2215 | DMAProperties.Bank = MTK_EIP197_INLINE_BANK_TRANSFORM; |
| 2216 | DMAProperties.Size = 4*SAWords; |
| 2217 | |
| 2218 | DMAStatus = DMABuf_Alloc(DMAProperties, &SAHostAddress, &SAHandle); |
| 2219 | if (DMAStatus != DMABUF_STATUS_OK) { |
| 2220 | rc = 1; |
| 2221 | CRYPTO_ERR("Allocation of SA failed\n"); |
| 2222 | goto error_exit; |
| 2223 | } |
| 2224 | |
| 2225 | rc = SABuilder_BuildSA(¶ms, (uint32_t *)SAHostAddress.p, NULL, NULL); |
| 2226 | |
| 2227 | if (rc != 0) { |
| 2228 | LOG_CRIT("SA not created because of errors\n"); |
| 2229 | goto error_exit; |
| 2230 | } |
| 2231 | |
| 2232 | rc = TokenBuilder_GetContextSize(¶ms, &TCRWords); |
| 2233 | |
| 2234 | if (rc != 0) { |
| 2235 | CRYPTO_ERR("TokenBuilder_GetContextSize returned errors\n"); |
| 2236 | goto error_exit; |
| 2237 | } |
| 2238 | |
| 2239 | // The Token Context Record does not need to be allocated |
| 2240 | // in a DMA-safe buffer. |
| 2241 | TCRData = kcalloc(4*TCRWords, sizeof(uint8_t), GFP_KERNEL); |
| 2242 | if (!TCRData) { |
| 2243 | rc = 1; |
| 2244 | CRYPTO_ERR("Allocation of TCR failed\n"); |
| 2245 | goto error_exit; |
| 2246 | } |
| 2247 | |
| 2248 | rc = TokenBuilder_BuildContext(¶ms, TCRData); |
| 2249 | |
| 2250 | if (rc != 0) { |
| 2251 | CRYPTO_ERR("TokenBuilder_BuildContext failed\n"); |
| 2252 | goto error_exit; |
| 2253 | } |
| 2254 | |
| 2255 | rc = TokenBuilder_GetSize(TCRData, &TokenMaxWords); |
| 2256 | if (rc != 0) { |
| 2257 | CRYPTO_ERR("TokenBuilder_GetSize failed\n"); |
| 2258 | goto error_exit; |
| 2259 | } |
| 2260 | |
| 2261 | // Allocate one buffer for the token and two packet buffers. |
| 2262 | |
| 2263 | DMAProperties.fCached = true; |
| 2264 | DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 2265 | DMAProperties.Bank = MTK_EIP197_INLINE_BANK_PACKET; |
| 2266 | DMAProperties.Size = 4*TokenMaxWords; |
| 2267 | |
| 2268 | DMAStatus = DMABuf_Alloc(DMAProperties, &TokenHostAddress, &TokenHandle); |
| 2269 | if (DMAStatus != DMABUF_STATUS_OK) { |
| 2270 | rc = 1; |
| 2271 | CRYPTO_ERR("Allocation of token buffer failed.\n"); |
| 2272 | goto error_exit; |
| 2273 | } |
| 2274 | |
| 2275 | DMAProperties.fCached = true; |
| 2276 | DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 2277 | DMAProperties.Bank = MTK_EIP197_INLINE_BANK_PACKET; |
| 2278 | DMAProperties.Size = 16; |
| 2279 | |
| 2280 | DMAStatus = DMABuf_Alloc(DMAProperties, &PktHostAddress, |
| 2281 | &PktHandle); |
| 2282 | if (DMAStatus != DMABUF_STATUS_OK) { |
| 2283 | rc = 1; |
| 2284 | CRYPTO_ERR("Allocation of source packet buffer failed.\n"); |
| 2285 | goto error_exit; |
| 2286 | } |
| 2287 | |
| 2288 | // Register the SA |
| 2289 | rc = PEC_SA_Register(PEC_INTERFACE_ID, SAHandle, DMABuf_NULLHandle, |
| 2290 | DMABuf_NULLHandle); |
| 2291 | if (rc != PEC_STATUS_OK) { |
| 2292 | CRYPTO_ERR("PEC_SA_Register failed\n"); |
| 2293 | goto error_exit; |
| 2294 | } |
| 2295 | |
| 2296 | // Copy input packet into source packet buffer. |
| 2297 | memcpy(PktHostAddress.p, InData_p, 16); |
| 2298 | |
| 2299 | // Set Token Parameters if specified in test vector. |
| 2300 | ZEROINIT(TokenParams); |
| 2301 | |
| 2302 | |
| 2303 | // Prepare a token to process the packet. |
| 2304 | rc = TokenBuilder_BuildToken(TCRData, |
| 2305 | (uint8_t *)PktHostAddress.p, |
| 2306 | 16, |
| 2307 | &TokenParams, |
| 2308 | (uint32_t *)TokenHostAddress.p, |
| 2309 | &TokenWords, |
| 2310 | &TokenHeaderWord); |
| 2311 | if (rc != TKB_STATUS_OK) { |
| 2312 | if (rc == TKB_BAD_PACKET) |
| 2313 | CRYPTO_ERR("Token not created because packet size is invalid\n"); |
| 2314 | else |
| 2315 | CRYPTO_ERR("Token builder failed\n"); |
| 2316 | goto error_exit_unregister; |
| 2317 | } |
| 2318 | |
| 2319 | ZEROINIT(Cmd); |
| 2320 | Cmd.Token_Handle = TokenHandle; |
| 2321 | Cmd.Token_WordCount = TokenWords; |
| 2322 | Cmd.SrcPkt_Handle = PktHandle; |
| 2323 | Cmd.SrcPkt_ByteCount = 16; |
| 2324 | Cmd.DstPkt_Handle = PktHandle; |
| 2325 | Cmd.SA_Handle1 = SAHandle; |
| 2326 | Cmd.SA_Handle2 = DMABuf_NULLHandle; |
| 2327 | |
| 2328 | InTokenDscrExt.HW_Services = IOTOKEN_CMD_PKT_LAC; |
| 2329 | InTokenDscr.TknHdrWordInit = TokenHeaderWord; |
| 2330 | |
| 2331 | if (!crypto_iotoken_create(&InTokenDscr, |
| 2332 | &InTokenDscrExt, |
| 2333 | InputToken, |
| 2334 | &Cmd)) { |
| 2335 | rc = 1; |
| 2336 | goto error_exit_unregister; |
| 2337 | } |
| 2338 | |
| 2339 | rc = PEC_Packet_Put(PEC_INTERFACE_ID, |
| 2340 | &Cmd, |
| 2341 | 1, |
| 2342 | &count); |
| 2343 | if (rc != PEC_STATUS_OK && count != 1) { |
| 2344 | rc = 1; |
| 2345 | CRYPTO_ERR("PEC_Packet_Put error\n"); |
| 2346 | goto error_exit_unregister; |
| 2347 | } |
| 2348 | |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 2349 | if (crypto_pe_busy_get_one(&OutTokenDscr, OutputToken, &Res, PEC_INTERFACE_ID) < 1) { |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2350 | rc = 1; |
| 2351 | CRYPTO_ERR("error from crypto_pe_busy_get_one\n"); |
| 2352 | goto error_exit_unregister; |
| 2353 | } |
| 2354 | memcpy(OutData_p, PktHostAddress.p, 16); |
| 2355 | |
| 2356 | |
| 2357 | error_exit_unregister: |
| 2358 | PEC_SA_UnRegister(PEC_INTERFACE_ID, SAHandle, DMABuf_NULLHandle, |
| 2359 | DMABuf_NULLHandle); |
| 2360 | |
| 2361 | error_exit: |
| 2362 | DMABuf_Release(SAHandle); |
| 2363 | DMABuf_Release(TokenHandle); |
| 2364 | DMABuf_Release(PktHandle); |
| 2365 | |
| 2366 | if (TCRData != NULL) |
| 2367 | kfree(TCRData); |
| 2368 | |
| 2369 | return rc == 0; |
| 2370 | |
| 2371 | } |
| 2372 | |
| 2373 | bool |
| 2374 | mtk_ddk_invalidate_rec( |
developer | 6ad3f36 | 2024-10-28 11:19:49 +0800 | [diff] [blame^] | 2375 | void *sa_p, |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2376 | const bool IsTransform) |
| 2377 | { |
| 2378 | PEC_Status_t PEC_Rc; |
| 2379 | PEC_CommandDescriptor_t Cmd; |
| 2380 | PEC_ResultDescriptor_t Res; |
| 2381 | unsigned int count; |
| 2382 | IOToken_Input_Dscr_t InTokenDscr; |
| 2383 | IOToken_Output_Dscr_t OutTokenDscr; |
| 2384 | uint32_t InputToken[IOTOKEN_IN_WORD_COUNT_IL]; |
| 2385 | uint32_t OutputToken[IOTOKEN_OUT_WORD_COUNT_IL]; |
| 2386 | void *InTokenDscrExt_p = NULL; |
| 2387 | void *OutTokenDscrExt_p = NULL; |
| 2388 | IOToken_Input_Dscr_Ext_t InTokenDscrExt; |
| 2389 | IOToken_Output_Dscr_Ext_t OutTokenDscrExt; |
developer | 6ad3f36 | 2024-10-28 11:19:49 +0800 | [diff] [blame^] | 2390 | DMABuf_Handle_t Rec_p; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2391 | |
developer | 6ad3f36 | 2024-10-28 11:19:49 +0800 | [diff] [blame^] | 2392 | Rec_p.p = sa_p; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2393 | ZEROINIT(InTokenDscrExt); |
| 2394 | InTokenDscrExt_p = &InTokenDscrExt; |
| 2395 | OutTokenDscrExt_p = &OutTokenDscrExt; |
| 2396 | |
| 2397 | ZEROINIT(InTokenDscr); |
| 2398 | |
| 2399 | // Fill in the command descriptor for the Invalidate command |
| 2400 | ZEROINIT(Cmd); |
| 2401 | |
| 2402 | Cmd.SrcPkt_Handle = DMABuf_NULLHandle; |
| 2403 | Cmd.DstPkt_Handle = DMABuf_NULLHandle; |
| 2404 | Cmd.SA_Handle1 = Rec_p; |
| 2405 | Cmd.SA_Handle2 = DMABuf_NULLHandle; |
| 2406 | Cmd.Token_Handle = DMABuf_NULLHandle; |
| 2407 | Cmd.SrcPkt_ByteCount = 0; |
| 2408 | |
| 2409 | #if defined(IOTOKEN_USE_HW_SERVICE) |
| 2410 | if (IsTransform) |
| 2411 | InTokenDscrExt.HW_Services = IOTOKEN_CMD_INV_TR; |
| 2412 | else |
| 2413 | InTokenDscrExt.HW_Services = IOTOKEN_CMD_INV_FR; |
| 2414 | #endif |
| 2415 | |
| 2416 | if (!crypto_iotoken_create(&InTokenDscr, InTokenDscrExt_p, InputToken, &Cmd)) |
| 2417 | return false; |
| 2418 | |
| 2419 | // Issue command |
| 2420 | PEC_Rc = PEC_Packet_Put(PEC_INTERFACE_ID, |
| 2421 | &Cmd, |
| 2422 | 1, |
| 2423 | &count); |
| 2424 | if (PEC_Rc != PEC_STATUS_OK || count != 1) { |
| 2425 | CRYPTO_ERR("%s: PEC_Packet_Put() error %d, count %d\n", |
| 2426 | __func__, |
| 2427 | PEC_Rc, |
| 2428 | count); |
| 2429 | return false; |
| 2430 | } |
| 2431 | |
| 2432 | // Receive the result packet ... do we care about contents ? |
developer | b3c41cd | 2024-06-04 17:52:08 +0800 | [diff] [blame] | 2433 | if (crypto_pe_busy_get_one(&OutTokenDscr, OutputToken, &Res, PEC_INTERFACE_ID) < 1) { |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2434 | CRYPTO_ERR("%s: crypto_pe_busy_get_one() failed\n", __func__); |
| 2435 | return false; |
| 2436 | } |
| 2437 | |
| 2438 | return true; |
| 2439 | } |
| 2440 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2441 | void set_capwap_algo(SABuilder_Params_t *params, uint8_t mode) |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2442 | { |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2443 | params->CryptoAlgo = SAB_CRYPTO_AES; |
| 2444 | params->CryptoMode = SAB_CRYPTO_MODE_CBC; |
| 2445 | params->KeyByteCount = 16; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2446 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2447 | switch (mode) { |
| 2448 | case AES256_CBC_HMAC_SHA1: |
| 2449 | params->KeyByteCount = 32; |
| 2450 | /* fallthrough */ |
| 2451 | case AES128_CBC_HMAC_SHA1: |
| 2452 | params->AuthAlgo = SAB_AUTH_HMAC_SHA1; |
| 2453 | params->AuthKeyByteCount = 20; |
| 2454 | break; |
| 2455 | case AES256_CBC_HMAC_SHA2_256: |
| 2456 | params->KeyByteCount = 32; |
| 2457 | /* fallthrough */ |
| 2458 | case AES128_CBC_HMAC_SHA2_256: |
| 2459 | params->AuthAlgo = SAB_AUTH_HMAC_SHA2_256; |
| 2460 | params->AuthKeyByteCount = 32; |
| 2461 | break; |
| 2462 | case AES256_GCM: |
| 2463 | params->KeyByteCount = 32; |
| 2464 | /* fallthrough */ |
| 2465 | case AES128_GCM: |
| 2466 | params->CryptoMode = SAB_CRYPTO_MODE_GCM; |
| 2467 | params->AuthAlgo = SAB_AUTH_AES_GCM; |
| 2468 | break; |
| 2469 | default: |
| 2470 | CRYPTO_ERR("No algorithms match for capwap-dtls\n"); |
| 2471 | params->CryptoAlgo = SAB_CRYPTO_NULL; |
| 2472 | break; |
| 2473 | } |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2474 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2475 | return; |
| 2476 | } |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2477 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2478 | void *mtk_ddk_tr_capwap_dtls_build( |
| 2479 | const bool capwap, |
| 2480 | struct DTLS_param *DTLSParam_p, u32 dir) |
| 2481 | { |
| 2482 | SABuilder_Status_t sa_status; |
| 2483 | SABuilder_Params_t params; |
| 2484 | SABuilder_Params_SSLTLS_t ssl_tls_params; |
| 2485 | uint16_t dtls_version; |
| 2486 | uint32_t sa_words; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2487 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2488 | static uint8_t zeros[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 2489 | uint8_t *hash_key = NULL; |
| 2490 | uint8_t *inner = NULL; |
| 2491 | uint8_t *outer = NULL; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2492 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2493 | DMABuf_Handle_t sa_handle = {0}; |
| 2494 | DMABuf_HostAddress_t sa_host_addr; |
| 2495 | DMABuf_Status_t DMAStatus; |
| 2496 | DMABuf_Properties_t DMAProperties = {0, 0, 0, 0}; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2497 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2498 | if (capwap) |
| 2499 | CRYPTO_INFO("Preparing Transforms for DTLS-CAPWAP\n"); |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2500 | else |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2501 | CRYPTO_INFO("Preparing Transforms for DTLS\n"); |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2502 | |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2503 | if (DTLSParam_p->dtls_version == MTK_DTLS_VERSION_1_0) |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2504 | dtls_version = SAB_DTLS_VERSION_1_0; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2505 | else if (DTLSParam_p->dtls_version == MTK_DTLS_VERSION_1_2) |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2506 | dtls_version = SAB_DTLS_VERSION_1_2; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2507 | else { |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2508 | CRYPTO_ERR("%s: Unknow dtls version: %u\n", __func__, DTLSParam_p->dtls_version); |
| 2509 | sa_handle.p = NULL; |
| 2510 | return sa_handle.p; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2511 | } |
| 2512 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2513 | sa_status = SABuilder_Init_SSLTLS(¶ms, |
| 2514 | &ssl_tls_params, |
| 2515 | dtls_version, |
| 2516 | dir); |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2517 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2518 | if (dir == SAB_DIRECTION_OUTBOUND) { |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2519 | params.Nonce_p = DTLSParam_p->dtls_encrypt_nonce; |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2520 | params.Key_p = DTLSParam_p->key_encrypt; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2521 | } else { |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2522 | params.Nonce_p = DTLSParam_p->dtls_decrypt_nonce; |
| 2523 | params.Key_p = DTLSParam_p->key_decrypt; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2524 | } |
| 2525 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2526 | set_capwap_algo(¶ms, DTLSParam_p->sec_mode); |
| 2527 | if (params.CryptoAlgo == SAB_CRYPTO_NULL) { |
| 2528 | sa_handle.p = NULL; |
| 2529 | return sa_handle.p; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2530 | } |
| 2531 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2532 | if (params.AuthAlgo == SAB_AUTH_AES_GCM) { |
| 2533 | hash_key = kcalloc(16, sizeof(uint8_t), GFP_KERNEL); |
| 2534 | if (hash_key == NULL) { |
| 2535 | CRYPTO_ERR("%s: kcalloc for hash key failed\n", __func__); |
| 2536 | sa_handle.p = NULL; |
| 2537 | return sa_handle.p; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2538 | } |
| 2539 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2540 | mtk_ddk_aes_block_encrypt(params.Key_p, 16, zeros, hash_key); |
| 2541 | |
| 2542 | /* Byte-swap the hash key */ |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2543 | { |
| 2544 | uint8_t t; |
| 2545 | unsigned int i; |
| 2546 | |
| 2547 | for (i = 0; i < 4; i++) { |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2548 | t = hash_key[4*i+3]; |
| 2549 | hash_key[4*i+3] = hash_key[4*i]; |
| 2550 | hash_key[4*i] = t; |
| 2551 | t = hash_key[4*i+2]; |
| 2552 | hash_key[4*i+2] = hash_key[4*i+1]; |
| 2553 | hash_key[4*i+1] = t; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2554 | } |
| 2555 | } |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2556 | params.AuthKey1_p = hash_key; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2557 | } else { |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2558 | /* Set authkey for HMAC */ |
| 2559 | inner = kcalloc((size_t) params.AuthKeyByteCount, sizeof(uint8_t), GFP_KERNEL); |
| 2560 | if (!inner) { |
| 2561 | CRYPTO_ERR("%s: kmalloc for hmac inner digest failed\n", __func__); |
| 2562 | sa_handle.p = NULL; |
| 2563 | return sa_handle.p; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2564 | } |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2565 | |
| 2566 | outer = kcalloc((size_t) params.AuthKeyByteCount, sizeof(uint8_t), GFP_KERNEL); |
| 2567 | if (!outer) { |
| 2568 | CRYPTO_ERR("%s: kmalloc for hmac outer digest failed\n", __func__); |
| 2569 | kfree(inner); |
| 2570 | sa_handle.p = NULL; |
| 2571 | return sa_handle.p; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2572 | } |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2573 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2574 | memset(inner, 0, params.AuthKeyByteCount); |
| 2575 | memset(outer, 0, params.AuthKeyByteCount); |
| 2576 | crypto_hmac_precompute(params.AuthAlgo, params.Key_p, |
| 2577 | params.AuthKeyByteCount, inner, outer); |
| 2578 | params.AuthKey1_p = inner; |
| 2579 | params.AuthKey2_p = outer; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2580 | } |
| 2581 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2582 | ssl_tls_params.epoch = DTLSParam_p->dtls_epoch; |
| 2583 | ssl_tls_params.SSLTLSFlags |= SAB_DTLS_PROCESS_IP_HEADERS | |
developer | 345e021 | 2024-08-08 16:33:02 +0800 | [diff] [blame] | 2584 | SAB_DTLS_EXT_PROCESSING; |
developer | 345e021 | 2024-08-08 16:33:02 +0800 | [diff] [blame] | 2585 | if (DTLSParam_p->net_type == MTK_DTLS_NET_IPV6) |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2586 | ssl_tls_params.SSLTLSFlags |= SAB_DTLS_IPV6; |
developer | 345e021 | 2024-08-08 16:33:02 +0800 | [diff] [blame] | 2587 | else |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2588 | ssl_tls_params.SSLTLSFlags |= SAB_DTLS_IPV4; |
developer | 345e021 | 2024-08-08 16:33:02 +0800 | [diff] [blame] | 2589 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2590 | if (capwap) |
| 2591 | ssl_tls_params.SSLTLSFlags |= SAB_DTLS_CAPWAP; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2592 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2593 | sa_status = SABuilder_GetSizes(¶ms, &sa_words, NULL, NULL); |
| 2594 | if (sa_status != SAB_STATUS_OK) { |
| 2595 | sa_handle.p = NULL; |
| 2596 | goto free_exit; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2597 | } |
| 2598 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2599 | /* Allocate a DMA-safe buffer for the SA. */ |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2600 | DMAProperties.fCached = true; |
| 2601 | DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT; |
| 2602 | DMAProperties.Bank = MTK_EIP197_INLINE_BANK_TRANSFORM; |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2603 | DMAProperties.Size = sa_words * sizeof(uint32_t); |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2604 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2605 | DMAStatus = DMABuf_Alloc(DMAProperties, &sa_host_addr, &sa_handle); |
| 2606 | if (DMAStatus != DMABUF_STATUS_OK) { |
| 2607 | CRYPTO_ERR("%s: allocate dma buffer for sa failed\n", __func__); |
| 2608 | sa_handle.p = NULL; |
| 2609 | goto free_exit; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2610 | } |
| 2611 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2612 | sa_status = SABuilder_BuildSA(¶ms, (uint32_t *) sa_host_addr.p, NULL, NULL); |
| 2613 | if (sa_status != SAB_STATUS_OK) { |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2614 | CRYPTO_ERR("%s: SA not created because of errors\n", __func__); |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2615 | DMABuf_Release(sa_handle); |
| 2616 | sa_handle.p = NULL; |
| 2617 | goto free_exit; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2618 | } |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2619 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2620 | free_exit: |
| 2621 | kfree(inner); |
| 2622 | kfree(outer); |
| 2623 | kfree(hash_key); |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2624 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2625 | return sa_handle.p; |
| 2626 | } |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2627 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2628 | int mtk_ddk_pcl_capwap_dtls_build( |
| 2629 | struct DTLS_param *DTLSParam_p, |
| 2630 | struct DTLSResourceMgmt *dtls_resource, u32 dir) |
| 2631 | { |
| 2632 | PCL_Status_t pcl_status; |
| 2633 | PCL_SelectorParams_t selector; |
| 2634 | PCL_DTL_TransformParams_t dtls_trans; |
| 2635 | PCL_DTL_Hash_Handle_t sa_hash_handle; |
| 2636 | DMABuf_Handle_t sa; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2637 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2638 | if (dir == SAB_DIRECTION_OUTBOUND) |
| 2639 | sa = dtls_resource->sa_out; |
| 2640 | else |
| 2641 | sa = dtls_resource->sa_in; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2642 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2643 | pcl_status = PCL_Transform_Register(sa); |
| 2644 | if (pcl_status != PCL_STATUS_OK) { |
| 2645 | CRYPTO_ERR("%s: PCL_Transform_Register outbound failed\n", __func__); |
| 2646 | return -1; |
| 2647 | } |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2648 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2649 | ZEROINIT(selector); |
| 2650 | ZEROINIT(dtls_trans); |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2651 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2652 | if (dir == SAB_DIRECTION_OUTBOUND) { |
developer | 345e021 | 2024-08-08 16:33:02 +0800 | [diff] [blame] | 2653 | if (DTLSParam_p->net_type == MTK_DTLS_NET_IPV6) { |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2654 | selector.flags = PCL_SELECT_IPV6; |
| 2655 | selector.SrcIp = ((unsigned char *)(&(DTLSParam_p->sip.ip6.addr))); |
| 2656 | selector.DstIp = ((unsigned char *)(&(DTLSParam_p->dip.ip6.addr))); |
developer | 345e021 | 2024-08-08 16:33:02 +0800 | [diff] [blame] | 2657 | } else { |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2658 | selector.flags = PCL_SELECT_IPV4; |
| 2659 | selector.SrcIp = ((unsigned char *)(&(DTLSParam_p->sip.ip4.addr32))); |
| 2660 | selector.DstIp = ((unsigned char *)(&(DTLSParam_p->dip.ip4.addr32))); |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2661 | } |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2662 | selector.epoch = 0; |
| 2663 | selector.SrcPort = DTLSParam_p->sport; |
| 2664 | selector.DstPort = DTLSParam_p->dport; |
| 2665 | } else { |
developer | 345e021 | 2024-08-08 16:33:02 +0800 | [diff] [blame] | 2666 | if (DTLSParam_p->net_type == MTK_DTLS_NET_IPV6) { |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2667 | selector.flags = PCL_SELECT_IPV6; |
| 2668 | /* For inbound, PCL SrcIP should be dip, |
| 2669 | * and DstIP should be sip in DTLSParam_p. |
| 2670 | */ |
| 2671 | selector.SrcIp = ((unsigned char *)(&(DTLSParam_p->dip.ip6.addr))); |
| 2672 | selector.DstIp = ((unsigned char *)(&(DTLSParam_p->sip.ip6.addr))); |
developer | 345e021 | 2024-08-08 16:33:02 +0800 | [diff] [blame] | 2673 | } else { |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2674 | selector.flags = PCL_SELECT_IPV4; |
| 2675 | /* For inbound, PCL SrcIP should be dip, |
| 2676 | * and DstIP should be sip in DTLSParam_p. |
| 2677 | */ |
| 2678 | selector.SrcIp = ((unsigned char *)(&(DTLSParam_p->dip.ip4.addr32))); |
| 2679 | selector.DstIp = ((unsigned char *)(&(DTLSParam_p->sip.ip4.addr32))); |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2680 | } |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2681 | selector.epoch = DTLSParam_p->dtls_epoch; |
| 2682 | /* src port and dst port should reverse for inbound, too */ |
| 2683 | selector.SrcPort = DTLSParam_p->dport; |
| 2684 | selector.DstPort = DTLSParam_p->sport; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2685 | } |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2686 | selector.IpProto = 17; |
| 2687 | selector.spi = 0; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2688 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2689 | pcl_status = PCL_Flow_Hash(&selector, dtls_trans.HashID); |
| 2690 | if (pcl_status != PCL_STATUS_OK) { |
| 2691 | CRYPTO_ERR("%s: PEC_Flow_Hash failed\n", __func__); |
| 2692 | goto unregister_exit; |
| 2693 | } |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2694 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2695 | pcl_status = PCL_DTL_Transform_Add(PCL_INTERFACE_ID, 0, |
| 2696 | &dtls_trans, sa, &sa_hash_handle); |
| 2697 | if (pcl_status != PCL_STATUS_OK) { |
| 2698 | CRYPTO_ERR("%s: PEC_DTL_Transform_Add failed\n", __func__); |
| 2699 | goto unregister_exit; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2700 | } |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2701 | return 0; |
| 2702 | unregister_exit: |
developer | 6ad3f36 | 2024-10-28 11:19:49 +0800 | [diff] [blame^] | 2703 | mtk_ddk_invalidate_rec(sa.p, true); |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2704 | PCL_Transform_UnRegister(sa); |
| 2705 | return -1; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2706 | } |
| 2707 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2708 | void mtk_ddk_remove_dtls_sa(struct DTLSResourceMgmt *dtls_res) |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2709 | { |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2710 | if (!dtls_res) { |
| 2711 | CRYPTO_ERR("Free NULL DTLS resource!\n"); |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2712 | return; |
| 2713 | } |
| 2714 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2715 | if (dtls_res->sa_out.p) { |
| 2716 | DMABuf_Release(dtls_res->sa_out); |
| 2717 | dtls_res->sa_out.p = NULL; |
| 2718 | } |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2719 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2720 | if (dtls_res->sa_in.p) { |
| 2721 | DMABuf_Release(dtls_res->sa_in); |
| 2722 | dtls_res->sa_in.p = NULL; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2723 | } |
| 2724 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2725 | return; |
| 2726 | } |
| 2727 | |
| 2728 | void mtk_ddk_remove_dtls_pcl(struct DTLSResourceMgmt *dtls_res, u32 dir) |
| 2729 | { |
| 2730 | PCL_Status_t pcl_status; |
| 2731 | DMABuf_Handle_t sa = {0}; |
| 2732 | int ret; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2733 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2734 | if (!dtls_res) |
| 2735 | return; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2736 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2737 | if (dir == SAB_DIRECTION_OUTBOUND) |
| 2738 | sa = dtls_res->sa_out; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2739 | else |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2740 | sa = dtls_res->sa_in; |
| 2741 | if (!sa.p) |
| 2742 | return; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2743 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2744 | pcl_status = PCL_DTL_Transform_Remove(PCL_INTERFACE_ID, 0, sa); |
developer | 6ad3f36 | 2024-10-28 11:19:49 +0800 | [diff] [blame^] | 2745 | ret = mtk_ddk_invalidate_rec(sa.p, true); |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2746 | pcl_status = PCL_Transform_UnRegister(sa); |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2747 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2748 | return; |
| 2749 | } |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2750 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2751 | void mtk_ddk_remove_dtls_param(struct DTLSResourceMgmt *dtls_res) |
| 2752 | { |
| 2753 | if (!dtls_res) |
| 2754 | return; |
| 2755 | mtk_ddk_remove_dtls_pcl(dtls_res, SAB_DIRECTION_OUTBOUND); |
| 2756 | mtk_ddk_remove_dtls_pcl(dtls_res, SAB_DIRECTION_INBOUND); |
| 2757 | mtk_ddk_remove_dtls_sa(dtls_res); |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame] | 2758 | |
developer | 230a1e3 | 2024-08-13 11:44:46 +0800 | [diff] [blame] | 2759 | return; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 2760 | } |