1 /***************************************************************************//**
2 * \file cy_crypto_core_hw.c
3 * \version 2.120
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 *vuMemAddrRemap = (uint32_t *)CY_REMAP_ADDRESS_FOR_CRYPTO(vuMemoryAddr);
233 uint32_t *vuMemAddrIn = (uint32_t *)vuMemoryAddr;
234
235 uint32_t vuMemSize = vuMemorySize;
236
237 #if !defined(CY_CRYPTO_CFG_HW_USE_MPN_SPECIFIC)
238 if (cy_cryptoIP != NULL)
239 {
240 #endif
241 if ((vuMemAddrIn == NULL) && (vuMemSize == 0uL))
242 {
243 vuMemAddrIn = REG_CRYPTO_MEM_BUFF(base);
244 vuMemSize = CY_CRYPTO_MEM_BUFF_SIZE;
245 }
246
247 /* Check for new memory size is less or equal to maximal IP allowed value */
248 if ((vuMemAddrIn != NULL) && (vuMemSize != 0uL) && (vuMemSize <= 32768u))
249 {
250 /* mxcrypto (V1) IP uses MEM_BUF aligned to 16KB */
251 uint32_t memAlignMask = 16384uL - 1uL;
252
253 uint32_t memFrameMask = 0xFFFFFFFFuL;
254 /*
255 Specifies the size of the vector operand memory region.
256 Legal values:
257 "0b0000000": 32 KB memory region (VU_CTL1.ADDR[14:8] ignored).
258 "0b1000000": 16 KB memory region (VU_CTL1.ADDR[13:8] ignored).
259 "0b1100000": 8 KB memory region (VU_CTL1.ADDR[12:8] ignored).
260 "0b1110000": 4 KB memory region (VU_CTL1.ADDR[11:8] ignored).
261 "0b1111000": 2 KB memory region (VU_CTL1.ADDR[10:8] ignored).
262 "0b1111100": 1 KB memory region (VU_CTL1.ADDR[9:8] ignored).
263 "0b1111110": 512 B memory region (VU_CTL1.ADDR[8] ignored).
264 "0b1111111": 256 B memory region.
265 */
266 switch (vuMemSize)
267 {
268 /* "0b0000000": 32 KB memory region (VU_CTL1.ADDR[14:8] ignored). */
269 case 32768uL:
270 memFrameMask = 0x0u;
271 break;
272 /* "0b1000000": 16 KB memory region (VU_CTL1.ADDR[13:8] ignored). */
273 case 16384uL:
274 memFrameMask = 0x40u;
275 break;
276 /* "0b1100000": 8 KB memory region (VU_CTL1.ADDR[12:8] ignored). */
277 case 8192uL:
278 memFrameMask = 0x60u;
279 break;
280 /* "0b1110000": 4 KB memory region (VU_CTL1.ADDR[11:8] ignored). */
281 case 4096uL:
282 memFrameMask = 0x70u;
283 break;
284 /* "0b1111000": 2 KB memory region (VU_CTL1.ADDR[10:8] ignored). */
285 case 2048uL:
286 memFrameMask = 0x78u;
287 break;
288 /* "0b1111100": 1 KB memory region (VU_CTL1.ADDR[9:8] ignored). */
289 case 1024uL:
290 memFrameMask = 0x7Cu;
291 break;
292 /* "0b1111110": 512 B memory region (VU_CTL1.ADDR[8] ignored). */
293 case 512uL:
294 memFrameMask = 0x7Eu;
295 break;
296 /* "0b1111111": 256 B memory region (default for HW). */
297 case 256uL:
298 memFrameMask = 0x7Fu;
299 break;
300 default:
301 /* Unknown mask */
302 break;
303 }
304
305 if (memFrameMask != 0xFFFFFFFFuL)
306 {
307 #if !defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
308 if (!(CY_CRYPTO_V1))
309 {
310 memAlignMask = vuMemSize - 1uL;
311 }
312 #endif
313
314 /* Use the new address when it aligned to appropriate memory block size */
315 if (((uint32_t)vuMemAddrIn & (memAlignMask)) == 0uL)
316 {
317 #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
318 if (!(CY_CRYPTO_V1))
319 {
320 REG_CRYPTO_VU_CTL2(base) = _VAL2FLD(CRYPTO_V2_VU_CTL2_MASK, memFrameMask);
321 }
322 #endif
323
324 REG_CRYPTO_VU_CTL1(base) = (uint32_t)vuMemAddrRemap;
325
326 /* Set the stack pointer to the Crypto buff size, in words */
327 CY_CRYPTO_VU_SET_REG(base, CY_CRYPTO_VU_HW_REG15, vuMemSize / 4u, 1u);
328
329 cy_cryptoVuMemSize = vuMemSize;
330
331 resultVal = CY_CRYPTO_SUCCESS;
332 }
333 }
334 }
335 #if !defined(CY_CRYPTO_CFG_HW_USE_MPN_SPECIFIC)
336 }
337 #endif
338 return resultVal;
339 }
340
341 /*******************************************************************************
342 * Function Name: Cy_Crypto_Core_GetVuMemorySize
343 ****************************************************************************//**
344 *
345 * Get Crypto memory buffer size
346 *
347 * \param base
348 * The pointer to the CRYPTO instance.
349 *
350 * \return
351 * The current MEM_BUFF size in bytes.
352 *
353 *******************************************************************************/
Cy_Crypto_Core_GetVuMemorySize(CRYPTO_Type * base)354 uint32_t Cy_Crypto_Core_GetVuMemorySize(CRYPTO_Type *base)
355 {
356 uint32_t memSize = CY_CRYPTO_MEM_BUFF_SIZE;
357
358 #if !defined(CY_CRYPTO_CFG_HW_USE_MPN_SPECIFIC)
359 if ( (cy_cryptoIP != NULL) && (cy_cryptoVuMemSize != 0uL))
360 {
361 #endif
362 if (CY_CRYPTO_V1)
363 {
364 #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
365 memSize = cy_cryptoVuMemSize;
366 #endif
367 }
368 else
369 {
370 #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
371 uint32_t memFrameMask = _FLD2VAL(CRYPTO_V2_VU_CTL2_MASK, REG_CRYPTO_VU_CTL2(base));
372 /*
373 Specifies the size of the vector operand memory region.
374 Legal values:
375 "0b0000000": 32 KB memory region.
376 "0b1000000": 16 KB memory region.
377 "0b1100000": 8 KB memory region.
378 "0b1110000": 4 KB memory region.
379 "0b1111000": 2 KB memory region.
380 "0b1111100": 1 KB memory region.
381 "0b1111110": 512 B memory region.
382 "0b1111111": 256 B memory region.
383 */
384 switch (memFrameMask)
385 {
386 case 0x0u:
387 memSize = 32768uL;
388 break;
389 case 0x40u:
390 memSize = 16384uL;
391 break;
392 case 0x60u:
393 memSize = 8192uL;
394 break;
395 case 0x70u:
396 memSize = 4096uL;
397 break;
398 case 0x78u:
399 memSize = 2048uL;
400 break;
401 case 0x7Cu:
402 memSize = 1024uL;
403 break;
404 case 0x7Eu:
405 memSize = 512uL;
406 break;
407 case 0x7Fu:
408 memSize = 256uL;
409 break;
410 default:
411 /* Unknown mask */
412 break;
413 }
414 #endif
415 }
416 #if !defined(CY_CRYPTO_CFG_HW_USE_MPN_SPECIFIC)
417 }
418 #endif
419
420 (void)base; /* Suppress a compiler warning about unused variables */
421
422 return memSize;
423 }
424
425 /*******************************************************************************
426 * Function Name: Cy_Crypto_Core_Enable
427 ****************************************************************************//**
428 *
429 * The function to enable the Crypto hardware.
430 *
431 * \param base
432 * The pointer to the CRYPTO instance.
433 *
434 * \return
435 * Crypto status \ref cy_en_crypto_status_t
436 *
437 * \funcusage
438 * \snippet crypto/snippet/main.c snippet_myCryptoCoreStartCryptoUse
439 *
440 *******************************************************************************/
Cy_Crypto_Core_Enable(CRYPTO_Type * base)441 cy_en_crypto_status_t Cy_Crypto_Core_Enable(CRYPTO_Type *base)
442 {
443 #if !defined(CY_CRYPTO_CFG_HW_USE_MPN_SPECIFIC)
444 Cy_Crypto_Core_HwInit();
445 #endif
446
447 /* Disable Crypto HW */
448 REG_CRYPTO_CTL(base) &= ~(_VAL2FLD(CRYPTO_CTL_ENABLED, 1uL));
449
450 if (CY_CRYPTO_V1)
451 {
452 /* Enable Crypto HW */
453 #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
454 REG_CRYPTO_CTL(base) = (uint32_t)(_VAL2FLD(CRYPTO_CTL_PWR_MODE, CY_CRYPTO_PWR_MODE_ENABLED) |
455 _VAL2FLD(CRYPTO_CTL_ENABLED, 1uL));
456 #endif
457 }
458 else
459 {
460 #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
461 REG_CRYPTO_CTL(base) &= ~(_VAL2FLD(CRYPTO_V2_CTL_ENABLED, 1uL) | _VAL2FLD(CRYPTO_V2_CTL_ECC_EN, 1uL));
462
463 REG_CRYPTO_INSTR_FF_CTL(base) = (uint32_t)(_VAL2FLD(CRYPTO_V2_INSTR_FF_CTL_BLOCK, 1u)
464 | _VAL2FLD(CRYPTO_V2_INSTR_FF_CTL_CLEAR, 0u)
465 | _VAL2FLD(CRYPTO_V2_INSTR_FF_CTL_EVENT_LEVEL, 1u));
466
467 REG_CRYPTO_CTL(base) |= _VAL2FLD(CRYPTO_V2_CTL_ENABLED, 1uL);
468
469 REG_CRYPTO_RAM_PWR_CTL(base) = (uint32_t)(CY_CRYPTO_PWR_MODE_ENABLED);
470 #endif
471 }
472
473 /*
474 Specifies if a conditional instruction is executed or not, when its condition
475 code evaluates to false("0"):
476 "0": The instruction is NOT executed. As a result, the instruction may be
477 handled faster than when it is executed.
478 "1": The instruction is executed, but the execution result (including
479 status field information) is not reflected in the IP. The instruction
480 is handled just as fast as when it is executed.
481 */
482 REG_CRYPTO_VU_CTL0(base) = (uint32_t)1u;
483
484 if (0uL == cy_cryptoVuMemSize)
485 {
486 /* Set the memory address and set stack pointer to the Crypto buff size, in words */
487 (void)Cy_Crypto_Core_SetVuMemoryAddress(base, REG_CRYPTO_MEM_BUFF(base), CY_CRYPTO_MEM_BUFF_SIZE);
488 }
489
490 /* Clear whole register file */
491 Cy_Crypto_Core_ClearVuRegisters(base);
492
493 return (CY_CRYPTO_SUCCESS);
494 }
495
496 /*******************************************************************************
497 * Function Name: Cy_Crypto_Core_GetLibInfo
498 ****************************************************************************//**
499 *
500 * Get Crypto service information
501 *
502 * \param libInfo
503 * The pointer to a variable to store gathered crypto library information.
504 *
505 * \return
506 * \ref cy_en_crypto_status_t
507 *
508 *******************************************************************************/
Cy_Crypto_Core_GetLibInfo(cy_en_crypto_lib_info_t * libInfo)509 cy_en_crypto_status_t Cy_Crypto_Core_GetLibInfo(cy_en_crypto_lib_info_t *libInfo)
510 {
511 *libInfo = (cy_en_crypto_lib_info_t)CY_CRYPTO_SERVICE_LIBRARY_LEVEL;
512
513 return (CY_CRYPTO_SUCCESS);
514 }
515
516 /*******************************************************************************
517 * Function Name: Cy_Crypto_Core_Disable
518 ****************************************************************************//**
519 *
520 * Disables the operation of the CRYPTO block.
521 *
522 * \param base
523 * The pointer to the CRYPTO instance.
524 *
525 * \return
526 * \ref cy_en_crypto_status_t
527 *
528 * \funcusage
529 * \snippet crypto/snippet/main.c snippet_myCryptoCoreStopCryptoUse
530 *
531 *******************************************************************************/
Cy_Crypto_Core_Disable(CRYPTO_Type * base)532 cy_en_crypto_status_t Cy_Crypto_Core_Disable(CRYPTO_Type *base)
533 {
534 /* Power-off Crypto RAM */
535 if (CY_CRYPTO_V1)
536 {
537 /* Disable Crypto HW */
538 #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
539 REG_CRYPTO_CTL(base) = (uint32_t)(_VAL2FLD(CRYPTO_CTL_PWR_MODE, CY_CRYPTO_PWR_MODE_OFF) |
540 _VAL2FLD(CRYPTO_CTL_ENABLED, 0uL));
541 #endif
542 }
543 else
544 {
545 #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
546 REG_CRYPTO_CTL(base) = (uint32_t)(_VAL2FLD(CRYPTO_V2_CTL_ENABLED, 0uL));
547 REG_CRYPTO_RAM_PWR_CTL(base) = (uint32_t)(CY_CRYPTO_PWR_MODE_OFF);
548 #endif
549 }
550
551 cy_cryptoVuMemSize = 0uL;
552
553 return (CY_CRYPTO_SUCCESS);
554 }
555
556
557 /*******************************************************************************
558 * Function Name: Cy_Crypto_Core_Cleanup
559 ****************************************************************************//**
560 *
561 * Cleans up the Crypto block.
562 *
563 * \param base
564 * The pointer to the CRYPTO instance.
565 *
566 * \return
567 * \ref cy_en_crypto_status_t
568 *
569 *******************************************************************************/
Cy_Crypto_Core_Cleanup(CRYPTO_Type * base)570 cy_en_crypto_status_t Cy_Crypto_Core_Cleanup(CRYPTO_Type *base)
571 {
572 uint16_t vu_mem_size = 0U;
573 void *vu_mem_address = NULL;
574 /* Clear whole register file */
575 Cy_Crypto_Core_ClearVuRegisters(base);
576
577 /* PRNG */
578 REG_CRYPTO_PR_LFSR_CTL0(base) = 0u;
579 REG_CRYPTO_PR_LFSR_CTL1(base) = 0u;
580 REG_CRYPTO_PR_LFSR_CTL2(base) = 0u;
581 REG_CRYPTO_PR_RESULT(base) = 0u;
582
583 /* TRNG */
584 REG_CRYPTO_TR_CTL0(base) = 0u;
585 REG_CRYPTO_TR_CTL1(base) = 0u;
586 REG_CRYPTO_TR_RESULT(base) = 0u;
587
588 /* CRC */
589 REG_CRYPTO_CRC_POL_CTL(base) = 0u;
590 REG_CRYPTO_CRC_REM_CTL(base) = 0u;
591
592 /* AES */
593 REG_CRYPTO_AES_CTL(base) = 0u;
594
595 if (CY_CRYPTO_V1)
596 {
597 #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
598 REG_CRYPTO_CRC_LFSR_CTL(base) = 0u;
599 REG_CRYPTO_SHA_CTL(base) = 0u;
600 #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
601 }
602 else
603 {
604 #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
605 REG_CRYPTO_TR_CTL2(base) = 0u;
606 REG_CRYPTO_RESULT(base) = 0u;
607
608 Cy_Crypto_Core_V2_FFStop(base, CY_CRYPTO_V2_RB_FF_LOAD0);
609 Cy_Crypto_Core_V2_FFStop(base, CY_CRYPTO_V2_RB_FF_LOAD1);
610 Cy_Crypto_Core_V2_FFStop(base, CY_CRYPTO_V2_RB_FF_STORE);
611 Cy_Crypto_Core_V2_RBClear(base);
612 #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
613 }
614
615 vu_mem_address = Cy_Crypto_Core_GetVuMemoryAddress(base);
616 vu_mem_size = (uint16_t)Cy_Crypto_Core_GetVuMemorySize(base);
617
618 Cy_Crypto_Core_MemSet(base, vu_mem_address, 0u, vu_mem_size);
619
620 return (CY_CRYPTO_SUCCESS);
621 }
622
623
624
625 /*******************************************************************************
626 * Function Name: Cy_Crypto_Core_Shutdown
627 ****************************************************************************//**
628 *
629 * Cleans up the Crypto block, disable it and reset all crypto registers.
630 *
631 * \param base
632 * The pointer to the CRYPTO instance.
633 *
634 * \return
635 * \ref cy_en_crypto_status_t
636 *
637 *******************************************************************************/
Cy_Crypto_Core_Shutdown(CRYPTO_Type * base)638 cy_en_crypto_status_t Cy_Crypto_Core_Shutdown(CRYPTO_Type *base)
639 {
640 /* Clear all sensitive information */
641 (void)Cy_Crypto_Core_Cleanup(base);
642
643 (void)Cy_Crypto_Core_Disable(base);
644
645 /* Restores Crypto configuration registers to their default values */
646 REG_CRYPTO_CTL(base) = 0UL;
647 REG_CRYPTO_INSTR_FF_CTL(base) = 0UL;
648 REG_CRYPTO_INSTR_FF_WR(base) = 0UL;
649
650 REG_CRYPTO_CRC_CTL(base) = 0UL;
651 REG_CRYPTO_CRC_DATA_CTL(base) = 0UL;
652 REG_CRYPTO_CRC_POL_CTL(base) = 0UL;
653 REG_CRYPTO_CRC_REM_CTL(base) = 0UL;
654 #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
655 REG_CRYPTO_CRC_LFSR_CTL(base) = 0UL;
656 #endif
657
658 REG_CRYPTO_VU_CTL0(base) = 0UL;
659 REG_CRYPTO_VU_CTL1(base) = 0UL;
660 #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
661 REG_CRYPTO_VU_CTL2(base) = 0UL;
662 #endif
663
664 REG_CRYPTO_INTR(base) = 0xFFFFFFFFUL; /* All bits are cleared by write to 1 */
665
666 return (CY_CRYPTO_SUCCESS);
667 }
668
669 /*******************************************************************************
670 * Function Name: Cy_Crypto_Core_InvertEndianness
671 ****************************************************************************//**
672 *
673 * This function reverts byte-array memory block, like:<br>
674 * inArr[0] <---> inArr[n]<br>
675 * inArr[1] <---> inArr[n-1]<br>
676 * inArr[2] <---> inArr[n-2]<br>
677 * ........................<br>
678 * inArr[n/2] <---> inArr[n/2-1]<br>
679 *
680 * Odd or even byteSize are acceptable.
681 *
682 * \param inArrPtr
683 * The pointer to the memory whose endianness is to be inverted.
684 *
685 * \param byteSize
686 * The length of the memory array whose endianness is to be inverted (in bytes)
687 *
688 *******************************************************************************/
Cy_Crypto_Core_InvertEndianness(void * inArrPtr,uint32_t byteSize)689 void Cy_Crypto_Core_InvertEndianness(void *inArrPtr, uint32_t byteSize)
690 {
691 int32_t limit;
692 int32_t i;
693 int32_t j = 0;
694 uint8_t temp;
695 uint8_t *tempPtr = (uint8_t*)inArrPtr;
696
697 if (byteSize > 1u)
698 {
699 limit = (int32_t)byteSize / 2;
700 if (0u == (byteSize % 2u))
701 {
702 --limit;
703 }
704
705 j = 0;
706 i = (int32_t)byteSize - 1;
707 while ( i > limit)
708 {
709 temp = tempPtr[j];
710 tempPtr[j] = tempPtr[i];
711 tempPtr[i] = temp;
712
713 --i;
714 ++j;
715 }
716 }
717 }
718
719 /** \} group_crypto_lld_hw_functions */
720
721 CY_MISRA_BLOCK_END('MISRA C-2012 Rule 14.3')
722 CY_MISRA_BLOCK_END('MISRA C-2012 Rule 11.3')
723
724 #if defined(__cplusplus)
725 }
726 #endif
727
728 #endif /* CY_IP_MXCRYPTO */
729
730
731 /* [] END OF FILE */
732