1 /***************************************************************************//**
2 * \file cy_crypto_server.c
3 * \version 2.120
4 *
5 * \brief
6 *  This file provides the source code to the API for Crypto Server
7 *  in the Crypto driver.
8 *
9 ********************************************************************************
10 * \copyright
11 * Copyright (c) (2020-2022), Cypress Semiconductor Corporation (an Infineon company) or
12 * an affiliate of Cypress Semiconductor Corporation.
13 * SPDX-License-Identifier: Apache-2.0
14 *
15 * Licensed under the Apache License, Version 2.0 (the "License");
16 * you may not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
18 *
19 *    http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
26 *******************************************************************************/
27 
28 #include "cy_device.h"
29 
30 #if defined (CY_IP_MXCRYPTO)
31 
32 #include "cy_crypto_server.h"
33 
34 #if defined(__cplusplus)
35 extern "C" {
36 #endif
37 
38 #include "cy_crypto_core_aes.h"
39 #include "cy_crypto_core_sha.h"
40 #include "cy_crypto_core_hmac.h"
41 #include "cy_crypto_core_cmac.h"
42 #include "cy_crypto_core_prng.h"
43 #include "cy_crypto_core_trng.h"
44 #include "cy_crypto_core_rsa.h"
45 #include "cy_crypto_core_mem.h"
46 #include "cy_crypto_core_crc.h"
47 #include "cy_crypto_core_des.h"
48 #include "cy_crypto_core_hw.h"
49 #include "cy_crypto_core_ecc.h"
50 #include "cy_ipc_drv.h"
51 #include "cy_sysint.h"
52 #include "cy_syslib.h"
53 #include <stdbool.h>
54 #include <string.h>
55 
56 /* The pointer to the CRYPTO instance. */
57 #define CY_CRYPTO_BASE           (CRYPTO)
58 
59 typedef struct
60 {
61 #if defined(CY_CRYPTO_CFG_PRNG_C)
62     cy_crypto_prng_init_func_t   prngInitFunc;
63     cy_crypto_prng_func_t        prngFunc;
64 #endif /* defined(CY_CRYPTO_CFG_PRNG_C) */
65 #if defined(CY_CRYPTO_CFG_TRNG_C)
66     cy_crypto_trng_func_t        trngFunc;
67 #endif /* defined(CY_CRYPTO_CFG_TRNG_C) */
68 #if defined(CY_CRYPTO_CFG_AES_C)
69     cy_crypto_aes_init_func_t    aesInitFunc;
70     cy_crypto_aes_ecb_func_t     aesEcbFunc;
71 #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CBC)
72     cy_crypto_aes_cbc_func_t     aesCbcFunc;
73 #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CBC) */
74 #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CFB)
75     cy_crypto_aes_cfb_func_t     aesCfbFunc;
76 #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CFB) */
77 #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CTR)
78     cy_crypto_aes_ctr_func_t     aesCtrFunc;
79 #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CTR) */
80 #if defined(CY_CRYPTO_CFG_CMAC_C)
81     cy_crypto_cmac_func_t        cmacFunc;
82 #endif /* defined(CY_CRYPTO_CFG_CMAC_C) */
83 #endif /* defined(CY_CRYPTO_CFG_AES_C) */
84 #if defined(CY_CRYPTO_CFG_SHA_C)
85     cy_crypto_sha_func_t         shaFunc;
86 #if defined(CY_CRYPTO_CFG_HMAC_C)
87     cy_crypto_hmac_func_t        hmacFunc;
88 #endif /* defined(CY_CRYPTO_CFG_HMAC_C) */
89 #endif /* defined(CY_CRYPTO_CFG_SHA_C) */
90     cy_crypto_memcpy_func_t      memCpyFunc;
91     cy_crypto_memset_func_t      memSetFunc;
92     cy_crypto_memcmp_func_t      memCmpFunc;
93     cy_crypto_memxor_func_t      memXorFunc;
94 #if defined(CY_CRYPTO_CFG_CRC_C)
95     cy_crypto_crc_init_func_t    crcInitFunc;
96     cy_crypto_crc_func_t         crcFunc;
97 #endif /* defined(CY_CRYPTO_CFG_CRC_C) */
98 #if defined(CY_CRYPTO_CFG_DES_C)
99     cy_crypto_des_func_t         desFunc;
100     cy_crypto_des_func_t         tdesFunc;
101 #endif /* defined(CY_CRYPTO_CFG_DES_C) */
102 #if defined(CY_CRYPTO_CFG_RSA_C)
103     cy_crypto_rsa_proc_func_t    rsaProcFunc;
104     cy_crypto_rsa_coef_func_t    rsaCoefFunc;
105 #if defined(CY_CRYPTO_CFG_RSA_VERIFY_ENABLED)
106     cy_crypto_rsa_ver_func_t     rsaVerifyFunc;
107 #endif /* defined(CY_CRYPTO_CFG_RSA_VERIFY_ENABLED) */
108 #endif /* defined(CY_CRYPTO_CFG_RSA_C) */
109 } cy_stc_crypto_pfn_t;
110 
111 static cy_stc_crypto_pfn_t const *cy_CryptoFunctionTable = NULL;
112 
113 /*
114  * The global variable to store a pointer to crypto processing context data.
115  */
116 static cy_stc_crypto_context_t *processData = NULL;
117 
118 static cy_stc_crypto_server_context_t *cy_crypto_serverContext;
119 
120 /* Functions Prototypes */
121 static cy_en_crypto_status_t Cy_Crypto_Core_CheckHwForErrors(cy_stc_crypto_context_t *cryptoContext);
122 
123 static cy_en_crypto_status_t Cy_Crypto_Server_Start_Common(cy_stc_crypto_config_t const *config,
124                                              cy_stc_crypto_server_context_t *context);
125 
Cy_Crypto_Server_Start(cy_stc_crypto_config_t const * config,cy_stc_crypto_server_context_t * context)126 cy_en_crypto_status_t Cy_Crypto_Server_Start(cy_stc_crypto_config_t const *config,
127                                              cy_stc_crypto_server_context_t *context)
128 {
129     cy_en_crypto_status_t status = CY_CRYPTO_HW_NOT_ENABLED;
130 
131     #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
132     static const cy_stc_crypto_pfn_t cryptoV1FullFuncs =
133     {
134     #if defined(CY_CRYPTO_CFG_PRNG_C)
135         &Cy_Crypto_Core_V1_Prng_Init,
136         &Cy_Crypto_Core_V1_Prng,
137     #endif /* defined(CY_CRYPTO_CFG_PRNG_C) */
138     #if defined(CY_CRYPTO_CFG_TRNG_C)
139         &Cy_Crypto_Core_Trng,
140     #endif /* defined(CY_CRYPTO_CFG_TRNG_C) */
141     #if defined(CY_CRYPTO_CFG_AES_C)
142         &Cy_Crypto_Core_V1_Aes_Init,
143         &Cy_Crypto_Core_V1_Aes_Ecb,
144     #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CBC)
145         &Cy_Crypto_Core_V1_Aes_Cbc,
146     #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CBC) */
147     #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CFB)
148         &Cy_Crypto_Core_V1_Aes_Cfb,
149     #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CFB) */
150     #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CTR)
151         &Cy_Crypto_Core_V1_Aes_Ctr,
152     #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CTR) */
153     #if defined(CY_CRYPTO_CFG_CMAC_C)
154         &Cy_Crypto_Core_V1_Cmac,
155     #endif /* defined(CY_CRYPTO_CFG_CMAC_C) */
156     #endif /* defined(CY_CRYPTO_CFG_AES_C) */
157     #if defined(CY_CRYPTO_CFG_SHA_C)
158         &Cy_Crypto_Core_V1_Sha,
159     #if defined(CY_CRYPTO_CFG_HMAC_C)
160         &Cy_Crypto_Core_V1_Hmac,
161     #endif /* defined(CY_CRYPTO_CFG_HMAC_C) */
162     #endif /* defined(CY_CRYPTO_CFG_SHA_C) */
163         &Cy_Crypto_Core_V1_MemCpy,
164         &Cy_Crypto_Core_V1_MemSet,
165         &Cy_Crypto_Core_V1_MemCmp,
166         &Cy_Crypto_Core_V1_MemXor,
167     #if defined(CY_CRYPTO_CFG_CRC_C)
168         &Cy_Crypto_Core_V1_Crc_Init,
169         &Cy_Crypto_Core_V1_Crc,
170     #endif /* defined(CY_CRYPTO_CFG_CRC_C) */
171     #if defined(CY_CRYPTO_CFG_DES_C)
172         &Cy_Crypto_Core_V1_Des,
173         &Cy_Crypto_Core_V1_Tdes,
174     #endif /* defined(CY_CRYPTO_CFG_DES_C) */
175     #if defined(CY_CRYPTO_CFG_RSA_C)
176         &Cy_Crypto_Core_Rsa_Proc,
177         &Cy_Crypto_Core_Rsa_Coef,
178     #if defined(CY_CRYPTO_CFG_RSA_VERIFY_ENABLED)
179         &Cy_Crypto_Core_Rsa_Verify,
180     #endif /* defined(CY_CRYPTO_CFG_RSA_VERIFY_ENABLED) */
181     #endif /* defined(CY_CRYPTO_CFG_RSA_C) */
182     };
183     #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
184 
185     #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
186     static const cy_stc_crypto_pfn_t cryptoV2FullFuncs =
187     {
188     #if defined(CY_CRYPTO_CFG_PRNG_C)
189         &Cy_Crypto_Core_V2_Prng_Init,
190         &Cy_Crypto_Core_V2_Prng,
191     #endif /* defined(CY_CRYPTO_CFG_PRNG_C) */
192     #if defined(CY_CRYPTO_CFG_TRNG_C)
193         &Cy_Crypto_Core_Trng,
194     #endif /* defined(CY_CRYPTO_CFG_TRNG_C) */
195     #if defined(CY_CRYPTO_CFG_AES_C)
196         &Cy_Crypto_Core_V2_Aes_Init,
197         &Cy_Crypto_Core_V2_Aes_Ecb,
198     #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CBC)
199         &Cy_Crypto_Core_V2_Aes_Cbc,
200     #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CBC) */
201     #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CFB)
202         &Cy_Crypto_Core_V2_Aes_Cfb,
203     #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CFB) */
204     #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CTR)
205         &Cy_Crypto_Core_V2_Aes_Ctr,
206     #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CTR) */
207     #if defined(CY_CRYPTO_CFG_CMAC_C)
208         &Cy_Crypto_Core_V2_Cmac,
209     #endif /* defined(CY_CRYPTO_CFG_CMAC_C) */
210     #endif /* defined(CY_CRYPTO_CFG_AES_C) */
211     #if defined(CY_CRYPTO_CFG_SHA_C)
212         &Cy_Crypto_Core_V2_Sha,
213     #if defined(CY_CRYPTO_CFG_HMAC_C)
214         &Cy_Crypto_Core_V2_Hmac,
215     #endif /* defined(CY_CRYPTO_CFG_HMAC_C) */
216     #endif /* defined(CY_CRYPTO_CFG_SHA_C) */
217         &Cy_Crypto_Core_V2_MemCpy,
218         &Cy_Crypto_Core_V2_MemSet,
219         &Cy_Crypto_Core_V2_MemCmp,
220         &Cy_Crypto_Core_V2_MemXor,
221     #if defined(CY_CRYPTO_CFG_CRC_C)
222         &Cy_Crypto_Core_V2_Crc_Init,
223         &Cy_Crypto_Core_V2_Crc,
224     #endif /* defined(CY_CRYPTO_CFG_CRC_C) */
225     #if defined(CY_CRYPTO_CFG_DES_C)
226         &Cy_Crypto_Core_V2_Des,
227         &Cy_Crypto_Core_V2_Tdes,
228     #endif /* defined(CY_CRYPTO_CFG_DES_C) */
229     #if defined(CY_CRYPTO_CFG_RSA_C)
230         &Cy_Crypto_Core_Rsa_Proc,
231         &Cy_Crypto_Core_Rsa_Coef,
232     #if defined(CY_CRYPTO_CFG_RSA_VERIFY_ENABLED)
233         &Cy_Crypto_Core_Rsa_Verify,
234     #endif /* defined(CY_CRYPTO_CFG_RSA_VERIFY_ENABLED) */
235     #endif /* defined(CY_CRYPTO_CFG_RSA_C) */
236     };
237     #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
238 
239     if (CY_CRYPTO_V1)
240     {
241         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
242         cy_CryptoFunctionTable = &cryptoV1FullFuncs;
243         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
244     }
245     else
246     {
247         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
248         cy_CryptoFunctionTable = &cryptoV2FullFuncs;
249         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
250     }
251 
252     if (cy_CryptoFunctionTable != NULL)
253     {
254         status = Cy_Crypto_Server_Start_Common(config, context);
255     }
256     return status;
257 }
258 
Cy_Crypto_Server_Start_Common(cy_stc_crypto_config_t const * config,cy_stc_crypto_server_context_t * context)259 static cy_en_crypto_status_t Cy_Crypto_Server_Start_Common(cy_stc_crypto_config_t const *config,
260                                              cy_stc_crypto_server_context_t *context)
261 {
262     cy_israddress isrHandler;
263 
264     CY_ASSERT(NULL != config);
265     CY_ASSERT(NULL != context);
266     CY_ASSERT(NULL != cy_CryptoFunctionTable);
267 
268     context->ipcChannel = config->ipcChannel;
269     context->acquireNotifierChannel = config->acquireNotifierChannel;
270     context->getDataHandlerPtr  = config->userGetDataHandler;
271     context->errorHandlerPtr    = config->userErrorHandler;
272     context->isHwErrorOccured = false;
273     context->acquireNotifierConfig.intrSrc = config->acquireNotifierConfig.intrSrc;
274     context->cryptoErrorIntrConfig.intrSrc = config->cryptoErrorIntrConfig.intrSrc;
275 
276     #if !defined(CY_CRYPTO_CFG_HW_USE_MPN_SPECIFIC)
277     Cy_Crypto_Core_HwInit();
278     #endif
279 
280     /* Initialize the interrupt controller for CM0+ and IPC Interrupt */
281     if (config->userGetDataHandler != NULL)
282     {
283         isrHandler = config->userGetDataHandler;
284     }
285     else
286     {
287         isrHandler = &Cy_Crypto_Server_GetDataHandler;
288     }
289 
290     (void)Cy_SysInt_Init(&config->acquireNotifierConfig, isrHandler);
291 
292     #if defined (CY_IP_M7CPUSS)
293         CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 10.8','Intentional typecast to IRQn_Type enum.');
294         NVIC_EnableIRQ((IRQn_Type)((config->acquireNotifierConfig.intrSrc >> 16) & 0x00FFUL));
295     #else
296         NVIC_EnableIRQ(config->acquireNotifierConfig.intrSrc);
297     #endif
298 
299 
300     /* Do not bring up an IPC release interrupt here, only set up a notify interrupt */
301     Cy_IPC_Drv_SetInterruptMask(Cy_IPC_Drv_GetIntrBaseAddr(config->acquireNotifierChannel),
302                                                       CY_IPC_NO_NOTIFICATION, (1uL << config->ipcChannel));
303 
304     /* Initialize and enable an interrupt to handle possible Crypto HW errors */
305     if (config->userErrorHandler != NULL)
306     {
307         isrHandler = config->userErrorHandler;
308     }
309     else
310     {
311         isrHandler = &Cy_Crypto_Server_ErrorHandler;
312     }
313 
314     (void)Cy_SysInt_Init(&config->cryptoErrorIntrConfig, isrHandler);
315 
316     #if defined (CY_IP_M7CPUSS)
317         CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 10.8','Intentional typecast to IRQn_Type enum.');
318         NVIC_ClearPendingIRQ((IRQn_Type)((config->cryptoErrorIntrConfig.intrSrc >> 16) & 0x00FFUL));
319 
320         CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 10.8','Intentional typecast to IRQn_Type enum.');
321         NVIC_EnableIRQ((IRQn_Type)((config->cryptoErrorIntrConfig.intrSrc >> 16) & 0x00FFUL));
322     #else
323         NVIC_ClearPendingIRQ(config->cryptoErrorIntrConfig.intrSrc);
324         NVIC_EnableIRQ(config->cryptoErrorIntrConfig.intrSrc);
325     #endif /* (CY_IP_M7CPUSS) */
326 
327     Cy_Crypto_Core_SetInterruptMask(CY_CRYPTO_BASE, CY_CRYPTO_INTR_MASK_ERROR_MASK);
328 
329     cy_crypto_serverContext = context;
330 
331     return (CY_CRYPTO_SUCCESS);
332 }
333 
Cy_Crypto_Server_Stop(void)334 cy_en_crypto_status_t Cy_Crypto_Server_Stop(void)
335 {
336     if (NULL != cy_crypto_serverContext)
337     {
338         uint32_t interruptMask;
339 
340         #if defined (CY_IP_M7CPUSS)
341             CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 10.8','Intentional typecast to IRQn_Type enum.');
342             NVIC_DisableIRQ((IRQn_Type)((cy_crypto_serverContext->acquireNotifierConfig.intrSrc >> 16) & 0x00FFUL));
343 
344             CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 10.8','Intentional typecast to IRQn_Type enum.');
345             NVIC_ClearPendingIRQ((IRQn_Type)((cy_crypto_serverContext->acquireNotifierConfig.intrSrc >> 16) & 0x00FFUL));
346 
347             CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 10.8','Intentional typecast to IRQn_Type enum.');
348             NVIC_DisableIRQ((IRQn_Type)((cy_crypto_serverContext->cryptoErrorIntrConfig.intrSrc >> 16) & 0x00FFUL));
349 
350             CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 10.8','Intentional typecast to IRQn_Type enum.');
351             NVIC_ClearPendingIRQ((IRQn_Type)((cy_crypto_serverContext->cryptoErrorIntrConfig.intrSrc >> 16) & 0x00FFUL));
352         #else
353 
354             /* Disable the Notify interrupt from IPC */
355             NVIC_DisableIRQ(cy_crypto_serverContext->acquireNotifierConfig.intrSrc);
356             NVIC_ClearPendingIRQ(cy_crypto_serverContext->acquireNotifierConfig.intrSrc);
357 
358             /* Disable the Error interrupt from Crypto */
359             NVIC_DisableIRQ(cy_crypto_serverContext->cryptoErrorIntrConfig.intrSrc);
360             NVIC_ClearPendingIRQ(cy_crypto_serverContext->cryptoErrorIntrConfig.intrSrc);
361         #endif /* (CY_IP_M7CPUSS) */
362 
363         Cy_Crypto_Core_SetInterruptMask(CY_CRYPTO_BASE, 0u);
364 
365         /* Disable CRYPTO IPC interrupt */
366         interruptMask = Cy_IPC_Drv_ExtractAcquireMask(Cy_IPC_Drv_GetInterruptMask(Cy_IPC_Drv_GetIntrBaseAddr(cy_crypto_serverContext->acquireNotifierChannel)));
367 
368         Cy_IPC_Drv_SetInterruptMask(Cy_IPC_Drv_GetIntrBaseAddr(cy_crypto_serverContext->acquireNotifierChannel),
369                                                           CY_IPC_NO_NOTIFICATION,
370                                                           interruptMask & ~(1uL << cy_crypto_serverContext->ipcChannel));
371 
372         cy_crypto_serverContext->getDataHandlerPtr = NULL;
373         cy_crypto_serverContext->errorHandlerPtr = NULL;
374         cy_crypto_serverContext = NULL;
375         cy_CryptoFunctionTable  = NULL;
376     }
377 
378     return (CY_CRYPTO_SUCCESS);
379 }
380 
Cy_Crypto_Server_ErrorHandler(void)381 void Cy_Crypto_Server_ErrorHandler(void)
382 {
383     uint32_t interrupts;
384     interrupts = Cy_Crypto_Core_GetInterruptStatus(CY_CRYPTO_BASE) & CY_CRYPTO_INTR_ERROR_MASK;
385 
386     if (interrupts != 0u)
387     {
388         Cy_Crypto_Core_ClearInterrupt(CY_CRYPTO_BASE, interrupts);
389 
390         if (false == cy_crypto_serverContext->isHwErrorOccured)
391         {
392             /* HW error is not captured before */
393             cy_crypto_serverContext->hwErrorStatus.errorStatus0 = REG_CRYPTO_ERROR_STATUS0(CY_CRYPTO_BASE);
394             cy_crypto_serverContext->hwErrorStatus.errorStatus1 = REG_CRYPTO_ERROR_STATUS1(CY_CRYPTO_BASE);
395 
396             cy_crypto_serverContext->isHwErrorOccured = true;
397         }
398         else
399         {
400             /* HW error is already captured by Cy_Crypto_Core_CheckHwForErrors function */
401             cy_crypto_serverContext->isHwErrorOccured = false;
402         }
403     }
404 }
405 
Cy_Crypto_Server_Process(void)406 void Cy_Crypto_Server_Process(void)
407 {
408     cy_stc_crypto_context_t* myData = processData;
409 
410     if (myData != NULL)
411     {
412         /* Default error */
413         myData->resp = CY_CRYPTO_HW_NOT_ENABLED;
414 
415         if (CY_CRYPTO_INSTR_ENABLE == myData->instr)
416         {
417             myData->resp = Cy_Crypto_Core_Enable(CY_CRYPTO_BASE);
418         }
419         else
420         {
421             /* Check if Crypto HW is enabled */
422             if (Cy_Crypto_Core_IsEnabled(CY_CRYPTO_BASE))
423             {
424                 myData->resp = CY_CRYPTO_NOT_SUPPORTED;
425 
426                 if (NULL != cy_CryptoFunctionTable)
427                 {
428                     switch(myData->instr)
429                     {
430                     case CY_CRYPTO_INSTR_DISABLE:
431                          myData->resp = Cy_Crypto_Core_Disable(CY_CRYPTO_BASE);
432                         break;
433 
434                     case CY_CRYPTO_INSTR_SRV_INFO:
435                          myData->resp = Cy_Crypto_Core_GetLibInfo((cy_en_crypto_lib_info_t*)myData->xdata);
436                         break;
437 
438                     /* MEM_BUFF memory management */
439                     #if defined(CY_CRYPTO_CFG_RSA_C)
440                     case CY_CRYPTO_INSTR_MEMBUF_SET:
441                         {
442                             cy_stc_crypto_context_str_t *cfContext = myData->xdata;
443                             myData->resp = Cy_Crypto_Core_SetVuMemoryAddress(CY_CRYPTO_BASE, cfContext->src0, cfContext->dataSize);
444                         }
445                         break;
446 
447                     case CY_CRYPTO_INSTR_MEMBUF_ADDR:
448                         {
449                             cy_stc_crypto_context_str_t *cfContext = myData->xdata;
450                             *(uint32_t *)(cfContext->dst) = (uint32_t)Cy_Crypto_Core_GetVuMemoryAddress(CY_CRYPTO_BASE);
451                             myData->resp = CY_CRYPTO_SUCCESS;
452                         }
453                         break;
454 
455                     case CY_CRYPTO_INSTR_MEMBUF_SIZE:
456                         {
457                             cy_stc_crypto_context_str_t *cfContext = myData->xdata;
458                             *(uint32_t *)(cfContext->dst) = Cy_Crypto_Core_GetVuMemorySize(CY_CRYPTO_BASE);
459                             myData->resp = CY_CRYPTO_SUCCESS;
460                         }
461                         break;
462                     #endif /* defined(CY_CRYPTO_CFG_RSA_C) */
463 
464                     #if defined(CY_CRYPTO_CFG_PRNG_C)
465                     case CY_CRYPTO_INSTR_PRNG_INIT:
466                         if (NULL != cy_CryptoFunctionTable->prngInitFunc)
467                         {
468                             cy_stc_crypto_context_prng_t *cfContext = myData->xdata;
469                             myData->resp = (cy_CryptoFunctionTable->prngInitFunc)(CY_CRYPTO_BASE,
470                                 cfContext->lfsr32InitState, cfContext->lfsr31InitState, cfContext->lfsr29InitState);
471                         }
472                         break;
473 
474                     case CY_CRYPTO_INSTR_PRNG:
475                         if (NULL != cy_CryptoFunctionTable->prngFunc)
476                         {
477                             cy_stc_crypto_context_prng_t *cfContext = myData->xdata;
478                             myData->resp = (cy_CryptoFunctionTable->prngFunc)(CY_CRYPTO_BASE,
479                                 cfContext->max, cfContext->prngNum);
480                         }
481                         break;
482                     #endif /* defined(CY_CRYPTO_CFG_PRNG_C) */
483 
484                     #if defined(CY_CRYPTO_CFG_TRNG_C)
485                     case CY_CRYPTO_INSTR_TRNG:
486                         if (NULL != cy_CryptoFunctionTable->trngFunc)
487                         {
488                             cy_stc_crypto_context_trng_t *cfContext = myData->xdata;
489                             myData->resp = (cy_CryptoFunctionTable->trngFunc)(CY_CRYPTO_BASE,
490                                 cfContext->GAROPol, cfContext->FIROPol, cfContext->max, cfContext->trngNum);
491                         }
492                         break;
493                     #endif /* defined(CY_CRYPTO_CFG_TRNG_C) */
494 
495                     #if defined(CY_CRYPTO_CFG_AES_C)
496                     case CY_CRYPTO_INSTR_AES_INIT:
497                         if (NULL != cy_CryptoFunctionTable->aesInitFunc)
498                         {
499                             cy_stc_crypto_context_aes_t *cfContext = (cy_stc_crypto_context_aes_t *)myData->xdata;
500                             myData->resp =
501                                 (cy_CryptoFunctionTable->aesInitFunc)(CY_CRYPTO_BASE,
502                                     (uint8_t*)cfContext->key, cfContext->keyLength, &cfContext->aesState, (cy_stc_crypto_aes_buffers_t *)((void *)(Cy_Crypto_Core_GetVuMemoryAddress(CY_CRYPTO_BASE))));
503                         }
504                         break;
505 
506                     case CY_CRYPTO_INSTR_AES_ECB:
507                         if (NULL != cy_CryptoFunctionTable->aesEcbFunc)
508                         {
509                             cy_stc_crypto_context_aes_t *cfContext = (cy_stc_crypto_context_aes_t *)myData->xdata;
510                             myData->resp =
511                                 (cy_CryptoFunctionTable->aesEcbFunc)(CY_CRYPTO_BASE,
512                                 cfContext->dirMode,
513                                 (uint8_t*)cfContext->dst,
514                                 (uint8_t*)cfContext->src,
515                                 &cfContext->aesState);
516                         }
517                         break;
518 
519                     #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CBC)
520                     case CY_CRYPTO_INSTR_AES_CBC:
521                         if (NULL != cy_CryptoFunctionTable->aesCbcFunc)
522                         {
523                             cy_stc_crypto_context_aes_t *cfContext = (cy_stc_crypto_context_aes_t *)myData->xdata;
524                             myData->resp =
525                                 (cy_CryptoFunctionTable->aesCbcFunc)(CY_CRYPTO_BASE,
526                                     cfContext->dirMode,
527                                     cfContext->srcSize,
528                                     (uint8_t*)cfContext->ivPtr,
529                                     (uint8_t*)cfContext->dst,
530                                     (uint8_t*)cfContext->src,
531                                     &cfContext->aesState);
532                         }
533                         break;
534                     #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CBC) */
535 
536                     #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CFB)
537                     case CY_CRYPTO_INSTR_AES_CFB:
538                         if (NULL != cy_CryptoFunctionTable->aesCfbFunc)
539                         {
540                             cy_stc_crypto_context_aes_t *cfContext = (cy_stc_crypto_context_aes_t *)myData->xdata;
541                             myData->resp =
542                                 (cy_CryptoFunctionTable->aesCfbFunc)(CY_CRYPTO_BASE,
543                                     cfContext->dirMode,
544                                     cfContext->srcSize,
545                                     (uint8_t*)cfContext->ivPtr,
546                                     (uint8_t*)cfContext->dst,
547                                     (uint8_t*)cfContext->src,
548                                     &cfContext->aesState);
549                         }
550                         break;
551                     #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CFB) */
552 
553                     #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CTR)
554                     case CY_CRYPTO_INSTR_AES_CTR:
555                         if (NULL != cy_CryptoFunctionTable->aesCtrFunc)
556                         {
557                             cy_stc_crypto_context_aes_t *cfContext = (cy_stc_crypto_context_aes_t *)myData->xdata;
558                             myData->resp =
559                                 (cy_CryptoFunctionTable->aesCtrFunc)(CY_CRYPTO_BASE,
560                                     cfContext->srcSize,
561                                     cfContext->srcOffset,
562                                     (uint8_t*)cfContext->ivPtr,
563                                     (uint8_t*)cfContext->streamBlock,
564                                     (uint8_t*)cfContext->dst,
565                                     (uint8_t*)cfContext->src,
566                                     &cfContext->aesState);
567                         }
568                         break;
569                     #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CTR) */
570                     #endif /* defined(CY_CRYPTO_CFG_AES_C) */
571 
572                     #if defined(CY_CRYPTO_CFG_CMAC_C)
573                     case CY_CRYPTO_INSTR_CMAC:
574                         if (NULL != cy_CryptoFunctionTable->cmacFunc)
575                         {
576                             cy_stc_crypto_context_aes_t *cfContext = myData->xdata;
577                             myData->resp = (cy_CryptoFunctionTable->cmacFunc)(CY_CRYPTO_BASE,
578                                 (uint8_t*)cfContext->src, cfContext->srcSize, (uint8_t*)cfContext->key, cfContext->keyLength,
579                                 (uint8_t*)cfContext->dst, &cfContext->aesState);
580                         }
581                         break;
582                     #endif /* defined(CY_CRYPTO_CFG_CMAC_C) */
583 
584                     #if defined(CY_CRYPTO_CFG_SHA_C)
585                     case CY_CRYPTO_INSTR_SHA:
586                         if (NULL != cy_CryptoFunctionTable->shaFunc)
587                         {
588                             cy_stc_crypto_context_sha_t *cfContext = myData->xdata;
589                             myData->resp = (cy_CryptoFunctionTable->shaFunc)(CY_CRYPTO_BASE,
590                                 (uint8_t *)cfContext->message, cfContext->messageSize,
591                                 (uint8_t *)cfContext->dst, cfContext->mode);
592                         }
593                         break;
594                     #endif /* defined(CY_CRYPTO_CFG_SHA_C) */
595 
596                     #if defined(CY_CRYPTO_CFG_HMAC_C)
597                     case CY_CRYPTO_INSTR_HMAC:
598                         if (NULL != cy_CryptoFunctionTable->hmacFunc)
599                         {
600                             cy_stc_crypto_context_sha_t *cfContext = myData->xdata;
601                             myData->resp = (cy_CryptoFunctionTable->hmacFunc)(CY_CRYPTO_BASE,
602                                 (uint8_t *)cfContext->dst, (uint8_t *)cfContext->message, cfContext->messageSize,
603                                 (uint8_t *)cfContext->key, cfContext->keyLength, cfContext->mode);
604                         }
605                         break;
606                     #endif /* defined(CY_CRYPTO_CFG_HMAC_C) */
607 
608                     case CY_CRYPTO_INSTR_MEM_CPY:
609                         if (NULL != cy_CryptoFunctionTable->memCpyFunc)
610                         {
611                             cy_stc_crypto_context_str_t *cfContext = myData->xdata;
612                             (cy_CryptoFunctionTable->memCpyFunc)(CY_CRYPTO_BASE,
613                                 cfContext->dst, cfContext->src0, (uint16_t)cfContext->dataSize);
614                             myData->resp = CY_CRYPTO_SUCCESS;
615                         }
616                         break;
617 
618                     case CY_CRYPTO_INSTR_MEM_SET:
619                         if (NULL != cy_CryptoFunctionTable->memSetFunc)
620                         {
621                             cy_stc_crypto_context_str_t *cfContext = myData->xdata;
622                             (cy_CryptoFunctionTable->memSetFunc)(CY_CRYPTO_BASE,
623                                 cfContext->dst, (uint8_t)cfContext->data, (uint16_t)cfContext->dataSize);
624                             myData->resp = CY_CRYPTO_SUCCESS;
625                         }
626                         break;
627 
628                     case CY_CRYPTO_INSTR_MEM_CMP:
629                         if (NULL != cy_CryptoFunctionTable->memCmpFunc)
630                         {
631                             cy_stc_crypto_context_str_t *cfContext = myData->xdata;
632                             *(uint32_t *)(cfContext->dst) =
633                                 (cy_CryptoFunctionTable->memCmpFunc)(CY_CRYPTO_BASE,
634                                     cfContext->src0, cfContext->src1, (uint16_t)cfContext->dataSize);
635                             myData->resp = CY_CRYPTO_SUCCESS;
636                         }
637                         break;
638 
639                     case CY_CRYPTO_INSTR_MEM_XOR:
640                         if (NULL != cy_CryptoFunctionTable->memXorFunc)
641                         {
642                             cy_stc_crypto_context_str_t *cfContext = myData->xdata;
643                             (cy_CryptoFunctionTable->memXorFunc)(CY_CRYPTO_BASE,
644                                 cfContext->dst, cfContext->src0, cfContext->src1, (uint16_t)cfContext->dataSize);
645                             myData->resp = CY_CRYPTO_SUCCESS;
646                         }
647                         break;
648 
649                     #if defined(CY_CRYPTO_CFG_CRC_C)
650                     case CY_CRYPTO_INSTR_CRC_INIT:
651                         if (NULL != cy_CryptoFunctionTable->crcInitFunc)
652                         {
653                             cy_stc_crypto_context_crc_t *cfContext = myData->xdata;
654                             myData->resp = (cy_CryptoFunctionTable->crcInitFunc)(CY_CRYPTO_BASE,
655                                 cfContext->polynomial, cfContext->dataReverse, cfContext->dataXor, cfContext->remReverse, cfContext->remXor);
656                         }
657                         break;
658 
659                     case CY_CRYPTO_INSTR_CRC:
660                         if (NULL != cy_CryptoFunctionTable->crcFunc)
661                         {
662                             cy_stc_crypto_context_crc_t *cfContext = myData->xdata;
663                             myData->resp = (cy_CryptoFunctionTable->crcFunc)(CY_CRYPTO_BASE,
664                                 cfContext->crc, cfContext->data, cfContext->dataSize, cfContext->lfsrInitState);
665                         }
666                         break;
667                     #endif /* defined(CY_CRYPTO_CFG_CRC_C) */
668 
669                     #if defined(CY_CRYPTO_CFG_DES_C)
670                     case CY_CRYPTO_INSTR_DES:
671                         if (NULL != cy_CryptoFunctionTable->desFunc)
672                         {
673                             cy_stc_crypto_context_des_t *cfContext = myData->xdata;
674                             myData->resp = (cy_CryptoFunctionTable->desFunc)(CY_CRYPTO_BASE,
675                                 cfContext->dirMode, (uint8_t const *)cfContext->key, (uint8_t *)cfContext->dst, (uint8_t const *)cfContext->src);
676                         }
677                         break;
678 
679                     case CY_CRYPTO_INSTR_3DES:
680                         if (NULL != cy_CryptoFunctionTable->tdesFunc)
681                         {
682                             cy_stc_crypto_context_des_t *cfContext = myData->xdata;
683                             myData->resp = (cy_CryptoFunctionTable->tdesFunc)(CY_CRYPTO_BASE,
684                                 cfContext->dirMode, (uint8_t const *)cfContext->key, (uint8_t *)cfContext->dst, (uint8_t  const *)cfContext->src);
685                         }
686                         break;
687                     #endif /* defined(CY_CRYPTO_CFG_DES_C) */
688 
689                     #if defined(CY_CRYPTO_CFG_RSA_C)
690                     case CY_CRYPTO_INSTR_RSA_PROC:
691                         if (NULL != cy_CryptoFunctionTable->rsaProcFunc)
692                         {
693                             cy_stc_crypto_context_rsa_t *cfContext = myData->xdata;
694                             myData->resp = (cy_CryptoFunctionTable->rsaProcFunc)(CY_CRYPTO_BASE,
695                                 cfContext->key, (uint8_t const *)cfContext->message, cfContext->messageSize, (uint8_t *)cfContext->result);
696                         }
697                         break;
698 
699                     case CY_CRYPTO_INSTR_RSA_COEF:
700                         if (NULL != cy_CryptoFunctionTable->rsaCoefFunc)
701                         {
702                             cy_stc_crypto_context_rsa_t *cfContext = myData->xdata;
703                             myData->resp = (cy_CryptoFunctionTable->rsaCoefFunc)(CY_CRYPTO_BASE, cfContext->key);
704                         }
705                         break;
706 
707                     #if defined(CY_CRYPTO_CFG_RSA_VERIFY_ENABLED)
708                     case CY_CRYPTO_INSTR_RSA_VER:
709                         if (NULL != cy_CryptoFunctionTable->rsaVerifyFunc)
710                         {
711                             cy_stc_crypto_context_rsa_ver_t *cfContext = myData->xdata;
712                             myData->resp = (cy_CryptoFunctionTable->rsaVerifyFunc)(CY_CRYPTO_BASE,
713                                 cfContext->verResult, cfContext->digestType, (uint8_t const *)cfContext->hash,
714                                 (uint8_t const *)cfContext->decryptedSignature, cfContext->decryptedSignatureLength);
715                         }
716                         break;
717                     #endif /* defined(CY_CRYPTO_CFG_RSA_VERIFY_ENABLED) */
718                     #endif /* defined(CY_CRYPTO_CFG_RSA_C) */
719 
720                     #if defined(CY_CRYPTO_CFG_ECDSA_SIGN_C)
721                     case CY_CRYPTO_INSTR_ECDSA_SIGN:
722                         {
723                             cy_stc_crypto_context_ecc_t *cfContext = myData->xdata;
724                             myData->resp = Cy_Crypto_Core_ECC_SignHash(CY_CRYPTO_BASE,
725                                 cfContext->src0, cfContext->datalen, cfContext->dst0,
726                                 cfContext->key, cfContext->src1);
727                         }
728                         break;
729                     #endif /* defined(CY_CRYPTO_CFG_ECDSA_SIGN_C) */
730 
731                     #if defined(CY_CRYPTO_CFG_ECDSA_VERIFY_C)
732                     case CY_CRYPTO_INSTR_ECDSA_VER:
733                         {
734                             cy_stc_crypto_context_ecc_t *cfContext = myData->xdata;
735                             myData->resp = Cy_Crypto_Core_ECC_VerifyHash(CY_CRYPTO_BASE,
736                                 cfContext->src1, cfContext->src0, cfContext->datalen,
737                                 cfContext->dst0, cfContext->key);
738                         }
739                         break;
740                     #endif /* defined(CY_CRYPTO_CFG_ECDSA_VERIFY_C) */
741 
742                     default:
743                         myData->resp = CY_CRYPTO_NOT_SUPPORTED;
744                         break;
745                     }
746                 }
747 
748                 if (CY_CRYPTO_SUCCESS == myData->resp)
749                 {
750                     myData->resp = Cy_Crypto_Core_CheckHwForErrors(myData);
751                 }
752             }
753         }
754 
755         processData = NULL;
756 
757         /* Release the Crypto IPC channel with the Release interrupt */
758         (void)Cy_IPC_Drv_LockRelease(Cy_IPC_Drv_GetIpcBaseAddress(myData->ipcChannel),
759                 (NULL != myData->userCompleteCallback) ? (1uL << myData->releaseNotifierChannel) : CY_IPC_NO_NOTIFICATION);
760     }
761 }
762 
Cy_Crypto_Server_GetDataHandler(void)763 void Cy_Crypto_Server_GetDataHandler(void)
764 {
765     uint32_t interruptMasked;
766 
767     /*
768      * Check that there is really the IPC Crypto Notify interrupt,
769      * because the same line can be used for the IPC Crypto Release interrupt.
770      */
771 
772     interruptMasked = Cy_IPC_Drv_ExtractAcquireMask(Cy_IPC_Drv_GetInterruptStatusMasked(
773                                          Cy_IPC_Drv_GetIntrBaseAddr(cy_crypto_serverContext->acquireNotifierChannel)));
774 
775     if ((1uL << (cy_crypto_serverContext->ipcChannel)) == interruptMasked )
776     {
777         Cy_IPC_Drv_ClearInterrupt(Cy_IPC_Drv_GetIntrBaseAddr(cy_crypto_serverContext->acquireNotifierChannel), CY_IPC_NO_NOTIFICATION, interruptMasked);
778 
779         if(CY_IPC_DRV_SUCCESS == Cy_IPC_Drv_ReadMsgPtr(Cy_IPC_Drv_GetIpcBaseAddress(cy_crypto_serverContext->ipcChannel), (void**)&processData))
780         {
781             if (cy_crypto_serverContext->getDataHandlerPtr == NULL)
782             {
783                 Cy_Crypto_Server_Process();
784             }
785         }
786     }
787 }
788 
789 /*******************************************************************************
790 * Function Name: Cy_Crypto_Core_CheckHwForErrors
791 ****************************************************************************//**
792 *
793 * The function checks if a Crypto HW error occurred. If yes, copies the error
794 * information from the Crypto registers to the communication structure.
795 *
796 * This function available for CM0+ core only.
797 *
798 * This function is internal and should not to be called directly by user software
799 *
800 * \param cryptoContext
801 * The pointer to cy_stc_crypto_context_t structure which stores
802 * the Crypto driver context.
803 *
804 * \return
805 * \ref cy_en_crypto_status_t
806 *
807 *******************************************************************************/
Cy_Crypto_Core_CheckHwForErrors(cy_stc_crypto_context_t * cryptoContext)808 static cy_en_crypto_status_t Cy_Crypto_Core_CheckHwForErrors(cy_stc_crypto_context_t *cryptoContext)
809 {
810     cy_en_crypto_status_t tmpResult = CY_CRYPTO_SUCCESS;
811     uint32_t myErrorStatus0;
812     uint32_t myErrorStatus1;
813 
814     CY_ASSERT(NULL != cryptoContext);
815 
816     if (false == cy_crypto_serverContext->isHwErrorOccured)
817     {
818         /* HW error is not captured before */
819         myErrorStatus0 = REG_CRYPTO_ERROR_STATUS0(CY_CRYPTO_BASE);
820         myErrorStatus1 = REG_CRYPTO_ERROR_STATUS1(CY_CRYPTO_BASE);
821 
822         if (_FLD2VAL(CRYPTO_ERROR_STATUS1_VALID, myErrorStatus1) == 1u)
823         {
824             tmpResult = CY_CRYPTO_HW_ERROR;
825 
826             cy_crypto_serverContext->isHwErrorOccured = true;
827         }
828     }
829     else
830     {
831         /* HW error is already captured by error interrupt handler */
832         myErrorStatus0 = cy_crypto_serverContext->hwErrorStatus.errorStatus0;
833         myErrorStatus1 = cy_crypto_serverContext->hwErrorStatus.errorStatus1;
834 
835         if (_FLD2VAL(CRYPTO_ERROR_STATUS1_VALID, myErrorStatus1) == 1u)
836         {
837             tmpResult = CY_CRYPTO_HW_ERROR;
838         }
839 
840         cy_crypto_serverContext->isHwErrorOccured = false;
841     }
842 
843     cryptoContext->hwErrorStatus.errorStatus0 = myErrorStatus0;
844     cryptoContext->hwErrorStatus.errorStatus1 = myErrorStatus1;
845 
846     return (tmpResult);
847 }
848 
849 #if defined(__cplusplus)
850 }
851 #endif
852 
853 #endif /* CY_IP_MXCRYPTO */
854 
855 
856 /* [] END OF FILE */
857