1 /***************************************************************************//**
2 * \file cy_crypto_core_mem_v1.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_v1.h"
33 
34 #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
35 
36 #if defined(__cplusplus)
37 extern "C" {
38 #endif
39 
40 #if (CPUSS_CRYPTO_STR == 1)
41 
42 #include "cy_crypto_core_hw_v1.h"
43 #include "cy_syslib.h"
44 
45 
46 /*******************************************************************************
47 * Function Name: Cy_Crypto_Core_V1_MemCpy
48 ****************************************************************************//**
49 *
50 * Function MemCpy uses Crypto HW.
51 * Memory structures should not overlap!
52 * There is no alignment restriction.
53 *
54 * \param base
55 * The pointer to the CRYPTO instance.
56 *
57 * \param src
58 * The pointer to the source of MemCpy.
59 *
60 * \param dst
61 * The pointer to the destination of MemCpy.
62 *
63 * \param size
64 * The size in bytes of the copy operation.
65 *
66 *******************************************************************************/
Cy_Crypto_Core_V1_MemCpy(CRYPTO_Type * base,void * dst,void const * src,uint16_t size)67 void Cy_Crypto_Core_V1_MemCpy(CRYPTO_Type *base, void* dst, void const *src, uint16_t size)
68 {
69     if (size != 0U)
70     {
71         /* Prepare data in the register file for next instruction */
72         Cy_Crypto_SetReg3Instr(base,
73                                (uint32_t)src,
74                                (uint32_t)dst,
75                                (uint32_t)size);
76 
77         /* Issue the STR_MEMCPY instruction */
78         Cy_Crypto_Run3ParamInstr(base,
79                                  CY_CRYPTO_V1_STR_MEMCPY_OPC,
80                                  CY_CRYPTO_RSRC0_SHIFT,
81                                  CY_CRYPTO_RSRC4_SHIFT,
82                                  CY_CRYPTO_RSRC8_SHIFT);
83 
84         /* Wait until the STR instruction is complete */
85         while (0uL != _FLD2VAL(CRYPTO_STATUS_STR_BUSY, REG_CRYPTO_STATUS(base)))
86         {
87         }
88     }
89 }
90 
91 /*******************************************************************************
92 * Function Name: Cy_Crypto_Core_V1_MemSet
93 ****************************************************************************//**
94 *
95 * Function MemSet uses Crypto HW.
96 * There is no alignment restriction.
97 *
98 * \param base
99 * The pointer to the CRYPTO instance.
100 *
101 * \param dst
102 * The pointer to the destination of MemSet.
103 
104 * \param data
105 * The value to be set.
106 
107 * \param size
108 * The size in bytes of the set operation.
109 *
110 *******************************************************************************/
Cy_Crypto_Core_V1_MemSet(CRYPTO_Type * base,void * dst,uint8_t data,uint16_t size)111 void Cy_Crypto_Core_V1_MemSet(CRYPTO_Type *base, void* dst, uint8_t data, uint16_t size)
112 {
113     if (size != 0U)
114     {
115         Cy_Crypto_SetReg3Instr(base,
116                                (uint32_t)dst,
117                                (uint32_t)size,
118                                (uint32_t)data);
119 
120         /* Issue the STR_MEMSET instruction */
121         Cy_Crypto_Run3ParamInstr(base,
122                                  CY_CRYPTO_V1_STR_MEMSET_OPC,
123                                  CY_CRYPTO_RSRC0_SHIFT,
124                                  CY_CRYPTO_RSRC8_SHIFT,
125                                  CY_CRYPTO_RSRC12_SHIFT);
126 
127         /* Wait until the STR instruction is complete */
128         while (0uL != _FLD2VAL(CRYPTO_STATUS_STR_BUSY, REG_CRYPTO_STATUS(base)))
129         {
130         }
131     }
132 }
133 
134 /*******************************************************************************
135 * Function Name: Cy_Crypto_Core_V1_MemCmp
136 ****************************************************************************//**
137 *
138 * Function MemCmp uses Crypto HW.
139 * There is no alignment restriction.
140 *
141 * \param base
142 * The pointer to the CRYPTO instance.
143 *
144 * \param src0
145 * The pointer to the first source of MemCmp.
146 
147 * \param src1
148 * The pointer to the second source of MemCmp.
149 
150 * \param size
151 * the size in bytes of the compare operation.
152 *
153 * \return
154 * 0 - if Source 1 = Source 2, 1 - if not.
155 *
156 *******************************************************************************/
Cy_Crypto_Core_V1_MemCmp(CRYPTO_Type * base,void const * src0,void const * src1,uint16_t size)157 uint32_t Cy_Crypto_Core_V1_MemCmp(CRYPTO_Type *base, void const *src0, void const *src1, uint16_t size)
158 {
159     uint32_t memResult = 1U;
160 
161     if (size != 0U)
162     {
163         Cy_Crypto_SetReg3Instr(base,
164                                (uint32_t)src0,
165                                (uint32_t)src1,
166                                (uint32_t)size);
167 
168         /* Issue the STR_MEMCMP instruction */
169         Cy_Crypto_Run3ParamInstr(base,
170                                  CY_CRYPTO_V1_STR_MEMCMP_OPC,
171                                  CY_CRYPTO_RSRC0_SHIFT,
172                                  CY_CRYPTO_RSRC4_SHIFT,
173                                  CY_CRYPTO_RSRC8_SHIFT);
174 
175         /* Wait until the STR instruction is complete */
176         while (0uL != _FLD2VAL(CRYPTO_STATUS_STR_BUSY, REG_CRYPTO_STATUS(base)))
177         {
178         }
179 
180         CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 11.3','This piece of code is written for CRYPTO_V1_Type and will not execute for CRYPTO_V2_Type');
181         memResult = (uint32_t)(REG_CRYPTO_STR_RESULT(base));
182     }
183 
184     return memResult;
185 }
186 
187 /*******************************************************************************
188 * Function Name: Cy_Crypto_Core_V1_MemXor
189 ****************************************************************************//**
190 *
191 * Function MemXor uses Crypto HW.
192 * Memory structures should not overlap!
193 * There is no alignment restriction.
194 *
195 * \param base
196 * The pointer to the CRYPTO instance.
197 *
198 * \param dst
199 * The pointer to the destination of MemXor.
200 *
201 * \param src0
202 * The pointer to the first source of MemXor.
203 
204 * \param src1
205 * The pointer to the second source of MemXor.
206 *
207 * \param size
208 * The size in bytes of the compare operation.
209 *
210 *******************************************************************************/
Cy_Crypto_Core_V1_MemXor(CRYPTO_Type * base,void * dst,void const * src0,void const * src1,uint16_t size)211 void Cy_Crypto_Core_V1_MemXor(CRYPTO_Type *base,
212                               void* dst, void const *src0, void const *src1, uint16_t size)
213 {
214     if (size != 0U)
215     {
216         Cy_Crypto_SetReg4Instr(base,
217                                (uint32_t)src0,
218                                (uint32_t)src1,
219                                (uint32_t)size,
220                                (uint32_t)dst);
221 
222         /* Issue the STR_MEMXOR instruction */
223         Cy_Crypto_Run4ParamInstr(base,
224                                  CY_CRYPTO_V1_STR_MEMXOR_OPC,
225                                  CY_CRYPTO_RSRC0_SHIFT,
226                                  CY_CRYPTO_RSRC4_SHIFT,
227                                  CY_CRYPTO_RSRC8_SHIFT,
228                                  CY_CRYPTO_RSRC12_SHIFT);
229 
230         /* Wait until the STR instruction is complete */
231         while (0uL != _FLD2VAL(CRYPTO_STATUS_STR_BUSY, REG_CRYPTO_STATUS(base)))
232         {
233         }
234     }
235 }
236 
237 #endif /* #if (CPUSS_CRYPTO_STR == 1) */
238 
239 #if defined(__cplusplus)
240 }
241 #endif
242 
243 #endif /*  defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
244 
245 #endif /* defined(CY_IP_MXCRYPTO) */
246 
247 
248 /* [] END OF FILE */
249