1 /***************************************************************************//**
2 * \file cy_crypto_core_hw.c
3 * \version 2.40
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 2016-2020 Cypress Semiconductor Corporation
11 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 *    http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *******************************************************************************/
25 
26 #include "cy_device.h"
27 
28 #if defined (CY_IP_MXCRYPTO)
29 
30 #include "cy_crypto_common.h"
31 
32 #if defined(__cplusplus)
33 extern "C" {
34 #endif
35 
36 #include "cy_crypto_core_hw.h"
37 #include "cy_crypto_core_hw_vu.h"
38 #include "cy_syslib.h"
39 #include <stdbool.h>
40 
41 CY_MISRA_DEVIATE_BLOCK_START('MISRA C-2012 Rule 11.3', 4, \
42 '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.');
43 
44 #if !defined (CY_CRYPTO_SERVICE_LIBRARY_LEVEL)
45     #define CY_CRYPTO_SERVICE_LIBRARY_LEVEL CY_CRYPTO_FULL_LIBRARY
46 #endif
47 
48 /*******************************************************************************
49 *                   Global Variables
50 *******************************************************************************/
51 
52 /* This is set in Cy_Crypto_Core_Enable() to the device information relevant
53  * for the current target.
54  */
55 const cy_stc_cryptoIP_t * cy_cryptoIP = NULL;
56 
57 static uint32_t  cy_cryptoVuMemSize = 0u;
58 
59 /* Platform and peripheral crypto block configuration */
60 const cy_stc_cryptoIP_t cy_cryptoIpBlockCfgPSoC6_01 =
61 {
62     /* CRYPTO register offsets */
63     /* cryptoStatusOffset        */ offsetof(CRYPTO_V1_Type, STATUS),
64     /* cryptoIstrFfCtlOffset     */ offsetof(CRYPTO_V1_Type, INSTR_FF_CTL),
65     /* cryptoInstrFfStatusOffset */ offsetof(CRYPTO_V1_Type, INSTR_FF_STATUS),
66     /* cryptoInstrFfWrOffset     */ offsetof(CRYPTO_V1_Type, INSTR_FF_WR),
67     /* cryptoVuRfDataOffset      */ offsetof(CRYPTO_V1_Type, RF_DATA),
68     /* cryptoAesCtlOffset        */ offsetof(CRYPTO_V1_Type, AES_CTL),
69     /* cryptoPrResultOffset      */ offsetof(CRYPTO_V1_Type, PR_RESULT),
70     /* cryptoTrResultOffset      */ offsetof(CRYPTO_V1_Type, TR_RESULT),
71     /* cryptoCrcCtlOffset        */ offsetof(CRYPTO_V1_Type, CRC_CTL),
72     /* cryptoCrcDataCtlOffset    */ offsetof(CRYPTO_V1_Type, CRC_DATA_CTL),
73     /* cryptoCrcPolCtlOffset     */ offsetof(CRYPTO_V1_Type, CRC_POL_CTL),
74     /* cryptoCrcRemCtlOffset     */ offsetof(CRYPTO_V1_Type, CRC_REM_CTL),
75     /* cryptoCrcRemResultOffset  */ offsetof(CRYPTO_V1_Type, CRC_REM_RESULT),
76     /* cryptoVuCtl0Offset        */ offsetof(CRYPTO_V1_Type, VU_CTL0),
77     /* cryptoVuCtl1Offset        */ offsetof(CRYPTO_V1_Type, VU_CTL1),
78     /* cryptoVuStatusOffset      */ offsetof(CRYPTO_V1_Type, VU_STATUS),
79     /* cryptoIntrOffset          */ offsetof(CRYPTO_V1_Type, INTR),
80     /* cryptoIntrSetOffset       */ offsetof(CRYPTO_V1_Type, INTR_SET),
81     /* cryptoIntrMaskOffset      */ offsetof(CRYPTO_V1_Type, INTR_MASK),
82     /* cryptoIntrMaskedOffset    */ offsetof(CRYPTO_V1_Type, INTR_MASKED),
83     /* cryptoMemBufOffset        */ offsetof(CRYPTO_V1_Type, MEM_BUFF),
84 };
85 
86 const cy_stc_cryptoIP_t cy_cryptoIpBlockCfgPSoC6_02 =
87 {
88     /* CRYPTO register offsets */
89     /* cryptoStatusOffset        */ offsetof(CRYPTO_V2_Type, STATUS),
90     /* cryptoIstrFfCtlOffset     */ offsetof(CRYPTO_V2_Type, INSTR_FF_CTL),
91     /* cryptoInstrFfStatusOffset */ offsetof(CRYPTO_V2_Type, INSTR_FF_STATUS),
92     /* cryptoInstrFfWrOffset     */ offsetof(CRYPTO_V2_Type, INSTR_FF_WR),
93     /* cryptoVuRfDataOffset      */ offsetof(CRYPTO_V2_Type, VU_RF_DATA),
94     /* cryptoAesCtlOffset        */ offsetof(CRYPTO_V2_Type, AES_CTL),
95     /* cryptoPrResultOffset      */ offsetof(CRYPTO_V2_Type, PR_RESULT),
96     /* cryptoTrResultOffset      */ offsetof(CRYPTO_V2_Type, TR_RESULT),
97     /* cryptoCrcCtlOffset        */ offsetof(CRYPTO_V2_Type, CRC_CTL),
98     /* cryptoCrcDataCtlOffset    */ offsetof(CRYPTO_V2_Type, CRC_DATA_CTL),
99     /* cryptoCrcPolCtlOffset     */ offsetof(CRYPTO_V2_Type, CRC_POL_CTL),
100     /* cryptoCrcRemCtlOffset     */ offsetof(CRYPTO_V2_Type, CRC_REM_CTL),
101     /* cryptoCrcRemResultOffset  */ offsetof(CRYPTO_V2_Type, CRC_REM_RESULT),
102     /* cryptoVuCtl0Offset        */ offsetof(CRYPTO_V2_Type, VU_CTL0),
103     /* cryptoVuCtl1Offset        */ offsetof(CRYPTO_V2_Type, VU_CTL1),
104     /* cryptoVuStatusOffset      */ offsetof(CRYPTO_V2_Type, VU_STATUS),
105     /* cryptoIntrOffset          */ offsetof(CRYPTO_V2_Type, INTR),
106     /* cryptoIntrSetOffset       */ offsetof(CRYPTO_V2_Type, INTR_SET),
107     /* cryptoIntrMaskOffset      */ offsetof(CRYPTO_V2_Type, INTR_MASK),
108     /* cryptoIntrMaskedOffset    */ offsetof(CRYPTO_V2_Type, INTR_MASKED),
109     /* cryptoMemBufOffset        */ offsetof(CRYPTO_V2_Type, MEM_BUFF),
110 };
111 
112 /* The defines of the power modes of the CRYPTO */
113 #define CY_CRYPTO_PWR_MODE_OFF               (0UL)
114 #define CY_CRYPTO_PWR_MODE_RETAINED          (2UL)
115 #define CY_CRYPTO_PWR_MODE_ENABLED           (3UL)
116 
117 /*******************************************************************************
118 * Function Name: Cy_Crypto_Core_Vu_RunInstr
119 *****************************************************************************//**
120 *
121 * Run the Crypto Vector Unit instruction with one parameter.
122 *
123 * \param base
124 * The pointer to the CRYPTO instance.
125 
126 * \param blockingMode
127 * Sets the blocking or non-blocking operation mode.
128 *
129 * \param instr
130 * The Opcode of the called instruction.
131 *
132 * \param params
133 * The parameters for the instruction operand.
134 *
135 *******************************************************************************/
Cy_Crypto_Core_Vu_RunInstr(CRYPTO_Type * base,bool blockingMode,uint32_t instr,uint32_t params)136 void Cy_Crypto_Core_Vu_RunInstr(CRYPTO_Type *base, bool blockingMode, uint32_t instr, uint32_t params)
137 {
138     bool isRelocated = Cy_Crypto_Core_GetVuMemoryAddress(base) != REG_CRYPTO_MEM_BUFF(base);
139 
140     /* Check whether FIFO has enough space for 1 instruction */
141     Cy_Crypto_Core_WaitForInstrFifoAvailable(base, CY_CRYPTO_INSTR_SINGLE);
142 
143     REG_CRYPTO_INSTR_FF_WR(base) = (uint32_t)((instr << CY_CRYPTO_OPCODE_POS) | (params));
144 
145     if ( (blockingMode) || (isRelocated) )
146     {
147         Cy_Crypto_Core_WaitForFifoAvailable(base);
148         Cy_Crypto_Core_Vu_WaitForComplete(base);
149     }
150 }
151 
152 /**
153 * \addtogroup group_crypto_lld_hw_functions
154 * \{
155 */
156 
157 /*******************************************************************************
158 * Function Name: Cy_Crypto_Core_ClearVuRegisters
159 ****************************************************************************//**
160 *
161 * The function to initialize the Crypto VU registers.
162 *
163 * \param base
164 * The pointer to the CRYPTO instance.
165 *
166 *******************************************************************************/
Cy_Crypto_Core_ClearVuRegisters(CRYPTO_Type * base)167 void Cy_Crypto_Core_ClearVuRegisters(CRYPTO_Type *base)
168 {
169     /* Clear whole register file */
170     CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG14, 0u, 1u);
171     CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG13, 0u, 1u);
172     CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG12, 0u, 1u);
173     CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG11, 0u, 1u);
174     CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG10, 0u, 1u);
175     CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG9,  0u, 1u);
176     CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG8,  0u, 1u);
177     CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG7,  0u, 1u);
178     CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG6,  0u, 1u);
179     CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG5,  0u, 1u);
180     CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG4,  0u, 1u);
181     CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG3,  0u, 1u);
182     CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG2,  0u, 1u);
183     CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG1,  0u, 1u);
184     CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG0,  0u, 1u);
185 
186     /* Set the stack pointer to the Crypto buff size, in words */
187     CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG15, cy_cryptoVuMemSize / 4u, 1u);
188 }
189 
190 /*******************************************************************************
191 * Function Name: Cy_Crypto_Core_HwInit
192 ****************************************************************************//**
193 *
194 * The function to initialize the Crypto hardware.
195 *
196 *******************************************************************************/
Cy_Crypto_Core_HwInit(void)197 void Cy_Crypto_Core_HwInit(void)
198 {
199     cy_cryptoIP = (CY_CRYPTO_V1) ? &cy_cryptoIpBlockCfgPSoC6_01 : &cy_cryptoIpBlockCfgPSoC6_02;
200 }
201 
202 /*******************************************************************************
203 * Function Name: Cy_Crypto_Core_SetVuMemoryAddress
204 ****************************************************************************//**
205 *
206 * Sets the new memory buffer address and size.
207 *
208 * \param base
209 * The pointer to the CRYPTO instance.
210 *
211 * \param vuMemoryAddr
212 * The memory buffer location that will be used as Crypto MEM_BUFF
213 *
214 * \param vuMemorySize
215 * The provided memory buffer size in bytes.
216 *
217 * \return
218 * \ref cy_en_crypto_status_t
219 *
220 * \note This function sets the default device specific values
221 *       when vuMemoryAddr parameter is NULL and vuMemorySize parameter is zero.
222 *
223 * \note New memory buffer should be allocated in a memory region that is not
224 *       protected by a protection scheme for use by Crypto hardware.
225 *
226 *******************************************************************************/
Cy_Crypto_Core_SetVuMemoryAddress(CRYPTO_Type * base,uint32_t const * vuMemoryAddr,uint32_t vuMemorySize)227 cy_en_crypto_status_t Cy_Crypto_Core_SetVuMemoryAddress(CRYPTO_Type *base,
228                                                         uint32_t const *vuMemoryAddr, uint32_t vuMemorySize)
229 {
230     cy_en_crypto_status_t resultVal = CY_CRYPTO_BAD_PARAMS;
231     uint32_t *vuMemAddr = (uint32_t *)vuMemoryAddr;
232     uint32_t  vuMemSize = vuMemorySize;
233 
234     if (cy_cryptoIP != NULL)
235     {
236         if ((vuMemAddr == NULL) && (vuMemSize == 0uL))
237         {
238             vuMemAddr = REG_CRYPTO_MEM_BUFF(base);
239             vuMemSize = CY_CRYPTO_MEM_BUFF_SIZE;
240         }
241 
242         /* Check for new memory size is less or equal to maximal IP allowed value */
243         if ((vuMemAddr != NULL) && (vuMemSize != 0uL) && (vuMemSize <= 32768u))
244         {
245             /* mxcrypto (V1) IP uses MEM_BUF aligned to 16KB */
246             uint32_t memAlignMask = 16384uL - 1uL;
247 
248             uint32_t memFrameMask = 0xFFFFFFFFuL;
249             /*
250             Specifies the size of  the vector operand memory region.
251             Legal values:
252             "0b0000000":  32 KB memory region (VU_CTL1.ADDR[14:8] ignored).
253             "0b1000000":  16 KB memory region (VU_CTL1.ADDR[13:8] ignored).
254             "0b1100000":   8 KB memory region (VU_CTL1.ADDR[12:8] ignored).
255             "0b1110000":   4 KB memory region (VU_CTL1.ADDR[11:8] ignored).
256             "0b1111000":   2 KB memory region (VU_CTL1.ADDR[10:8] ignored).
257             "0b1111100":   1 KB memory region (VU_CTL1.ADDR[9:8]  ignored).
258             "0b1111110": 512  B memory region (VU_CTL1.ADDR[8]    ignored).
259             "0b1111111": 256  B memory region.
260             */
261             switch (vuMemSize)
262             {
263                 /* "0b0000000": 32 KB memory region (VU_CTL1.ADDR[14:8] ignored). */
264                 case 32768uL:
265                     memFrameMask = 0x0u;
266                     break;
267                 /* "0b1000000": 16 KB memory region (VU_CTL1.ADDR[13:8] ignored). */
268                 case 16384uL:
269                     memFrameMask = 0x40u;
270                     break;
271                 /* "0b1100000":  8 KB memory region (VU_CTL1.ADDR[12:8] ignored). */
272                 case 8192uL:
273                     memFrameMask = 0x60u;
274                     break;
275                 /* "0b1110000":  4 KB memory region (VU_CTL1.ADDR[11:8] ignored). */
276                 case 4096uL:
277                     memFrameMask = 0x70u;
278                     break;
279                 /* "0b1111000":  2 KB memory region (VU_CTL1.ADDR[10:8] ignored). */
280                 case 2048uL:
281                     memFrameMask = 0x78u;
282                     break;
283                 /* "0b1111100":  1 KB memory region (VU_CTL1.ADDR[9:8]  ignored). */
284                 case 1024uL:
285                     memFrameMask = 0x7Cu;
286                     break;
287                 /* "0b1111110": 512 B memory region (VU_CTL1.ADDR[8]    ignored). */
288                 case 512uL:
289                     memFrameMask = 0x7Eu;
290                     break;
291                 /* "0b1111111": 256 B memory region (default for HW). */
292                 case 256uL:
293                     memFrameMask = 0x7Fu;
294                     break;
295                 default:
296             /* Unknown mask */
297                     break;
298             }
299 
300             if (memFrameMask != 0xFFFFFFFFuL)
301             {
302                 if (!(CY_CRYPTO_V1))
303                 {
304                     memAlignMask = vuMemSize - 1uL;
305                 }
306 
307                 /* Use the new address when it aligned to appropriate memory block size */
308                 if (((uint32_t)vuMemAddr & (memAlignMask)) == 0uL)
309                 {
310                     if (!(CY_CRYPTO_V1))
311                     {
312                         REG_CRYPTO_VU_CTL2(base) = _VAL2FLD(CRYPTO_V2_VU_CTL2_MASK, memFrameMask);
313                     }
314 
315                     REG_CRYPTO_VU_CTL1(base) = (uint32_t)vuMemAddr;
316 
317                     /* Set the stack pointer to the Crypto buff size, in words */
318                     CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG15, vuMemSize / 4u, 1u);
319 
320                     cy_cryptoVuMemSize = vuMemSize;
321 
322                     resultVal = CY_CRYPTO_SUCCESS;
323                 }
324             }
325         }
326     }
327 
328     return resultVal;
329 }
330 
331 /*******************************************************************************
332 * Function Name: Cy_Crypto_Core_GetVuMemorySize
333 ****************************************************************************//**
334 *
335 * Get Crypto memory buffer size
336 *
337 * \param base
338 * The pointer to the CRYPTO instance.
339 *
340 * \return
341 * The current MEM_BUFF size in bytes.
342 *
343 *******************************************************************************/
Cy_Crypto_Core_GetVuMemorySize(CRYPTO_Type * base)344 uint32_t Cy_Crypto_Core_GetVuMemorySize(CRYPTO_Type *base)
345 {
346     uint32_t memSize = CY_CRYPTO_MEM_BUFF_SIZE;
347 
348     if ( (cy_cryptoIP != NULL) && (cy_cryptoVuMemSize != 0uL))
349     {
350         if (CY_CRYPTO_V1)
351         {
352             memSize = cy_cryptoVuMemSize;
353         }
354         else
355         {
356             uint32_t memFrameMask = _FLD2VAL(CRYPTO_V2_VU_CTL2_MASK, REG_CRYPTO_VU_CTL2(base));
357             /*
358             Specifies the size of  the vector operand memory region.
359             Legal values:
360             "0b0000000":  32 KB memory region.
361             "0b1000000":  16 KB memory region.
362             "0b1100000":   8 KB memory region.
363             "0b1110000":   4 KB memory region.
364             "0b1111000":   2 KB memory region.
365             "0b1111100":   1 KB memory region.
366             "0b1111110": 512  B memory region.
367             "0b1111111": 256  B memory region.
368             */
369             switch (memFrameMask)
370             {
371                 case 0x0u:
372                     memSize = 32768uL;
373                     break;
374                 case 0x40u:
375                     memSize = 16384uL;
376                     break;
377                 case 0x60u:
378                     memSize = 8192uL;
379                     break;
380                 case 0x70u:
381                     memSize = 4096uL;
382                     break;
383                 case 0x78u:
384                     memSize = 2048uL;
385                     break;
386                 case 0x7Cu:
387                     memSize = 1024uL;
388                     break;
389                 case 0x7Eu:
390                     memSize = 512uL;
391                     break;
392                 case 0x7Fu:
393                     memSize = 256uL;
394                     break;
395                 default:
396             /* Unknown mask */
397                     break;
398             }
399         }
400     }
401 
402     return memSize;
403 }
404 
405 /*******************************************************************************
406 * Function Name: Cy_Crypto_Core_Enable
407 ****************************************************************************//**
408 *
409 * The function to enable the Crypto hardware.
410 *
411 * \param base
412 * The pointer to the CRYPTO instance.
413 *
414 * \return
415 * Crypto status \ref cy_en_crypto_status_t
416 *
417 *******************************************************************************/
Cy_Crypto_Core_Enable(CRYPTO_Type * base)418 cy_en_crypto_status_t Cy_Crypto_Core_Enable(CRYPTO_Type *base)
419 {
420     Cy_Crypto_Core_HwInit();
421 
422     if (CY_CRYPTO_V1)
423     {
424         /* Enable Crypto HW */
425         REG_CRYPTO_CTL(base) = (uint32_t)(_VAL2FLD(CRYPTO_CTL_PWR_MODE, CY_CRYPTO_PWR_MODE_ENABLED) |
426                                _VAL2FLD(CRYPTO_CTL_ENABLED,  1uL));
427     }
428     else
429     {
430         REG_CRYPTO_CTL(base) &= ~(_VAL2FLD(CRYPTO_V2_CTL_ENABLED,  1uL) | _VAL2FLD(CRYPTO_V2_CTL_ECC_EN, 1uL));
431 
432         REG_CRYPTO_INSTR_FF_CTL(base) = (uint32_t)(_VAL2FLD(CRYPTO_V2_INSTR_FF_CTL_BLOCK, 1u)
433                                               | _VAL2FLD(CRYPTO_V2_INSTR_FF_CTL_CLEAR, 0u)
434                                               | _VAL2FLD(CRYPTO_V2_INSTR_FF_CTL_EVENT_LEVEL, 1u));
435 
436         REG_CRYPTO_CTL(base) |= _VAL2FLD(CRYPTO_V2_CTL_ENABLED,  1uL);
437 
438         REG_CRYPTO_RAM_PWR_CTL(base) = (uint32_t)(CY_CRYPTO_PWR_MODE_ENABLED);
439     }
440 
441     /*
442     Specifies if a conditional instruction is executed or not, when its condition
443     code evaluates to false("0"):
444       "0": The instruction is NOT executed. As a result, the instruction may be
445            handled faster than when it is executed.
446       "1": The instruction is executed, but the execution result (including
447            status field information) is not reflected in the IP. The instruction
448            is handled just as fast as when it is executed.
449     */
450     REG_CRYPTO_VU_CTL0(base) = (uint32_t)1u;
451 
452     if (0uL == cy_cryptoVuMemSize)
453     {
454         /* Set the memory address and set stack pointer to the Crypto buff size, in words */
455         (void)Cy_Crypto_Core_SetVuMemoryAddress(base, REG_CRYPTO_MEM_BUFF(base), CY_CRYPTO_MEM_BUFF_SIZE);
456     }
457 
458     /* Clear whole register file */
459     Cy_Crypto_Core_ClearVuRegisters(base);
460 
461     return (CY_CRYPTO_SUCCESS);
462 }
463 
464 /*******************************************************************************
465 * Function Name: Cy_Crypto_Core_GetLibInfo
466 ****************************************************************************//**
467 *
468 * Get Crypto service information
469 *
470 * \param libInfo
471 * The pointer to a variable to store gathered crypto library information.
472 *
473 * \return
474 * \ref cy_en_crypto_status_t
475 *
476 *******************************************************************************/
Cy_Crypto_Core_GetLibInfo(cy_en_crypto_lib_info_t * libInfo)477 cy_en_crypto_status_t Cy_Crypto_Core_GetLibInfo(cy_en_crypto_lib_info_t *libInfo)
478 {
479     *libInfo = (cy_en_crypto_lib_info_t)CY_CRYPTO_SERVICE_LIBRARY_LEVEL;
480 
481     return (CY_CRYPTO_SUCCESS);
482 }
483 
484 /*******************************************************************************
485 * Function Name: Cy_Crypto_Core_Disable
486 ****************************************************************************//**
487 *
488 * Disables the operation of the CRYPTO block.
489 *
490 * \param base
491 * The pointer to the CRYPTO instance.
492 *
493 * \return
494 * \ref cy_en_crypto_status_t
495 *
496 *******************************************************************************/
Cy_Crypto_Core_Disable(CRYPTO_Type * base)497 cy_en_crypto_status_t Cy_Crypto_Core_Disable(CRYPTO_Type *base)
498 {
499     if (CY_CRYPTO_V1)
500     {
501         /* Disable Crypto HW */
502         REG_CRYPTO_CTL(base) = (uint32_t)(_VAL2FLD(CRYPTO_CTL_PWR_MODE, CY_CRYPTO_PWR_MODE_OFF) |
503                                _VAL2FLD(CRYPTO_CTL_ENABLED, 0uL));
504     }
505     else
506     {
507         REG_CRYPTO_CTL(base) = (uint32_t)(_VAL2FLD(CRYPTO_V2_CTL_ENABLED,  0uL));
508         REG_CRYPTO_RAM_PWR_CTL(base) = (uint32_t)(CY_CRYPTO_PWR_MODE_OFF);
509     }
510 
511     cy_cryptoVuMemSize = 0uL;
512 
513     return (CY_CRYPTO_SUCCESS);
514 }
515 
516 /*******************************************************************************
517 * Function Name: Cy_Crypto_Core_InvertEndianness
518 ****************************************************************************//**
519 *
520 * This function reverts byte-array memory block, like:<br>
521 * inArr[0] <---> inArr[n]<br>
522 * inArr[1] <---> inArr[n-1]<br>
523 * inArr[2] <---> inArr[n-2]<br>
524 * ........................<br>
525 * inArr[n/2] <---> inArr[n/2-1]<br>
526 *
527 * Odd or even byteSize are acceptable.
528 *
529 * \param inArrPtr
530 * The pointer to the memory whose endianness is to be inverted.
531 *
532 * \param byteSize
533 * The length of the memory array whose endianness is to be inverted (in bytes)
534 *
535 *******************************************************************************/
Cy_Crypto_Core_InvertEndianness(void * inArrPtr,uint32_t byteSize)536 void Cy_Crypto_Core_InvertEndianness(void *inArrPtr, uint32_t byteSize)
537 {
538     int32_t limit;
539     int32_t i;
540     int32_t j = 0;
541     uint8_t temp;
542     uint8_t *tempPtr = (uint8_t*)inArrPtr;
543 
544     if (byteSize > 1u)
545     {
546         limit = (int32_t)byteSize / 2;
547         if (0u == (byteSize % 2u))
548         {
549             --limit;
550         }
551 
552         j = 0;
553         i = (int32_t)byteSize - 1;
554         while ( i > limit)
555         {
556             temp = tempPtr[j];
557             tempPtr[j] = tempPtr[i];
558             tempPtr[i] = temp;
559 
560             --i;
561             ++j;
562         }
563     }
564 }
565 
566 /** \} group_crypto_lld_hw_functions */
567 CY_MISRA_BLOCK_END('MISRA C-2012 Rule 11.3');
568 
569 #if defined(__cplusplus)
570 }
571 #endif
572 
573 #endif /* CY_IP_MXCRYPTO */
574 
575 
576 /* [] END OF FILE */
577