1 /***************************************************************************//**
2 * \file cy_crypto_core_vu.c
3 * \version 2.40
4 *
5 * \brief
6 *  This file provides the source code to the API for the Vector Unit helpers
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_core_vu.h"
31 
32 #if defined(__cplusplus)
33 extern "C" {
34 #endif
35 
36 #if (CPUSS_CRYPTO_VU == 1)
37 
38 #include "cy_crypto_core_hw_v1.h"
39 #include "cy_crypto_core_hw_v2.h"
40 #include "cy_crypto_core_mem.h"
41 #include "cy_syslib.h"
42 
43 CY_MISRA_DEVIATE_BLOCK_START('MISRA C-2012 Rule 11.3', 4, \
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 #if !defined (CY_CRYPTO_SERVICE_LIBRARY_LEVEL)
47     #define CY_CRYPTO_SERVICE_LIBRARY_LEVEL CY_CRYPTO_FULL_LIBRARY
48 #endif
49 
Cy_Crypto_Core_Vu_SetMemValue(CRYPTO_Type * base,uint32_t dstReg,uint8_t const * src,uint32_t size)50 void Cy_Crypto_Core_Vu_SetMemValue(CRYPTO_Type *base, uint32_t dstReg, uint8_t const *src, uint32_t size)
51 {
52     uint32_t reg0_data = 0uL;
53     uint32_t reg1_data = 0uL;
54 
55     Cy_Crypto_Core_Vu_WaitForComplete(base);
56 
57     if (CY_CRYPTO_V1)
58     {
59         CY_CRYPTO_VU_SAVE_REG(base, CY_CRYPTO_VU_HW_REG0, &reg0_data);
60         CY_CRYPTO_VU_SAVE_REG(base, CY_CRYPTO_VU_HW_REG1, &reg1_data);
61     }
62 
63     /* Copy value to Crypto SRAM */
64     uint16_t byteSize = (uint16_t)CY_CRYPTO_BYTE_SIZE_OF_BITS(size);
65     uint32_t destAddr = (uint32_t)Cy_Crypto_Core_Vu_RegMemPointer(base, dstReg);
66 
67     CY_ASSERT_L1(size <= Cy_Crypto_Core_Vu_RegBitSizeRead(base, dstReg));
68     CY_ASSERT_L1( ((destAddr + byteSize) - 1u) < ((uint32_t)Cy_Crypto_Core_GetVuMemoryAddress(base) + Cy_Crypto_Core_GetVuMemorySize(base)));
69 
70     Cy_Crypto_Core_MemSet(base, (void*)destAddr, 0u, (uint16_t)CY_CRYPTO_WORD_SIZE_OF_BITS(size) * 4u);
71     Cy_Crypto_Core_MemCpy(base, (void*)destAddr, (const void*)src, byteSize);
72 
73     if (CY_CRYPTO_V1)
74     {
75         CY_CRYPTO_VU_RESTORE_REG(base, CY_CRYPTO_VU_HW_REG0, reg0_data);
76         CY_CRYPTO_VU_RESTORE_REG(base, CY_CRYPTO_VU_HW_REG1, reg1_data);
77     }
78 }
79 
Cy_Crypto_Core_Vu_GetMemValue(CRYPTO_Type * base,uint8_t * dst,uint32_t srcReg,uint32_t size)80 void Cy_Crypto_Core_Vu_GetMemValue(CRYPTO_Type *base, uint8_t *dst, uint32_t srcReg, uint32_t size)
81 {
82     uint32_t reg0_data = 0uL;
83     uint32_t reg1_data = 0uL;
84 
85     Cy_Crypto_Core_Vu_WaitForComplete(base);
86 
87     if (CY_CRYPTO_V1)
88     {
89         CY_CRYPTO_VU_SAVE_REG(base, CY_CRYPTO_VU_HW_REG0, &reg0_data);
90         CY_CRYPTO_VU_SAVE_REG(base, CY_CRYPTO_VU_HW_REG1, &reg1_data);
91     }
92 
93     /* Copy value from Crypto SRAM */
94     uint16_t byteSize = (uint16_t)CY_CRYPTO_BYTE_SIZE_OF_BITS(size);
95     uint32_t dataAddr = (uint32_t)Cy_Crypto_Core_Vu_RegMemPointer(base, srcReg);
96 
97     CY_ASSERT_L1(size <= Cy_Crypto_Core_Vu_RegBitSizeRead(base, srcReg));
98     CY_ASSERT_L1( ((dataAddr + byteSize) - 1u) < ((uint32_t)Cy_Crypto_Core_GetVuMemoryAddress(base) + Cy_Crypto_Core_GetVuMemorySize(base)));
99 
100     Cy_Crypto_Core_MemCpy(base, (void*)dst, (void*)dataAddr, byteSize);
101 
102     if (CY_CRYPTO_V1)
103     {
104         CY_CRYPTO_VU_RESTORE_REG(base, CY_CRYPTO_VU_HW_REG0, reg0_data);
105         CY_CRYPTO_VU_RESTORE_REG(base, CY_CRYPTO_VU_HW_REG1, reg1_data);
106     }
107 }
108 
Cy_Crypto_Core_Cleanup(CRYPTO_Type * base)109 cy_en_crypto_status_t Cy_Crypto_Core_Cleanup(CRYPTO_Type *base)
110 {
111     uint16_t vu_mem_size = 0U;
112     void *vu_mem_address = NULL;
113     /* Clear whole register file */
114     Cy_Crypto_Core_ClearVuRegisters(base);
115 
116     /* PRNG */
117     REG_CRYPTO_PR_LFSR_CTL0(base) = 0u;
118     REG_CRYPTO_PR_LFSR_CTL1(base) = 0u;
119     REG_CRYPTO_PR_LFSR_CTL2(base) = 0u;
120     REG_CRYPTO_PR_RESULT(base)    = 0u;
121 
122     /* TRNG */
123     REG_CRYPTO_TR_CTL0(base)      = 0u;
124     REG_CRYPTO_TR_CTL1(base)      = 0u;
125     REG_CRYPTO_TR_RESULT(base)    = 0u;
126 
127     /* CRC */
128     REG_CRYPTO_CRC_POL_CTL(base)  = 0u;
129     REG_CRYPTO_CRC_REM_CTL(base)  = 0u;
130 
131     /* AES */
132     REG_CRYPTO_AES_CTL(base)      = 0u;
133 
134     if (CY_CRYPTO_V1)
135     {
136         REG_CRYPTO_CRC_LFSR_CTL(base) = 0u;
137         REG_CRYPTO_SHA_CTL(base)  = 0u;
138     }
139     else
140     {
141         REG_CRYPTO_TR_CTL2(base)  = 0u;
142         REG_CRYPTO_RESULT(base)   = 0u;
143 
144         Cy_Crypto_Core_V2_FFStop(base, CY_CRYPTO_V2_RB_FF_LOAD0);
145         Cy_Crypto_Core_V2_FFStop(base, CY_CRYPTO_V2_RB_FF_LOAD1);
146         Cy_Crypto_Core_V2_FFStop(base, CY_CRYPTO_V2_RB_FF_STORE);
147         Cy_Crypto_Core_V2_RBClear(base);
148     }
149 
150     vu_mem_address = Cy_Crypto_Core_GetVuMemoryAddress(base);
151     vu_mem_size = (uint16_t)Cy_Crypto_Core_GetVuMemorySize(base);
152 
153     Cy_Crypto_Core_MemSet(base, vu_mem_address, 0u, vu_mem_size);
154 
155     return (CY_CRYPTO_SUCCESS);
156 }
157 
Cy_Crypto_Core_Vu_IsRegZero(CRYPTO_Type * base,uint32_t srcReg)158 bool Cy_Crypto_Core_Vu_IsRegZero(CRYPTO_Type *base, uint32_t srcReg)
159 {
160     bool tmpResult;
161     uint32_t status;
162 
163     CY_CRYPTO_VU_TST(base, srcReg);
164     status = Cy_Crypto_Core_Vu_StatusRead(base);
165 
166     if (0u != (status & CY_CRYPTO_VU_STATUS_ZERO_BIT))
167     {
168         tmpResult = true;
169     }
170     else
171     {
172         tmpResult = false;
173     }
174 
175     return tmpResult;
176 }
177 
Cy_Crypto_Core_Vu_IsRegEqual(CRYPTO_Type * base,uint32_t srcReg0,uint32_t srcReg1)178 bool Cy_Crypto_Core_Vu_IsRegEqual(CRYPTO_Type *base, uint32_t srcReg0, uint32_t srcReg1)
179 {
180     bool tmpResult;
181     uint32_t status;
182 
183     CY_CRYPTO_VU_CMP_SUB (base, srcReg1, srcReg0);                /* C = (a >= b) */
184     status = Cy_Crypto_Core_Vu_StatusRead(base);
185 
186     if (0u != (status &  CY_CRYPTO_VU_STATUS_ZERO_BIT))
187     {
188         tmpResult = true;
189     }
190     else
191     {
192         tmpResult = false;
193     }
194 
195     return tmpResult;
196 }
197 
Cy_Crypto_Core_Vu_IsRegLess(CRYPTO_Type * base,uint32_t srcReg0,uint32_t srcReg1)198 bool Cy_Crypto_Core_Vu_IsRegLess(CRYPTO_Type *base, uint32_t srcReg0, uint32_t srcReg1)
199 {
200     bool tmpResult;
201     uint32_t status;
202 
203     CY_CRYPTO_VU_CMP_SUB (base, srcReg1, srcReg0);                /* C = (a >= b) */
204     status = Cy_Crypto_Core_Vu_StatusRead(base);
205 
206     if (0u != (status &  CY_CRYPTO_VU_STATUS_CARRY_BIT))
207     {
208         tmpResult = true;
209     }
210     else
211     {
212         tmpResult = false;
213     }
214 
215     return tmpResult;
216 }
217 
Cy_Crypto_Core_VU_RegInvertEndianness(CRYPTO_Type * base,uint32_t srcReg)218 void Cy_Crypto_Core_VU_RegInvertEndianness(CRYPTO_Type *base, uint32_t srcReg)
219 {
220     uint32_t  byteSize = CY_CRYPTO_BYTE_SIZE_OF_BITS(Cy_Crypto_Core_Vu_RegBitSizeRead(base, srcReg));
221     uint32_t *dataAddr = Cy_Crypto_Core_Vu_RegMemPointer(base, srcReg);
222     Cy_Crypto_Core_InvertEndianness(dataAddr, byteSize);
223 }
224 CY_MISRA_BLOCK_END('MISRA C-2012 Rule 11.3');
225 
226 #endif /* #if (CPUSS_CRYPTO_VU == 1) */
227 
228 #if defined(__cplusplus)
229 }
230 #endif
231 
232 #endif /* CY_IP_MXCRYPTO */
233 
234 
235 /* [] END OF FILE */
236