1 /***************************************************************************//**
2 * \file cy_crypto.c
3 * \version 2.120
4 *
5 * \brief
6 *  Provides API implementation of the Cypress PDL Crypto driver.
7 *
8 ********************************************************************************
9 * \copyright
10 * Copyright (c) (2020-2022), Cypress Semiconductor Corporation (an Infineon company) or
11 * an affiliate of Cypress Semiconductor Corporation.
12 * SPDX-License-Identifier: Apache-2.0
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License");
15 * you may not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 *    http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 *******************************************************************************/
26 
27 #include "cy_device.h"
28 
29 #if defined (CY_IP_MXCRYPTO)
30 
31 #include "cy_crypto.h"
32 
33 #if defined(__cplusplus)
34 extern "C" {
35 #endif
36 
37 #include "cy_ipc_drv.h"
38 #include "cy_sysint.h"
39 #include "cy_syslib.h"
40 
41 /*
42  * The global variable to store a pointer to the customer's defined context.
43  * This variable is global because it is accessed from an interrupt.
44  */
45 static cy_stc_crypto_context_t *clientContext = NULL;
46 
47 static void Cy_Crypto_Client_ReleaseIntrHndlr(void);
48 static bool Cy_Crypto_IsServerStarted(cy_stc_crypto_context_t const *context);
49 static bool Cy_Crypto_IsServerReady(cy_stc_crypto_context_t const *context);
50 static cy_en_crypto_status_t Cy_Crypto_Client_Send(void);
51 
52 /*******************************************************************************
53 * Function Name: Cy_Crypto_Client_ReleaseIntrHndlr
54 ****************************************************************************//**
55 *
56 * The interrupt handler for the Crypto IPC Release interrupt; happens
57 * when Crypto hardware completes operation. This function clears the specific
58 * interrupt source and calls the customer interrupt handler.
59 *
60 * This function is internal and should not to be called directly by user software
61 *
62 *******************************************************************************/
Cy_Crypto_Client_ReleaseIntrHndlr(void)63 static void Cy_Crypto_Client_ReleaseIntrHndlr(void)
64 {
65     uint32_t interruptMasked;
66 
67     interruptMasked = Cy_IPC_Drv_ExtractReleaseMask(Cy_IPC_Drv_GetInterruptStatusMasked(Cy_IPC_Drv_GetIntrBaseAddr(clientContext->releaseNotifierChannel)));
68 
69     /* Check that there is really the IPC Crypto Release interrupt */
70     if((1uL << clientContext->ipcChannel) == interruptMasked)
71     {
72         Cy_IPC_Drv_ClearInterrupt(Cy_IPC_Drv_GetIntrBaseAddr(clientContext->releaseNotifierChannel),
73                                                         interruptMasked, CY_IPC_NO_NOTIFICATION);
74 
75         if (clientContext->userCompleteCallback != NULL)
76         {
77             (clientContext->userCompleteCallback)();
78         }
79     }
80 }
81 
82 /*******************************************************************************
83 * Function Name: Cy_Crypto_IsServerStarted
84 ****************************************************************************//**
85 *
86 * Checks whether the CryptoServer is started.
87 *
88 * This function is internal and should not to be called directly by user software
89 *
90 * \param context
91 * The pointer to the \ref cy_stc_crypto_context_t structure that stores
92 * the Crypto server context.
93 *
94 * \return
95 * True  - CryptoServer is started.
96 * False - CryptoServer is not started.
97 *
98 *******************************************************************************/
Cy_Crypto_IsServerStarted(cy_stc_crypto_context_t const * context)99 static bool Cy_Crypto_IsServerStarted(cy_stc_crypto_context_t const *context)
100 {
101     /* Check whether the CryptoServer is started (Crypto IPC Notify interrupt is started) */
102     return ((1uL << (context->ipcChannel)) ==
103         Cy_IPC_Drv_ExtractAcquireMask(Cy_IPC_Drv_GetInterruptMask(Cy_IPC_Drv_GetIntrBaseAddr(context->acquireNotifierChannel))));
104 }
105 
106 /*******************************************************************************
107 * Function Name: Cy_Crypto_IsServerReady
108 ****************************************************************************//**
109 *
110 * Checks whether the CryptoServer is ready for operations.
111 *
112 * This function is internal and should not to be called directly by user software.
113 *
114 * \param context
115 * The pointer to the \ref cy_stc_crypto_context_t structure that stores
116 * the Crypto server context.
117 *
118 * \return
119 * True  - CryptoServer is ready.
120 * False - CryptoServer is not ready.
121 *
122 *******************************************************************************/
Cy_Crypto_IsServerReady(cy_stc_crypto_context_t const * context)123 static bool Cy_Crypto_IsServerReady(cy_stc_crypto_context_t const *context)
124 {
125     return (!Cy_IPC_Drv_IsLockAcquired(Cy_IPC_Drv_GetIpcBaseAddress(context->ipcChannel)));
126 }
127 
Cy_Crypto_Sync(bool isBlocking)128 cy_en_crypto_status_t Cy_Crypto_Sync(bool isBlocking)
129 {
130     cy_en_crypto_status_t status = CY_CRYPTO_NOT_INITIALIZED;
131 
132     if (NULL != clientContext)
133     {
134         if (CY_CRYPTO_INSTR_UNKNOWN != clientContext->instr)
135         {
136             status = CY_CRYPTO_SERVER_NOT_STARTED;
137 
138             /* Check if the Crypto server started (Crypto IPC Notify interrupt is started) */
139             if (Cy_Crypto_IsServerStarted(clientContext))
140             {
141 
142                 if (!isBlocking)
143                 {
144                     status = CY_CRYPTO_SERVER_BUSY;
145 
146                     if (Cy_Crypto_IsServerReady(clientContext))
147                     {
148                         status = clientContext->resp;
149                     }
150                 }
151                 else
152                 {
153                     while (!Cy_Crypto_IsServerReady(clientContext))
154                     {
155                     }
156                     status = clientContext->resp;
157                 }
158             }
159         }
160     }
161 
162     return (status);
163 }
164 
165 /*******************************************************************************
166 * Function Name: Cy_Crypto_Client_Send
167 ****************************************************************************//**
168 *
169 * This function sends a pointer to the Crypto Server.
170 *
171 * This function is internal and should not to be called directly by user software.
172 *
173 * \return
174 * \ref cy_en_crypto_status_t
175 *
176 *******************************************************************************/
Cy_Crypto_Client_Send(void)177 static cy_en_crypto_status_t Cy_Crypto_Client_Send(void)
178 {
179     cy_en_crypto_status_t status = CY_CRYPTO_SERVER_NOT_STARTED;
180 
181     if (Cy_Crypto_IsServerStarted(clientContext))
182     {
183         status = CY_CRYPTO_SERVER_BUSY;
184 
185         if (Cy_Crypto_IsServerReady(clientContext))
186         {
187             status = CY_CRYPTO_SUCCESS;
188 
189             if (CY_IPC_DRV_SUCCESS != Cy_IPC_Drv_SendMsgPtr(Cy_IPC_Drv_GetIpcBaseAddress(clientContext->ipcChannel), (1uL << clientContext->acquireNotifierChannel), clientContext))
190             {
191                 status = CY_CRYPTO_COMM_FAIL;
192             }
193         }
194     }
195 
196     return (status);
197 }
198 
Cy_Crypto_GetErrorStatus(cy_stc_crypto_hw_error_t * hwErrorCause)199 cy_en_crypto_status_t Cy_Crypto_GetErrorStatus(cy_stc_crypto_hw_error_t *hwErrorCause)
200 {
201     if(NULL != hwErrorCause)
202     {
203         hwErrorCause->errorStatus0 = clientContext->hwErrorStatus.errorStatus0;
204         hwErrorCause->errorStatus1 = clientContext->hwErrorStatus.errorStatus1;
205     }
206 
207     return (clientContext->resp);
208 }
209 
Cy_Crypto_Init(cy_stc_crypto_config_t const * config,cy_stc_crypto_context_t * context)210 cy_en_crypto_status_t Cy_Crypto_Init(cy_stc_crypto_config_t const *config,
211                                      cy_stc_crypto_context_t *context)
212 {
213     cy_en_crypto_status_t status = CY_CRYPTO_SERVER_NOT_STARTED;
214 
215     CY_ASSERT(NULL != config);
216     CY_ASSERT(NULL != context);
217 
218     context->ipcChannel = config->ipcChannel;
219     context->acquireNotifierChannel = config->acquireNotifierChannel;
220     context->releaseNotifierChannel = config->releaseNotifierChannel;
221     context->userCompleteCallback   = config->userCompleteCallback;
222     context->releaseNotifierConfig.intrSrc = config->releaseNotifierConfig.intrSrc;
223 
224     /* Release the Crypto IPC channel with the Release interrupt */
225     (void)Cy_IPC_Drv_LockRelease(Cy_IPC_Drv_GetIpcBaseAddress(context->ipcChannel), CY_IPC_NO_NOTIFICATION);
226 
227     context->instr = CY_CRYPTO_INSTR_ENABLE;
228 
229     /* Check whether the Crypto server started (Crypto IPC Notify interrupt is started) */
230     if (Cy_Crypto_IsServerStarted(context))
231     {
232         /* Sets up a Release interrupt if the customer wants */
233         if (NULL != context->userCompleteCallback)
234         {
235             (void)Cy_SysInt_Init(&config->releaseNotifierConfig, &Cy_Crypto_Client_ReleaseIntrHndlr);
236 
237             /* Sets up the IPC Release interrupt here */
238             Cy_IPC_Drv_SetInterruptMask(Cy_IPC_Drv_GetIntrBaseAddr(context->releaseNotifierChannel),
239                                                             (1uL << context->ipcChannel), CY_IPC_NO_NOTIFICATION);
240 
241 
242             #if defined (CY_IP_M7CPUSS)
243                 CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 10.8','Intentional typecast to IRQn_Type enum.');
244                 NVIC_EnableIRQ((IRQn_Type)((context->releaseNotifierConfig.intrSrc >> 16) & 0x00FFUL));
245             #else
246                 NVIC_EnableIRQ(context->releaseNotifierConfig.intrSrc);
247             #endif
248         }
249 
250         clientContext = context;
251 
252         status = CY_CRYPTO_SUCCESS;
253     }
254 
255     return (status);
256 }
257 
Cy_Crypto_DeInit(void)258 cy_en_crypto_status_t Cy_Crypto_DeInit(void)
259 {
260     uint32_t interruptMasked;
261     cy_en_crypto_status_t err = CY_CRYPTO_SUCCESS;
262 
263     if (clientContext != NULL)
264     {
265         clientContext->instr = CY_CRYPTO_INSTR_UNKNOWN;
266 
267         /* If the release interrupt was enabled, disable it here */
268         if (NULL != clientContext->userCompleteCallback)
269         {
270             /* Disable the Release interrupt from IPC */
271             #if defined (CY_IP_M7CPUSS)
272                 CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 10.8','Intentional typecast to IRQn_Type enum.');
273                 NVIC_DisableIRQ((IRQn_Type)((clientContext->releaseNotifierConfig.intrSrc >> 16) & 0x00FFUL));
274             #else
275                 NVIC_DisableIRQ(clientContext->releaseNotifierConfig.intrSrc);
276             #endif
277 
278 
279             /* Re-set up the IPC Release interrupt here */
280             interruptMasked = Cy_IPC_Drv_ExtractReleaseMask(Cy_IPC_Drv_GetInterruptStatusMasked(Cy_IPC_Drv_GetIntrBaseAddr(clientContext->releaseNotifierChannel)));
281 
282             /* Check that there really is the IPC Crypto Release interrupt */
283             if ((1uL << clientContext->ipcChannel) == interruptMasked)
284             {
285                 Cy_IPC_Drv_ClearInterrupt(Cy_IPC_Drv_GetIntrBaseAddr(clientContext->releaseNotifierChannel), interruptMasked, CY_IPC_NO_NOTIFICATION);
286             }
287         }
288 
289         clientContext = NULL;
290     }
291     return (err);
292 }
293 
Cy_Crypto_Enable(void)294 cy_en_crypto_status_t Cy_Crypto_Enable(void)
295 {
296     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
297 
298     if (clientContext != NULL)
299     {
300         clientContext->instr = CY_CRYPTO_INSTR_ENABLE;
301         clientContext->xdata = NULL;
302 
303 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
304         /* Flush the cache */
305         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
306 #endif
307         err = Cy_Crypto_Client_Send();
308 
309 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
310         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
311 #endif
312         /* Wait until initialization completes */
313         if (CY_CRYPTO_SUCCESS == err)
314         {
315             err = Cy_Crypto_Sync(CY_CRYPTO_SYNC_BLOCKING);
316         }
317 
318     }
319     return (err);
320 }
321 
322 /*******************************************************************************
323 * Function Name: Cy_Crypto_GetLibraryInfo
324 ****************************************************************************//**
325 *
326 * This function gets information about Crypto library.
327 *
328 * \param cryptoInfo
329 * The pointer to a variable to store gathered crypto library information.
330 *
331 * \return
332 * \ref cy_en_crypto_status_t
333 *
334 *******************************************************************************/
Cy_Crypto_GetLibraryInfo(cy_en_crypto_lib_info_t * cryptoInfo)335 cy_en_crypto_status_t Cy_Crypto_GetLibraryInfo(cy_en_crypto_lib_info_t *cryptoInfo)
336 {
337     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
338 
339     if (clientContext != NULL)
340     {
341         clientContext->instr = CY_CRYPTO_INSTR_SRV_INFO;
342         clientContext->xdata = (void *)cryptoInfo;
343 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
344         /* Flush the cache */
345         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
346         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
347 #endif
348         err = Cy_Crypto_Client_Send();
349     }
350     return (err);
351 }
352 
Cy_Crypto_Disable(void)353 cy_en_crypto_status_t Cy_Crypto_Disable(void)
354 {
355     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
356 
357     if (clientContext != NULL)
358     {
359         clientContext->instr = CY_CRYPTO_INSTR_DISABLE;
360         clientContext->xdata = NULL;
361 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
362         /* Flush the cache */
363         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
364 #endif
365 
366         err = Cy_Crypto_Client_Send();
367 
368 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
369         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
370 #endif
371         /* Wait until initialization completes */
372         if (CY_CRYPTO_SUCCESS == err)
373         {
374             err = Cy_Crypto_Sync(CY_CRYPTO_SYNC_BLOCKING);
375         }
376     }
377    return (err);
378 }
379 
380 #if (CPUSS_CRYPTO_PR == 1) && defined(CY_CRYPTO_CFG_PRNG_C)
Cy_Crypto_Prng_Init(uint32_t lfsr32InitState,uint32_t lfsr31InitState,uint32_t lfsr29InitState,cy_stc_crypto_context_prng_t * cfContext)381 cy_en_crypto_status_t Cy_Crypto_Prng_Init(uint32_t lfsr32InitState,
382                                           uint32_t lfsr31InitState,
383                                           uint32_t lfsr29InitState,
384                                           cy_stc_crypto_context_prng_t *cfContext)
385 {
386     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
387 
388     if (clientContext != NULL)
389     {
390         clientContext->instr = CY_CRYPTO_INSTR_PRNG_INIT;
391         clientContext->xdata = (void *)cfContext;
392 
393         cfContext->lfsr32InitState = lfsr32InitState;
394         cfContext->lfsr31InitState = lfsr31InitState;
395         cfContext->lfsr29InitState = lfsr29InitState;
396 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
397         /* Flush the cache */
398         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
399         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
400         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
401 #endif
402 
403         err = Cy_Crypto_Client_Send();
404     }
405     return (err);
406 }
407 
Cy_Crypto_Prng_Generate(uint32_t max,uint32_t * randomNum,cy_stc_crypto_context_prng_t * cfContext)408 cy_en_crypto_status_t Cy_Crypto_Prng_Generate(uint32_t max,
409                                               uint32_t *randomNum,
410                                               cy_stc_crypto_context_prng_t *cfContext)
411 {
412     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
413 
414     if (clientContext != NULL)
415     {
416         clientContext->instr = CY_CRYPTO_INSTR_PRNG;
417         clientContext->xdata = (void *)cfContext;
418 
419         cfContext->max = max;
420         cfContext->prngNum = randomNum;
421 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
422         /* Flush the cache */
423         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
424         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
425         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
426         SCB_InvalidateDCache_by_Addr((volatile void *)randomNum, (int32_t)sizeof(uint32_t));
427 #endif
428 
429         err = Cy_Crypto_Client_Send();
430     }
431     return (err);
432 }
433 #endif /* (CPUSS_CRYPTO_PR == 1) && defined(CY_CRYPTO_CFG_PRNG_C) */
434 
435 #if (CPUSS_CRYPTO_AES == 1) && defined(CY_CRYPTO_CFG_AES_C)
Cy_Crypto_Aes_Init(uint32_t * key,cy_en_crypto_aes_key_length_t keyLength,cy_stc_crypto_context_aes_t * cfContext)436 cy_en_crypto_status_t Cy_Crypto_Aes_Init(uint32_t *key,
437                                          cy_en_crypto_aes_key_length_t keyLength,
438                                          cy_stc_crypto_context_aes_t *cfContext)
439 {
440     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
441 
442     if (clientContext != NULL)
443     {
444         clientContext->instr = CY_CRYPTO_INSTR_AES_INIT;
445         clientContext->xdata = cfContext;
446 
447         cfContext->key = key;
448         cfContext->keyLength = keyLength;
449 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
450         /* Flush the cache */
451         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
452         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
453         CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 10.8','Intentional typecast to int32_t.');
454         SCB_CleanDCache_by_Addr((volatile void *)key,(int32_t)(CY_CRYPTO_AES_128_KEY_SIZE + ((uint32_t)keyLength << 3)));
455         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
456 #endif
457 
458         err = Cy_Crypto_Client_Send();
459     }
460     return (err);
461 }
462 
Cy_Crypto_Aes_Ecb_Run(cy_en_crypto_dir_mode_t dirMode,uint32_t * dstBlock,uint32_t * srcBlock,cy_stc_crypto_context_aes_t * cfContext)463 cy_en_crypto_status_t Cy_Crypto_Aes_Ecb_Run(cy_en_crypto_dir_mode_t dirMode,
464                                             uint32_t *dstBlock,
465                                             uint32_t *srcBlock,
466                                             cy_stc_crypto_context_aes_t *cfContext)
467 {
468     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
469 
470     if (clientContext != NULL)
471     {
472         clientContext->instr = CY_CRYPTO_INSTR_AES_ECB;
473         clientContext->xdata = cfContext;
474 
475         cfContext->dirMode = dirMode;
476         cfContext->dst = dstBlock;
477         cfContext->src = srcBlock;
478 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
479         /* Flush the cache */
480         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
481         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
482         SCB_CleanDCache_by_Addr((volatile void *)srcBlock,(int32_t)CY_CRYPTO_AES_BLOCK_SIZE);
483         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
484         SCB_InvalidateDCache_by_Addr((volatile void *)dstBlock, (int32_t)CY_CRYPTO_AES_BLOCK_SIZE);
485 #endif
486         err = Cy_Crypto_Client_Send();
487     }
488     return (err);
489 }
490 
491 #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CBC)
Cy_Crypto_Aes_Cbc_Run(cy_en_crypto_dir_mode_t dirMode,uint32_t srcSize,uint32_t * ivPtr,uint32_t * dst,uint32_t * src,cy_stc_crypto_context_aes_t * cfContext)492 cy_en_crypto_status_t Cy_Crypto_Aes_Cbc_Run(cy_en_crypto_dir_mode_t dirMode,
493                                             uint32_t srcSize,
494                                             uint32_t *ivPtr,
495                                             uint32_t *dst,
496                                             uint32_t *src,
497                                             cy_stc_crypto_context_aes_t *cfContext)
498 {
499     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
500 
501     if (clientContext != NULL)
502     {
503         clientContext->instr = CY_CRYPTO_INSTR_AES_CBC;
504         clientContext->xdata = cfContext;
505 
506         cfContext->dirMode = dirMode;
507         cfContext->srcSize = srcSize;
508         cfContext->ivPtr = ivPtr;
509         cfContext->dst = dst;
510         cfContext->src = src;
511 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
512         /* Flush the cache */
513         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
514         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
515         SCB_CleanDCache_by_Addr((volatile void *)src,(int32_t)srcSize);
516         SCB_CleanDCache_by_Addr((volatile void *)ivPtr,(int32_t)CY_CRYPTO_AES_BLOCK_SIZE);
517         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
518         SCB_InvalidateDCache_by_Addr((volatile void *)dst, (int32_t)srcSize);
519 #endif
520 
521         err = Cy_Crypto_Client_Send();
522     }
523     return (err);
524 }
525 #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CBC) */
526 
527 #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CFB)
Cy_Crypto_Aes_Cfb_Run(cy_en_crypto_dir_mode_t dirMode,uint32_t srcSize,uint32_t * ivPtr,uint32_t * dst,uint32_t * src,cy_stc_crypto_context_aes_t * cfContext)528 cy_en_crypto_status_t Cy_Crypto_Aes_Cfb_Run(cy_en_crypto_dir_mode_t dirMode,
529                                             uint32_t srcSize,
530                                             uint32_t *ivPtr,
531                                             uint32_t *dst,
532                                             uint32_t *src,
533                                             cy_stc_crypto_context_aes_t *cfContext)
534 {
535     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
536 
537     if (clientContext != NULL)
538     {
539         clientContext->instr = CY_CRYPTO_INSTR_AES_CFB;
540         clientContext->xdata = cfContext;
541 
542         cfContext->dirMode = dirMode;
543         cfContext->srcSize = srcSize;
544         cfContext->ivPtr = ivPtr;
545         cfContext->dst = dst;
546         cfContext->src = src;
547 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
548         /* Flush the cache */
549         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
550         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
551         SCB_CleanDCache_by_Addr((volatile void *)src,(int32_t)srcSize);
552         SCB_CleanDCache_by_Addr((volatile void *)ivPtr,(int32_t)CY_CRYPTO_AES_BLOCK_SIZE);
553         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
554         SCB_InvalidateDCache_by_Addr((volatile void *)dst, (int32_t)srcSize);
555 #endif
556 
557         err = Cy_Crypto_Client_Send();
558     }
559     return (err);
560 }
561 #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CFB) */
562 
563 #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CTR)
Cy_Crypto_Aes_Ctr_Run(cy_en_crypto_dir_mode_t dirMode,uint32_t srcSize,uint32_t * srcOffset,uint32_t nonceCounter[CY_CRYPTO_AES_BLOCK_SIZE/8u],uint32_t streamBlock[CY_CRYPTO_AES_BLOCK_SIZE/8u],uint32_t * dst,uint32_t * src,cy_stc_crypto_context_aes_t * cfContext)564 cy_en_crypto_status_t Cy_Crypto_Aes_Ctr_Run(cy_en_crypto_dir_mode_t dirMode,
565                                             uint32_t srcSize,
566                                             uint32_t *srcOffset,
567                                             uint32_t nonceCounter[CY_CRYPTO_AES_BLOCK_SIZE / 8u],
568                                             uint32_t streamBlock[CY_CRYPTO_AES_BLOCK_SIZE / 8u],
569                                             uint32_t *dst,
570                                             uint32_t *src,
571                                             cy_stc_crypto_context_aes_t *cfContext)
572 {
573     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
574 
575     if (clientContext != NULL)
576     {
577         clientContext->instr = CY_CRYPTO_INSTR_AES_CTR;
578         clientContext->xdata = cfContext;
579 
580         cfContext->dirMode = dirMode;
581         cfContext->srcSize = srcSize;
582         cfContext->srcOffset = srcOffset;
583         cfContext->ivPtr = nonceCounter;
584         cfContext->streamBlock = streamBlock;
585         cfContext->dst = dst;
586         cfContext->src = src;
587 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
588         /* Flush the cache */
589         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
590         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
591         SCB_CleanDCache_by_Addr((volatile void *)src,(int32_t)srcSize);
592         SCB_CleanDCache_by_Addr((volatile void *)nonceCounter,(int32_t)CY_CRYPTO_AES_BLOCK_SIZE);
593         SCB_CleanDCache_by_Addr((volatile void *)srcOffset, (int32_t)sizeof(*srcOffset));
594         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
595         SCB_InvalidateDCache_by_Addr((volatile void *)dst, (int32_t)srcSize);
596         SCB_InvalidateDCache_by_Addr((volatile void *)srcOffset, (int32_t)sizeof(*srcOffset));
597 #endif
598 
599         err = Cy_Crypto_Client_Send();
600     }
601     return (err);
602 }
603 #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CTR) */
604 #endif /* (CPUSS_CRYPTO_AES == 1) && defined(CY_CRYPTO_CFG_AES_C) */
605 
606 #if (CPUSS_CRYPTO_AES == 1) && defined(CY_CRYPTO_CFG_CMAC_C)
Cy_Crypto_Aes_Cmac_Run(uint32_t * src,uint32_t srcSize,uint32_t * key,cy_en_crypto_aes_key_length_t keyLength,uint32_t * cmacPtr,cy_stc_crypto_context_aes_t * cfContext)607 cy_en_crypto_status_t Cy_Crypto_Aes_Cmac_Run(uint32_t *src,
608                                              uint32_t srcSize,
609                                              uint32_t *key,
610                                              cy_en_crypto_aes_key_length_t keyLength,
611                                              uint32_t *cmacPtr,
612                                              cy_stc_crypto_context_aes_t *cfContext)
613 {
614     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
615 
616     if (clientContext != NULL)
617     {
618         clientContext->instr = CY_CRYPTO_INSTR_CMAC;
619         clientContext->xdata = cfContext;
620 
621         cfContext->srcSize = srcSize;
622         cfContext->dst = cmacPtr;
623         cfContext->src = src;
624         cfContext->key = key;
625         cfContext->keyLength = keyLength;
626 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
627         /* Flush the cache */
628         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
629         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
630         SCB_CleanDCache_by_Addr((volatile void *)src,(int32_t)srcSize);
631         CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 10.8','Intentional typecast to int32_t.');
632         SCB_CleanDCache_by_Addr((volatile void *)key,(int32_t)(CY_CRYPTO_AES_128_KEY_SIZE + ((uint32_t)keyLength << 3)));
633         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
634         SCB_InvalidateDCache_by_Addr((volatile void *)cmacPtr, (int32_t)CY_CRYPTO_AES_BLOCK_SIZE);
635 #endif
636 
637         err = Cy_Crypto_Client_Send();
638     }
639     return (err);
640 }
641 #endif /* (CPUSS_CRYPTO_AES == 1) && defined(CY_CRYPTO_CFG_CMAC_C) */
642 
643 #if (CPUSS_CRYPTO_SHA == 1) && defined(CY_CRYPTO_CFG_SHA_C)
644 
645 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
646 CY_MISRA_DEVIATE_BLOCK_START('MISRA C-2012 Rule 10.3', 1, 'Intentional typecast to int32_t.')
647 static const int32_t Cy_Crypto_Sha_Digest_size[] = { CY_CRYPTO_SHA1_DIGEST_SIZE, CY_CRYPTO_SHA224_DIGEST_SIZE,
648                                             CY_CRYPTO_SHA256_DIGEST_SIZE, CY_CRYPTO_SHA384_DIGEST_SIZE,
649                                             CY_CRYPTO_SHA512_DIGEST_SIZE, CY_CRYPTO_SHA512_256_DIGEST_SIZE,
650                                             CY_CRYPTO_SHA512_224_DIGEST_SIZE, 0};
651 CY_MISRA_BLOCK_END('MISRA C-2012 Rule 10.3')
652 #endif
Cy_Crypto_Sha_Run(uint32_t * message,uint32_t messageSize,uint32_t * digest,cy_en_crypto_sha_mode_t mode,cy_stc_crypto_context_sha_t * cfContext)653 cy_en_crypto_status_t Cy_Crypto_Sha_Run(uint32_t *message,
654                                         uint32_t messageSize,
655                                         uint32_t *digest,
656                                         cy_en_crypto_sha_mode_t mode,
657                                         cy_stc_crypto_context_sha_t *cfContext)
658 {
659     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
660 
661     if (clientContext != NULL)
662     {
663         clientContext->instr = CY_CRYPTO_INSTR_SHA;
664         clientContext->xdata = cfContext;
665 
666         cfContext->message = message;
667         cfContext->messageSize = messageSize;
668         cfContext->dst = digest;
669         cfContext->mode = mode;
670 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
671         /* Flush the cache */
672         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
673         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
674         SCB_CleanDCache_by_Addr((volatile void *)message,(int32_t)messageSize);
675         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
676         SCB_InvalidateDCache_by_Addr((volatile void *)digest, Cy_Crypto_Sha_Digest_size[mode]);
677 #endif
678         err = Cy_Crypto_Client_Send();
679     }
680     return (err);
681 }
682 #endif /* (CPUSS_CRYPTO_SHA == 1) && defined(CY_CRYPTO_CFG_SHA_C) */
683 
684 #if (CPUSS_CRYPTO_SHA == 1) && defined(CY_CRYPTO_CFG_HMAC_C)
Cy_Crypto_Hmac_Run(uint32_t * hmac,uint32_t * message,uint32_t messageSize,uint32_t * key,uint32_t keyLength,cy_en_crypto_sha_mode_t mode,cy_stc_crypto_context_sha_t * cfContext)685 cy_en_crypto_status_t Cy_Crypto_Hmac_Run(uint32_t *hmac,
686                                          uint32_t *message,
687                                          uint32_t messageSize,
688                                          uint32_t *key,
689                                          uint32_t keyLength,
690                                          cy_en_crypto_sha_mode_t mode,
691                                          cy_stc_crypto_context_sha_t *cfContext)
692 {
693     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
694 
695     if (clientContext != NULL)
696     {
697         clientContext->instr = CY_CRYPTO_INSTR_HMAC;
698         clientContext->xdata = cfContext;
699 
700         cfContext->message = message;
701         cfContext->messageSize = messageSize;
702         cfContext->dst = hmac;
703         cfContext->mode = mode;
704         cfContext->key = key;
705         cfContext->keyLength = keyLength;
706 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
707         /* Flush the cache */
708         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
709         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
710         SCB_CleanDCache_by_Addr((volatile void *)message,(int32_t)messageSize);
711         SCB_CleanDCache_by_Addr((volatile void *)key,(int32_t)keyLength);
712         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
713         SCB_InvalidateDCache_by_Addr((volatile void *)hmac, Cy_Crypto_Sha_Digest_size[mode]);
714 #endif
715 
716         err = Cy_Crypto_Client_Send();
717     }
718     return (err);
719 }
720 #endif /* (CPUSS_CRYPTO_SHA == 1) && defined(CY_CRYPTO_CFG_HMAC_C) */
721 
722 #if (CPUSS_CRYPTO_STR == 1)
Cy_Crypto_Str_MemCpy(void * dst,void const * src,uint16_t size,cy_stc_crypto_context_str_t * cfContext)723 cy_en_crypto_status_t Cy_Crypto_Str_MemCpy(void* dst,
724                                            void const *src,
725                                            uint16_t size,
726                                            cy_stc_crypto_context_str_t *cfContext)
727 {
728     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
729 
730     if (clientContext != NULL)
731     {
732         clientContext->instr = CY_CRYPTO_INSTR_MEM_CPY;
733         clientContext->xdata = cfContext;
734 
735         cfContext->dst = dst;
736         cfContext->src0 = src;
737         cfContext->dataSize = size;
738 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
739         /* Flush the cache */
740         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
741         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
742         SCB_CleanDCache_by_Addr((volatile void *)src,(int32_t)size);
743         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
744         SCB_InvalidateDCache_by_Addr((volatile void *)dst, (int32_t)size);
745 #endif
746 
747         err = Cy_Crypto_Client_Send();
748     }
749     return (err);
750 }
751 
Cy_Crypto_Str_MemSet(void * dst,uint8_t data,uint16_t size,cy_stc_crypto_context_str_t * cfContext)752 cy_en_crypto_status_t Cy_Crypto_Str_MemSet(void* dst,
753                                            uint8_t data,
754                                            uint16_t size,
755                                            cy_stc_crypto_context_str_t *cfContext)
756 {
757     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
758 
759     if (clientContext != NULL)
760     {
761         clientContext->instr = CY_CRYPTO_INSTR_MEM_SET;
762         clientContext->xdata = cfContext;
763 
764         cfContext->dst = dst;
765         cfContext->data = data;
766         cfContext->dataSize = size;
767 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
768         /* Flush the cache */
769         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
770         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
771         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
772         SCB_InvalidateDCache_by_Addr((volatile void *)dst, (int32_t)size);
773 #endif
774 
775         err = Cy_Crypto_Client_Send();
776     }
777     return (err);
778 }
779 
Cy_Crypto_Str_MemCmp(void const * src0,void const * src1,uint16_t size,uint32_t * resultPtr,cy_stc_crypto_context_str_t * cfContext)780 cy_en_crypto_status_t Cy_Crypto_Str_MemCmp(void const *src0,
781                                            void const *src1,
782                                            uint16_t size,
783                                            uint32_t *resultPtr,
784                                            cy_stc_crypto_context_str_t *cfContext)
785 {
786     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
787 
788     if (clientContext != NULL)
789     {
790         clientContext->instr = CY_CRYPTO_INSTR_MEM_CMP;
791         clientContext->xdata = cfContext;
792 
793         cfContext->src0 = src0;
794         cfContext->src1 = src1;
795         cfContext->dataSize = size;
796         cfContext->dst = (void* )resultPtr;
797 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
798         /* Flush the cache */
799         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
800         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
801         SCB_CleanDCache_by_Addr((volatile void *)src0,(int32_t)size);
802         SCB_CleanDCache_by_Addr((volatile void *)src1,(int32_t)size);
803         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
804         SCB_InvalidateDCache_by_Addr((volatile void *)resultPtr, (int32_t)sizeof(*resultPtr));
805 #endif
806         err = Cy_Crypto_Client_Send();
807     }
808     return (err);
809 }
810 
Cy_Crypto_Str_MemXor(void const * src0,void const * src1,void * dst,uint16_t size,cy_stc_crypto_context_str_t * cfContext)811 cy_en_crypto_status_t Cy_Crypto_Str_MemXor(void const *src0,
812                                            void const *src1,
813                                            void* dst,
814                                            uint16_t size,
815                                            cy_stc_crypto_context_str_t *cfContext)
816 {
817     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
818 
819     if (clientContext != NULL)
820     {
821         clientContext->instr = CY_CRYPTO_INSTR_MEM_XOR;
822         clientContext->xdata = cfContext;
823 
824         cfContext->src0 = src0;
825         cfContext->src1 = src1;
826         cfContext->dst = dst;
827         cfContext->dataSize = size;
828 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
829         /* Flush the cache */
830         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
831         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
832         SCB_CleanDCache_by_Addr((volatile void *)src0,(int32_t)size);
833         SCB_CleanDCache_by_Addr((volatile void *)src1,(int32_t)size);
834         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
835         SCB_InvalidateDCache_by_Addr((volatile void *)dst, (int32_t)size);
836 #endif
837 
838         err = Cy_Crypto_Client_Send();
839     }
840     return (err);
841 }
842 #endif /* #if (CPUSS_CRYPTO_STR == 1) */
843 
844 #if (CPUSS_CRYPTO_CRC == 1) && defined(CY_CRYPTO_CFG_CRC_C)
Cy_Crypto_Crc_Init(uint32_t polynomial,uint8_t dataReverse,uint8_t dataXor,uint8_t remReverse,uint32_t remXor,cy_stc_crypto_context_crc_t * cfContext)845 cy_en_crypto_status_t Cy_Crypto_Crc_Init(uint32_t polynomial,
846                                          uint8_t  dataReverse,
847                                          uint8_t  dataXor,
848                                          uint8_t  remReverse,
849                                          uint32_t remXor,
850                                          cy_stc_crypto_context_crc_t *cfContext)
851 {
852     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
853 
854     if (clientContext != NULL)
855     {
856         clientContext->instr = CY_CRYPTO_INSTR_CRC_INIT;
857         clientContext->xdata = cfContext;
858 
859         cfContext->dataReverse = dataReverse;
860         cfContext->remReverse = remReverse;
861         cfContext->dataXor = dataXor;
862         cfContext->polynomial = polynomial;
863         cfContext->remXor = remXor;
864 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
865         /* Flush the cache */
866         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
867         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
868         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
869 #endif
870 
871         err = Cy_Crypto_Client_Send();
872     }
873     return (err);
874 }
875 
Cy_Crypto_Crc_Run(void * data,uint16_t dataSize,uint32_t * crc,uint32_t lfsrInitState,cy_stc_crypto_context_crc_t * cfContext)876 cy_en_crypto_status_t Cy_Crypto_Crc_Run(void *data,
877                                         uint16_t dataSize,
878                                         uint32_t *crc,
879                                         uint32_t lfsrInitState,
880                                         cy_stc_crypto_context_crc_t *cfContext)
881 {
882     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
883 
884     if (clientContext != NULL)
885     {
886         clientContext->instr = CY_CRYPTO_INSTR_CRC;
887         clientContext->xdata = cfContext;
888 
889         cfContext->lfsrInitState = lfsrInitState;
890         cfContext->data  = data;
891         cfContext->dataSize = dataSize;
892         cfContext->crc = crc;
893 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
894         /* Flush the cache */
895         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
896         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
897         SCB_CleanDCache_by_Addr((volatile void *)data,(int32_t)dataSize);
898         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
899         SCB_InvalidateDCache_by_Addr((volatile void *)crc, (int32_t)sizeof(*crc));
900 #endif
901 
902         err = Cy_Crypto_Client_Send();
903     }
904     return (err);
905 }
906 #endif /* (CPUSS_CRYPTO_CRC == 1) && defined(CY_CRYPTO_CFG_CRC_C) */
907 
908 #if (CPUSS_CRYPTO_TR == 1) && defined(CY_CRYPTO_CFG_TRNG_C)
Cy_Crypto_Trng_Generate(uint32_t GAROPol,uint32_t FIROPol,uint32_t max,uint32_t * randomNum,cy_stc_crypto_context_trng_t * cfContext)909 cy_en_crypto_status_t Cy_Crypto_Trng_Generate(uint32_t  GAROPol,
910                                               uint32_t  FIROPol,
911                                               uint32_t  max,
912                                               uint32_t *randomNum,
913                                               cy_stc_crypto_context_trng_t *cfContext)
914 {
915     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
916 
917     if (clientContext != NULL)
918     {
919         clientContext->instr = CY_CRYPTO_INSTR_TRNG;
920         clientContext->xdata = cfContext;
921 
922         cfContext->GAROPol = GAROPol;
923         cfContext->FIROPol = FIROPol;
924         cfContext->max = max;
925         cfContext->trngNum = randomNum;
926 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
927         /* Flush the cache */
928         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
929         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
930         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
931         SCB_InvalidateDCache_by_Addr((volatile void *)randomNum, (int32_t)sizeof(*randomNum));
932 #endif
933 
934         err = Cy_Crypto_Client_Send();
935     }
936     return (err);
937 }
938 #endif /* (CPUSS_CRYPTO_TR == 1) && defined(CY_CRYPTO_CFG_TRNG_C) */
939 
940 #if (CPUSS_CRYPTO_DES == 1) && defined(CY_CRYPTO_CFG_DES_C)
Cy_Crypto_Des_Run(cy_en_crypto_dir_mode_t dirMode,uint32_t * key,uint32_t * dstBlock,uint32_t * srcBlock,cy_stc_crypto_context_des_t * cfContext)941 cy_en_crypto_status_t Cy_Crypto_Des_Run(cy_en_crypto_dir_mode_t dirMode,
942                                         uint32_t *key,
943                                         uint32_t *dstBlock,
944                                         uint32_t *srcBlock,
945                                         cy_stc_crypto_context_des_t *cfContext)
946 {
947     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
948 
949     if (clientContext != NULL)
950     {
951         clientContext->instr = CY_CRYPTO_INSTR_DES;
952         clientContext->xdata = cfContext;
953 
954         cfContext->dirMode = dirMode;
955         cfContext->key = key;
956         cfContext->dst = dstBlock;
957         cfContext->src = srcBlock;
958 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
959         /* Flush the cache */
960         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
961         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
962         SCB_CleanDCache_by_Addr((volatile void *)key,(int32_t)CY_CRYPTO_DES_KEY_SIZE);
963         SCB_CleanDCache_by_Addr((volatile void *)srcBlock,(int32_t)CY_CRYPTO_DES_BLOCK_SIZE);
964         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
965         SCB_InvalidateDCache_by_Addr((volatile void *)dstBlock, (int32_t)CY_CRYPTO_DES_BLOCK_SIZE);
966 #endif
967 
968         err = Cy_Crypto_Client_Send();
969     }
970     return (err);
971 }
972 
Cy_Crypto_Tdes_Run(cy_en_crypto_dir_mode_t dirMode,uint32_t * key,uint32_t * dstBlock,uint32_t * srcBlock,cy_stc_crypto_context_des_t * cfContext)973 cy_en_crypto_status_t Cy_Crypto_Tdes_Run(cy_en_crypto_dir_mode_t dirMode,
974                                          uint32_t *key,
975                                          uint32_t *dstBlock,
976                                          uint32_t *srcBlock,
977                                          cy_stc_crypto_context_des_t *cfContext)
978 {
979     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
980 
981     if (clientContext != NULL)
982     {
983         clientContext->instr = CY_CRYPTO_INSTR_3DES;
984         clientContext->xdata = cfContext;
985 
986         cfContext->dirMode = dirMode;
987         cfContext->key = key;
988         cfContext->dst = dstBlock;
989         cfContext->src = srcBlock;
990 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
991         /* Flush the cache */
992         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
993         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
994         SCB_CleanDCache_by_Addr((volatile void *)key,(int32_t)CY_CRYPTO_TDES_KEY_SIZE);
995         SCB_CleanDCache_by_Addr((volatile void *)srcBlock,(int32_t)CY_CRYPTO_DES_BLOCK_SIZE);
996         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
997         SCB_InvalidateDCache_by_Addr((volatile void *)dstBlock, (int32_t)CY_CRYPTO_DES_BLOCK_SIZE);
998 #endif
999 
1000         err = Cy_Crypto_Client_Send();
1001     }
1002     return (err);
1003 }
1004 #endif /* (CPUSS_CRYPTO_DES == 1) && defined(CY_CRYPTO_CFG_DES_C) */
1005 
1006 #if (CPUSS_CRYPTO_VU == 1) && defined(CY_CRYPTO_CFG_RSA_C)
Cy_Crypto_SetMemBufAddress(uint32_t const * newMembufAddress,uint32_t newMembufSize,cy_stc_crypto_context_str_t * cfContext)1007 cy_en_crypto_status_t Cy_Crypto_SetMemBufAddress(uint32_t const *newMembufAddress,
1008                                            uint32_t newMembufSize,
1009                                            cy_stc_crypto_context_str_t *cfContext)
1010 {
1011     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
1012 
1013     if (clientContext != NULL)
1014     {
1015         clientContext->instr = CY_CRYPTO_INSTR_MEMBUF_SET;
1016         clientContext->xdata = cfContext;
1017 
1018         cfContext->src0 = newMembufAddress;
1019         cfContext->dataSize = newMembufSize;
1020 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
1021         /* Flush the cache */
1022         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
1023         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
1024         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
1025 #endif
1026 
1027         err = Cy_Crypto_Client_Send();
1028     }
1029     return (err);
1030 }
1031 
Cy_Crypto_GetMemBufAddress(uint32_t ** membufAddress,cy_stc_crypto_context_str_t * cfContext)1032 cy_en_crypto_status_t Cy_Crypto_GetMemBufAddress(uint32_t **membufAddress,
1033                                            cy_stc_crypto_context_str_t *cfContext)
1034 {
1035     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
1036 
1037     if (clientContext != NULL)
1038     {
1039         clientContext->instr = CY_CRYPTO_INSTR_MEMBUF_ADDR;
1040         clientContext->xdata = cfContext;
1041 
1042         cfContext->dst = (void *)membufAddress;
1043 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
1044         /* Flush the cache */
1045         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
1046         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
1047         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
1048 #endif
1049 
1050         err = Cy_Crypto_Client_Send();
1051     }
1052     return (err);
1053 }
1054 
Cy_Crypto_GetMemBufSize(uint32_t * membufSize,cy_stc_crypto_context_str_t * cfContext)1055 cy_en_crypto_status_t Cy_Crypto_GetMemBufSize(uint32_t *membufSize,
1056                                            cy_stc_crypto_context_str_t *cfContext)
1057 {
1058     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
1059 
1060     if (clientContext != NULL)
1061     {
1062         clientContext->instr = CY_CRYPTO_INSTR_MEMBUF_SIZE;
1063         clientContext->xdata = cfContext;
1064 
1065         cfContext->dst = (void *)membufSize;
1066 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
1067         /* Flush the cache */
1068         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
1069         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
1070         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
1071 #endif
1072 
1073         err = Cy_Crypto_Client_Send();
1074     }
1075     return (err);
1076 }
1077 
Cy_Crypto_Rsa_Proc(cy_stc_crypto_rsa_pub_key_t const * pubKey,uint32_t const * message,uint32_t messageSize,uint32_t * processedMessage,cy_stc_crypto_context_rsa_t * cfContext)1078 cy_en_crypto_status_t Cy_Crypto_Rsa_Proc(cy_stc_crypto_rsa_pub_key_t const *pubKey,
1079                                          uint32_t const *message,
1080                                          uint32_t messageSize,
1081                                          uint32_t *processedMessage,
1082                                          cy_stc_crypto_context_rsa_t *cfContext)
1083 {
1084     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
1085 
1086     if (clientContext != NULL)
1087     {
1088         clientContext->instr = CY_CRYPTO_INSTR_RSA_PROC;
1089         clientContext->xdata = cfContext;
1090 
1091         cfContext->key = pubKey;
1092         cfContext->message = message;
1093         cfContext->messageSize = messageSize;
1094         cfContext->result = processedMessage;
1095 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
1096         /* Flush the cache */
1097         if(NULL != pubKey->barretCoefPtr)
1098         {
1099             CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 10.8','Intentional typecast to int32_t.');
1100             SCB_CleanDCache_by_Addr((volatile void *)pubKey->barretCoefPtr,(int32_t)(pubKey->moduloLength+1u));
1101         }
1102         if(NULL != pubKey->inverseModuloPtr)
1103         {
1104             SCB_CleanDCache_by_Addr((volatile void *)pubKey->inverseModuloPtr,(int32_t)pubKey->moduloLength);
1105         }
1106         if(NULL != pubKey->rBarPtr)
1107         {
1108             SCB_CleanDCache_by_Addr((volatile void *)pubKey->rBarPtr,(int32_t)pubKey->moduloLength);
1109         }
1110         SCB_CleanDCache_by_Addr((volatile void *)pubKey->moduloPtr,(int32_t)pubKey->moduloLength);
1111         SCB_CleanDCache_by_Addr((volatile void *)pubKey->pubExpPtr,(int32_t)pubKey->pubExpLength);
1112         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
1113         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
1114         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
1115         SCB_InvalidateDCache_by_Addr((volatile void *)processedMessage, (int32_t)messageSize);
1116 #endif
1117 
1118         err = Cy_Crypto_Client_Send();
1119     }
1120     return (err);
1121 }
1122 
Cy_Crypto_Rsa_CalcCoefs(cy_stc_crypto_rsa_pub_key_t const * pubKey,cy_stc_crypto_context_rsa_t * cfContext)1123 cy_en_crypto_status_t Cy_Crypto_Rsa_CalcCoefs(cy_stc_crypto_rsa_pub_key_t const *pubKey,
1124                                               cy_stc_crypto_context_rsa_t *cfContext)
1125 {
1126     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
1127 
1128     if (clientContext != NULL)
1129     {
1130         clientContext->instr = CY_CRYPTO_INSTR_RSA_COEF;
1131         clientContext->xdata = cfContext;
1132 
1133         cfContext->key = pubKey;
1134 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
1135         SCB_CleanDCache_by_Addr((volatile void *)pubKey->moduloPtr,(int32_t)pubKey->moduloLength);
1136         SCB_CleanDCache_by_Addr((volatile void *)pubKey->pubExpPtr,(int32_t)pubKey->pubExpLength);
1137         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
1138         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
1139         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
1140         CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 10.8','Intentional typecast to int32_t.');
1141         SCB_InvalidateDCache_by_Addr((volatile void *)pubKey->barretCoefPtr,(int32_t)(pubKey->moduloLength+1U));
1142         SCB_InvalidateDCache_by_Addr((volatile void *)pubKey->inverseModuloPtr,(int32_t)pubKey->moduloLength);
1143         SCB_InvalidateDCache_by_Addr((volatile void *)pubKey->rBarPtr,(int32_t)pubKey->moduloLength);
1144 #endif
1145 
1146         err = Cy_Crypto_Client_Send();
1147     }
1148     return (err);
1149 }
1150 
1151 #if (CPUSS_CRYPTO_SHA == 1) && defined(CY_CRYPTO_CFG_SHA_C) && defined(CY_CRYPTO_CFG_RSA_VERIFY_ENABLED)
Cy_Crypto_Rsa_Verify(cy_en_crypto_rsa_ver_result_t * verResult,cy_en_crypto_sha_mode_t digestType,uint32_t const * digest,uint32_t const * decryptedSignature,uint32_t decryptedSignatureLength,cy_stc_crypto_context_rsa_ver_t * cfContext)1152 cy_en_crypto_status_t Cy_Crypto_Rsa_Verify(cy_en_crypto_rsa_ver_result_t *verResult,
1153                                            cy_en_crypto_sha_mode_t digestType,
1154                                            uint32_t const *digest,
1155                                            uint32_t const *decryptedSignature,
1156                                            uint32_t decryptedSignatureLength,
1157                                            cy_stc_crypto_context_rsa_ver_t *cfContext)
1158 {
1159     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
1160 
1161     if (clientContext != NULL)
1162     {
1163         clientContext->instr = CY_CRYPTO_INSTR_RSA_VER;
1164         clientContext->xdata = cfContext;
1165 
1166         cfContext->verResult = verResult;
1167         cfContext->digestType = digestType;
1168         cfContext->hash = digest;
1169         cfContext->decryptedSignature = decryptedSignature;
1170         cfContext->decryptedSignatureLength = decryptedSignatureLength;
1171 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
1172         /* Flush the cache */
1173         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
1174         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
1175         SCB_CleanDCache_by_Addr((volatile void *)decryptedSignature,(int32_t)decryptedSignatureLength);
1176         SCB_CleanDCache_by_Addr((volatile void *)digest, Cy_Crypto_Sha_Digest_size[digestType]);
1177         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
1178         SCB_InvalidateDCache_by_Addr((volatile void *)verResult, (int32_t)sizeof(*verResult));
1179 #endif
1180 
1181         err = Cy_Crypto_Client_Send();
1182     }
1183     return (err);
1184 }
1185 #endif /* (CPUSS_CRYPTO_SHA == 1) && defined(CY_CRYPTO_CFG_SHA_C) && defined(CY_CRYPTO_CFG_RSA_VERIFY_ENABLED) */
1186 #endif /* (CPUSS_CRYPTO_VU == 1) && defined(CY_CRYPTO_CFG_RSA_C) */
1187 
1188 #if (CPUSS_CRYPTO_VU == 1) && defined(CY_CRYPTO_CFG_ECDSA_C)
1189 #if defined(CY_CRYPTO_CFG_ECDSA_SIGN_C)
Cy_Crypto_ECDSA_SignHash(const uint8_t * hash,uint32_t hashlen,uint8_t * sig,const cy_stc_crypto_ecc_key * key,const uint8_t * messageKey,cy_stc_crypto_context_ecc_t * cfContext)1190 cy_en_crypto_status_t Cy_Crypto_ECDSA_SignHash(const uint8_t *hash,
1191                                         uint32_t hashlen,
1192                                         uint8_t *sig,
1193                                         const cy_stc_crypto_ecc_key *key,
1194                                         const uint8_t *messageKey,
1195                                         cy_stc_crypto_context_ecc_t *cfContext)
1196 {
1197     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
1198 
1199     if (clientContext != NULL)
1200     {
1201         clientContext->instr = CY_CRYPTO_INSTR_ECDSA_SIGN;
1202         clientContext->xdata = cfContext;
1203 
1204         cfContext->datalen = hashlen;
1205         cfContext->src0 = hash;
1206         cfContext->dst0 = sig;
1207         cfContext->key  = key;
1208         cfContext->src1 = messageKey;
1209 
1210 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
1211         /* Flush the cache */
1212         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
1213         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
1214         SCB_CleanDCache_by_Addr((volatile void *)hash,(int32_t)hashlen);
1215         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
1216 #endif
1217         err = Cy_Crypto_Client_Send();
1218     }
1219     return (err);
1220 }
1221 #endif /* defined(CY_CRYPTO_CFG_ECDSA_SIGN_C) */
1222 
1223 #if defined(CY_CRYPTO_CFG_ECDSA_VERIFY_C)
Cy_Crypto_ECDSA_VerifyHash(const uint8_t * sig,const uint8_t * hash,uint32_t hashlen,uint8_t * stat,const cy_stc_crypto_ecc_key * key,cy_stc_crypto_context_ecc_t * cfContext)1224 cy_en_crypto_status_t Cy_Crypto_ECDSA_VerifyHash(const uint8_t *sig,
1225                                         const uint8_t *hash,
1226                                         uint32_t hashlen,
1227                                         uint8_t *stat,
1228                                         const cy_stc_crypto_ecc_key *key,
1229                                         cy_stc_crypto_context_ecc_t *cfContext)
1230 {
1231     cy_en_crypto_status_t err = CY_CRYPTO_NOT_INITIALIZED;
1232 
1233     if (clientContext != NULL)
1234     {
1235         clientContext->instr = CY_CRYPTO_INSTR_ECDSA_VER;
1236         clientContext->xdata = cfContext;
1237 
1238         cfContext->datalen = hashlen;
1239         cfContext->src0 = hash;
1240         cfContext->src1 = sig;
1241         cfContext->dst0 = stat;
1242         cfContext->key  = key;
1243 
1244 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
1245         /* Flush the cache */
1246         SCB_CleanDCache_by_Addr((volatile void *)clientContext,(int32_t)sizeof(*clientContext));
1247         SCB_CleanDCache_by_Addr((volatile void *)cfContext,(int32_t)sizeof(*cfContext));
1248         SCB_CleanDCache_by_Addr((volatile void *)hash,(int32_t)hashlen);
1249         SCB_InvalidateDCache_by_Addr((volatile void *)clientContext, (int32_t)sizeof(*clientContext));
1250 #endif
1251 
1252         err = Cy_Crypto_Client_Send();
1253     }
1254     return (err);
1255 }
1256 #endif /* defined(CY_CRYPTO_CFG_ECDSA_VERIFY_C) */
1257 #endif /* (CPUSS_CRYPTO_VU == 1) && defined(CY_CRYPTO_CFG_ECDSA_C) */
1258 
Cy_Crypto_InvertEndianness(void * inArrPtr,uint32_t byteSize)1259 void Cy_Crypto_InvertEndianness(void *inArrPtr, uint32_t byteSize)
1260 {
1261     int32_t limit;
1262     int32_t i;
1263     int32_t j = 0;
1264     uint8_t temp;
1265     uint8_t *tempPtr = (uint8_t*)inArrPtr;
1266 
1267     if (byteSize > 1u)
1268     {
1269         limit = (int32_t)byteSize / 2;
1270         if (0u == (byteSize % 2u))
1271         {
1272             --limit;
1273         }
1274 
1275         j = 0;
1276         i = (int32_t)byteSize - 1;
1277         while ( i > limit)
1278         {
1279             temp = tempPtr[j];
1280             tempPtr[j] = tempPtr[i];
1281             tempPtr[i] = temp;
1282 
1283             --i;
1284             ++j;
1285         }
1286     }
1287 }
1288 
1289 
1290 #if defined(__cplusplus)
1291 }
1292 #endif
1293 
1294 
1295 #endif /* CY_IP_MXCRYPTO */
1296 
1297 
1298 /* [] END OF FILE */
1299