1 /***************************************************************************//**
2 * \file cy_crypto_core_hmac.h
3 * \version 2.120
4 *
5 * \brief
6 * This file provides constants and function prototypes
7 * for the API for the HMAC 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
29 #if !defined (CY_CRYPTO_CORE_HMAC_H)
30 #define CY_CRYPTO_CORE_HMAC_H
31
32 #include "cy_device.h"
33
34 #if defined (CY_IP_MXCRYPTO)
35
36 #include "cy_crypto_common.h"
37
38 #if defined(__cplusplus)
39 extern "C" {
40 #endif
41
42 #if (CPUSS_CRYPTO_SHA == 1) && defined(CY_CRYPTO_CFG_HMAC_C)
43
44 #include "cy_crypto_core_hmac_v1.h"
45 #include "cy_crypto_core_hmac_v2.h"
46
47 typedef cy_en_crypto_status_t (*cy_crypto_hmac_func_t)(CRYPTO_Type *base,
48 uint8_t *hmac,
49 uint8_t const *message,
50 uint32_t messageSize,
51 uint8_t const *key,
52 uint32_t keyLength,
53 cy_en_crypto_sha_mode_t mode);
54
55 /**
56 * \addtogroup group_crypto_lld_mac_functions
57 * \{
58 */
59
60 /*******************************************************************************
61 * Function Name: Cy_Crypto_Core_Hmac
62 ****************************************************************************//**
63 *
64 * Performs the HMAC calculation.
65 *
66 * \param base
67 * The pointer to the CRYPTO instance.
68 *
69 * \param hmac
70 * The pointer to the calculated HMAC. Must be 4-byte aligned.
71 *
72 * \param message
73 * The pointer to the message whose hash value is being computed.
74 *
75 * \param messageSize
76 * The size of the message.
77 *
78 * \param key
79 * The pointer to the key.
80 *
81 * \param keyLength
82 * The length of the key.
83 *
84 * \param mode
85 * \ref cy_en_crypto_sha_mode_t
86 *
87 * \return
88 * \ref cy_en_crypto_status_t
89 *
90 * \funcusage
91 * \snippet crypto/snippet/main.c snippet_myCryptoCoreHmacUse
92 *
93 *******************************************************************************/
Cy_Crypto_Core_Hmac(CRYPTO_Type * base,uint8_t * hmac,uint8_t const * message,uint32_t messageSize,uint8_t const * key,uint32_t keyLength,cy_en_crypto_sha_mode_t mode)94 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Hmac(CRYPTO_Type *base,
95 uint8_t *hmac,
96 uint8_t const *message,
97 uint32_t messageSize,
98 uint8_t const *key,
99 uint32_t keyLength,
100 cy_en_crypto_sha_mode_t mode)
101 {
102 cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
103
104 if (CY_CRYPTO_V1)
105 {
106 #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
107 tmpResult = Cy_Crypto_Core_V1_Hmac(base, hmac, message, messageSize, key, keyLength, mode);
108 #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
109 }
110 else
111 {
112 #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
113 tmpResult = Cy_Crypto_Core_V2_Hmac(base, hmac, message, messageSize, key, keyLength, mode);
114 #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
115 }
116
117 return tmpResult;
118 }
119
120
121
122 /*******************************************************************************
123 * Function Name: Cy_Crypto_Core_Hmac_Init
124 ****************************************************************************//**
125 *
126 * The function to initialize the HMAC operation.
127 * For CAT1C & CAT1D(CM55) devices when D-Cache is enabled parameters hmacState and hmacBuffer must align and end in 32 byte boundary.
128 *
129 * \param base
130 * The pointer to the CRYPTO instance.
131 *
132 * \param hmacState
133 * The pointer to the HMAC state.
134 *
135 * \param hmacBuffer
136 * The pointer to the chmac buffer structure that stores all
137 * buffers for HMAC operation.
138 *
139 * \param mode
140 * \ref cy_en_crypto_sha_mode_t
141 *
142 * \return
143 * \ref cy_en_crypto_status_t
144 *
145 *******************************************************************************/
Cy_Crypto_Core_Hmac_Init(CRYPTO_Type * base,cy_stc_crypto_hmac_state_t * hmacState,cy_en_crypto_sha_mode_t mode,void * hmacBuffer)146 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Hmac_Init(CRYPTO_Type *base, cy_stc_crypto_hmac_state_t *hmacState, cy_en_crypto_sha_mode_t mode, void *hmacBuffer)
147 {
148 cy_en_crypto_status_t tmpResult;
149
150 if (CY_CRYPTO_V1)
151 {
152 #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
153 tmpResult = CY_CRYPTO_NOT_SUPPORTED;
154 #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
155 }
156 else
157 {
158 #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
159 tmpResult = Cy_Crypto_Core_V2_Hmac_Init(base, hmacState, mode, (cy_stc_crypto_v2_hmac_buffers_t *)hmacBuffer);
160 #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
161 }
162
163 (void)base; /* Suppress a compiler warning about unused variables */
164 (void)hmacState; /* Suppress a compiler warning about unused variables */
165 (void)mode; /* Suppress a compiler warning about unused variables */
166 (void)hmacBuffer; /* Suppress a compiler warning about unused variables */
167
168 return tmpResult;
169 }
170
171
172
173 /*******************************************************************************
174 * Function Name: Cy_Crypto_Core_Hmac_Start
175 ****************************************************************************//**
176 *
177 * Initializes the HMAC key.
178 * For CAT1C & CAT1D(CM55) devices when D-Cache is enabled parameters key & hmacState (m0Key) must align and end in 32 byte boundary.
179 *
180 * \param base
181 * The pointer to the CRYPTO instance.
182 *
183 * \param hmacState
184 * The pointer to the HMAC state.
185 *
186 * \param key
187 * the pointer to the hmac key
188 *
189 * \param keyLength
190 * the length of the key
191 *
192 * \return
193 * \ref cy_en_crypto_status_t
194 *
195 *******************************************************************************/
Cy_Crypto_Core_Hmac_Start(CRYPTO_Type * base,cy_stc_crypto_hmac_state_t * hmacState,uint8_t const * key,uint32_t keyLength)196 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Hmac_Start(CRYPTO_Type *base, cy_stc_crypto_hmac_state_t *hmacState,
197 uint8_t const *key,
198 uint32_t keyLength
199 )
200 {
201 cy_en_crypto_status_t tmpResult;
202
203 if (CY_CRYPTO_V1)
204 {
205 #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
206 tmpResult = CY_CRYPTO_NOT_SUPPORTED;
207 #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
208 }
209 else
210 {
211 #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
212 tmpResult = Cy_Crypto_Core_V2_Hmac_Start(base, hmacState, key, keyLength);
213 #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
214 }
215
216 (void)base; /* Suppress a compiler warning about unused variables */
217 (void)hmacState; /* Suppress a compiler warning about unused variables */
218 (void)key; /* Suppress a compiler warning about unused variables */
219 (void)keyLength; /* Suppress a compiler warning about unused variables */
220
221 return tmpResult;
222 }
223
224
225 /*******************************************************************************
226 * Function Name: Cy_Crypto_Core_Hmac_Update
227 ****************************************************************************//**
228 *
229 * Performs the multipart hmac operation.
230 *
231 * \param base
232 * The pointer to the CRYPTO instance.
233 *
234 * \param hmacState
235 * The pointer to the HMAC state.
236 *
237 * \param message
238 * the pointer to the message
239 *
240 * \param messageSize
241 * the length of the message
242 *
243 * \return
244 * \ref cy_en_crypto_status_t
245 *
246 *******************************************************************************/
Cy_Crypto_Core_Hmac_Update(CRYPTO_Type * base,cy_stc_crypto_hmac_state_t * hmacState,uint8_t const * message,uint32_t messageSize)247 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Hmac_Update(CRYPTO_Type *base, cy_stc_crypto_hmac_state_t *hmacState,
248 uint8_t const *message,
249 uint32_t messageSize
250 )
251 {
252 cy_en_crypto_status_t tmpResult;
253
254 if (CY_CRYPTO_V1)
255 {
256 #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
257 tmpResult = CY_CRYPTO_NOT_SUPPORTED;
258 #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
259 }
260 else
261 {
262 #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
263 tmpResult = Cy_Crypto_Core_V2_Hmac_Update(base, hmacState, message, messageSize);
264 #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
265 }
266
267 (void)base; /* Suppress a compiler warning about unused variables */
268 (void)hmacState; /* Suppress a compiler warning about unused variables */
269 (void)message; /* Suppress a compiler warning about unused variables */
270 (void)messageSize; /* Suppress a compiler warning about unused variables */
271
272 return tmpResult;
273 }
274
275
276
277 /*******************************************************************************
278 * Function Name: Cy_Crypto_Core_Hmac_Finish
279 ****************************************************************************//**
280 *
281 * Finishes the hmac operation.
282 *
283 * \param base
284 * The pointer to the CRYPTO instance.
285 *
286 * \param hmacState
287 * The pointer to the HMAC state.
288 *
289 * \param hmac
290 * the pointer to store the hmac.
291 *
292 * \return
293 * \ref cy_en_crypto_status_t
294 *
295 *******************************************************************************/
Cy_Crypto_Core_Hmac_Finish(CRYPTO_Type * base,cy_stc_crypto_hmac_state_t * hmacState,uint8_t * hmac)296 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Hmac_Finish(CRYPTO_Type *base, cy_stc_crypto_hmac_state_t *hmacState,
297 uint8_t *hmac)
298 {
299 cy_en_crypto_status_t tmpResult;
300
301 if (CY_CRYPTO_V1)
302 {
303 #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
304 tmpResult = CY_CRYPTO_NOT_SUPPORTED;
305 #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
306 }
307 else
308 {
309 #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
310 tmpResult = Cy_Crypto_Core_V2_Hmac_Finish(base, hmacState, hmac);
311 #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
312 }
313
314 (void)base; /* Suppress a compiler warning about unused variables */
315 (void)hmac; /* Suppress a compiler warning about unused variables */
316 (void)hmacState; /* Suppress a compiler warning about unused variables */
317
318 return tmpResult;
319 }
320
321
322 /*******************************************************************************
323 * Function Name: Cy_Crypto_Core_Hmac_Free
324 ****************************************************************************//**
325 *
326 * Frees the internally stored buffers in hmac context.
327 * For CAT1C & CAT1D(CM55) devices when D-Cache is enabled parameters hmacState must align and end in 32 byte boundary.
328 *
329 * \param base
330 * The pointer to the CRYPTO instance.
331 *
332 * \param hmacState
333 * The pointer to the HMAC state.
334 *
335 * \return
336 * \ref cy_en_crypto_status_t
337 *
338 *******************************************************************************/
339
Cy_Crypto_Core_Hmac_Free(CRYPTO_Type * base,cy_stc_crypto_hmac_state_t * hmacState)340 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Hmac_Free(CRYPTO_Type *base, cy_stc_crypto_hmac_state_t *hmacState)
341 {
342 cy_en_crypto_status_t tmpResult;
343
344 if (CY_CRYPTO_V1)
345 {
346 #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
347 tmpResult = CY_CRYPTO_NOT_SUPPORTED;
348 #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
349 }
350 else
351 {
352 #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
353 tmpResult = Cy_Crypto_Core_V2_Hmac_Free(base, hmacState);
354 #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
355 }
356
357 (void)base; /* Suppress a compiler warning about unused variables */
358 (void)hmacState; /* Suppress a compiler warning about unused variables */
359
360 return tmpResult;
361 }
362
363 /** \} group_crypto_lld_mac_functions */
364
365 #endif /* (CPUSS_CRYPTO_SHA == 1) && defined(CY_CRYPTO_CFG_HMAC_C) */
366
367 #if defined(__cplusplus)
368 }
369 #endif
370
371 #endif /* CY_IP_MXCRYPTO */
372
373 #endif /* #if !defined (CY_CRYPTO_CORE_HMAC_H) */
374
375
376 /* [] END OF FILE */
377