]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmac.c
2 HMAC-SHA256/SHA384 Wrapper Implementation over OpenSSL.
4 Copyright (c) 2016 - 2022, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "InternalCryptLib.h"
10 #include <openssl/hmac.h>
13 Allocates and initializes one HMAC_CTX context for subsequent HMAC-MD use.
15 @return Pointer to the HMAC_CTX context that has been initialized.
16 If the allocations fails, HmacMdNew() returns NULL.
25 // Allocates & Initializes HMAC_CTX Context by OpenSSL HMAC_CTX_new()
27 return (VOID
*)HMAC_CTX_new ();
31 Release the specified HMAC_CTX context.
33 @param[in] HmacMdCtx Pointer to the HMAC_CTX context to be released.
42 // Free OpenSSL HMAC_CTX Context
44 HMAC_CTX_free ((HMAC_CTX
*)HmacMdCtx
);
48 Set user-supplied key for subsequent use. It must be done before any
49 calling to HmacMdUpdate().
51 If HmacMdContext is NULL, then return FALSE.
53 @param[in] Md Message Digest.
54 @param[out] HmacMdContext Pointer to HMAC-MD context.
55 @param[in] Key Pointer to the user-supplied key.
56 @param[in] KeySize Key size in bytes.
58 @retval TRUE The Key is set successfully.
59 @retval FALSE The Key is set unsuccessfully.
65 OUT VOID
*HmacMdContext
,
71 // Check input parameters.
73 if ((HmacMdContext
== NULL
) || (KeySize
> INT_MAX
)) {
77 if (HMAC_Init_ex ((HMAC_CTX
*)HmacMdContext
, Key
, (UINT32
)KeySize
, Md
, NULL
) != 1) {
85 Makes a copy of an existing HMAC-MD context.
87 If HmacMdContext is NULL, then return FALSE.
88 If NewHmacMdContext is NULL, then return FALSE.
90 @param[in] HmacMdContext Pointer to HMAC-MD context being copied.
91 @param[out] NewHmacMdContext Pointer to new HMAC-MD context.
93 @retval TRUE HMAC-MD context copy succeeded.
94 @retval FALSE HMAC-MD context copy failed.
99 IN CONST VOID
*HmacMdContext
,
100 OUT VOID
*NewHmacMdContext
104 // Check input parameters.
106 if ((HmacMdContext
== NULL
) || (NewHmacMdContext
== NULL
)) {
110 if (HMAC_CTX_copy ((HMAC_CTX
*)NewHmacMdContext
, (HMAC_CTX
*)HmacMdContext
) != 1) {
118 Digests the input data and updates HMAC-MD context.
120 This function performs HMAC-MD digest on a data buffer of the specified size.
121 It can be called multiple times to compute the digest of long or discontinuous data streams.
122 HMAC-MD context should be initialized by HmacMdNew(), and should not be finalized
123 by HmacMdFinal(). Behavior with invalid context is undefined.
125 If HmacMdContext is NULL, then return FALSE.
127 @param[in, out] HmacMdContext Pointer to the HMAC-MD context.
128 @param[in] Data Pointer to the buffer containing the data to be digested.
129 @param[in] DataSize Size of Data buffer in bytes.
131 @retval TRUE HMAC-MD data digest succeeded.
132 @retval FALSE HMAC-MD data digest failed.
137 IN OUT VOID
*HmacMdContext
,
143 // Check input parameters.
145 if (HmacMdContext
== NULL
) {
150 // Check invalid parameters, in case that only DataLength was checked in OpenSSL
152 if ((Data
== NULL
) && (DataSize
!= 0)) {
157 // OpenSSL HMAC-MD digest update
159 if (HMAC_Update ((HMAC_CTX
*)HmacMdContext
, Data
, DataSize
) != 1) {
167 Completes computation of the HMAC-MD digest value.
169 This function completes HMAC-MD hash computation and retrieves the digest value into
170 the specified memory. After this function has been called, the HMAC-MD context cannot
172 HMAC-MD context should be initialized by HmacMdNew(), and should not be finalized
173 by HmacMdFinal(). Behavior with invalid HMAC-MD context is undefined.
175 If HmacMdContext is NULL, then return FALSE.
176 If HmacValue is NULL, then return FALSE.
178 @param[in, out] HmacMdContext Pointer to the HMAC-MD context.
179 @param[out] HmacValue Pointer to a buffer that receives the HMAC-MD digest
182 @retval TRUE HMAC-MD digest computation succeeded.
183 @retval FALSE HMAC-MD digest computation failed.
188 IN OUT VOID
*HmacMdContext
,
195 // Check input parameters.
197 if ((HmacMdContext
== NULL
) || (HmacValue
== NULL
)) {
202 // OpenSSL HMAC-MD digest finalization
204 if (HMAC_Final ((HMAC_CTX
*)HmacMdContext
, HmacValue
, &Length
) != 1) {
208 if (HMAC_CTX_reset ((HMAC_CTX
*)HmacMdContext
) != 1) {
216 Computes the HMAC-MD digest of a input data buffer.
218 This function performs the HMAC-MD digest of a given data buffer, and places
219 the digest value into the specified memory.
221 If this interface is not supported, then return FALSE.
223 @param[in] Md Message Digest.
224 @param[in] Data Pointer to the buffer containing the data to be digested.
225 @param[in] DataSize Size of Data buffer in bytes.
226 @param[in] Key Pointer to the user-supplied key.
227 @param[in] KeySize Key size in bytes.
228 @param[out] HmacValue Pointer to a buffer that receives the HMAC-MD digest
231 @retval TRUE HMAC-MD digest computation succeeded.
232 @retval FALSE HMAC-MD digest computation failed.
233 @retval FALSE This interface is not supported.
250 Ctx
= HMAC_CTX_new ();
255 RetVal
= (BOOLEAN
)HMAC_CTX_reset (Ctx
);
260 RetVal
= (BOOLEAN
)HMAC_Init_ex (Ctx
, Key
, (UINT32
)KeySize
, Md
, NULL
);
265 RetVal
= (BOOLEAN
)HMAC_Update (Ctx
, Data
, DataSize
);
270 RetVal
= (BOOLEAN
)HMAC_Final (Ctx
, HmacValue
, &Length
);
282 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA256 use.
284 @return Pointer to the HMAC_CTX context that has been initialized.
285 If the allocations fails, HmacSha256New() returns NULL.
298 Release the specified HMAC_CTX context.
300 @param[in] HmacSha256Ctx Pointer to the HMAC_CTX context to be released.
306 IN VOID
*HmacSha256Ctx
309 HmacMdFree (HmacSha256Ctx
);
313 Set user-supplied key for subsequent use. It must be done before any
314 calling to HmacSha256Update().
316 If HmacSha256Context is NULL, then return FALSE.
318 @param[out] HmacSha256Context Pointer to HMAC-SHA256 context.
319 @param[in] Key Pointer to the user-supplied key.
320 @param[in] KeySize Key size in bytes.
322 @retval TRUE The Key is set successfully.
323 @retval FALSE The Key is set unsuccessfully.
329 OUT VOID
*HmacSha256Context
,
334 return HmacMdSetKey (EVP_sha256 (), HmacSha256Context
, Key
, KeySize
);
338 Makes a copy of an existing HMAC-SHA256 context.
340 If HmacSha256Context is NULL, then return FALSE.
341 If NewHmacSha256Context is NULL, then return FALSE.
343 @param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.
344 @param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.
346 @retval TRUE HMAC-SHA256 context copy succeeded.
347 @retval FALSE HMAC-SHA256 context copy failed.
352 HmacSha256Duplicate (
353 IN CONST VOID
*HmacSha256Context
,
354 OUT VOID
*NewHmacSha256Context
357 return HmacMdDuplicate (HmacSha256Context
, NewHmacSha256Context
);
361 Digests the input data and updates HMAC-SHA256 context.
363 This function performs HMAC-SHA256 digest on a data buffer of the specified size.
364 It can be called multiple times to compute the digest of long or discontinuous data streams.
365 HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized
366 by HmacSha256Final(). Behavior with invalid context is undefined.
368 If HmacSha256Context is NULL, then return FALSE.
370 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
371 @param[in] Data Pointer to the buffer containing the data to be digested.
372 @param[in] DataSize Size of Data buffer in bytes.
374 @retval TRUE HMAC-SHA256 data digest succeeded.
375 @retval FALSE HMAC-SHA256 data digest failed.
381 IN OUT VOID
*HmacSha256Context
,
386 return HmacMdUpdate (HmacSha256Context
, Data
, DataSize
);
390 Completes computation of the HMAC-SHA256 digest value.
392 This function completes HMAC-SHA256 hash computation and retrieves the digest value into
393 the specified memory. After this function has been called, the HMAC-SHA256 context cannot
395 HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized
396 by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined.
398 If HmacSha256Context is NULL, then return FALSE.
399 If HmacValue is NULL, then return FALSE.
401 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
402 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA256 digest
405 @retval TRUE HMAC-SHA256 digest computation succeeded.
406 @retval FALSE HMAC-SHA256 digest computation failed.
412 IN OUT VOID
*HmacSha256Context
,
416 return HmacMdFinal (HmacSha256Context
, HmacValue
);
420 Computes the HMAC-SHA256 digest of a input data buffer.
422 This function performs the HMAC-SHA256 digest of a given data buffer, and places
423 the digest value into the specified memory.
425 If this interface is not supported, then return FALSE.
427 @param[in] Data Pointer to the buffer containing the data to be digested.
428 @param[in] DataSize Size of Data buffer in bytes.
429 @param[in] Key Pointer to the user-supplied key.
430 @param[in] KeySize Key size in bytes.
431 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA256 digest
434 @retval TRUE HMAC-SHA256 digest computation succeeded.
435 @retval FALSE HMAC-SHA256 digest computation failed.
436 @retval FALSE This interface is not supported.
449 return HmacMdAll (EVP_sha256 (), Data
, DataSize
, Key
, KeySize
, HmacValue
);
453 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA384 use.
455 @return Pointer to the HMAC_CTX context that has been initialized.
456 If the allocations fails, HmacSha384New() returns NULL.
469 Release the specified HMAC_CTX context.
471 @param[in] HmacSha384Ctx Pointer to the HMAC_CTX context to be released.
477 IN VOID
*HmacSha384Ctx
480 HmacMdFree (HmacSha384Ctx
);
484 Set user-supplied key for subsequent use. It must be done before any
485 calling to HmacSha384Update().
487 If HmacSha384Context is NULL, then return FALSE.
488 If this interface is not supported, then return FALSE.
490 @param[out] HmacSha384Context Pointer to HMAC-SHA384 context.
491 @param[in] Key Pointer to the user-supplied key.
492 @param[in] KeySize Key size in bytes.
494 @retval TRUE The Key is set successfully.
495 @retval FALSE The Key is set unsuccessfully.
496 @retval FALSE This interface is not supported.
502 OUT VOID
*HmacSha384Context
,
507 return HmacMdSetKey (EVP_sha384 (), HmacSha384Context
, Key
, KeySize
);
511 Makes a copy of an existing HMAC-SHA384 context.
513 If HmacSha384Context is NULL, then return FALSE.
514 If NewHmacSha384Context is NULL, then return FALSE.
515 If this interface is not supported, then return FALSE.
517 @param[in] HmacSha384Context Pointer to HMAC-SHA384 context being copied.
518 @param[out] NewHmacSha384Context Pointer to new HMAC-SHA384 context.
520 @retval TRUE HMAC-SHA384 context copy succeeded.
521 @retval FALSE HMAC-SHA384 context copy failed.
522 @retval FALSE This interface is not supported.
527 HmacSha384Duplicate (
528 IN CONST VOID
*HmacSha384Context
,
529 OUT VOID
*NewHmacSha384Context
532 return HmacMdDuplicate (HmacSha384Context
, NewHmacSha384Context
);
536 Digests the input data and updates HMAC-SHA384 context.
538 This function performs HMAC-SHA384 digest on a data buffer of the specified size.
539 It can be called multiple times to compute the digest of long or discontinuous data streams.
540 HMAC-SHA384 context should be initialized by HmacSha384New(), and should not be finalized
541 by HmacSha384Final(). Behavior with invalid context is undefined.
543 If HmacSha384Context is NULL, then return FALSE.
544 If this interface is not supported, then return FALSE.
546 @param[in, out] HmacSha384Context Pointer to the HMAC-SHA384 context.
547 @param[in] Data Pointer to the buffer containing the data to be digested.
548 @param[in] DataSize Size of Data buffer in bytes.
550 @retval TRUE HMAC-SHA384 data digest succeeded.
551 @retval FALSE HMAC-SHA384 data digest failed.
552 @retval FALSE This interface is not supported.
558 IN OUT VOID
*HmacSha384Context
,
563 return HmacMdUpdate (HmacSha384Context
, Data
, DataSize
);
567 Completes computation of the HMAC-SHA384 digest value.
569 This function completes HMAC-SHA384 hash computation and retrieves the digest value into
570 the specified memory. After this function has been called, the HMAC-SHA384 context cannot
572 HMAC-SHA384 context should be initialized by HmacSha384New(), and should not be finalized
573 by HmacSha384Final(). Behavior with invalid HMAC-SHA384 context is undefined.
575 If HmacSha384Context is NULL, then return FALSE.
576 If HmacValue is NULL, then return FALSE.
577 If this interface is not supported, then return FALSE.
579 @param[in, out] HmacSha384Context Pointer to the HMAC-SHA384 context.
580 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA384 digest
583 @retval TRUE HMAC-SHA384 digest computation succeeded.
584 @retval FALSE HMAC-SHA384 digest computation failed.
585 @retval FALSE This interface is not supported.
591 IN OUT VOID
*HmacSha384Context
,
595 return HmacMdFinal (HmacSha384Context
, HmacValue
);
599 Computes the HMAC-SHA384 digest of a input data buffer.
601 This function performs the HMAC-SHA384 digest of a given data buffer, and places
602 the digest value into the specified memory.
604 If this interface is not supported, then return FALSE.
606 @param[in] Data Pointer to the buffer containing the data to be digested.
607 @param[in] DataSize Size of Data buffer in bytes.
608 @param[in] Key Pointer to the user-supplied key.
609 @param[in] KeySize Key size in bytes.
610 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA384 digest
613 @retval TRUE HMAC-SHA384 digest computation succeeded.
614 @retval FALSE HMAC-SHA384 digest computation failed.
615 @retval FALSE This interface is not supported.
628 return HmacMdAll (EVP_sha384 (), Data
, DataSize
, Key
, KeySize
, HmacValue
);