1 /***************************************************************************//**
2 * \file cy_crypto_core_sha.h
3 * \version 2.120
4 *
5 * \brief
6 *  This file provides constants and function prototypes
7 *  for the API for the SHA method 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 #if !defined (CY_CRYPTO_CORE_SHA_H)
29 #define CY_CRYPTO_CORE_SHA_H
30 
31 #include "cy_device.h"
32 
33 #if defined (CY_IP_MXCRYPTO)
34 
35 #include "cy_crypto_common.h"
36 
37 #if defined(__cplusplus)
38 extern "C" {
39 #endif
40 
41 #if (CPUSS_CRYPTO_SHA == 1) && defined(CY_CRYPTO_CFG_SHA_C)
42 
43 #include "cy_crypto_core_sha_v1.h"
44 #include "cy_crypto_core_sha_v2.h"
45 
46 typedef cy_en_crypto_status_t (*cy_crypto_sha_func_t)(CRYPTO_Type *base,
47                                          uint8_t const *message,
48                                          uint32_t  messageSize,
49                                          uint8_t *digest,
50                                          cy_en_crypto_sha_mode_t mode);
51 
52 /**
53 * \addtogroup group_crypto_lld_sha_functions
54 * \{
55 */
56 
57 /*******************************************************************************
58 * Function Name: Cy_Crypto_Core_Sha
59 ****************************************************************************//**
60 *
61 * Performs the SHA Hash function.
62 *
63 * \param base
64 * The pointer to the CRYPTO instance.
65 *
66 * \param mode
67 * \ref cy_en_crypto_sha_mode_t
68 *
69 * \param message
70 * The pointer to the message whose hash value is being computed.
71 *
72 * \param messageSize
73 * The size of the message.
74 *
75 * \param digest
76 * The pointer to the hash digest.
77 *
78 * \return
79 * \ref cy_en_crypto_status_t
80 *
81 * \funcusage
82 * \snippet crypto/snippet/main.c snippet_myCryptoCoreSha256Use
83 *
84 *******************************************************************************/
Cy_Crypto_Core_Sha(CRYPTO_Type * base,uint8_t const * message,uint32_t messageSize,uint8_t * digest,cy_en_crypto_sha_mode_t mode)85 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Sha(CRYPTO_Type *base,
86                                 uint8_t const *message,
87                                 uint32_t messageSize,
88                                 uint8_t *digest,
89                                 cy_en_crypto_sha_mode_t mode)
90 {
91     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
92 
93     if ((bool)CY_CRYPTO_V1)
94     {
95         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
96         tmpResult = Cy_Crypto_Core_V1_Sha(base, message, messageSize, digest, mode);
97         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
98     }
99     else
100     {
101         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
102         tmpResult = Cy_Crypto_Core_V2_Sha(base, message, messageSize, digest, mode);
103         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
104     }
105 
106     return tmpResult;
107 }
108 
109 /*******************************************************************************
110 * Function Name: Cy_Crypto_Core_Sha_Init
111 ****************************************************************************//**
112 *
113 * The function to initialize the SHA operation.
114 *
115 * \param base
116 * The pointer to the CRYPTO instance.
117 *
118 * \param shaHashState
119 * The pointer to a Hash state.
120 *
121 * \param mode
122 * One of these: \ref cy_en_crypto_sha_mode_t
123 *
124 * \param shaBuffers
125 * The pointer to the memory buffers storage.
126 * Can be one of two buffers types according to selected hardware platform:
127 * cy_stc_crypto_v1_sha_buffers_t or cy_stc_crypto_v2_sha_buffers_t
128 *
129 * \return
130 * \ref cy_en_crypto_status_t
131 *
132 *******************************************************************************/
Cy_Crypto_Core_Sha_Init(CRYPTO_Type * base,cy_stc_crypto_sha_state_t * shaHashState,cy_en_crypto_sha_mode_t mode,void * shaBuffers)133 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Sha_Init(CRYPTO_Type *base,
134                              cy_stc_crypto_sha_state_t *shaHashState,
135                              cy_en_crypto_sha_mode_t mode,
136                              void *shaBuffers)
137 {
138     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
139 
140     if ((bool)CY_CRYPTO_V1)
141     {
142         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
143         tmpResult = Cy_Crypto_Core_V1_Sha_Init(base, shaHashState, mode, shaBuffers);
144         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
145     }
146     else
147     {
148         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
149         tmpResult = Cy_Crypto_Core_V2_Sha_Init(base, shaHashState, mode, shaBuffers);
150         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
151     }
152 
153     return tmpResult;
154 }
155 
156 /*******************************************************************************
157 * Function Name: Cy_Crypto_Core_Sha_Start
158 ****************************************************************************//**
159 *
160 * Initializes the initial Hash vector.
161 *
162 * \param base
163 * The pointer to the CRYPTO instance.
164 *
165 * \param hashState
166 * The pointer to the SHA context.
167 *
168 * \return
169 * \ref cy_en_crypto_status_t
170 *
171 *******************************************************************************/
Cy_Crypto_Core_Sha_Start(CRYPTO_Type * base,cy_stc_crypto_sha_state_t * hashState)172 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Sha_Start(CRYPTO_Type *base, cy_stc_crypto_sha_state_t *hashState)
173 {
174     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
175 
176     if ((bool)CY_CRYPTO_V1)
177     {
178         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
179         tmpResult = Cy_Crypto_Core_V1_Sha_Start(base, hashState);
180         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
181     }
182     else
183     {
184         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
185         tmpResult = Cy_Crypto_Core_V2_Sha_Start(base, hashState);
186         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
187     }
188 
189     return tmpResult;
190 }
191 
192 /*******************************************************************************
193 * Function Name: Cy_Crypto_Core_Sha_Update
194 ****************************************************************************//**
195 *
196 * Performs the SHA calculation on one message.
197 * For CAT1C & CAT1D(CM55) devices when D-Cache is enabled parameter message must align and end in 32 byte boundary.
198 *
199 * \param base
200 * The pointer to the CRYPTO instance.
201 *
202 * \param hashState
203 * The pointer to the SHA context.
204 *
205 * \param message
206 * The pointer to the message whose Hash is being computed.
207 *
208 * \param messageSize
209 * The size of the message whose Hash is being computed.
210 *
211 * \return
212 * \ref cy_en_crypto_status_t
213 *
214 * \note
215 * This function can be called several times only with message lengths dividable
216 * by the block size. Only the last call to the function can process a message with
217 * a not-dividable size.
218 *
219 *******************************************************************************/
Cy_Crypto_Core_Sha_Update(CRYPTO_Type * base,cy_stc_crypto_sha_state_t * hashState,uint8_t const * message,uint32_t messageSize)220 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Sha_Update(CRYPTO_Type *base,
221                                cy_stc_crypto_sha_state_t *hashState,
222                                uint8_t const *message,
223                                uint32_t  messageSize)
224 {
225     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
226 
227     if ((bool)CY_CRYPTO_V1)
228     {
229         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
230         tmpResult = Cy_Crypto_Core_V1_Sha_Update(base, hashState, message, messageSize);
231         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
232     }
233     else
234     {
235         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
236         tmpResult = Cy_Crypto_Core_V2_Sha_Update(base, hashState, message, messageSize);
237         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
238     }
239 
240     return tmpResult;
241 }
242 
243 /*******************************************************************************
244 * Function Name: Cy_Crypto_Core_Sha_Finish
245 ****************************************************************************//**
246 *
247 * Completes the SHA calculation.
248 * For CAT1C & CAT1D(CM55) devices when D-Cache is enabled parameter digest must align and end in 32 byte boundary.
249 *
250 * \param base
251 * The pointer to the CRYPTO instance.
252 *
253 * \param hashState
254 * The pointer to the SHA context.
255 *
256 * \param digest
257 * The pointer to the calculated Hash digest.
258 *
259 * \return
260 * \ref cy_en_crypto_status_t
261 *
262 *******************************************************************************/
Cy_Crypto_Core_Sha_Finish(CRYPTO_Type * base,cy_stc_crypto_sha_state_t * hashState,uint8_t * digest)263 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Sha_Finish(CRYPTO_Type *base,
264                                cy_stc_crypto_sha_state_t *hashState,
265                                uint8_t *digest)
266 {
267     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
268 
269     if ((bool)CY_CRYPTO_V1)
270     {
271         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
272         tmpResult = Cy_Crypto_Core_V1_Sha_Finish(base, hashState, digest);
273         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
274     }
275     else
276     {
277         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
278         tmpResult = Cy_Crypto_Core_V2_Sha_Finish(base, hashState, digest);
279         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
280     }
281 
282     return tmpResult;
283 }
284 
285 /*******************************************************************************
286 * Function Name: Cy_Crypto_Core_Sha_Free
287 ****************************************************************************//**
288 *
289 * Clears the used memory buffers.
290 * For CAT1C & CAT1D(CM55) devices when D-Cache is enabled parameter hashState must align and end in 32 byte boundary.
291 * \param base
292 * The pointer to the CRYPTO instance.
293 *
294 * \param hashState
295 * The pointer to the SHA context.
296 *
297 * \return
298 * \ref cy_en_crypto_status_t
299 *
300 *******************************************************************************/
Cy_Crypto_Core_Sha_Free(CRYPTO_Type * base,cy_stc_crypto_sha_state_t * hashState)301 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Sha_Free(CRYPTO_Type *base, cy_stc_crypto_sha_state_t *hashState)
302 {
303     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
304 
305     if ((bool)CY_CRYPTO_V1)
306     {
307         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
308         tmpResult = Cy_Crypto_Core_V1_Sha_Free(base, hashState);
309         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
310     }
311     else
312     {
313         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
314         tmpResult = Cy_Crypto_Core_V2_Sha_Free(base, hashState);
315         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
316     }
317 
318     return tmpResult;
319 }
320 
321 /** \} group_crypto_lld_sha_functions */
322 
323 #endif /* (CPUSS_CRYPTO_SHA == 1) && defined(CY_CRYPTO_CFG_SHA_C) */
324 
325 #if defined(__cplusplus)
326 }
327 #endif
328 
329 #endif /* CY_IP_MXCRYPTO */
330 
331 #endif /* #if !defined (CY_CRYPTO_CORE_SHA_H) */
332 
333 
334 /* [] END OF FILE */
335