1 /***************************************************************************//**
2 * \file cy_crypto_core_mem_v2.c
3 * \version 2.120
4 *
5 * \brief
6 * This file provides the source code to the API for the PRNG
7 * in the Crypto block 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_core_mem_v2.h"
33
34 #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
35
36 #if defined(__cplusplus)
37 extern "C" {
38 #endif
39
40 #include "cy_crypto_core_hw_v2.h"
41 #include "cy_syslib.h"
42
43 CY_MISRA_DEVIATE_BLOCK_START('MISRA C-2012 Rule 11.3', 2, \
44 '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.')
45
46 /*******************************************************************************
47 * Function Name: Cy_Crypto_Core_V2_MemCpy
48 ****************************************************************************//**
49 *
50 * Function MemCpy uses Crypto HW.
51 * Memory structures should not overlap!
52 *
53 * For CAT1C & CAT1D(CM55) devices when D-Cache is enabled parameters dst and src must align and end in 32 byte boundary.
54 *
55 * \param base
56 * The pointer to the CRYPTO instance.
57 *
58 * \param src
59 * The pointer to the source of MemCpy.
60 *
61 * \param dst
62 * The pointer to the destination of MemCpy.
63 *
64 * \param size
65 * The size in bytes of the copy operation.
66 *
67 *******************************************************************************/
Cy_Crypto_Core_V2_MemCpy(CRYPTO_Type * base,void * dst,void const * src,uint16_t size)68 void Cy_Crypto_Core_V2_MemCpy(CRYPTO_Type *base, void* dst, void const *src, uint16_t size)
69 {
70 void *dstRemap;
71 void *srcRemap;
72
73 if (size != 0U)
74 {
75
76 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
77 /* Flush the cache */
78 if(((uint32_t)src & 0x1FU) == 0u)
79 {
80
81 SCB_CleanDCache_by_Addr((volatile void *)src,(int32_t)size);
82
83 }
84
85 if(((uint32_t)dst & 0x1FU) == 0u)
86 {
87
88 SCB_InvalidateDCache_by_Addr((volatile void *)dst,(int32_t)size);
89
90 }
91
92 #endif
93
94 dstRemap = CY_REMAP_ADDRESS_FOR_CRYPTO(dst);
95 srcRemap = (void *)CY_REMAP_ADDRESS_FOR_CRYPTO(src);
96
97 Cy_Crypto_Core_V2_FFContinue(base, CY_CRYPTO_V2_RB_FF_LOAD0, (const uint8_t*)srcRemap, (uint32_t)size);
98 Cy_Crypto_Core_V2_FFStart (base, CY_CRYPTO_V2_RB_FF_STORE, (const uint8_t*)dstRemap, (uint32_t)size);
99
100 while (size >= CY_CRYPTO_V2_DATA_FIFODEPTH)
101 {
102 Cy_Crypto_Core_V2_BlockMov(base, CY_CRYPTO_V2_RB_FF_STORE, CY_CRYPTO_V2_RB_FF_LOAD0, CY_CRYPTO_V2_DATA_FIFODEPTH);
103 size -= CY_CRYPTO_V2_DATA_FIFODEPTH;
104 }
105
106 if (size != 0u)
107 {
108 Cy_Crypto_Core_V2_BlockMov(base, CY_CRYPTO_V2_RB_FF_STORE, CY_CRYPTO_V2_RB_FF_LOAD0, (uint32_t)size);
109 }
110
111 Cy_Crypto_Core_V2_Sync(base);
112
113 }
114 }
115
116 /*******************************************************************************
117 * Function Name: Cy_Crypto_Core_V2_MemSet
118 ****************************************************************************//**
119 *
120 * Function MemSet uses Crypto HW.
121 *
122 * For CAT1C & CAT1D(CM55) devices when D-Cache is enabled parameter dst must align and end in 32 byte boundary.
123 *
124 *
125 * \param base
126 * The pointer to the CRYPTO instance.
127 *
128 * \param dst
129 * The pointer to the destination of MemSet.
130
131 * \param data
132 * The value to be set.
133
134 * \param size
135 * The size in bytes of the set operation.
136 *
137 *******************************************************************************/
Cy_Crypto_Core_V2_MemSet(CRYPTO_Type * base,void * dst,uint8_t data,uint16_t size)138 void Cy_Crypto_Core_V2_MemSet(CRYPTO_Type *base, void* dst, uint8_t data, uint16_t size)
139 {
140 void *dstRemap;
141 if (size != 0U)
142 {
143
144 dstRemap = CY_REMAP_ADDRESS_FOR_CRYPTO(dst);
145
146 #if (((CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)) || CY_CPU_CORTEX_M55)
147 if(((uint32_t)dst & 0x1FU) == 0u)
148 {
149
150 SCB_InvalidateDCache_by_Addr((volatile void *)dst,(int32_t)size);
151
152 }
153
154 #endif
155
156 Cy_Crypto_Core_V2_FFStart(base, CY_CRYPTO_V2_RB_FF_STORE, dstRemap, (uint32_t)size);
157
158 while (size >= CY_CRYPTO_V2_DATA_FIFODEPTH)
159 {
160 Cy_Crypto_Core_V2_BlockSet(base, CY_CRYPTO_V2_RB_FF_STORE, data, CY_CRYPTO_V2_DATA_FIFODEPTH);
161 size -= CY_CRYPTO_V2_DATA_FIFODEPTH;
162 }
163
164 if (size != 0u)
165 {
166 Cy_Crypto_Core_V2_BlockSet(base, CY_CRYPTO_V2_RB_FF_STORE, data, (uint32_t)size);
167 }
168
169 Cy_Crypto_Core_V2_Sync(base);
170
171
172
173 }
174 }
175
176 /*******************************************************************************
177 * Function Name: Cy_Crypto_Core_V2_MemCmp
178 ****************************************************************************//**
179 *
180 * Function MemCmp uses Crypto HW.
181 *
182 *
183 * For CAT1C & CAT1D(CM55) devices when D-Cache is enabled parameters src0 and src1 must align and end in 32 byte boundary.
184 *
185 * \param base
186 * The pointer to the CRYPTO instance.
187 *
188 * \param src0
189 * The pointer to the first source of MemCmp.
190
191 * \param src1
192 * The pointer to the second source of MemCmp.
193
194 * \param size
195 * the size in bytes of the compare operation.
196 *
197 * \return
198 * 0 - if Source 1 = Source 2, 1 - if not.
199 *
200 *******************************************************************************/
Cy_Crypto_Core_V2_MemCmp(CRYPTO_Type * base,void const * src0,void const * src1,uint16_t size)201 uint32_t Cy_Crypto_Core_V2_MemCmp(CRYPTO_Type *base, void const *src0, void const *src1, uint16_t size)
202 {
203 uint32_t memResult = 1U;
204 void *src0Remap;
205 void *src1Remap;
206
207 if (size != 0U)
208 {
209
210 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
211 /* Flush the cache */
212 if(((uint32_t)src0 & 0x1FU) == 0u)
213 {
214
215 SCB_CleanDCache_by_Addr((volatile void *)src0,(int32_t)size);
216
217 }
218
219 if(((uint32_t)src1 & 0x1FU) == 0u)
220 {
221
222 SCB_CleanDCache_by_Addr((volatile void *)src1,(int32_t)size);
223
224 }
225
226 #endif
227
228 src0Remap = (void *)CY_REMAP_ADDRESS_FOR_CRYPTO(src0);
229 src1Remap = (void *)CY_REMAP_ADDRESS_FOR_CRYPTO(src1);
230
231 REG_CRYPTO_RESULT(base) = 0UL;
232
233 Cy_Crypto_Core_V2_FFContinue(base, CY_CRYPTO_V2_RB_FF_LOAD0, (const uint8_t*)src0Remap, (uint32_t)size);
234 Cy_Crypto_Core_V2_FFContinue(base, CY_CRYPTO_V2_RB_FF_LOAD1, (const uint8_t*)src1Remap, (uint32_t)size);
235
236 while (size >= CY_CRYPTO_V2_DATA_FIFODEPTH)
237 {
238 Cy_Crypto_Core_V2_BlockCmp(base, CY_CRYPTO_V2_RB_FF_LOAD0, CY_CRYPTO_V2_RB_FF_LOAD1, CY_CRYPTO_V2_DATA_FIFODEPTH);
239 size -= CY_CRYPTO_V2_DATA_FIFODEPTH;
240 }
241
242 if (size != 0u)
243 {
244 Cy_Crypto_Core_V2_BlockCmp(base, CY_CRYPTO_V2_RB_FF_LOAD0, CY_CRYPTO_V2_RB_FF_LOAD1, (uint32_t)size);
245 }
246
247 Cy_Crypto_Core_V2_Sync(base);
248
249 memResult = (uint32_t)(REG_CRYPTO_RESULT(base));
250
251
252 }
253
254
255 return memResult;
256 }
257
258 /*******************************************************************************
259 * Function Name: Cy_Crypto_Core_V2_MemXor
260 ****************************************************************************//**
261 *
262 * Function MemXor uses Crypto HW.
263 * Memory structures should not overlap!
264 *
265 * For CAT1C & CAT1D(CM55) devices when D-Cache is enabled parameters dst, src0 and src1 must align and end in 32 byte boundary.
266 *
267 *
268 * \param base
269 * The pointer to the CRYPTO instance.
270 *
271 * \param src0
272 * The pointer to the first source of MemXor.
273
274 * \param src1
275 * The pointer to the second source of MemXor.
276
277 * \param dst
278 * The pointer to the destination of MemXor.
279 *
280 * \param size
281 * The size in bytes of the compare operation.
282 *
283 *******************************************************************************/
Cy_Crypto_Core_V2_MemXor(CRYPTO_Type * base,void * dst,void const * src0,void const * src1,uint16_t size)284 void Cy_Crypto_Core_V2_MemXor(CRYPTO_Type *base,
285 void* dst, void const *src0, void const *src1, uint16_t size)
286 {
287 void *src0Remap;
288 void *src1Remap;
289 void *dstRemap;
290
291 if (size != 0U)
292 {
293
294 #if (CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)
295 /* Flush the cache */
296 if(((uint32_t)src0 & 0x1FU) == 0u)
297 {
298 SCB_CleanDCache_by_Addr((volatile void *)src0,(int32_t)size);
299 }
300
301 if(((uint32_t)src1 & 0x1FU) == 0u)
302 {
303 SCB_CleanDCache_by_Addr((volatile void *)src1,(int32_t)size);
304 }
305
306 if(((uint32_t)dst & 0x1FU) == 0u)
307 {
308 SCB_InvalidateDCache_by_Addr((volatile void *)dst,(int32_t)size);
309 }
310
311
312 #endif
313
314 dstRemap = CY_REMAP_ADDRESS_FOR_CRYPTO(dst);
315 src0Remap = (void *)CY_REMAP_ADDRESS_FOR_CRYPTO(src0);
316 src1Remap = (void *)CY_REMAP_ADDRESS_FOR_CRYPTO(src1);
317
318 Cy_Crypto_Core_V2_FFContinue(base, CY_CRYPTO_V2_RB_FF_LOAD0, (const uint8_t*)src0Remap, (uint32_t)size);
319 Cy_Crypto_Core_V2_FFContinue(base, CY_CRYPTO_V2_RB_FF_LOAD1, (const uint8_t*)src1Remap, (uint32_t)size);
320 Cy_Crypto_Core_V2_FFStart (base, CY_CRYPTO_V2_RB_FF_STORE, dstRemap, (uint32_t)size);
321
322 while (size >= CY_CRYPTO_V2_DATA_FIFODEPTH)
323 {
324 Cy_Crypto_Core_V2_BlockXor(base, CY_CRYPTO_V2_RB_FF_STORE, CY_CRYPTO_V2_RB_FF_LOAD0,
325 CY_CRYPTO_V2_RB_FF_LOAD1, CY_CRYPTO_V2_DATA_FIFODEPTH);
326
327 size -= CY_CRYPTO_V2_DATA_FIFODEPTH;
328 }
329
330 if (size != 0u)
331 {
332 Cy_Crypto_Core_V2_BlockXor(base, CY_CRYPTO_V2_RB_FF_STORE, CY_CRYPTO_V2_RB_FF_LOAD0,
333 CY_CRYPTO_V2_RB_FF_LOAD1, (uint32_t)size);
334 }
335
336 Cy_Crypto_Core_V2_Sync(base);
337
338 }
339 }
340 CY_MISRA_BLOCK_END('MISRA C-2012 Rule 11.3')
341
342 #if defined(__cplusplus)
343 }
344 #endif
345
346 #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
347
348 #endif /* defined(CY_IP_MXCRYPTO) */
349
350 /* [] END OF FILE */
351