1 /***************************************************************************//**
2 * \file cy_crypto_core_hw.c
3 * \version 2.90
4 *
5 * \brief
6 *  This file provides the source code to the API for the utils
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_common.h"
33 
34 #if defined(__cplusplus)
35 extern "C" {
36 #endif
37 
38 #include "cy_crypto_core_hw.h"
39 #include "cy_crypto_core_hw_vu.h"
40 #include "cy_crypto_core_mem.h"
41 #include "cy_syslib.h"
42 #include <stdbool.h>
43 
44 CY_MISRA_DEVIATE_BLOCK_START('MISRA C-2012 Rule 11.3', 4, \
45 'CRYPTO_Type will typecast to either CRYPTO_V1_Type or CRYPTO_V2_Type but not both on PDL initialization based on the target device at compile time.')
46 CY_MISRA_DEVIATE_BLOCK_START('MISRA C-2012 Rule 14.3', 2, \
47 'Since value of CY_CRYPTO_V1 is decided by PDL device agnostic / hardware specific model, controlling expression will not have an invariant value.')
48 
49 #if !defined(CY_CRYPTO_SERVICE_LIBRARY_LEVEL)
50     #define CY_CRYPTO_SERVICE_LIBRARY_LEVEL CY_CRYPTO_FULL_LIBRARY
51 #endif
52 
53 #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
54 #include "cy_crypto_core_hw_v2.h"
55 #endif
56 /*******************************************************************************
57 *                   Global Variables
58 *******************************************************************************/
59 
60 static uint32_t  cy_cryptoVuMemSize = 0u;
61 
62 #if !defined(CY_CRYPTO_CFG_HW_USE_MPN_SPECIFIC)
63 /* This is set in Cy_Crypto_Core_Enable() to the device information relevant
64  * for the current target.
65  */
66 const cy_stc_cryptoIP_t * cy_cryptoIP = NULL;
67 
68 /* Platform and peripheral crypto block configuration */
69 const cy_stc_cryptoIP_t cy_cryptoIpBlockCfgPSoC6_01 =
70 {
71     /* CRYPTO register offsets */
72     /* cryptoStatusOffset        */ offsetof(CRYPTO_V1_Type, STATUS),
73     /* cryptoIstrFfCtlOffset     */ offsetof(CRYPTO_V1_Type, INSTR_FF_CTL),
74     /* cryptoInstrFfStatusOffset */ offsetof(CRYPTO_V1_Type, INSTR_FF_STATUS),
75     /* cryptoInstrFfWrOffset     */ offsetof(CRYPTO_V1_Type, INSTR_FF_WR),
76     /* cryptoVuRfDataOffset      */ offsetof(CRYPTO_V1_Type, RF_DATA),
77     /* cryptoAesCtlOffset        */ offsetof(CRYPTO_V1_Type, AES_CTL),
78     /* cryptoPrResultOffset      */ offsetof(CRYPTO_V1_Type, PR_RESULT),
79     /* cryptoTrResultOffset      */ offsetof(CRYPTO_V1_Type, TR_RESULT),
80     /* cryptoCrcCtlOffset        */ offsetof(CRYPTO_V1_Type, CRC_CTL),
81     /* cryptoCrcDataCtlOffset    */ offsetof(CRYPTO_V1_Type, CRC_DATA_CTL),
82     /* cryptoCrcPolCtlOffset     */ offsetof(CRYPTO_V1_Type, CRC_POL_CTL),
83     /* cryptoCrcRemCtlOffset     */ offsetof(CRYPTO_V1_Type, CRC_REM_CTL),
84     /* cryptoCrcRemResultOffset  */ offsetof(CRYPTO_V1_Type, CRC_REM_RESULT),
85     /* cryptoVuCtl0Offset        */ offsetof(CRYPTO_V1_Type, VU_CTL0),
86     /* cryptoVuCtl1Offset        */ offsetof(CRYPTO_V1_Type, VU_CTL1),
87     /* cryptoVuStatusOffset      */ offsetof(CRYPTO_V1_Type, VU_STATUS),
88     /* cryptoIntrOffset          */ offsetof(CRYPTO_V1_Type, INTR),
89     /* cryptoIntrSetOffset       */ offsetof(CRYPTO_V1_Type, INTR_SET),
90     /* cryptoIntrMaskOffset      */ offsetof(CRYPTO_V1_Type, INTR_MASK),
91     /* cryptoIntrMaskedOffset    */ offsetof(CRYPTO_V1_Type, INTR_MASKED),
92     /* cryptoMemBufOffset        */ offsetof(CRYPTO_V1_Type, MEM_BUFF),
93 };
94 
95 const cy_stc_cryptoIP_t cy_cryptoIpBlockCfgPSoC6_02 =
96 {
97     /* CRYPTO register offsets */
98     /* cryptoStatusOffset        */ offsetof(CRYPTO_V2_Type, STATUS),
99     /* cryptoIstrFfCtlOffset     */ offsetof(CRYPTO_V2_Type, INSTR_FF_CTL),
100     /* cryptoInstrFfStatusOffset */ offsetof(CRYPTO_V2_Type, INSTR_FF_STATUS),
101     /* cryptoInstrFfWrOffset     */ offsetof(CRYPTO_V2_Type, INSTR_FF_WR),
102     /* cryptoVuRfDataOffset      */ offsetof(CRYPTO_V2_Type, VU_RF_DATA),
103     /* cryptoAesCtlOffset        */ offsetof(CRYPTO_V2_Type, AES_CTL),
104     /* cryptoPrResultOffset      */ offsetof(CRYPTO_V2_Type, PR_RESULT),
105     /* cryptoTrResultOffset      */ offsetof(CRYPTO_V2_Type, TR_RESULT),
106     /* cryptoCrcCtlOffset        */ offsetof(CRYPTO_V2_Type, CRC_CTL),
107     /* cryptoCrcDataCtlOffset    */ offsetof(CRYPTO_V2_Type, CRC_DATA_CTL),
108     /* cryptoCrcPolCtlOffset     */ offsetof(CRYPTO_V2_Type, CRC_POL_CTL),
109     /* cryptoCrcRemCtlOffset     */ offsetof(CRYPTO_V2_Type, CRC_REM_CTL),
110     /* cryptoCrcRemResultOffset  */ offsetof(CRYPTO_V2_Type, CRC_REM_RESULT),
111     /* cryptoVuCtl0Offset        */ offsetof(CRYPTO_V2_Type, VU_CTL0),
112     /* cryptoVuCtl1Offset        */ offsetof(CRYPTO_V2_Type, VU_CTL1),
113     /* cryptoVuStatusOffset      */ offsetof(CRYPTO_V2_Type, VU_STATUS),
114     /* cryptoIntrOffset          */ offsetof(CRYPTO_V2_Type, INTR),
115     /* cryptoIntrSetOffset       */ offsetof(CRYPTO_V2_Type, INTR_SET),
116     /* cryptoIntrMaskOffset      */ offsetof(CRYPTO_V2_Type, INTR_MASK),
117     /* cryptoIntrMaskedOffset    */ offsetof(CRYPTO_V2_Type, INTR_MASKED),
118     /* cryptoMemBufOffset        */ offsetof(CRYPTO_V2_Type, MEM_BUFF),
119 };
120 #endif
121 
122 /* The defines of the power modes of the CRYPTO */
123 #define CY_CRYPTO_PWR_MODE_OFF               (0UL)
124 #define CY_CRYPTO_PWR_MODE_RETAINED          (2UL)
125 #define CY_CRYPTO_PWR_MODE_ENABLED           (3UL)
126 
127 /*******************************************************************************
128 * Function Name: Cy_Crypto_Core_Vu_RunInstr
129 *****************************************************************************//**
130 *
131 * Run the Crypto Vector Unit instruction with one parameter.
132 *
133 * \param base
134 * The pointer to the CRYPTO instance.
135 
136 * \param blockingMode
137 * Sets the blocking or non-blocking operation mode.
138 *
139 * \param instr
140 * The Opcode of the called instruction.
141 *
142 * \param params
143 * The parameters for the instruction operand.
144 *
145 *******************************************************************************/
Cy_Crypto_Core_Vu_RunInstr(CRYPTO_Type * base,bool blockingMode,uint32_t instr,uint32_t params)146 void Cy_Crypto_Core_Vu_RunInstr(CRYPTO_Type *base, bool blockingMode, uint32_t instr, uint32_t params)
147 {
148     bool isRelocated = Cy_Crypto_Core_GetVuMemoryAddress(base) != REG_CRYPTO_MEM_BUFF(base);
149 
150     /* Check whether FIFO has enough space for 1 instruction */
151     Cy_Crypto_Core_WaitForInstrFifoAvailable(base, CY_CRYPTO_INSTR_SINGLE);
152 
153     REG_CRYPTO_INSTR_FF_WR(base) = (uint32_t)((instr << CY_CRYPTO_OPCODE_POS) | (params));
154 
155     if ( (blockingMode) || (isRelocated) )
156     {
157         Cy_Crypto_Core_WaitForFifoAvailable(base);
158         Cy_Crypto_Core_Vu_WaitForComplete(base);
159     }
160 }
161 
162 /**
163 * \addtogroup group_crypto_lld_hw_functions
164 * \{
165 */
166 
167 /*******************************************************************************
168 * Function Name: Cy_Crypto_Core_ClearVuRegisters
169 ****************************************************************************//**
170 *
171 * The function to initialize the Crypto VU registers.
172 *
173 * \param base
174 * The pointer to the CRYPTO instance.
175 *
176 *******************************************************************************/
Cy_Crypto_Core_ClearVuRegisters(CRYPTO_Type * base)177 void Cy_Crypto_Core_ClearVuRegisters(CRYPTO_Type *base)
178 {
179     if (Cy_Crypto_Core_IsEnabled(base))
180     {
181         /* Clear whole register file */
182         CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG14, 0u, 1u);
183         CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG13, 0u, 1u);
184         CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG12, 0u, 1u);
185         CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG11, 0u, 1u);
186         CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG10, 0u, 1u);
187         CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG9,  0u, 1u);
188         CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG8,  0u, 1u);
189         CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG7,  0u, 1u);
190         CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG6,  0u, 1u);
191         CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG5,  0u, 1u);
192         CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG4,  0u, 1u);
193         CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG3,  0u, 1u);
194         CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG2,  0u, 1u);
195         CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG1,  0u, 1u);
196         CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG0,  0u, 1u);
197 
198         /* Set the stack pointer to the Crypto buff size, in words */
199         CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG15, cy_cryptoVuMemSize / 4u, 1u);
200     }
201 }
202 
203 /*******************************************************************************
204 * Function Name: Cy_Crypto_Core_SetVuMemoryAddress
205 ****************************************************************************//**
206 *
207 * Sets the new memory buffer address and size.
208 *
209 * \param base
210 * The pointer to the CRYPTO instance.
211 *
212 * \param vuMemoryAddr
213 * The memory buffer location that will be used as Crypto MEM_BUFF and it should be aligned to the vuMemorySize
214 *
215 * \param vuMemorySize
216 * The provided memory buffer size in bytes.
217 *
218 * \return
219 * \ref cy_en_crypto_status_t
220 *
221 * \note This function sets the default device specific values
222 *       when vuMemoryAddr parameter is NULL and vuMemorySize parameter is zero.
223 *
224 * \note New memory buffer should be allocated in a memory region that is not
225 *       protected by a protection scheme for use by Crypto hardware.
226 *
227 *******************************************************************************/
Cy_Crypto_Core_SetVuMemoryAddress(CRYPTO_Type * base,uint32_t const * vuMemoryAddr,uint32_t vuMemorySize)228 cy_en_crypto_status_t Cy_Crypto_Core_SetVuMemoryAddress(CRYPTO_Type *base,
229                                                         uint32_t const *vuMemoryAddr, uint32_t vuMemorySize)
230 {
231     cy_en_crypto_status_t resultVal = CY_CRYPTO_BAD_PARAMS;
232     uint32_t *vuMemAddr = (uint32_t *)vuMemoryAddr;
233     uint32_t  vuMemSize = vuMemorySize;
234 
235 #if !defined(CY_CRYPTO_CFG_HW_USE_MPN_SPECIFIC)
236     if (cy_cryptoIP != NULL)
237     {
238 #endif
239         if ((vuMemAddr == NULL) && (vuMemSize == 0uL))
240         {
241             vuMemAddr = REG_CRYPTO_MEM_BUFF(base);
242             vuMemSize = CY_CRYPTO_MEM_BUFF_SIZE;
243         }
244 
245         /* Check for new memory size is less or equal to maximal IP allowed value */
246         if ((vuMemAddr != NULL) && (vuMemSize != 0uL) && (vuMemSize <= 32768u))
247         {
248             /* mxcrypto (V1) IP uses MEM_BUF aligned to 16KB */
249             uint32_t memAlignMask = 16384uL - 1uL;
250 
251             uint32_t memFrameMask = 0xFFFFFFFFuL;
252             /*
253             Specifies the size of  the vector operand memory region.
254             Legal values:
255             "0b0000000":  32 KB memory region (VU_CTL1.ADDR[14:8] ignored).
256             "0b1000000":  16 KB memory region (VU_CTL1.ADDR[13:8] ignored).
257             "0b1100000":   8 KB memory region (VU_CTL1.ADDR[12:8] ignored).
258             "0b1110000":   4 KB memory region (VU_CTL1.ADDR[11:8] ignored).
259             "0b1111000":   2 KB memory region (VU_CTL1.ADDR[10:8] ignored).
260             "0b1111100":   1 KB memory region (VU_CTL1.ADDR[9:8]  ignored).
261             "0b1111110": 512  B memory region (VU_CTL1.ADDR[8]    ignored).
262             "0b1111111": 256  B memory region.
263             */
264             switch (vuMemSize)
265             {
266                 /* "0b0000000": 32 KB memory region (VU_CTL1.ADDR[14:8] ignored). */
267                 case 32768uL:
268                     memFrameMask = 0x0u;
269                     break;
270                 /* "0b1000000": 16 KB memory region (VU_CTL1.ADDR[13:8] ignored). */
271                 case 16384uL:
272                     memFrameMask = 0x40u;
273                     break;
274                 /* "0b1100000":  8 KB memory region (VU_CTL1.ADDR[12:8] ignored). */
275                 case 8192uL:
276                     memFrameMask = 0x60u;
277                     break;
278                 /* "0b1110000":  4 KB memory region (VU_CTL1.ADDR[11:8] ignored). */
279                 case 4096uL:
280                     memFrameMask = 0x70u;
281                     break;
282                 /* "0b1111000":  2 KB memory region (VU_CTL1.ADDR[10:8] ignored). */
283                 case 2048uL:
284                     memFrameMask = 0x78u;
285                     break;
286                 /* "0b1111100":  1 KB memory region (VU_CTL1.ADDR[9:8]  ignored). */
287                 case 1024uL:
288                     memFrameMask = 0x7Cu;
289                     break;
290                 /* "0b1111110": 512 B memory region (VU_CTL1.ADDR[8]    ignored). */
291                 case 512uL:
292                     memFrameMask = 0x7Eu;
293                     break;
294                 /* "0b1111111": 256 B memory region (default for HW). */
295                 case 256uL:
296                     memFrameMask = 0x7Fu;
297                     break;
298                 default:
299             /* Unknown mask */
300                     break;
301             }
302 
303             if (memFrameMask != 0xFFFFFFFFuL)
304             {
305                 #if !defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
306                 if (!(CY_CRYPTO_V1))
307                 {
308                     memAlignMask = vuMemSize - 1uL;
309                 }
310                 #endif
311 
312                 /* Use the new address when it aligned to appropriate memory block size */
313                 if (((uint32_t)vuMemAddr & (memAlignMask)) == 0uL)
314                 {
315                     #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
316                     if (!(CY_CRYPTO_V1))
317                     {
318                         REG_CRYPTO_VU_CTL2(base) = _VAL2FLD(CRYPTO_V2_VU_CTL2_MASK, memFrameMask);
319                     }
320                     #endif
321 
322                     REG_CRYPTO_VU_CTL1(base) = (uint32_t)vuMemAddr;
323 
324                     /* Set the stack pointer to the Crypto buff size, in words */
325                     CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG15, vuMemSize / 4u, 1u);
326 
327                     cy_cryptoVuMemSize = vuMemSize;
328 
329                     resultVal = CY_CRYPTO_SUCCESS;
330                 }
331             }
332         }
333 #if !defined(CY_CRYPTO_CFG_HW_USE_MPN_SPECIFIC)
334     }
335 #endif
336     return resultVal;
337 }
338 
339 /*******************************************************************************
340 * Function Name: Cy_Crypto_Core_GetVuMemorySize
341 ****************************************************************************//**
342 *
343 * Get Crypto memory buffer size
344 *
345 * \param base
346 * The pointer to the CRYPTO instance.
347 *
348 * \return
349 * The current MEM_BUFF size in bytes.
350 *
351 *******************************************************************************/
Cy_Crypto_Core_GetVuMemorySize(CRYPTO_Type * base)352 uint32_t Cy_Crypto_Core_GetVuMemorySize(CRYPTO_Type *base)
353 {
354     uint32_t memSize = CY_CRYPTO_MEM_BUFF_SIZE;
355 
356 #if !defined(CY_CRYPTO_CFG_HW_USE_MPN_SPECIFIC)
357     if ( (cy_cryptoIP != NULL) && (cy_cryptoVuMemSize != 0uL))
358     {
359 #endif
360         if (CY_CRYPTO_V1)
361         {
362         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
363             memSize = cy_cryptoVuMemSize;
364         #endif
365         }
366         else
367         {
368         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
369             uint32_t memFrameMask = _FLD2VAL(CRYPTO_V2_VU_CTL2_MASK, REG_CRYPTO_VU_CTL2(base));
370             /*
371             Specifies the size of  the vector operand memory region.
372             Legal values:
373             "0b0000000":  32 KB memory region.
374             "0b1000000":  16 KB memory region.
375             "0b1100000":   8 KB memory region.
376             "0b1110000":   4 KB memory region.
377             "0b1111000":   2 KB memory region.
378             "0b1111100":   1 KB memory region.
379             "0b1111110": 512  B memory region.
380             "0b1111111": 256  B memory region.
381             */
382             switch (memFrameMask)
383             {
384                 case 0x0u:
385                     memSize = 32768uL;
386                     break;
387                 case 0x40u:
388                     memSize = 16384uL;
389                     break;
390                 case 0x60u:
391                     memSize = 8192uL;
392                     break;
393                 case 0x70u:
394                     memSize = 4096uL;
395                     break;
396                 case 0x78u:
397                     memSize = 2048uL;
398                     break;
399                 case 0x7Cu:
400                     memSize = 1024uL;
401                     break;
402                 case 0x7Eu:
403                     memSize = 512uL;
404                     break;
405                 case 0x7Fu:
406                     memSize = 256uL;
407                     break;
408                 default:
409             /* Unknown mask */
410                     break;
411             }
412         #endif
413         }
414 #if !defined(CY_CRYPTO_CFG_HW_USE_MPN_SPECIFIC)
415     }
416 #endif
417 
418     (void)base; /* Suppress a compiler warning about unused variables */
419 
420     return memSize;
421 }
422 
423 /*******************************************************************************
424 * Function Name: Cy_Crypto_Core_Enable
425 ****************************************************************************//**
426 *
427 * The function to enable the Crypto hardware.
428 *
429 * \param base
430 * The pointer to the CRYPTO instance.
431 *
432 * \return
433 * Crypto status \ref cy_en_crypto_status_t
434 *
435 * \funcusage
436 * \snippet crypto/snippet/main.c snippet_myCryptoCoreStartCryptoUse
437 *
438 *******************************************************************************/
Cy_Crypto_Core_Enable(CRYPTO_Type * base)439 cy_en_crypto_status_t Cy_Crypto_Core_Enable(CRYPTO_Type *base)
440 {
441     #if !defined(CY_CRYPTO_CFG_HW_USE_MPN_SPECIFIC)
442     Cy_Crypto_Core_HwInit();
443     #endif
444 
445     /* Disable Crypto HW */
446     REG_CRYPTO_CTL(base) &= ~(_VAL2FLD(CRYPTO_CTL_ENABLED,  1uL));
447 
448     if (CY_CRYPTO_V1)
449     {
450         /* Enable Crypto HW */
451         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
452         REG_CRYPTO_CTL(base) = (uint32_t)(_VAL2FLD(CRYPTO_CTL_PWR_MODE, CY_CRYPTO_PWR_MODE_ENABLED) |
453                                _VAL2FLD(CRYPTO_CTL_ENABLED,  1uL));
454         #endif
455     }
456     else
457     {
458         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
459         REG_CRYPTO_CTL(base) &= ~(_VAL2FLD(CRYPTO_V2_CTL_ENABLED,  1uL) | _VAL2FLD(CRYPTO_V2_CTL_ECC_EN, 1uL));
460 
461         REG_CRYPTO_INSTR_FF_CTL(base) = (uint32_t)(_VAL2FLD(CRYPTO_V2_INSTR_FF_CTL_BLOCK, 1u)
462                                               | _VAL2FLD(CRYPTO_V2_INSTR_FF_CTL_CLEAR, 0u)
463                                               | _VAL2FLD(CRYPTO_V2_INSTR_FF_CTL_EVENT_LEVEL, 1u));
464 
465         REG_CRYPTO_CTL(base) |= _VAL2FLD(CRYPTO_V2_CTL_ENABLED,  1uL);
466 
467         REG_CRYPTO_RAM_PWR_CTL(base) = (uint32_t)(CY_CRYPTO_PWR_MODE_ENABLED);
468         #endif
469     }
470 
471     /*
472     Specifies if a conditional instruction is executed or not, when its condition
473     code evaluates to false("0"):
474       "0": The instruction is NOT executed. As a result, the instruction may be
475            handled faster than when it is executed.
476       "1": The instruction is executed, but the execution result (including
477            status field information) is not reflected in the IP. The instruction
478            is handled just as fast as when it is executed.
479     */
480     REG_CRYPTO_VU_CTL0(base) = (uint32_t)1u;
481 
482     if (0uL == cy_cryptoVuMemSize)
483     {
484         /* Set the memory address and set stack pointer to the Crypto buff size, in words */
485         (void)Cy_Crypto_Core_SetVuMemoryAddress(base, REG_CRYPTO_MEM_BUFF(base), CY_CRYPTO_MEM_BUFF_SIZE);
486     }
487 
488     /* Clear whole register file */
489     Cy_Crypto_Core_ClearVuRegisters(base);
490 
491     return (CY_CRYPTO_SUCCESS);
492 }
493 
494 /*******************************************************************************
495 * Function Name: Cy_Crypto_Core_GetLibInfo
496 ****************************************************************************//**
497 *
498 * Get Crypto service information
499 *
500 * \param libInfo
501 * The pointer to a variable to store gathered crypto library information.
502 *
503 * \return
504 * \ref cy_en_crypto_status_t
505 *
506 *******************************************************************************/
Cy_Crypto_Core_GetLibInfo(cy_en_crypto_lib_info_t * libInfo)507 cy_en_crypto_status_t Cy_Crypto_Core_GetLibInfo(cy_en_crypto_lib_info_t *libInfo)
508 {
509     *libInfo = (cy_en_crypto_lib_info_t)CY_CRYPTO_SERVICE_LIBRARY_LEVEL;
510 
511     return (CY_CRYPTO_SUCCESS);
512 }
513 
514 /*******************************************************************************
515 * Function Name: Cy_Crypto_Core_Disable
516 ****************************************************************************//**
517 *
518 * Disables the operation of the CRYPTO block.
519 *
520 * \param base
521 * The pointer to the CRYPTO instance.
522 *
523 * \return
524 * \ref cy_en_crypto_status_t
525 *
526 * \funcusage
527 * \snippet crypto/snippet/main.c snippet_myCryptoCoreStopCryptoUse
528 *
529 *******************************************************************************/
Cy_Crypto_Core_Disable(CRYPTO_Type * base)530 cy_en_crypto_status_t Cy_Crypto_Core_Disable(CRYPTO_Type *base)
531 {
532     /* Power-off Crypto RAM */
533     if (CY_CRYPTO_V1)
534     {
535         /* Disable Crypto HW */
536         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
537         REG_CRYPTO_CTL(base) = (uint32_t)(_VAL2FLD(CRYPTO_CTL_PWR_MODE, CY_CRYPTO_PWR_MODE_OFF) |
538                                _VAL2FLD(CRYPTO_CTL_ENABLED, 0uL));
539         #endif
540     }
541     else
542     {
543         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
544         REG_CRYPTO_CTL(base) = (uint32_t)(_VAL2FLD(CRYPTO_V2_CTL_ENABLED,  0uL));
545         REG_CRYPTO_RAM_PWR_CTL(base) = (uint32_t)(CY_CRYPTO_PWR_MODE_OFF);
546         #endif
547     }
548 
549     cy_cryptoVuMemSize = 0uL;
550 
551     return (CY_CRYPTO_SUCCESS);
552 }
553 
554 
555 /*******************************************************************************
556 * Function Name: Cy_Crypto_Core_Cleanup
557 ****************************************************************************//**
558 *
559 * Cleans up the Crypto block.
560 *
561 * \param base
562 * The pointer to the CRYPTO instance.
563 *
564 * \return
565 * \ref cy_en_crypto_status_t
566 *
567 *******************************************************************************/
Cy_Crypto_Core_Cleanup(CRYPTO_Type * base)568 cy_en_crypto_status_t Cy_Crypto_Core_Cleanup(CRYPTO_Type *base)
569 {
570     uint16_t vu_mem_size = 0U;
571     void *vu_mem_address = NULL;
572     /* Clear whole register file */
573     Cy_Crypto_Core_ClearVuRegisters(base);
574 
575     /* PRNG */
576     REG_CRYPTO_PR_LFSR_CTL0(base) = 0u;
577     REG_CRYPTO_PR_LFSR_CTL1(base) = 0u;
578     REG_CRYPTO_PR_LFSR_CTL2(base) = 0u;
579     REG_CRYPTO_PR_RESULT(base)    = 0u;
580 
581     /* TRNG */
582     REG_CRYPTO_TR_CTL0(base)      = 0u;
583     REG_CRYPTO_TR_CTL1(base)      = 0u;
584     REG_CRYPTO_TR_RESULT(base)    = 0u;
585 
586     /* CRC */
587     REG_CRYPTO_CRC_POL_CTL(base)  = 0u;
588     REG_CRYPTO_CRC_REM_CTL(base)  = 0u;
589 
590     /* AES */
591     REG_CRYPTO_AES_CTL(base)      = 0u;
592 
593     if (CY_CRYPTO_V1)
594     {
595         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
596         REG_CRYPTO_CRC_LFSR_CTL(base) = 0u;
597         REG_CRYPTO_SHA_CTL(base)  = 0u;
598         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
599     }
600     else
601     {
602         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
603         REG_CRYPTO_TR_CTL2(base)  = 0u;
604         REG_CRYPTO_RESULT(base)   = 0u;
605 
606         Cy_Crypto_Core_V2_FFStop(base, CY_CRYPTO_V2_RB_FF_LOAD0);
607         Cy_Crypto_Core_V2_FFStop(base, CY_CRYPTO_V2_RB_FF_LOAD1);
608         Cy_Crypto_Core_V2_FFStop(base, CY_CRYPTO_V2_RB_FF_STORE);
609         Cy_Crypto_Core_V2_RBClear(base);
610         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
611     }
612 
613     vu_mem_address = Cy_Crypto_Core_GetVuMemoryAddress(base);
614     vu_mem_size = (uint16_t)Cy_Crypto_Core_GetVuMemorySize(base);
615 
616     Cy_Crypto_Core_MemSet(base, vu_mem_address, 0u, vu_mem_size);
617 
618     return (CY_CRYPTO_SUCCESS);
619 }
620 
621 
622 
623 /*******************************************************************************
624 * Function Name: Cy_Crypto_Core_Shutdown
625 ****************************************************************************//**
626 *
627 * Cleans up the Crypto block, disable it and reset all crypto registers.
628 *
629 * \param base
630 * The pointer to the CRYPTO instance.
631 *
632 * \return
633 * \ref cy_en_crypto_status_t
634 *
635 *******************************************************************************/
Cy_Crypto_Core_Shutdown(CRYPTO_Type * base)636 cy_en_crypto_status_t Cy_Crypto_Core_Shutdown(CRYPTO_Type *base)
637 {
638     /* Clear all sensitive information */
639     (void)Cy_Crypto_Core_Cleanup(base);
640 
641     (void)Cy_Crypto_Core_Disable(base);
642 
643     /* Restores Crypto configuration registers to their default values */
644     REG_CRYPTO_CTL(base) = 0UL;
645     REG_CRYPTO_INSTR_FF_CTL(base) = 0UL;
646     REG_CRYPTO_INSTR_FF_WR(base) = 0UL;
647 
648     REG_CRYPTO_CRC_CTL(base) = 0UL;
649     REG_CRYPTO_CRC_DATA_CTL(base) = 0UL;
650     REG_CRYPTO_CRC_POL_CTL(base) = 0UL;
651     REG_CRYPTO_CRC_REM_CTL(base) = 0UL;
652     #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
653     REG_CRYPTO_CRC_LFSR_CTL(base) = 0UL;
654     #endif
655 
656     REG_CRYPTO_VU_CTL0(base) = 0UL;
657     REG_CRYPTO_VU_CTL1(base) = 0UL;
658     #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
659     REG_CRYPTO_VU_CTL2(base) = 0UL;
660     #endif
661 
662     REG_CRYPTO_INTR(base) = 0xFFFFFFFFUL; /* All bits are cleared by write to 1 */
663 
664     return (CY_CRYPTO_SUCCESS);
665 }
666 
667 /*******************************************************************************
668 * Function Name: Cy_Crypto_Core_InvertEndianness
669 ****************************************************************************//**
670 *
671 * This function reverts byte-array memory block, like:<br>
672 * inArr[0] <---> inArr[n]<br>
673 * inArr[1] <---> inArr[n-1]<br>
674 * inArr[2] <---> inArr[n-2]<br>
675 * ........................<br>
676 * inArr[n/2] <---> inArr[n/2-1]<br>
677 *
678 * Odd or even byteSize are acceptable.
679 *
680 * \param inArrPtr
681 * The pointer to the memory whose endianness is to be inverted.
682 *
683 * \param byteSize
684 * The length of the memory array whose endianness is to be inverted (in bytes)
685 *
686 *******************************************************************************/
Cy_Crypto_Core_InvertEndianness(void * inArrPtr,uint32_t byteSize)687 void Cy_Crypto_Core_InvertEndianness(void *inArrPtr, uint32_t byteSize)
688 {
689     int32_t limit;
690     int32_t i;
691     int32_t j = 0;
692     uint8_t temp;
693     uint8_t *tempPtr = (uint8_t*)inArrPtr;
694 
695     if (byteSize > 1u)
696     {
697         limit = (int32_t)byteSize / 2;
698         if (0u == (byteSize % 2u))
699         {
700             --limit;
701         }
702 
703         j = 0;
704         i = (int32_t)byteSize - 1;
705         while ( i > limit)
706         {
707             temp = tempPtr[j];
708             tempPtr[j] = tempPtr[i];
709             tempPtr[i] = temp;
710 
711             --i;
712             ++j;
713         }
714     }
715 }
716 
717 /** \} group_crypto_lld_hw_functions */
718 
719 CY_MISRA_BLOCK_END('MISRA C-2012 Rule 14.3')
720 CY_MISRA_BLOCK_END('MISRA C-2012 Rule 11.3')
721 
722 #if defined(__cplusplus)
723 }
724 #endif
725 
726 #endif /* CY_IP_MXCRYPTO */
727 
728 
729 /* [] END OF FILE */
730