1 /***************************************************************************//**
2 * \file cy_crypto_core_vu.c
3 * \version 2.120
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
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_vu.h"
33 
34 #if defined(__cplusplus)
35 extern "C" {
36 #endif
37 
38 #if (CPUSS_CRYPTO_VU == 1)
39 
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 CY_MISRA_DEVIATE_BLOCK_START('MISRA C-2012 Rule 14.3', 4, \
46 'Since value of CY_CRYPTO_V1 is decided by PDL device agnostic / hardware specific model, controlling expression will not have an invariant value.')
47 
48 #if !defined (CY_CRYPTO_SERVICE_LIBRARY_LEVEL)
49     #define CY_CRYPTO_SERVICE_LIBRARY_LEVEL CY_CRYPTO_FULL_LIBRARY
50 #endif
51 
Cy_Crypto_Core_Vu_SetMemValue(CRYPTO_Type * base,uint32_t dstReg,uint8_t const * src,uint32_t size)52 void Cy_Crypto_Core_Vu_SetMemValue(CRYPTO_Type *base, uint32_t dstReg, uint8_t const *src, uint32_t size)
53 {
54     uint32_t reg0_data = 0uL;
55     uint32_t reg1_data = 0uL;
56     uint8_t *srcRemap;
57 
58     srcRemap = (uint8_t *)CY_REMAP_ADDRESS_FOR_CRYPTO(src);
59 
60     Cy_Crypto_Core_Vu_WaitForComplete(base);
61 
62     if (CY_CRYPTO_V1)
63     {
64         CY_CRYPTO_VU_SAVE_REG(base, CY_CRYPTO_VU_HW_REG0, &reg0_data);
65         CY_CRYPTO_VU_SAVE_REG(base, CY_CRYPTO_VU_HW_REG1, &reg1_data);
66     }
67 
68     /* Copy value to Crypto SRAM */
69     uint16_t byteSize = (uint16_t)CY_CRYPTO_BYTE_SIZE_OF_BITS(size);
70     uint32_t destAddr = (uint32_t)Cy_Crypto_Core_Vu_RegMemPointer(base, dstReg);
71 
72     CY_ASSERT_L1(size <= Cy_Crypto_Core_Vu_RegBitSizeRead(base, dstReg));
73     CY_ASSERT_L1( ((destAddr + byteSize) - 1u) < ((uint32_t)Cy_Crypto_Core_GetVuMemoryAddress(base) + Cy_Crypto_Core_GetVuMemorySize(base)));
74 
75     Cy_Crypto_Core_MemSet(base, (void*)destAddr, 0u, (uint16_t)CY_CRYPTO_WORD_SIZE_OF_BITS(size) * 4u);
76     Cy_Crypto_Core_MemCpy(base, (void*)destAddr, (const void*)srcRemap, byteSize);
77 
78     if (CY_CRYPTO_V1)
79     {
80         CY_CRYPTO_VU_RESTORE_REG(base, CY_CRYPTO_VU_HW_REG0, reg0_data);
81         CY_CRYPTO_VU_RESTORE_REG(base, CY_CRYPTO_VU_HW_REG1, reg1_data);
82     }
83 }
84 
Cy_Crypto_Core_Vu_GetMemValue(CRYPTO_Type * base,uint8_t * dst,uint32_t srcReg,uint32_t size)85 void Cy_Crypto_Core_Vu_GetMemValue(CRYPTO_Type *base, uint8_t *dst, uint32_t srcReg, uint32_t size)
86 {
87     uint32_t reg0_data = 0uL;
88     uint32_t reg1_data = 0uL;
89     uint8_t *dstRemap;
90     dstRemap = (uint8_t *)CY_REMAP_ADDRESS_FOR_CRYPTO(dst);
91 
92     Cy_Crypto_Core_Vu_WaitForComplete(base);
93 
94     if (CY_CRYPTO_V1)
95     {
96         CY_CRYPTO_VU_SAVE_REG(base, CY_CRYPTO_VU_HW_REG0, &reg0_data);
97         CY_CRYPTO_VU_SAVE_REG(base, CY_CRYPTO_VU_HW_REG1, &reg1_data);
98     }
99 
100     /* Copy value from Crypto SRAM */
101     uint16_t byteSize = (uint16_t)CY_CRYPTO_BYTE_SIZE_OF_BITS(size);
102     uint32_t dataAddr = (uint32_t)Cy_Crypto_Core_Vu_RegMemPointer(base, srcReg);
103 
104     CY_ASSERT_L1(size <= Cy_Crypto_Core_Vu_RegBitSizeRead(base, srcReg));
105     CY_ASSERT_L1( ((dataAddr + byteSize) - 1u) < ((uint32_t)Cy_Crypto_Core_GetVuMemoryAddress(base) + Cy_Crypto_Core_GetVuMemorySize(base)));
106 
107     Cy_Crypto_Core_MemCpy(base, (void*)dstRemap, (void*)dataAddr, byteSize);
108 
109     if (CY_CRYPTO_V1)
110     {
111         CY_CRYPTO_VU_RESTORE_REG(base, CY_CRYPTO_VU_HW_REG0, reg0_data);
112         CY_CRYPTO_VU_RESTORE_REG(base, CY_CRYPTO_VU_HW_REG1, reg1_data);
113     }
114 }
115 
Cy_Crypto_Core_Vu_IsRegZero(CRYPTO_Type * base,uint32_t srcReg)116 bool Cy_Crypto_Core_Vu_IsRegZero(CRYPTO_Type *base, uint32_t srcReg)
117 {
118     bool tmpResult;
119     uint32_t status;
120 
121     CY_CRYPTO_VU_TST(base, srcReg);
122     status = Cy_Crypto_Core_Vu_StatusRead(base);
123 
124     if (0u != (status & CY_CRYPTO_VU_STATUS_ZERO_BIT))
125     {
126         tmpResult = true;
127     }
128     else
129     {
130         tmpResult = false;
131     }
132 
133     return tmpResult;
134 }
135 
Cy_Crypto_Core_Vu_IsRegEqual(CRYPTO_Type * base,uint32_t srcReg0,uint32_t srcReg1)136 bool Cy_Crypto_Core_Vu_IsRegEqual(CRYPTO_Type *base, uint32_t srcReg0, uint32_t srcReg1)
137 {
138     bool tmpResult;
139     uint32_t status;
140 
141     CY_CRYPTO_VU_CMP_SUB (base, srcReg1, srcReg0);                /* C = (a >= b) */
142     status = Cy_Crypto_Core_Vu_StatusRead(base);
143 
144     if (0u != (status &  CY_CRYPTO_VU_STATUS_ZERO_BIT))
145     {
146         tmpResult = true;
147     }
148     else
149     {
150         tmpResult = false;
151     }
152 
153     return tmpResult;
154 }
155 
Cy_Crypto_Core_Vu_IsRegLess(CRYPTO_Type * base,uint32_t srcReg0,uint32_t srcReg1)156 bool Cy_Crypto_Core_Vu_IsRegLess(CRYPTO_Type *base, uint32_t srcReg0, uint32_t srcReg1)
157 {
158     bool tmpResult;
159     uint32_t status;
160 
161     CY_CRYPTO_VU_CMP_SUB (base, srcReg1, srcReg0);                /* C = (a >= b) */
162     status = Cy_Crypto_Core_Vu_StatusRead(base);
163 
164     if (0u != (status &  CY_CRYPTO_VU_STATUS_CARRY_BIT))
165     {
166         tmpResult = true;
167     }
168     else
169     {
170         tmpResult = false;
171     }
172 
173     return tmpResult;
174 }
175 
Cy_Crypto_Core_VU_RegInvertEndianness(CRYPTO_Type * base,uint32_t srcReg)176 void Cy_Crypto_Core_VU_RegInvertEndianness(CRYPTO_Type *base, uint32_t srcReg)
177 {
178     uint32_t  byteSize = CY_CRYPTO_BYTE_SIZE_OF_BITS(Cy_Crypto_Core_Vu_RegBitSizeRead(base, srcReg));
179     uint32_t *dataAddr = Cy_Crypto_Core_Vu_RegMemPointer(base, srcReg);
180     Cy_Crypto_Core_InvertEndianness(dataAddr, byteSize);
181 }
182 
183 CY_MISRA_BLOCK_END('MISRA C-2012 Rule 14.3')
184 CY_MISRA_BLOCK_END('MISRA C-2012 Rule 11.3')
185 
186 #endif /* #if (CPUSS_CRYPTO_VU == 1) */
187 
188 #if defined(__cplusplus)
189 }
190 #endif
191 
192 #endif /* CY_IP_MXCRYPTO */
193 
194 
195 /* [] END OF FILE */
196