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