1 /***************************************************************************//**
2 * \file cy_cryptolite_hmac.h
3 * \version 2.50
4 * \brief
5 *  This file provides common constants and parameters
6 *  for the Cryptolite hmac sha256 driver.
7 *
8 ********************************************************************************
9 * Copyright 2022 Cypress Semiconductor Corporation
10 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 *    http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *******************************************************************************/
24 
25 #if !defined (CY_CRYPTOLITE_HMAC_H)
26 #define CY_CRYPTOLITE_HMAC_H
27 
28 #include "cy_device.h"
29 
30 #if defined (CY_IP_MXCRYPTOLITE)
31 
32 #include "cy_cryptolite_common.h"
33 
34 #if defined(__cplusplus)
35 extern "C" {
36 #endif
37 
38 #if (CRYPTOLITE_SHA_PRESENT == 1)
39 #if defined(CY_CRYPTOLITE_CFG_HMAC_C) && defined(CY_CRYPTOLITE_CFG_SHA2_256_ENABLED)
40 
41 #include "cy_cryptolite_sha256.h"
42 
43 /** \cond INTERNAL */
44 /** \endcond */
45 
46 /**
47 * \addtogroup group_cryptolite_data_structures
48 * \{
49 */
50 
51 /** \cond INTERNAL */
52 #define CY_CRYPTOLITE_HMAC_IPAD               (0x36u)
53 #define CY_CRYPTOLITE_HMAC_0PAD               (0x5Cu)
54 #define CY_CRYPTOLITE_HMAC_MAX_PAD_SIZE       (CY_CRYPTOLITE_SHA256_BLOCK_SIZE)
55 #define CY_CRYPTOLITE_HMAC_MAX_M0_KEY_SIZE    (CY_CRYPTOLITE_SHA256_BLOCK_SIZE)
56 /** \endcond */
57 
58 /** The structure for storing the SHA256 context.
59 * All fields for the context structure are internal. Firmware never reads or
60 * writes these values. Firmware allocates the structure and provides the
61 * address of the structure to the driver in the function calls. Firmware must
62 * ensure that the defined instance of this structure remains in scope
63 * while the drive is in use.
64 */
65 /*Check : ALIGN to be enforced with separate buf type*/
66 typedef struct
67 {
68     /** \cond INTERNAL */
69     CY_ALIGN(4) uint8_t ipad[CY_CRYPTOLITE_HMAC_MAX_PAD_SIZE];
70     CY_ALIGN(4) uint8_t opad[CY_CRYPTOLITE_HMAC_MAX_PAD_SIZE];
71     CY_ALIGN(4) uint8_t m0Key[CY_CRYPTOLITE_SHA256_BLOCK_SIZE];
72     uint16_t blocksize;
73     uint16_t digestsize;
74     /** sha256 context */
75     cy_stc_cryptolite_context_sha256_t ctx_sha256;    /** \endcond */
76 } cy_stc_cryptolite_context_hmac_sha256_t;
77 
78 
79 /** \} group_cryptolite_data_structures */
80 
81 /**
82 * \addtogroup group_cryptolite_lld_mac_functions
83 * \{
84 */
85 /*******************************************************************************
86 * Function Name: Cy_Cryptolite_Hmac_Sha256_Init
87 ****************************************************************************//**
88 *
89 * The function to initialize the HMAC operation.
90 *
91 * \param base
92 * The pointer to the Cryptolite instance.
93 *
94 * \param cfContext
95 * The pointer to the \ref cy_stc_cryptolite_context_hmac_sha256_t structure that stores all
96 * internal variables for Cryptolite driver.
97 *
98 * \return
99 * \ref cy_en_cryptolite_status_t
100 *
101 * \funcusage
102 * \snippet cryptolite/snippet/main.c snippet_Cy_Cryptolite_Hmac_Sha256_init_start_update_finish_free
103 *******************************************************************************/
104 cy_en_cryptolite_status_t Cy_Cryptolite_Hmac_Sha256_Init(CRYPTOLITE_Type *base,
105                                         cy_stc_cryptolite_context_hmac_sha256_t *cfContext);
106 
107 /*******************************************************************************
108 * Function Name: Cy_Cryptolite_Hmac_Sha256_Start
109 ****************************************************************************//**
110 *
111 * Initializes the initial Hash vector.
112 *
113 * \param base
114 * The pointer to the CRYPTOLITE instance.
115 *
116 * \param key
117 * The pointer to the HMAC key.
118 *
119 * \param keyLength
120 * The length of the HMAC key.
121 *
122 *  key
123 * The pointer to the HMAC key.
124 *
125 *  keyLength
126 * The length of the HMAC key.
127 *
128 * \param cfContext
129 * The pointer to the \ref cy_stc_cryptolite_context_hmac_sha256_t structure that stores all
130 * internal variables for Cryptolite driver.
131 *
132 * \return
133 * \ref cy_en_cryptolite_status_t
134 *
135 * \funcusage
136 * \snippet cryptolite/snippet/main.c snippet_Cy_Cryptolite_Hmac_Sha256_init_start_update_finish_free
137 *******************************************************************************/
138 cy_en_cryptolite_status_t Cy_Cryptolite_Hmac_Sha256_Start(CRYPTOLITE_Type *base, uint8_t const *key, uint32_t keyLength,
139                                         cy_stc_cryptolite_context_hmac_sha256_t *cfContext);
140 
141 /*******************************************************************************
142 * Function Name: Cy_Cryptolite_Hmac_Sha256_Update
143 ****************************************************************************//**
144 *
145 * Performs the HMAC update on message.
146 *
147 * \param base
148 * The pointer to the CRYPTOLITE instance.
149 *
150 * \param message
151 * The address pointer to the message whose Hash is being computed.
152 *
153 * \param messageSize
154 * The size of the message whose Hash is being computed.
155 *
156 * \param cfContext
157 * The pointer to the \ref cy_stc_cryptolite_context_hmac_sha256_t structure that stores all
158 * internal variables for Cryptolite driver.
159 *
160 * \return
161 * \ref cy_en_cryptolite_status_t
162 *
163 * \funcusage
164 * \snippet cryptolite/snippet/main.c snippet_Cy_Cryptolite_Hmac_Sha256_init_start_update_finish_free
165 *
166 * \note There is no alignment or size restriction for message buffer, However providing
167 * a four byte aligned buffer with size in multiple of \ref CY_CRYPTOLITE_SHA256_BLOCK_SIZE,
168 * will result in best execution time.
169 *******************************************************************************/
170 cy_en_cryptolite_status_t Cy_Cryptolite_Hmac_Sha256_Update(CRYPTOLITE_Type *base,
171                                                     uint8_t const *message,
172                                                     uint32_t  messageSize,
173                                                     cy_stc_cryptolite_context_hmac_sha256_t *cfContext);
174 
175 /*******************************************************************************
176 * Function Name: Cy_Cryptolite_Hmac_Sha256_Finish
177 ****************************************************************************//**
178 *
179 * Completes the HMAC SHA256 calculation.
180 *
181 * \param base
182 * The pointer to the CRYPTOLITE instance.
183 *
184 * \param hmac
185 * The address pointer to the calculated Hmac.
186 *
187 * \param cfContext
188 * The pointer to the  cy_stc_cryptolite_context_hmac_sha_t structure that stores all
189 * internal variables for Cryptolite driver.
190 *
191 * \return
192 * \ref cy_en_cryptolite_status_t
193 *
194 * \funcusage
195 * \snippet cryptolite/snippet/main.c snippet_Cy_Cryptolite_Hmac_Sha256_init_start_update_finish_free
196 *******************************************************************************/
197 cy_en_cryptolite_status_t Cy_Cryptolite_Hmac_Sha256_Finish(CRYPTOLITE_Type *base,
198                                uint8_t *hmac,
199                                cy_stc_cryptolite_context_hmac_sha256_t *cfContext);
200 
201 /*******************************************************************************
202 * Function Name: Cy_Cryptolite_Hmac_Sha256_Free
203 ****************************************************************************//**
204 *
205 * Clears the used memory and context data.
206 *
207 * \param base
208 * The pointer to the CRYPTOLITE instance.
209 *
210 * \param cfContext
211 * The pointer to the \ref cy_stc_cryptolite_context_hmac_sha256_t structure that stores all
212 * internal variables for Cryptolite driver.
213 *
214 * \return
215 * \ref cy_en_cryptolite_status_t
216 *
217 * \funcusage
218 * \snippet cryptolite/snippet/main.c snippet_Cy_Cryptolite_Hmac_Sha256_init_start_update_finish_free
219 *******************************************************************************/
220 cy_en_cryptolite_status_t Cy_Cryptolite_Hmac_Sha256_Free(CRYPTOLITE_Type *base,
221                                                     cy_stc_cryptolite_context_hmac_sha256_t *cfContext);
222 
223 /*******************************************************************************
224 * Function Name: Cy_Cryptolite_Hmac_Sha256_Run
225 ****************************************************************************//**
226 *
227 * This function performs the HMAC SHA256 function.
228 * Provide the required parameters and the pointer
229 * to the context structure when making this function call.
230 * It is independent of the previous Crypto state because it already contains
231 * preparation, calculation, and finalization steps.
232 *
233 * \param base
234 * The pointer to the CRYPTOLITE instance.
235 *
236 * \param key
237 * The pointer to the HMAC key.
238 *
239 * \param keyLength
240 * The length of the HMAC key.
241 *
242 * \param message
243 * The address pointer to a message whose hash value is being computed.
244 *
245 * \param messageSize
246 * The size of a message in bytes.
247 *
248 * \param hmac
249 * The address pointer to the hmac.
250 *
251 * \param cfContext
252 * The pointer to the \ref cy_stc_cryptolite_context_hmac_sha256_t structure that stores all
253 * internal variables for Cryptolite driver.
254 *
255 * \return
256 * \ref cy_en_cryptolite_status_t
257 *
258 * \funcusage
259 * \snippet cryptolite/snippet/main.c snippet_Cy_Cryptolite_Hmac_Sha256_Run
260 *
261 * \note There is no alignment or size restriction for message buffer, However providing
262 * a four byte aligned buffer with size in multiple of \ref CY_CRYPTOLITE_SHA256_BLOCK_SIZE,
263 * will result in best execution time.
264 *******************************************************************************/
265 cy_en_cryptolite_status_t Cy_Cryptolite_Hmac_Sha256_Run(CRYPTOLITE_Type *base,
266                                         uint8_t const *key,
267                                         uint32_t keyLength,
268                                         uint8_t const *message,
269                                         uint32_t  messageSize,
270                                         uint8_t *hmac,
271                                         cy_stc_cryptolite_context_hmac_sha256_t *cfContext);
272 
273 #endif /* #if defined(CY_CRYPTO_CFG_HMAC_C)*/
274 #endif /* #if CRYPTOLITE_SHA_PRESENT */
275 
276 /** \} group_cryptolite_lld_mac_functions */
277 #if defined(__cplusplus)
278 }
279 #endif
280 
281 #endif /* CY_IP_MXCRYPTOLITE */
282 
283 #endif /* #if !defined (CY_CRYPTOLITE_HMAC_H) */
284 /** \} group_cryptolite */
285 
286 /* [] END OF FILE */
287