blob: e704e77dd3a7aeac64ded7c31b3e49a156ffa699 [file] [log] [blame]
developer94c513e2023-08-21 17:33:25 +08001// 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>
developerd690dc92024-08-19 14:31:18 +080014#include <linux/jiffies.h>
developerb3c41cd2024-06-04 17:52:08 +080015#include <crypto/internal/hash.h>
developer94c513e2023-08-21 17:33:25 +080016
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>
developer94c513e2023-08-21 17:33:25 +080020
21#include "crypto-eip/crypto-eip.h"
22#include "crypto-eip/ddk-wrapper.h"
23#include "crypto-eip/internal.h"
developerebe38fd2024-03-22 14:36:06 +080024
developerb3c41cd2024-06-04 17:52:08 +080025void crypto_free_sa(void *sa_pointer, int ring)
developerebe38fd2024-03-22 14:36:06 +080026{
27 DMABuf_Handle_t SAHandle = {0};
28
developerb3c41cd2024-06-04 17:52:08 +080029 if (ring < 0)
30 return;
31
developerebe38fd2024-03-22 14:36:06 +080032 SAHandle.p = sa_pointer;
developerb3c41cd2024-06-04 17:52:08 +080033 PEC_SA_UnRegister(ring, SAHandle, DMABuf_NULLHandle,
developerebe38fd2024-03-22 14:36:06 +080034 DMABuf_NULLHandle);
35 DMABuf_Release(SAHandle);
36}
37
38void 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*/
47void crypto_free_pkt(void *pkt)
48{
49 DMABuf_Handle_t PktHandle = {0};
50
51 PktHandle.p = pkt;
52 DMABuf_Release(PktHandle);
53}
54
55void 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}
developer94c513e2023-08-21 17:33:25 +080080
81static 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
developerebe38fd2024-03-22 14:36:06 +080087 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
101unsigned int crypto_pe_busy_get_one(IOToken_Output_Dscr_t *const OutTokenDscr_p,
102 u32 *OutTokenData_p,
developerb3c41cd2024-06-04 17:52:08 +0800103 PEC_ResultDescriptor_t *RD_p, int ring)
developerebe38fd2024-03-22 14:36:06 +0800104{
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
developerb3c41cd2024-06-04 17:52:08 +0800119 pecres = PEC_Packet_Get(ring, RD_p, 1, &Counter);
developerebe38fd2024-03-22 14:36:06 +0800120 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
154unsigned int crypto_pe_get_one(IOToken_Output_Dscr_t *const OutTokenDscr_p,
155 u32 *OutTokenData_p,
developerb3c41cd2024-06-04 17:52:08 +0800156 PEC_ResultDescriptor_t *RD_p, int ring)
developerebe38fd2024-03-22 14:36:06 +0800157{
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 */
developerb3c41cd2024-06-04 17:52:08 +0800168 pecres = PEC_Packet_Get(ring, RD_p, 1, &Counter);
developerebe38fd2024-03-22 14:36:06 +0800169 if (pecres != PEC_STATUS_OK) {
170 /* IO error */
developerb3c41cd2024-06-04 17:52:08 +0800171 CRYPTO_ERR("PEC_Packet_Get error %d, ring id %d\n", pecres, ring);
developerebe38fd2024-03-22 14:36:06 +0800172 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
196SABuilder_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
220SABuilder_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
234SABuilder_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
developerb3c41cd2024-06-04 17:52:08 +0800260void mtk_crypto_ring3_handler(void)
developerebe38fd2024-03-22 14:36:06 +0800261{
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) {
developerb3c41cd2024-06-04 17:52:08 +0800270 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);
developerebe38fd2024-03-22 14:36:06 +0800273 return;
developer0bed96a2024-04-01 19:03:52 +0800274 }
developerb3c41cd2024-06-04 17:52:08 +0800275 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);
developerebe38fd2024-03-22 14:36:06 +0800277
developerb3c41cd2024-06-04 17:52:08 +0800278 ctx = crypto_tfm_ctx(rd->async->tfm);
279 if (crypto_pe_get_one(&OutTokenDscr, OutputToken, &Res, ctx->ring) < 1) {
developerebe38fd2024-03-22 14:36:06 +0800280 PEC_NotifyFunction_t CBFunc;
281
developerb3c41cd2024-06-04 17:52:08 +0800282 CBFunc = mtk_crypto_ring3_handler;
283 if (OutTokenDscr.ErrorCode == 0)
284 continue;
285 else if (OutTokenDscr.ErrorCode & BIT(9))
developerebe38fd2024-03-22 14:36:06 +0800286 ret = -EBADMSG;
developerb3c41cd2024-06-04 17:52:08 +0800287 else if (OutTokenDscr.ErrorCode == 0x4003)
developerebe38fd2024-03-22 14:36:06 +0800288 ret = 0;
developerb3c41cd2024-06-04 17:52:08 +0800289 else
developerebe38fd2024-03-22 14:36:06 +0800290 ret = 1;
291
292 CRYPTO_ERR("error from crypto_pe_get_one: %d\n", ret);
293 }
294
developerebe38fd2024-03-22 14:36:06 +0800295 ret = ctx->handle_result(rd, ret);
296
developerb3c41cd2024-06-04 17:52:08 +0800297 spin_lock_bh(&priv->mtk_eip_ring[3].ring_lock);
developerebe38fd2024-03-22 14:36:06 +0800298 list_del(&rd->list);
developerb3c41cd2024-06-04 17:52:08 +0800299 spin_unlock_bh(&priv->mtk_eip_ring[3].ring_lock);
developerebe38fd2024-03-22 14:36:06 +0800300 kfree(rd);
301 }
302}
303
developerb3c41cd2024-06-04 17:52:08 +0800304void mtk_crypto_ring2_handler(void)
developerebe38fd2024-03-22 14:36:06 +0800305{
developerb3c41cd2024-06-04 17:52:08 +0800306 struct mtk_crypto_result *rd;
307 struct mtk_crypto_context *ctx;
developerebe38fd2024-03-22 14:36:06 +0800308 IOToken_Output_Dscr_t OutTokenDscr;
developerb3c41cd2024-06-04 17:52:08 +0800309 PEC_ResultDescriptor_t Res;
310 uint32_t OutputToken[IOTOKEN_OUT_WORD_COUNT];
311 int ret = 0;
developerebe38fd2024-03-22 14:36:06 +0800312
developerb3c41cd2024-06-04 17:52:08 +0800313 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;
developerebe38fd2024-03-22 14:36:06 +0800318 }
developerb3c41cd2024-06-04 17:52:08 +0800319 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);
developerebe38fd2024-03-22 14:36:06 +0800321
developerb3c41cd2024-06-04 17:52:08 +0800322 ctx = crypto_tfm_ctx(rd->async->tfm);
323 if (crypto_pe_get_one(&OutTokenDscr, OutputToken, &Res, ctx->ring) < 1) {
324 PEC_NotifyFunction_t CBFunc;
developerebe38fd2024-03-22 14:36:06 +0800325
developerb3c41cd2024-06-04 17:52:08 +0800326 CBFunc = mtk_crypto_ring2_handler;
327 if (OutTokenDscr.ErrorCode == 0)
developerebe38fd2024-03-22 14:36:06 +0800328 continue;
developerb3c41cd2024-06-04 17:52:08 +0800329 else if (OutTokenDscr.ErrorCode & BIT(9))
330 ret = -EBADMSG;
331 else if (OutTokenDscr.ErrorCode == 0x4003)
332 ret = 0;
333 else
334 ret = 1;
developerebe38fd2024-03-22 14:36:06 +0800335
developerb3c41cd2024-06-04 17:52:08 +0800336 CRYPTO_ERR("error from crypto_pe_get_one: %d\n", ret);
developerebe38fd2024-03-22 14:36:06 +0800337 }
338
developerb3c41cd2024-06-04 17:52:08 +0800339 ret = ctx->handle_result(rd, ret);
developerebe38fd2024-03-22 14:36:06 +0800340
developerb3c41cd2024-06-04 17:52:08 +0800341 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);
developerebe38fd2024-03-22 14:36:06 +0800345 }
developerb3c41cd2024-06-04 17:52:08 +0800346}
developerebe38fd2024-03-22 14:36:06 +0800347
developerb3c41cd2024-06-04 17:52:08 +0800348void 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;
developerebe38fd2024-03-22 14:36:06 +0800356
developerb3c41cd2024-06-04 17:52:08 +0800357 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;
developerebe38fd2024-03-22 14:36:06 +0800362 }
developerb3c41cd2024-06-04 17:52:08 +0800363 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);
developerebe38fd2024-03-22 14:36:06 +0800365
developerb3c41cd2024-06-04 17:52:08 +0800366 ctx = crypto_tfm_ctx(rd->async->tfm);
367 if (crypto_pe_get_one(&OutTokenDscr, OutputToken, &Res, ctx->ring) < 1) {
368 PEC_NotifyFunction_t CBFunc;
developerebe38fd2024-03-22 14:36:06 +0800369
developerb3c41cd2024-06-04 17:52:08 +0800370 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;
developerebe38fd2024-03-22 14:36:06 +0800379
developerb3c41cd2024-06-04 17:52:08 +0800380 CRYPTO_ERR("error from crypto_pe_get_one: %d\n", ret);
381 }
developerebe38fd2024-03-22 14:36:06 +0800382
developerb3c41cd2024-06-04 17:52:08 +0800383 ret = ctx->handle_result(rd, ret);
developerebe38fd2024-03-22 14:36:06 +0800384
developerb3c41cd2024-06-04 17:52:08 +0800385 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);
developerebe38fd2024-03-22 14:36:06 +0800389 }
developerb3c41cd2024-06-04 17:52:08 +0800390}
developerebe38fd2024-03-22 14:36:06 +0800391
developerb3c41cd2024-06-04 17:52:08 +0800392void 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;
developerebe38fd2024-03-22 14:36:06 +0800400
developerb3c41cd2024-06-04 17:52:08 +0800401 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);
developerebe38fd2024-03-22 14:36:06 +0800409
developerb3c41cd2024-06-04 17:52:08 +0800410 ctx = crypto_tfm_ctx(rd->async->tfm);
411 if (crypto_pe_get_one(&OutTokenDscr, OutputToken, &Res, ctx->ring) < 1) {
412 PEC_NotifyFunction_t CBFunc;
developerebe38fd2024-03-22 14:36:06 +0800413
developerb3c41cd2024-06-04 17:52:08 +0800414 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;
developerebe38fd2024-03-22 14:36:06 +0800423
developerb3c41cd2024-06-04 17:52:08 +0800424 CRYPTO_ERR("error from crypto_pe_get_one: %d\n", ret);
425 }
developerebe38fd2024-03-22 14:36:06 +0800426
developerb3c41cd2024-06-04 17:52:08 +0800427 ret = ctx->handle_result(rd, ret);
developerebe38fd2024-03-22 14:36:06 +0800428
developerb3c41cd2024-06-04 17:52:08 +0800429 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 }
developerebe38fd2024-03-22 14:36:06 +0800434}
435
developerb3c41cd2024-06-04 17:52:08 +0800436void (*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
developerd690dc92024-08-19 14:31:18 +0800443void 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
developerb3c41cd2024-06-04 17:52:08 +0800459int mtk_crypto_ddk_alloc_buff(struct mtk_crypto_cipher_ctx *ctx, int dir, unsigned int digestsize,
460 struct mtk_crypto_engine_data *data)
developerebe38fd2024-03-22 14:36:06 +0800461{
developerebe38fd2024-03-22 14:36:06 +0800462 SABuilder_Params_t params;
463 SABuilder_Params_Basic_t ProtocolParams;
developerebe38fd2024-03-22 14:36:06 +0800464
465 DMABuf_Status_t DMAStatus;
466 DMABuf_Properties_t DMAProperties = {0, 0, 0, 0};
467 DMABuf_HostAddress_t SAHostAddress;
468 DMABuf_HostAddress_t TokenHostAddress;
developerebe38fd2024-03-22 14:36:06 +0800469
470 DMABuf_Handle_t SAHandle = {0};
471 DMABuf_Handle_t TokenHandle = {0};
developerb3c41cd2024-06-04 17:52:08 +0800472 unsigned int SAWords = 0;
developerebe38fd2024-03-22 14:36:06 +0800473
474 unsigned int TCRWords = 0;
developerebe38fd2024-03-22 14:36:06 +0800475 unsigned int TokenMaxWords = 0;
476
developerebe38fd2024-03-22 14:36:06 +0800477 IOToken_Input_Dscr_t InTokenDscr;
478 IOToken_Output_Dscr_t OutTokenDscr;
developerebe38fd2024-03-22 14:36:06 +0800479 void *InTokenDscrExt_p = NULL;
developerb3c41cd2024-06-04 17:52:08 +0800480 int rc;
developerebe38fd2024-03-22 14:36:06 +0800481
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
developerb3c41cd2024-06-04 17:52:08 +0800491 if (dir == MTK_CRYPTO_ENCRYPT)
developerebe38fd2024-03-22 14:36:06 +0800492 rc = SABuilder_Init_Basic(&params, &ProtocolParams, SAB_DIRECTION_OUTBOUND);
493 else
494 rc = SABuilder_Init_Basic(&params, &ProtocolParams, SAB_DIRECTION_INBOUND);
developerb3c41cd2024-06-04 17:52:08 +0800495
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
developerebe38fd2024-03-22 14:36:06 +0800502 if (rc) {
503 CRYPTO_ERR("SABuilder_Init_Basic failed: %d\n", rc);
developerb3c41cd2024-06-04 17:52:08 +0800504 return rc;
developerebe38fd2024-03-22 14:36:06 +0800505 }
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;
developerb3c41cd2024-06-04 17:52:08 +0800512 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
developerebe38fd2024-03-22 14:36:06 +0800528 if (params.CryptoMode == SAB_CRYPTO_MODE_CTR)
529 params.Nonce_p = (uint8_t *) &ctx->nonce;
developerb3c41cd2024-06-04 17:52:08 +0800530
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;
developerebe38fd2024-03-22 14:36:06 +0800536
537 rc = SABuilder_GetSizes(&params, &SAWords, NULL, NULL);
538 if (rc) {
539 CRYPTO_ERR("SA not created because of size errors: %d\n", rc);
developerb3c41cd2024-06-04 17:52:08 +0800540 return rc;
developerebe38fd2024-03-22 14:36:06 +0800541 }
542
developerb3c41cd2024-06-04 17:52:08 +0800543 data->sa_size = SAWords;
544
developerebe38fd2024-03-22 14:36:06 +0800545 DMAProperties.fCached = true;
546 DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT;
547 DMAProperties.Bank = MTK_EIP197_INLINE_BANK_TRANSFORM;
developerb3c41cd2024-06-04 17:52:08 +0800548 DMAProperties.Size = MAX(4 * SAWords, 256);
developerebe38fd2024-03-22 14:36:06 +0800549
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);
developerb3c41cd2024-06-04 17:52:08 +0800554 return rc;
developerebe38fd2024-03-22 14:36:06 +0800555 }
556
developerb3c41cd2024-06-04 17:52:08 +0800557 rc = SABuilder_BuildSA(&params, (u32 *) SAHostAddress.p, NULL, NULL);
developerebe38fd2024-03-22 14:36:06 +0800558 if (rc) {
559 CRYPTO_ERR("SA not created because of errors: %d\n", rc);
developerb3c41cd2024-06-04 17:52:08 +0800560 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;
developerebe38fd2024-03-22 14:36:06 +0800568 }
569
developerb3c41cd2024-06-04 17:52:08 +0800570 data->sa_addr = SAHostAddress.p;
571 data->sa_handle = SAHandle.p;
572
developerebe38fd2024-03-22 14:36:06 +0800573 /* Build Token */
574 rc = TokenBuilder_GetContextSize(&params, &TCRWords);
575 if (rc) {
developerb3c41cd2024-06-04 17:52:08 +0800576 CRYPTO_ERR("TokenBuilder_GetContextSize returned errors: f%d\n", rc);
577 goto release_sa;
developerebe38fd2024-03-22 14:36:06 +0800578 }
579
developerb3c41cd2024-06-04 17:52:08 +0800580 data->token_context = kmalloc(4 * TCRWords, GFP_KERNEL);
581 if (!data->token_context) {
developerebe38fd2024-03-22 14:36:06 +0800582 rc = 1;
583 CRYPTO_ERR("Allocation of TCR failed\n");
developerb3c41cd2024-06-04 17:52:08 +0800584 goto release_sa;
developerebe38fd2024-03-22 14:36:06 +0800585 }
586
developerb3c41cd2024-06-04 17:52:08 +0800587 rc = TokenBuilder_BuildContext(&params, data->token_context);
developerebe38fd2024-03-22 14:36:06 +0800588 if (rc) {
589 CRYPTO_ERR("TokenBuilder_BuildContext failed: %d\n", rc);
developerb3c41cd2024-06-04 17:52:08 +0800590 goto release_context;
developerebe38fd2024-03-22 14:36:06 +0800591 }
592
developerb3c41cd2024-06-04 17:52:08 +0800593 rc = TokenBuilder_GetSize(data->token_context, &TokenMaxWords);
developerebe38fd2024-03-22 14:36:06 +0800594 if (rc) {
595 CRYPTO_ERR("TokenBuilder_GetSize failed: %d\n", rc);
developerb3c41cd2024-06-04 17:52:08 +0800596 goto release_context;
developerebe38fd2024-03-22 14:36:06 +0800597 }
developerb3c41cd2024-06-04 17:52:08 +0800598 data->token_size = TokenMaxWords;
developerebe38fd2024-03-22 14:36:06 +0800599
600 DMAProperties.fCached = true;
601 DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT;
602 DMAProperties.Bank = MTK_EIP197_INLINE_BANK_TOKEN;
developerb3c41cd2024-06-04 17:52:08 +0800603 DMAProperties.Size = 4 * TokenMaxWords;
developerebe38fd2024-03-22 14:36:06 +0800604
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);
developerb3c41cd2024-06-04 17:52:08 +0800609 goto release_context;
developerebe38fd2024-03-22 14:36:06 +0800610 }
developerb3c41cd2024-06-04 17:52:08 +0800611 data->token_addr = TokenHostAddress.p;
612 data->token_handle = TokenHandle.p;
613 data->valid = 1;
developerebe38fd2024-03-22 14:36:06 +0800614
developerb3c41cd2024-06-04 17:52:08 +0800615 return rc;
616
617release_context:
618 kfree(data->token_context);
619release_sa:
620 DMABuf_Release(SAHandle);
621
622 return rc;
623}
624
625int 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;
developerebe38fd2024-03-22 14:36:06 +0800705 }
706
developerb3c41cd2024-06-04 17:52:08 +0800707 /* Check dst buffer has enough size */
developerebe38fd2024-03-22 14:36:06 +0800708 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");
developerb3c41cd2024-06-04 17:52:08 +0800716 goto error_remove_sg;
developerebe38fd2024-03-22 14:36:06 +0800717 }
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");
developerb3c41cd2024-06-04 17:52:08 +0800722 goto error_remove_sg;
developerebe38fd2024-03-22 14:36:06 +0800723 }
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);
developerb3c41cd2024-06-04 17:52:08 +0800729 goto error_remove_sg;
developerebe38fd2024-03-22 14:36:06 +0800730 }
731 dma_map_sg(crypto_dev, dst, mtk_req->nr_dst, DMA_FROM_DEVICE);
732 }
733
developerb3c41cd2024-06-04 17:52:08 +0800734 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 */
developerebe38fd2024-03-22 14:36:06 +0800746 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
developerb3c41cd2024-06-04 17:52:08 +0800752 pass_id = 0;
developerebe38fd2024-03-22 14:36:06 +0800753 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
developerb3c41cd2024-06-04 17:52:08 +0800764 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);
developerebe38fd2024-03-22 14:36:06 +0800794 }
developerebe38fd2024-03-22 14:36:06 +0800795
796 totlen_src -= len;
797 if (!totlen_src)
798 break;
799 }
800
developerb3c41cd2024-06-04 17:52:08 +0800801 /* Alloc sg list for result */
developerebe38fd2024-03-22 14:36:06 +0800802 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);
developerb3c41cd2024-06-04 17:52:08 +0800823 if (rc != PEC_STATUS_OK)
824 pr_notice("PEC_SGList_Write failed rc = %d\n", rc);
developerebe38fd2024-03-22 14:36:06 +0800825
826 if (unlikely(!len))
827 break;
828 totlen_dst -= len;
829 }
830
developerb3c41cd2024-06-04 17:52:08 +0800831 /* Build Token */
developerebe38fd2024-03-22 14:36:06 +0800832 ZEROINIT(TokenParams);
developerb3c41cd2024-06-04 17:52:08 +0800833
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,
developerebe38fd2024-03-22 14:36:06 +0800849 &TokenWords, &TokenHeaderWord);
developerebe38fd2024-03-22 14:36:06 +0800850 if (rc != TKB_STATUS_OK) {
851 CRYPTO_ERR("Token builder failed: %d\n", rc);
852 goto error_remove_sg;
853 }
854
developerb3c41cd2024-06-04 17:52:08 +0800855 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
developerebe38fd2024-03-22 14:36:06 +0800859 ZEROINIT(Cmd);
860 Cmd.Token_Handle = TokenHandle;
861 Cmd.Token_WordCount = TokenWords;
862 Cmd.SrcPkt_Handle = SrcSGListHandle;
developerb3c41cd2024-06-04 17:52:08 +0800863 Cmd.SrcPkt_ByteCount = src_pkt;
developerebe38fd2024-03-22 14:36:06 +0800864 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
developerb3c41cd2024-06-04 17:52:08 +0800881 rc = PEC_Packet_Put(ring, &Cmd, 1, &count);
developerebe38fd2024-03-22 14:36:06 +0800882 if (rc != PEC_STATUS_OK && count != 1) {
developerebe38fd2024-03-22 14:36:06 +0800883 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);
developerebe38fd2024-03-22 14:36:06 +0800893 result->eip.pkt_handle = SrcSGListHandle.p;
894 result->async = async;
895 result->dst = DstSGListHandle.p;
developerd690dc92024-08-19 14:31:18 +0800896 CBFunc = mtk_crypto_interrupt_handler[ring];
developerebe38fd2024-03-22 14:36:06 +0800897
developerb3c41cd2024-06-04 17:52:08 +0800898 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);
developerd690dc92024-08-19 14:31:18 +0800901
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 }
developerebe38fd2024-03-22 14:36:06 +0800915 }
developerb3c41cd2024-06-04 17:52:08 +0800916
917 return rc;
developerebe38fd2024-03-22 14:36:06 +0800918
919error_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
developerb3c41cd2024-06-04 17:52:08 +0800927 if (aad != NULL)
928 kfree(aad);
929
developerebe38fd2024-03-22 14:36:06 +0800930 crypto_free_sglist(SrcSGListHandle.p);
931 crypto_free_sglist(DstSGListHandle.p);
932
developerebe38fd2024-03-22 14:36:06 +0800933 return rc;
934}
935
936SABuilder_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
964int 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;
developerb3c41cd2024-06-04 17:52:08 +0800968 struct ahash_request *areq = ahash_request_cast(async);
969 struct mtk_crypto_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
developerebe38fd2024-03-22 14:36:06 +0800970
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;
developerb3c41cd2024-06-04 17:52:08 +0800984 int ring = ctx->base.ring;
developerebe38fd2024-03-22 14:36:06 +0800985
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
developerb3c41cd2024-06-04 17:52:08 +08001077 rc = PEC_Packet_Put(ring, &Cmd, 1, &count);
developerebe38fd2024-03-22 14:36:06 +08001078 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);
developerb3c41cd2024-06-04 17:52:08 +08001091 result->eip.token_handle = TokenHandle.p;
developerebe38fd2024-03-22 14:36:06 +08001092 result->eip.pkt_handle = PktHandle.p;
1093 result->async = async;
1094 result->dst = PktHostAddress.p;
1095
developerb3c41cd2024-06-04 17:52:08 +08001096 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);
developerebe38fd2024-03-22 14:36:06 +08001101
1102 return rc;
1103
1104error_exit_unregister:
developerb3c41cd2024-06-04 17:52:08 +08001105 PEC_SA_UnRegister(ring, SAHandle, DMABuf_NULLHandle,
developerebe38fd2024-03-22 14:36:06 +08001106 DMABuf_NULLHandle);
1107
1108error_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
1119int 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;
developerb3c41cd2024-06-04 17:52:08 +08001128 int ring = ctx->base.ring;
developerebe38fd2024-03-22 14:36:06 +08001129
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(&params, &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(&params, &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(&params, (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(&params, &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(&params, 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
developerb3c41cd2024-06-04 17:52:08 +08001264 rc = PEC_SA_Register(ring, SAHandle, DMABuf_NULLHandle,
developerebe38fd2024-03-22 14:36:06 +08001265 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 }
developer94c513e2023-08-21 17:33:25 +08001303
developerb3c41cd2024-06-04 17:52:08 +08001304 rc = PEC_Packet_Put(ring, &Cmd, 1, &count);
developerebe38fd2024-03-22 14:36:06 +08001305 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;
developer94c513e2023-08-21 17:33:25 +08001309 }
1310
developerebe38fd2024-03-22 14:36:06 +08001311 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);
developerb3c41cd2024-06-04 17:52:08 +08001318 result->eip.sa_handle = SAHandle.p;
1319 result->eip.token_handle = TokenHandle.p;
developerebe38fd2024-03-22 14:36:06 +08001320 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;
developer94c513e2023-08-21 17:33:25 +08001325
developerb3c41cd2024-06-04 17:52:08 +08001326 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);
developerebe38fd2024-03-22 14:36:06 +08001329
developerb3c41cd2024-06-04 17:52:08 +08001330 CBFunc = mtk_crypto_interrupt_handler[ring];
1331 rc = PEC_ResultNotify_Request(ring, CBFunc, 1);
developerebe38fd2024-03-22 14:36:06 +08001332 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
1338error_exit_unregister:
developerb3c41cd2024-06-04 17:52:08 +08001339 PEC_SA_UnRegister(ring, SAHandle, DMABuf_NULLHandle,
developerebe38fd2024-03-22 14:36:06 +08001340 DMABuf_NULLHandle);
1341
1342error_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;
developer94c513e2023-08-21 17:33:25 +08001351}
1352
developerebe38fd2024-03-22 14:36:06 +08001353int 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)
developer94c513e2023-08-21 17:33:25 +08001356{
developerebe38fd2024-03-22 14:36:06 +08001357 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;
developerb3c41cd2024-06-04 17:52:08 +08001364 int ring = ctx->base.ring;
developer94c513e2023-08-21 17:33:25 +08001365
developerebe38fd2024-03-22 14:36:06 +08001366 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;
developer94c513e2023-08-21 17:33:25 +08001371
developerebe38fd2024-03-22 14:36:06 +08001372 DMABuf_Handle_t TokenHandle = {0};
1373 DMABuf_Handle_t PktHandle = {0};
1374 DMABuf_Handle_t SAHandle = {0};
developer94c513e2023-08-21 17:33:25 +08001375
developerebe38fd2024-03-22 14:36:06 +08001376 unsigned int TokenMaxWords = 0;
1377 unsigned int TokenHeaderWord;
1378 unsigned int TokenWords = 0;
1379 unsigned int TCRWords = 0;
1380 void *TCRData = 0;
developer94c513e2023-08-21 17:33:25 +08001381
developerebe38fd2024-03-22 14:36:06 +08001382 TokenBuilder_Params_t TokenParams;
1383 PEC_CommandDescriptor_t Cmd;
1384 PEC_NotifyFunction_t CBFunc;
developer94c513e2023-08-21 17:33:25 +08001385
developerebe38fd2024-03-22 14:36:06 +08001386 unsigned int count;
1387 int i;
developer94c513e2023-08-21 17:33:25 +08001388
developerebe38fd2024-03-22 14:36:06 +08001389 u32 InputToken[IOTOKEN_IN_WORD_COUNT];
1390 IOToken_Output_Dscr_t OutTokenDscr;
1391 IOToken_Input_Dscr_t InTokenDscr;
1392 void *InTokenDscrExt_p = NULL;
developer94c513e2023-08-21 17:33:25 +08001393
developerebe38fd2024-03-22 14:36:06 +08001394#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(&params, &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]);
developer94c513e2023-08-21 17:33:25 +08001426 }
developerebe38fd2024-03-22 14:36:06 +08001427 }
developer94c513e2023-08-21 17:33:25 +08001428
developerebe38fd2024-03-22 14:36:06 +08001429 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(&params, &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;
developer94c513e2023-08-21 17:33:25 +08001451 }
1452
developerebe38fd2024-03-22 14:36:06 +08001453 rc = SABuilder_BuildSA(&params, (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 }
developer94c513e2023-08-21 17:33:25 +08001458
developerebe38fd2024-03-22 14:36:06 +08001459 rc = TokenBuilder_GetContextSize(&params, &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(&params, 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 }
developer94c513e2023-08-21 17:33:25 +08001509
developerb3c41cd2024-06-04 17:52:08 +08001510 rc = PEC_SA_Register(ring, SAHandle, DMABuf_NULLHandle,
developerebe38fd2024-03-22 14:36:06 +08001511 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
developerb3c41cd2024-06-04 17:52:08 +08001557 rc = PEC_Packet_Put(ring, &Cmd, 1, &count);
developerebe38fd2024-03-22 14:36:06 +08001558 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);
developerb3c41cd2024-06-04 17:52:08 +08001571 result->eip.token_handle = TokenHandle.p;
developerebe38fd2024-03-22 14:36:06 +08001572 result->eip.pkt_handle = PktHandle.p;
1573 result->async = async;
1574 result->dst = PktHostAddress.p;
1575
developerb3c41cd2024-06-04 17:52:08 +08001576 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);
developerebe38fd2024-03-22 14:36:06 +08001581
1582 return rc;
1583
1584error_exit_unregister:
developerb3c41cd2024-06-04 17:52:08 +08001585 PEC_SA_UnRegister(ring, SAHandle, DMABuf_NULLHandle,
developerebe38fd2024-03-22 14:36:06 +08001586 DMABuf_NULLHandle);
1587
1588error_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}
developer94c513e2023-08-21 17:33:25 +08001598
1599bool 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(&params, &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(&params, &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(&params, (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(&params, &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(&params, 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
developer94c513e2023-08-21 17:33:25 +08001766#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
developerb3c41cd2024-06-04 17:52:08 +08001786 if (crypto_pe_busy_get_one(&OutTokenDscr, OutputToken, &Res, PEC_INTERFACE_ID) < 1) {
developer94c513e2023-08-21 17:33:25 +08001787 rc = 1;
developerebe38fd2024-03-22 14:36:06 +08001788 CRYPTO_ERR("error from crypto_pe_busy_get_one\n");
developer94c513e2023-08-21 17:33:25 +08001789 goto error_exit_unregister;
1790 }
1791 memcpy(Output_p, PktHostAddress.p, OutputByteCount);
1792
1793error_exit_unregister:
1794 PEC_SA_UnRegister(PEC_INTERFACE_ID, SAHandle, DMABuf_NULLHandle,
1795 DMABuf_NULLHandle);
1796
1797error_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
1808bool 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
1887static 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
1899static 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
1950u32 *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;
developer6ad3f362024-10-28 11:19:49 +08001958 uint8_t *inner = NULL;
1959 uint8_t *outer = NULL;
developer94c513e2023-08-21 17:33:25 +08001960
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
developer6ad3f362024-10-28 11:19:49 +08001967 PCL_Status_t pcl_status;
1968
developer94c513e2023-08-21 17:33:25 +08001969 sa_status = SABuilder_Init_ESP(&params,
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");
developer6ad3f362024-10-28 11:19:49 +08001978 sa_host_addr.p = NULL;
1979 goto error_ret;
developer94c513e2023-08-21 17:33:25 +08001980 }
1981
developer584d7e52023-12-13 19:28:49 +08001982 /* No support for aead now */
1983 if (xs->aead) {
1984 CRYPTO_ERR("AEAD not supported\n");
developer6ad3f362024-10-28 11:19:49 +08001985 sa_host_addr.p = NULL;
1986 goto error_ret;
developer584d7e52023-12-13 19:28:49 +08001987 }
1988
developer9d17aad2024-08-01 17:36:15 +08001989 /* Check algorithm exist in xfrm state*/
1990 if (!xs->ealg || !xs->aalg) {
1991 CRYPTO_ERR("NULL algorithm in xfrm state\n");
developer6ad3f362024-10-28 11:19:49 +08001992 sa_host_addr.p = NULL;
1993 goto error_ret;
developer9d17aad2024-08-01 17:36:15 +08001994 }
1995
developer94c513e2023-08-21 17:33:25 +08001996 /* 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, &params, inner, outer);
2004 if (set_auth_success != true) {
2005 CRYPTO_ERR("Set Auth Algo failed\n");
developer6ad3f362024-10-28 11:19:49 +08002006 sa_host_addr.p = NULL;
2007 goto error_ret;
developer94c513e2023-08-21 17:33:25 +08002008 }
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(&params, &SAWords, NULL, NULL);
2018 if (sa_status != SAB_STATUS_OK) {
2019 CRYPTO_ERR("SA not created because of size errors\n");
developer6ad3f362024-10-28 11:19:49 +08002020 sa_host_addr.p = NULL;
2021 goto error_ret;
developer94c513e2023-08-21 17:33:25 +08002022 }
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");
developer6ad3f362024-10-28 11:19:49 +08002032 sa_host_addr.p = NULL;
2033 goto error_ret;
developer94c513e2023-08-21 17:33:25 +08002034 }
2035
2036 sa_status = SABuilder_BuildSA(&params, (u32 *) sa_host_addr.p, NULL, NULL);
2037 if (sa_status != SAB_STATUS_OK) {
2038 CRYPTO_ERR("SA not created because of errors\n");
developer6ad3f362024-10-28 11:19:49 +08002039 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;
developer94c513e2023-08-21 17:33:25 +08002050 }
2051
developer6ad3f362024-10-28 11:19:49 +08002052 xfrm_params->p_handle = (u32 *) sa_handle.p;
2053
2054error_ret:
developer94c513e2023-08-21 17:33:25 +08002055 kfree(inner);
2056 kfree(outer);
2057 return (u32 *) sa_host_addr.p;
2058}
2059
2060int 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;
developerb3c41cd2024-06-04 17:52:08 +08002066 u32 j;
developer49489b82024-03-28 15:18:08 +08002067#ifdef PEC_PCL_EIP197
2068 PCL_Status_t pcl_sta;
2069#endif
developer94c513e2023-08-21 17:33:25 +08002070
developer49489b82024-03-28 15:18:08 +08002071#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
developerb3c41cd2024-06-04 17:52:08 +08002084 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 }
developer94c513e2023-08-21 17:33:25 +08002093
developerb3c41cd2024-06-04 17:52:08 +08002094 mdelay(MTK_EIP197_INLINE_RETRY_DELAY_MS);
2095 i--;
2096 }
developer94c513e2023-08-21 17:33:25 +08002097 }
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);
developer49489b82024-03-28 15:18:08 +08002107#ifdef PEC_PCL_EIP197
2108 PCL_UnInit(PCL_INTERFACE_ID);
2109#endif
developer94c513e2023-08-21 17:33:25 +08002110 return pec_sta;
2111 }
2112
2113 CRYPTO_INFO("PEC Capabilities: %s\n", pec_cap.szTextDescription);
2114
2115 return 0;
2116}
2117
2118void mtk_ddk_pec_deinit(void)
2119{
developer49489b82024-03-28 15:18:08 +08002120 unsigned int LoopCounter = MTK_EIP197_INLINE_NOF_TRIES;
2121 PEC_Status_t PEC_Status;
developerb3c41cd2024-06-04 17:52:08 +08002122 int j;
developer49489b82024-03-28 15:18:08 +08002123
developerb3c41cd2024-06-04 17:52:08 +08002124 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");
developer49489b82024-03-28 15:18:08 +08002140 return;
2141 }
developer49489b82024-03-28 15:18:08 +08002142 }
2143
2144#ifdef PEC_PCL_EIP197
2145 PCL_DTL_UnInit(PCL_INTERFACE_ID);
2146 PCL_UnInit(PCL_INTERFACE_ID);
2147#endif
2148}
2149
2150bool
2151mtk_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(&params, &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(&params, &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(&params, (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(&params, &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(&params, 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
developerb3c41cd2024-06-04 17:52:08 +08002349 if (crypto_pe_busy_get_one(&OutTokenDscr, OutputToken, &Res, PEC_INTERFACE_ID) < 1) {
developer49489b82024-03-28 15:18:08 +08002350 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
2357error_exit_unregister:
2358 PEC_SA_UnRegister(PEC_INTERFACE_ID, SAHandle, DMABuf_NULLHandle,
2359 DMABuf_NULLHandle);
2360
2361error_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
2373bool
2374mtk_ddk_invalidate_rec(
developer6ad3f362024-10-28 11:19:49 +08002375 void *sa_p,
developer49489b82024-03-28 15:18:08 +08002376 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;
developer6ad3f362024-10-28 11:19:49 +08002390 DMABuf_Handle_t Rec_p;
developer49489b82024-03-28 15:18:08 +08002391
developer6ad3f362024-10-28 11:19:49 +08002392 Rec_p.p = sa_p;
developer49489b82024-03-28 15:18:08 +08002393 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 ?
developerb3c41cd2024-06-04 17:52:08 +08002433 if (crypto_pe_busy_get_one(&OutTokenDscr, OutputToken, &Res, PEC_INTERFACE_ID) < 1) {
developer49489b82024-03-28 15:18:08 +08002434 CRYPTO_ERR("%s: crypto_pe_busy_get_one() failed\n", __func__);
2435 return false;
2436 }
2437
2438 return true;
2439}
2440
developer230a1e32024-08-13 11:44:46 +08002441void set_capwap_algo(SABuilder_Params_t *params, uint8_t mode)
developer49489b82024-03-28 15:18:08 +08002442{
developer230a1e32024-08-13 11:44:46 +08002443 params->CryptoAlgo = SAB_CRYPTO_AES;
2444 params->CryptoMode = SAB_CRYPTO_MODE_CBC;
2445 params->KeyByteCount = 16;
developer49489b82024-03-28 15:18:08 +08002446
developer230a1e32024-08-13 11:44:46 +08002447 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 }
developer49489b82024-03-28 15:18:08 +08002474
developer230a1e32024-08-13 11:44:46 +08002475 return;
2476}
developer49489b82024-03-28 15:18:08 +08002477
developer230a1e32024-08-13 11:44:46 +08002478void *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;
developer49489b82024-03-28 15:18:08 +08002487
developer230a1e32024-08-13 11:44:46 +08002488 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;
developer49489b82024-03-28 15:18:08 +08002492
developer230a1e32024-08-13 11:44:46 +08002493 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};
developer49489b82024-03-28 15:18:08 +08002497
developer230a1e32024-08-13 11:44:46 +08002498 if (capwap)
2499 CRYPTO_INFO("Preparing Transforms for DTLS-CAPWAP\n");
developer49489b82024-03-28 15:18:08 +08002500 else
developer230a1e32024-08-13 11:44:46 +08002501 CRYPTO_INFO("Preparing Transforms for DTLS\n");
developer49489b82024-03-28 15:18:08 +08002502
developer49489b82024-03-28 15:18:08 +08002503 if (DTLSParam_p->dtls_version == MTK_DTLS_VERSION_1_0)
developer230a1e32024-08-13 11:44:46 +08002504 dtls_version = SAB_DTLS_VERSION_1_0;
developer49489b82024-03-28 15:18:08 +08002505 else if (DTLSParam_p->dtls_version == MTK_DTLS_VERSION_1_2)
developer230a1e32024-08-13 11:44:46 +08002506 dtls_version = SAB_DTLS_VERSION_1_2;
developer49489b82024-03-28 15:18:08 +08002507 else {
developer230a1e32024-08-13 11:44:46 +08002508 CRYPTO_ERR("%s: Unknow dtls version: %u\n", __func__, DTLSParam_p->dtls_version);
2509 sa_handle.p = NULL;
2510 return sa_handle.p;
developer49489b82024-03-28 15:18:08 +08002511 }
2512
developer230a1e32024-08-13 11:44:46 +08002513 sa_status = SABuilder_Init_SSLTLS(&params,
2514 &ssl_tls_params,
2515 dtls_version,
2516 dir);
developer49489b82024-03-28 15:18:08 +08002517
developer230a1e32024-08-13 11:44:46 +08002518 if (dir == SAB_DIRECTION_OUTBOUND) {
developer49489b82024-03-28 15:18:08 +08002519 params.Nonce_p = DTLSParam_p->dtls_encrypt_nonce;
developer230a1e32024-08-13 11:44:46 +08002520 params.Key_p = DTLSParam_p->key_encrypt;
developer49489b82024-03-28 15:18:08 +08002521 } else {
developer230a1e32024-08-13 11:44:46 +08002522 params.Nonce_p = DTLSParam_p->dtls_decrypt_nonce;
2523 params.Key_p = DTLSParam_p->key_decrypt;
developer49489b82024-03-28 15:18:08 +08002524 }
2525
developer230a1e32024-08-13 11:44:46 +08002526 set_capwap_algo(&params, DTLSParam_p->sec_mode);
2527 if (params.CryptoAlgo == SAB_CRYPTO_NULL) {
2528 sa_handle.p = NULL;
2529 return sa_handle.p;
developer49489b82024-03-28 15:18:08 +08002530 }
2531
developer230a1e32024-08-13 11:44:46 +08002532 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;
developer49489b82024-03-28 15:18:08 +08002538 }
2539
developer230a1e32024-08-13 11:44:46 +08002540 mtk_ddk_aes_block_encrypt(params.Key_p, 16, zeros, hash_key);
2541
2542 /* Byte-swap the hash key */
developer49489b82024-03-28 15:18:08 +08002543 {
2544 uint8_t t;
2545 unsigned int i;
2546
2547 for (i = 0; i < 4; i++) {
developer230a1e32024-08-13 11:44:46 +08002548 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;
developer49489b82024-03-28 15:18:08 +08002554 }
2555 }
developer230a1e32024-08-13 11:44:46 +08002556 params.AuthKey1_p = hash_key;
developer49489b82024-03-28 15:18:08 +08002557 } else {
developer230a1e32024-08-13 11:44:46 +08002558 /* 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;
developer49489b82024-03-28 15:18:08 +08002564 }
developer230a1e32024-08-13 11:44:46 +08002565
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;
developer49489b82024-03-28 15:18:08 +08002572 }
developer49489b82024-03-28 15:18:08 +08002573
developer230a1e32024-08-13 11:44:46 +08002574 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;
developer49489b82024-03-28 15:18:08 +08002580 }
2581
developer230a1e32024-08-13 11:44:46 +08002582 ssl_tls_params.epoch = DTLSParam_p->dtls_epoch;
2583 ssl_tls_params.SSLTLSFlags |= SAB_DTLS_PROCESS_IP_HEADERS |
developer345e0212024-08-08 16:33:02 +08002584 SAB_DTLS_EXT_PROCESSING;
developer345e0212024-08-08 16:33:02 +08002585 if (DTLSParam_p->net_type == MTK_DTLS_NET_IPV6)
developer230a1e32024-08-13 11:44:46 +08002586 ssl_tls_params.SSLTLSFlags |= SAB_DTLS_IPV6;
developer345e0212024-08-08 16:33:02 +08002587 else
developer230a1e32024-08-13 11:44:46 +08002588 ssl_tls_params.SSLTLSFlags |= SAB_DTLS_IPV4;
developer345e0212024-08-08 16:33:02 +08002589
developer230a1e32024-08-13 11:44:46 +08002590 if (capwap)
2591 ssl_tls_params.SSLTLSFlags |= SAB_DTLS_CAPWAP;
developer49489b82024-03-28 15:18:08 +08002592
developer230a1e32024-08-13 11:44:46 +08002593 sa_status = SABuilder_GetSizes(&params, &sa_words, NULL, NULL);
2594 if (sa_status != SAB_STATUS_OK) {
2595 sa_handle.p = NULL;
2596 goto free_exit;
developer49489b82024-03-28 15:18:08 +08002597 }
2598
developer230a1e32024-08-13 11:44:46 +08002599 /* Allocate a DMA-safe buffer for the SA. */
developer49489b82024-03-28 15:18:08 +08002600 DMAProperties.fCached = true;
2601 DMAProperties.Alignment = MTK_EIP197_INLINE_DMA_ALIGNMENT_BYTE_COUNT;
2602 DMAProperties.Bank = MTK_EIP197_INLINE_BANK_TRANSFORM;
developer230a1e32024-08-13 11:44:46 +08002603 DMAProperties.Size = sa_words * sizeof(uint32_t);
developer49489b82024-03-28 15:18:08 +08002604
developer230a1e32024-08-13 11:44:46 +08002605 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;
developer49489b82024-03-28 15:18:08 +08002610 }
2611
developer230a1e32024-08-13 11:44:46 +08002612 sa_status = SABuilder_BuildSA(&params, (uint32_t *) sa_host_addr.p, NULL, NULL);
2613 if (sa_status != SAB_STATUS_OK) {
developer49489b82024-03-28 15:18:08 +08002614 CRYPTO_ERR("%s: SA not created because of errors\n", __func__);
developer230a1e32024-08-13 11:44:46 +08002615 DMABuf_Release(sa_handle);
2616 sa_handle.p = NULL;
2617 goto free_exit;
developer49489b82024-03-28 15:18:08 +08002618 }
developer49489b82024-03-28 15:18:08 +08002619
developer230a1e32024-08-13 11:44:46 +08002620free_exit:
2621 kfree(inner);
2622 kfree(outer);
2623 kfree(hash_key);
developer49489b82024-03-28 15:18:08 +08002624
developer230a1e32024-08-13 11:44:46 +08002625 return sa_handle.p;
2626}
developer49489b82024-03-28 15:18:08 +08002627
developer230a1e32024-08-13 11:44:46 +08002628int 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;
developer49489b82024-03-28 15:18:08 +08002637
developer230a1e32024-08-13 11:44:46 +08002638 if (dir == SAB_DIRECTION_OUTBOUND)
2639 sa = dtls_resource->sa_out;
2640 else
2641 sa = dtls_resource->sa_in;
developer49489b82024-03-28 15:18:08 +08002642
developer230a1e32024-08-13 11:44:46 +08002643 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 }
developer49489b82024-03-28 15:18:08 +08002648
developer230a1e32024-08-13 11:44:46 +08002649 ZEROINIT(selector);
2650 ZEROINIT(dtls_trans);
developer49489b82024-03-28 15:18:08 +08002651
developer230a1e32024-08-13 11:44:46 +08002652 if (dir == SAB_DIRECTION_OUTBOUND) {
developer345e0212024-08-08 16:33:02 +08002653 if (DTLSParam_p->net_type == MTK_DTLS_NET_IPV6) {
developer230a1e32024-08-13 11:44:46 +08002654 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)));
developer345e0212024-08-08 16:33:02 +08002657 } else {
developer230a1e32024-08-13 11:44:46 +08002658 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)));
developer49489b82024-03-28 15:18:08 +08002661 }
developer230a1e32024-08-13 11:44:46 +08002662 selector.epoch = 0;
2663 selector.SrcPort = DTLSParam_p->sport;
2664 selector.DstPort = DTLSParam_p->dport;
2665 } else {
developer345e0212024-08-08 16:33:02 +08002666 if (DTLSParam_p->net_type == MTK_DTLS_NET_IPV6) {
developer230a1e32024-08-13 11:44:46 +08002667 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)));
developer345e0212024-08-08 16:33:02 +08002673 } else {
developer230a1e32024-08-13 11:44:46 +08002674 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)));
developer49489b82024-03-28 15:18:08 +08002680 }
developer230a1e32024-08-13 11:44:46 +08002681 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;
developer49489b82024-03-28 15:18:08 +08002685 }
developer230a1e32024-08-13 11:44:46 +08002686 selector.IpProto = 17;
2687 selector.spi = 0;
developer49489b82024-03-28 15:18:08 +08002688
developer230a1e32024-08-13 11:44:46 +08002689 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 }
developer49489b82024-03-28 15:18:08 +08002694
developer230a1e32024-08-13 11:44:46 +08002695 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;
developer49489b82024-03-28 15:18:08 +08002700 }
developer230a1e32024-08-13 11:44:46 +08002701 return 0;
2702unregister_exit:
developer6ad3f362024-10-28 11:19:49 +08002703 mtk_ddk_invalidate_rec(sa.p, true);
developer230a1e32024-08-13 11:44:46 +08002704 PCL_Transform_UnRegister(sa);
2705 return -1;
developer49489b82024-03-28 15:18:08 +08002706}
2707
developer230a1e32024-08-13 11:44:46 +08002708void mtk_ddk_remove_dtls_sa(struct DTLSResourceMgmt *dtls_res)
developer49489b82024-03-28 15:18:08 +08002709{
developer230a1e32024-08-13 11:44:46 +08002710 if (!dtls_res) {
2711 CRYPTO_ERR("Free NULL DTLS resource!\n");
developer49489b82024-03-28 15:18:08 +08002712 return;
2713 }
2714
developer230a1e32024-08-13 11:44:46 +08002715 if (dtls_res->sa_out.p) {
2716 DMABuf_Release(dtls_res->sa_out);
2717 dtls_res->sa_out.p = NULL;
2718 }
developer49489b82024-03-28 15:18:08 +08002719
developer230a1e32024-08-13 11:44:46 +08002720 if (dtls_res->sa_in.p) {
2721 DMABuf_Release(dtls_res->sa_in);
2722 dtls_res->sa_in.p = NULL;
developer49489b82024-03-28 15:18:08 +08002723 }
2724
developer230a1e32024-08-13 11:44:46 +08002725 return;
2726}
2727
2728void 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;
developer49489b82024-03-28 15:18:08 +08002733
developer230a1e32024-08-13 11:44:46 +08002734 if (!dtls_res)
2735 return;
developer49489b82024-03-28 15:18:08 +08002736
developer230a1e32024-08-13 11:44:46 +08002737 if (dir == SAB_DIRECTION_OUTBOUND)
2738 sa = dtls_res->sa_out;
developer49489b82024-03-28 15:18:08 +08002739 else
developer230a1e32024-08-13 11:44:46 +08002740 sa = dtls_res->sa_in;
2741 if (!sa.p)
2742 return;
developer49489b82024-03-28 15:18:08 +08002743
developer230a1e32024-08-13 11:44:46 +08002744 pcl_status = PCL_DTL_Transform_Remove(PCL_INTERFACE_ID, 0, sa);
developer6ad3f362024-10-28 11:19:49 +08002745 ret = mtk_ddk_invalidate_rec(sa.p, true);
developer230a1e32024-08-13 11:44:46 +08002746 pcl_status = PCL_Transform_UnRegister(sa);
developer49489b82024-03-28 15:18:08 +08002747
developer230a1e32024-08-13 11:44:46 +08002748 return;
2749}
developer49489b82024-03-28 15:18:08 +08002750
developer230a1e32024-08-13 11:44:46 +08002751void 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);
developer49489b82024-03-28 15:18:08 +08002758
developer230a1e32024-08-13 11:44:46 +08002759 return;
developer94c513e2023-08-21 17:33:25 +08002760}