]>
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.
26 // Allocates & Initializes HMAC_CTX Context by OpenSSL HMAC_CTX_new()
28 return (VOID
*)HMAC_CTX_new ();
32 Release the specified HMAC_CTX context.
34 @param[in] HmacMdCtx Pointer to the HMAC_CTX context to be released.
44 // Free OpenSSL HMAC_CTX Context
46 HMAC_CTX_free ((HMAC_CTX
*)HmacMdCtx
);
50 Set user-supplied key for subsequent use. It must be done before any
51 calling to HmacMdUpdate().
53 If HmacMdContext is NULL, then return FALSE.
55 @param[in] Md Message Digest.
56 @param[out] HmacMdContext Pointer to HMAC-MD context.
57 @param[in] Key Pointer to the user-supplied key.
58 @param[in] KeySize Key size in bytes.
60 @retval TRUE The Key is set successfully.
61 @retval FALSE The Key is set unsuccessfully.
68 OUT VOID
*HmacMdContext
,
74 // Check input parameters.
76 if ((HmacMdContext
== NULL
) || (KeySize
> INT_MAX
)) {
80 if (HMAC_Init_ex ((HMAC_CTX
*)HmacMdContext
, Key
, (UINT32
)KeySize
, Md
, NULL
) != 1) {
88 Makes a copy of an existing HMAC-MD context.
90 If HmacMdContext is NULL, then return FALSE.
91 If NewHmacMdContext is NULL, then return FALSE.
93 @param[in] HmacMdContext Pointer to HMAC-MD context being copied.
94 @param[out] NewHmacMdContext Pointer to new HMAC-MD context.
96 @retval TRUE HMAC-MD context copy succeeded.
97 @retval FALSE HMAC-MD context copy failed.
103 IN CONST VOID
*HmacMdContext
,
104 OUT VOID
*NewHmacMdContext
108 // Check input parameters.
110 if ((HmacMdContext
== NULL
) || (NewHmacMdContext
== NULL
)) {
114 if (HMAC_CTX_copy ((HMAC_CTX
*)NewHmacMdContext
, (HMAC_CTX
*)HmacMdContext
) != 1) {
122 Digests the input data and updates HMAC-MD context.
124 This function performs HMAC-MD digest on a data buffer of the specified size.
125 It can be called multiple times to compute the digest of long or discontinuous data streams.
126 HMAC-MD context should be initialized by HmacMdNew(), and should not be finalized
127 by HmacMdFinal(). Behavior with invalid context is undefined.
129 If HmacMdContext is NULL, then return FALSE.
131 @param[in, out] HmacMdContext Pointer to the HMAC-MD context.
132 @param[in] Data Pointer to the buffer containing the data to be digested.
133 @param[in] DataSize Size of Data buffer in bytes.
135 @retval TRUE HMAC-MD data digest succeeded.
136 @retval FALSE HMAC-MD data digest failed.
142 IN OUT VOID
*HmacMdContext
,
148 // Check input parameters.
150 if (HmacMdContext
== NULL
) {
155 // Check invalid parameters, in case that only DataLength was checked in OpenSSL
157 if ((Data
== NULL
) && (DataSize
!= 0)) {
162 // OpenSSL HMAC-MD digest update
164 if (HMAC_Update ((HMAC_CTX
*)HmacMdContext
, Data
, DataSize
) != 1) {
172 Completes computation of the HMAC-MD digest value.
174 This function completes HMAC-MD hash computation and retrieves the digest value into
175 the specified memory. After this function has been called, the HMAC-MD context cannot
177 HMAC-MD context should be initialized by HmacMdNew(), and should not be finalized
178 by HmacMdFinal(). Behavior with invalid HMAC-MD context is undefined.
180 If HmacMdContext is NULL, then return FALSE.
181 If HmacValue is NULL, then return FALSE.
183 @param[in, out] HmacMdContext Pointer to the HMAC-MD context.
184 @param[out] HmacValue Pointer to a buffer that receives the HMAC-MD digest
187 @retval TRUE HMAC-MD digest computation succeeded.
188 @retval FALSE HMAC-MD digest computation failed.
194 IN OUT VOID
*HmacMdContext
,
201 // Check input parameters.
203 if ((HmacMdContext
== NULL
) || (HmacValue
== NULL
)) {
208 // OpenSSL HMAC-MD digest finalization
210 if (HMAC_Final ((HMAC_CTX
*)HmacMdContext
, HmacValue
, &Length
) != 1) {
214 if (HMAC_CTX_reset ((HMAC_CTX
*)HmacMdContext
) != 1) {
222 Computes the HMAC-MD digest of a input data buffer.
224 This function performs the HMAC-MD digest of a given data buffer, and places
225 the digest value into the specified memory.
227 If this interface is not supported, then return FALSE.
229 @param[in] Md Message Digest.
230 @param[in] Data Pointer to the buffer containing the data to be digested.
231 @param[in] DataSize Size of Data buffer in bytes.
232 @param[in] Key Pointer to the user-supplied key.
233 @param[in] KeySize Key size in bytes.
234 @param[out] HmacValue Pointer to a buffer that receives the HMAC-MD digest
237 @retval TRUE HMAC-MD digest computation succeeded.
238 @retval FALSE HMAC-MD digest computation failed.
239 @retval FALSE This interface is not supported.
257 Ctx
= HMAC_CTX_new ();
262 RetVal
= (BOOLEAN
)HMAC_CTX_reset (Ctx
);
267 RetVal
= (BOOLEAN
)HMAC_Init_ex (Ctx
, Key
, (UINT32
)KeySize
, Md
, NULL
);
272 RetVal
= (BOOLEAN
)HMAC_Update (Ctx
, Data
, DataSize
);
277 RetVal
= (BOOLEAN
)HMAC_Final (Ctx
, HmacValue
, &Length
);
289 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA256 use.
291 @return Pointer to the HMAC_CTX context that has been initialized.
292 If the allocations fails, HmacSha256New() returns NULL.
305 Release the specified HMAC_CTX context.
307 @param[in] HmacSha256Ctx Pointer to the HMAC_CTX context to be released.
313 IN VOID
*HmacSha256Ctx
316 HmacMdFree (HmacSha256Ctx
);
320 Set user-supplied key for subsequent use. It must be done before any
321 calling to HmacSha256Update().
323 If HmacSha256Context is NULL, then return FALSE.
325 @param[out] HmacSha256Context Pointer to HMAC-SHA256 context.
326 @param[in] Key Pointer to the user-supplied key.
327 @param[in] KeySize Key size in bytes.
329 @retval TRUE The Key is set successfully.
330 @retval FALSE The Key is set unsuccessfully.
336 OUT VOID
*HmacSha256Context
,
341 return HmacMdSetKey (EVP_sha256 (), HmacSha256Context
, Key
, KeySize
);
345 Makes a copy of an existing HMAC-SHA256 context.
347 If HmacSha256Context is NULL, then return FALSE.
348 If NewHmacSha256Context is NULL, then return FALSE.
350 @param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.
351 @param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.
353 @retval TRUE HMAC-SHA256 context copy succeeded.
354 @retval FALSE HMAC-SHA256 context copy failed.
359 HmacSha256Duplicate (
360 IN CONST VOID
*HmacSha256Context
,
361 OUT VOID
*NewHmacSha256Context
364 return HmacMdDuplicate (HmacSha256Context
, NewHmacSha256Context
);
368 Digests the input data and updates HMAC-SHA256 context.
370 This function performs HMAC-SHA256 digest on a data buffer of the specified size.
371 It can be called multiple times to compute the digest of long or discontinuous data streams.
372 HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized
373 by HmacSha256Final(). Behavior with invalid context is undefined.
375 If HmacSha256Context is NULL, then return FALSE.
377 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
378 @param[in] Data Pointer to the buffer containing the data to be digested.
379 @param[in] DataSize Size of Data buffer in bytes.
381 @retval TRUE HMAC-SHA256 data digest succeeded.
382 @retval FALSE HMAC-SHA256 data digest failed.
388 IN OUT VOID
*HmacSha256Context
,
393 return HmacMdUpdate (HmacSha256Context
, Data
, DataSize
);
397 Completes computation of the HMAC-SHA256 digest value.
399 This function completes HMAC-SHA256 hash computation and retrieves the digest value into
400 the specified memory. After this function has been called, the HMAC-SHA256 context cannot
402 HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized
403 by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined.
405 If HmacSha256Context is NULL, then return FALSE.
406 If HmacValue is NULL, then return FALSE.
408 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
409 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA256 digest
412 @retval TRUE HMAC-SHA256 digest computation succeeded.
413 @retval FALSE HMAC-SHA256 digest computation failed.
419 IN OUT VOID
*HmacSha256Context
,
423 return HmacMdFinal (HmacSha256Context
, HmacValue
);
427 Computes the HMAC-SHA256 digest of a input data buffer.
429 This function performs the HMAC-SHA256 digest of a given data buffer, and places
430 the digest value into the specified memory.
432 If this interface is not supported, then return FALSE.
434 @param[in] Data Pointer to the buffer containing the data to be digested.
435 @param[in] DataSize Size of Data buffer in bytes.
436 @param[in] Key Pointer to the user-supplied key.
437 @param[in] KeySize Key size in bytes.
438 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA256 digest
441 @retval TRUE HMAC-SHA256 digest computation succeeded.
442 @retval FALSE HMAC-SHA256 digest computation failed.
443 @retval FALSE This interface is not supported.
456 return HmacMdAll (EVP_sha256 (), Data
, DataSize
, Key
, KeySize
, HmacValue
);
460 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA384 use.
462 @return Pointer to the HMAC_CTX context that has been initialized.
463 If the allocations fails, HmacSha384New() returns NULL.
476 Release the specified HMAC_CTX context.
478 @param[in] HmacSha384Ctx Pointer to the HMAC_CTX context to be released.
484 IN VOID
*HmacSha384Ctx
487 HmacMdFree (HmacSha384Ctx
);
491 Set user-supplied key for subsequent use. It must be done before any
492 calling to HmacSha384Update().
494 If HmacSha384Context is NULL, then return FALSE.
495 If this interface is not supported, then return FALSE.
497 @param[out] HmacSha384Context Pointer to HMAC-SHA384 context.
498 @param[in] Key Pointer to the user-supplied key.
499 @param[in] KeySize Key size in bytes.
501 @retval TRUE The Key is set successfully.
502 @retval FALSE The Key is set unsuccessfully.
503 @retval FALSE This interface is not supported.
509 OUT VOID
*HmacSha384Context
,
514 return HmacMdSetKey (EVP_sha384 (), HmacSha384Context
, Key
, KeySize
);
518 Makes a copy of an existing HMAC-SHA384 context.
520 If HmacSha384Context is NULL, then return FALSE.
521 If NewHmacSha384Context is NULL, then return FALSE.
522 If this interface is not supported, then return FALSE.
524 @param[in] HmacSha384Context Pointer to HMAC-SHA384 context being copied.
525 @param[out] NewHmacSha384Context Pointer to new HMAC-SHA384 context.
527 @retval TRUE HMAC-SHA384 context copy succeeded.
528 @retval FALSE HMAC-SHA384 context copy failed.
529 @retval FALSE This interface is not supported.
534 HmacSha384Duplicate (
535 IN CONST VOID
*HmacSha384Context
,
536 OUT VOID
*NewHmacSha384Context
539 return HmacMdDuplicate (HmacSha384Context
, NewHmacSha384Context
);
543 Digests the input data and updates HMAC-SHA384 context.
545 This function performs HMAC-SHA384 digest on a data buffer of the specified size.
546 It can be called multiple times to compute the digest of long or discontinuous data streams.
547 HMAC-SHA384 context should be initialized by HmacSha384New(), and should not be finalized
548 by HmacSha384Final(). Behavior with invalid context is undefined.
550 If HmacSha384Context is NULL, then return FALSE.
551 If this interface is not supported, then return FALSE.
553 @param[in, out] HmacSha384Context Pointer to the HMAC-SHA384 context.
554 @param[in] Data Pointer to the buffer containing the data to be digested.
555 @param[in] DataSize Size of Data buffer in bytes.
557 @retval TRUE HMAC-SHA384 data digest succeeded.
558 @retval FALSE HMAC-SHA384 data digest failed.
559 @retval FALSE This interface is not supported.
565 IN OUT VOID
*HmacSha384Context
,
570 return HmacMdUpdate (HmacSha384Context
, Data
, DataSize
);
574 Completes computation of the HMAC-SHA384 digest value.
576 This function completes HMAC-SHA384 hash computation and retrieves the digest value into
577 the specified memory. After this function has been called, the HMAC-SHA384 context cannot
579 HMAC-SHA384 context should be initialized by HmacSha384New(), and should not be finalized
580 by HmacSha384Final(). Behavior with invalid HMAC-SHA384 context is undefined.
582 If HmacSha384Context is NULL, then return FALSE.
583 If HmacValue is NULL, then return FALSE.
584 If this interface is not supported, then return FALSE.
586 @param[in, out] HmacSha384Context Pointer to the HMAC-SHA384 context.
587 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA384 digest
590 @retval TRUE HMAC-SHA384 digest computation succeeded.
591 @retval FALSE HMAC-SHA384 digest computation failed.
592 @retval FALSE This interface is not supported.
598 IN OUT VOID
*HmacSha384Context
,
602 return HmacMdFinal (HmacSha384Context
, HmacValue
);
606 Computes the HMAC-SHA384 digest of a input data buffer.
608 This function performs the HMAC-SHA384 digest of a given data buffer, and places
609 the digest value into the specified memory.
611 If this interface is not supported, then return FALSE.
613 @param[in] Data Pointer to the buffer containing the data to be digested.
614 @param[in] DataSize Size of Data buffer in bytes.
615 @param[in] Key Pointer to the user-supplied key.
616 @param[in] KeySize Key size in bytes.
617 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA384 digest
620 @retval TRUE HMAC-SHA384 digest computation succeeded.
621 @retval FALSE HMAC-SHA384 digest computation failed.
622 @retval FALSE This interface is not supported.
635 return HmacMdAll (EVP_sha384 (), Data
, DataSize
, Key
, KeySize
, HmacValue
);