1 /***************************************************************************//**
2 * \file cy_cryptolite_sha256.h
3 * \version 2.30
4 *
5 * \brief
6 *  This file provides common constants and parameters
7 *  for the Cryptolite SHA256 driver.
8 *
9 ********************************************************************************
10 * Copyright 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 #if !defined (CY_CRYPTOLITE_SHA256_H)
26 #define CY_CRYPTOLITE_SHA256_H
27 
28 #include "cy_device.h"
29 
30 #if defined (CY_IP_MXCRYPTOLITE)
31 
32 #if defined(__cplusplus)
33 extern "C" {
34 #endif
35 
36 #include "cy_cryptolite_common.h"
37 
38 #if (CRYPTOLITE_SHA_PRESENT == 1)
39 #if defined(CY_CRYPTOLITE_CFG_SHA_C) && defined(CY_CRYPTOLITE_CFG_SHA2_256_ENABLED)
40 
41 /** \cond INTERNAL */
42 /** PAD size for the SHA256(in bytes)    */
43 #define CY_CRYPTOLITE_SHA256_PAD_SIZE           (56uL)
44 /** \endcond */
45 
46 /**
47 * \addtogroup group_cryptolite_macros
48 * \{
49 */
50 /** Block size for the SHA256(in bytes)  */
51 #define CY_CRYPTOLITE_SHA256_BLOCK_SIZE         (64UL)
52 
53 /** HASH size for the SHA256(in bytes)   */
54 #define CY_CRYPTOLITE_SHA256_HASH_SIZE          (32UL)
55 
56 /** \} group_cryptolite_macros */
57 
58 
59 /**
60 * \addtogroup group_cryptolite_data_structures
61 * \{
62 */
63 
64 /** The structure for storing the SHA256 context.
65 * All fields for the context structure are internal. Firmware never reads or
66 * writes these values. Firmware allocates the structure and provides the
67 * address of the structure to the driver in the function calls. Firmware must
68 * ensure that the defined instance of this structure remains in scope
69 * while the drive is in use.
70 */
71 typedef struct
72 {
73     /** \cond INTERNAL */
74     uint32_t msgblock[CY_CRYPTOLITE_SHA256_BLOCK_SIZE / 4u];
75     uint32_t hash[CY_CRYPTOLITE_SHA256_HASH_SIZE / 4u];
76     uint32_t message_schedule[CY_CRYPTOLITE_SHA256_BLOCK_SIZE];
77     uint8_t *message;
78     uint32_t messageSize;
79     uint32_t msgIdx;
80     /** Operation data descriptors */
81     cy_stc_cryptolite_descr_t message_schedule_struct;
82     cy_stc_cryptolite_descr_t message_process_struct;
83     /** \endcond */
84 } cy_stc_cryptolite_context_sha256_t;
85 
86 
87 /** \} group_cryptolite_data_structures */
88 
89 /**
90 * \addtogroup group_cryptolite_lld_sha_functions
91 * \{
92 */
93 /*******************************************************************************
94 * Function Name: Cy_Cryptolite_Sha256_Init
95 ****************************************************************************//**
96 *
97 * The function to initialize the SHA256 operation.
98 *
99 * \param base
100 * The pointer to the Cryptolite instance.
101 *
102 * \param cfContext
103 * The pointer to the \ref cy_stc_cryptolite_context_sha256_t structure that stores all
104 * internal variables for Cryptolite driver.
105 *
106 * \return
107 * \ref cy_en_cryptolite_status_t
108 *
109 * \funcusage
110 * \snippet cryptolite/snippet/main.c snippet_Cy_Cryptolite_Sha256_init_start_update_finish_free
111 *******************************************************************************/
112 cy_en_cryptolite_status_t Cy_Cryptolite_Sha256_Init(CRYPTOLITE_Type *base,
113                                         cy_stc_cryptolite_context_sha256_t *cfContext);
114 
115 /*******************************************************************************
116 * Function Name: Cy_Cryptolite_Sha256_Start
117 ****************************************************************************//**
118 *
119 * Initializes the initial Hash vector.
120 *
121 * \param base
122 * The pointer to the CRYPTOLITE instance.
123 *
124 * \param cfContext
125 * The pointer to the \ref cy_stc_cryptolite_context_sha256_t structure that stores all
126 * internal variables for Cryptolite driver.
127 *
128 * \return
129 * \ref cy_en_cryptolite_status_t
130 *
131 * \funcusage
132 * \snippet cryptolite/snippet/main.c snippet_Cy_Cryptolite_Sha256_init_start_update_finish_free
133 *******************************************************************************/
134 cy_en_cryptolite_status_t Cy_Cryptolite_Sha256_Start(CRYPTOLITE_Type *base,
135                                         cy_stc_cryptolite_context_sha256_t *cfContext);
136 
137 /*******************************************************************************
138 * Function Name: Cy_Cryptolite_Sha256_Update
139 ****************************************************************************//**
140 *
141 * Performs the SHA256 calculation on one message.
142 *
143 * \param base
144 * The pointer to the CRYPTOLITE instance.
145 *
146 * \param message
147 * The SAHB mapped address pointer to the message whose Hash is being computed.
148 *
149 * \param messageSize
150 * The size of the message whose Hash is being computed.
151 *
152 * \param cfContext
153 * The pointer to the \ref cy_stc_cryptolite_context_sha256_t structure that stores all
154 * internal variables for Cryptolite driver.
155 *
156 * \return
157 * \ref cy_en_cryptolite_status_t
158 *
159 * \funcusage
160 * \snippet cryptolite/snippet/main.c snippet_Cy_Cryptolite_Sha256_init_start_update_finish_free
161 *
162 * \note There is no alignment or size restriction for message buffer, However providing
163 * a four byte aligned buffer with size in multiple of \ref CY_CRYPTOLITE_SHA256_BLOCK_SIZE,
164 * will result in best execution time.
165 *******************************************************************************/
166 cy_en_cryptolite_status_t Cy_Cryptolite_Sha256_Update(CRYPTOLITE_Type *base,
167                                                     uint8_t const *message,
168                                                     uint32_t  messageSize,
169                                                     cy_stc_cryptolite_context_sha256_t *cfContext);
170 
171 /*******************************************************************************
172 * Function Name: Cy_Cryptolite_Sha256_Finish
173 ****************************************************************************//**
174 *
175 * Completes the SHA256 calculation.
176 *
177 * \param base
178 * The pointer to the CRYPTOLITE instance.
179 *
180 * \param digest
181 * The SAHB mapped address pointer to the calculated Hash digest.
182 *
183 * \param cfContext
184 * The pointer to the \ref cy_stc_cryptolite_context_sha256_t structure that stores all
185 * internal variables for Cryptolite driver.
186 *
187 * \return
188 * \ref cy_en_cryptolite_status_t
189 *
190 * \funcusage
191 * \snippet cryptolite/snippet/main.c snippet_Cy_Cryptolite_Sha256_init_start_update_finish_free
192 *******************************************************************************/
193 cy_en_cryptolite_status_t Cy_Cryptolite_Sha256_Finish(CRYPTOLITE_Type *base,
194                                uint8_t *digest,
195                                cy_stc_cryptolite_context_sha256_t *cfContext);
196 
197 /*******************************************************************************
198 * Function Name: Cy_Cryptolite_Sha256_Free
199 ****************************************************************************//**
200 *
201 * Clears the used memory and context data.
202 *
203 * \param base
204 * The pointer to the CRYPTOLITE instance.
205 *
206 * \param cfContext
207 * The pointer to the \ref cy_stc_cryptolite_context_sha256_t structure that stores all
208 * internal variables for Cryptolite driver.
209 *
210 * \return
211 * \ref cy_en_cryptolite_status_t
212 *
213 * \funcusage
214 * \snippet cryptolite/snippet/main.c snippet_Cy_Cryptolite_Sha256_init_start_update_finish_free
215 *******************************************************************************/
216 cy_en_cryptolite_status_t Cy_Cryptolite_Sha256_Free(CRYPTOLITE_Type *base,
217                                                     cy_stc_cryptolite_context_sha256_t *cfContext);
218 
219 /*******************************************************************************
220 * Function Name: Cy_Cryptolite_Sha256_Run
221 ****************************************************************************//**
222 *
223 * This function performs the SHA256 Hash function.
224 * Provide the required parameters and the pointer
225 * to the context structure when making this function call.
226 * It is independent of the previous Crypto state because it already contains
227 * preparation, calculation, and finalization steps.
228 *
229 * \param base
230 * The pointer to the CRYPTOLITE instance.
231 *
232 * \param message
233 * The SAHB mapped address pointer to a message whose hash value is being computed.
234 *
235 * \param messageSize
236 * The size of a message in bytes.
237 *
238 * \param digest
239 * The SAHB mapped address pointer to the hash digest.
240 *
241 * \param cfContext
242 * The pointer to the \ref cy_stc_cryptolite_context_sha256_t structure that stores all
243 * internal variables for Cryptolite driver.
244 *
245 * \return
246 * \ref cy_en_cryptolite_status_t
247 *
248 * \funcusage
249 * \snippet cryptolite/snippet/main.c snippet_Cy_Cryptolite_Sha256_Run
250 *
251 * \note There is no alignment or size restriction for message buffer, However providing
252 * a four byte aligned buffer with size in multiple of \ref CY_CRYPTOLITE_SHA256_BLOCK_SIZE,
253 * will result in best execution time.
254 *******************************************************************************/
255 cy_en_cryptolite_status_t Cy_Cryptolite_Sha256_Run(CRYPTOLITE_Type *base,
256                                         uint8_t const *message,
257                                         uint32_t  messageSize,
258                                         uint8_t *digest,
259                                         cy_stc_cryptolite_context_sha256_t *cfContext);
260 
261 #endif /* #if (CY_CRYPTOLITE_CFG_SHA_C)*/
262 #endif /* #if CRYPTOLITE_SHA_PRESENT*/
263 
264 /** \} group_cryptolite_lld_sha_functions */
265 #if defined(__cplusplus)
266 }
267 #endif
268 
269 #endif /* CY_IP_MXCRYPTOLITE */
270 
271 #endif /* #if !defined (CY_CRYPTOLITE_SHA256_H) */
272 
273 /* [] END OF FILE */
274