]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacSha256.c
3fdef064f93a4e80ffcfc79674b685aaa1962c6b
2 HMAC-SHA256 Wrapper Implementation over OpenSSL.
4 Copyright (c) 2016 - 2020, 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-SHA256 use.
15 @return Pointer to the HMAC_CTX context that has been initialized.
16 If the allocations fails, HmacSha256New() 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] HmacSha256Ctx Pointer to the HMAC_CTX context to be released.
40 IN VOID
*HmacSha256Ctx
44 // Free OpenSSL HMAC_CTX Context
46 HMAC_CTX_free ((HMAC_CTX
*)HmacSha256Ctx
);
50 Set user-supplied key for subsequent use. It must be done before any
51 calling to HmacSha256Update().
53 If HmacSha256Context is NULL, then return FALSE.
55 @param[out] HmacSha256Context Pointer to HMAC-SHA256 context.
56 @param[in] Key Pointer to the user-supplied key.
57 @param[in] KeySize Key size in bytes.
59 @retval TRUE The Key is set successfully.
60 @retval FALSE The Key is set unsuccessfully.
66 OUT VOID
*HmacSha256Context
,
72 // Check input parameters.
74 if (HmacSha256Context
== NULL
|| KeySize
> INT_MAX
) {
78 if (HMAC_Init_ex ((HMAC_CTX
*)HmacSha256Context
, Key
, (UINT32
) KeySize
, EVP_sha256(), NULL
) != 1) {
86 Makes a copy of an existing HMAC-SHA256 context.
88 If HmacSha256Context is NULL, then return FALSE.
89 If NewHmacSha256Context is NULL, then return FALSE.
91 @param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.
92 @param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.
94 @retval TRUE HMAC-SHA256 context copy succeeded.
95 @retval FALSE HMAC-SHA256 context copy failed.
100 HmacSha256Duplicate (
101 IN CONST VOID
*HmacSha256Context
,
102 OUT VOID
*NewHmacSha256Context
106 // Check input parameters.
108 if (HmacSha256Context
== NULL
|| NewHmacSha256Context
== NULL
) {
112 if (HMAC_CTX_copy ((HMAC_CTX
*)NewHmacSha256Context
, (HMAC_CTX
*)HmacSha256Context
) != 1) {
120 Digests the input data and updates HMAC-SHA256 context.
122 This function performs HMAC-SHA256 digest on a data buffer of the specified size.
123 It can be called multiple times to compute the digest of long or discontinuous data streams.
124 HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized
125 by HmacSha256Final(). Behavior with invalid context is undefined.
127 If HmacSha256Context is NULL, then return FALSE.
129 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
130 @param[in] Data Pointer to the buffer containing the data to be digested.
131 @param[in] DataSize Size of Data buffer in bytes.
133 @retval TRUE HMAC-SHA256 data digest succeeded.
134 @retval FALSE HMAC-SHA256 data digest failed.
140 IN OUT VOID
*HmacSha256Context
,
146 // Check input parameters.
148 if (HmacSha256Context
== NULL
) {
153 // Check invalid parameters, in case that only DataLength was checked in OpenSSL
155 if (Data
== NULL
&& DataSize
!= 0) {
160 // OpenSSL HMAC-SHA256 digest update
162 if (HMAC_Update ((HMAC_CTX
*)HmacSha256Context
, Data
, DataSize
) != 1) {
170 Completes computation of the HMAC-SHA256 digest value.
172 This function completes HMAC-SHA256 hash computation and retrieves the digest value into
173 the specified memory. After this function has been called, the HMAC-SHA256 context cannot
175 HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized
176 by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined.
178 If HmacSha256Context is NULL, then return FALSE.
179 If HmacValue is NULL, then return FALSE.
181 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
182 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA256 digest
185 @retval TRUE HMAC-SHA256 digest computation succeeded.
186 @retval FALSE HMAC-SHA256 digest computation failed.
192 IN OUT VOID
*HmacSha256Context
,
199 // Check input parameters.
201 if (HmacSha256Context
== NULL
|| HmacValue
== NULL
) {
206 // OpenSSL HMAC-SHA256 digest finalization
208 if (HMAC_Final ((HMAC_CTX
*)HmacSha256Context
, HmacValue
, &Length
) != 1) {
211 if (HMAC_CTX_reset ((HMAC_CTX
*)HmacSha256Context
) != 1) {