]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacSha256.c
5d349dc0850bae5bf582b6ac0806f7df49181145
2 HMAC-SHA256 Wrapper Implementation over OpenSSL.
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "InternalCryptLib.h"
16 #include <openssl/hmac.h>
19 Retrieves the size, in bytes, of the context buffer required for HMAC-SHA256 operations.
21 @return The size, in bytes, of the context buffer required for HMAC-SHA256 operations.
26 HmacSha256GetContextSize (
31 // Retrieves the OpenSSL HMAC-SHA256 Context Size
33 return (UINTN
) (sizeof (HMAC_CTX
));
37 Initializes user-supplied memory pointed by HmacSha256Context as HMAC-SHA256 context for
40 If HmacSha256Context is NULL, then return FALSE.
42 @param[out] HmacSha256Context Pointer to HMAC-SHA256 context being initialized.
43 @param[in] Key Pointer to the user-supplied key.
44 @param[in] KeySize Key size in bytes.
46 @retval TRUE HMAC-SHA256 context initialization succeeded.
47 @retval FALSE HMAC-SHA256 context initialization failed.
53 OUT VOID
*HmacSha256Context
,
59 // Check input parameters.
61 if (HmacSha256Context
== NULL
|| KeySize
> INT_MAX
) {
66 // OpenSSL HMAC-SHA256 Context Initialization
68 HMAC_CTX_init (HmacSha256Context
);
69 HMAC_Init_ex (HmacSha256Context
, Key
, (UINT32
) KeySize
, EVP_sha256(), NULL
);
75 Makes a copy of an existing HMAC-SHA256 context.
77 If HmacSha256Context is NULL, then return FALSE.
78 If NewHmacSha256Context is NULL, then return FALSE.
80 @param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.
81 @param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.
83 @retval TRUE HMAC-SHA256 context copy succeeded.
84 @retval FALSE HMAC-SHA256 context copy failed.
90 IN CONST VOID
*HmacSha256Context
,
91 OUT VOID
*NewHmacSha256Context
95 // Check input parameters.
97 if (HmacSha256Context
== NULL
|| NewHmacSha256Context
== NULL
) {
101 CopyMem (NewHmacSha256Context
, HmacSha256Context
, sizeof (HMAC_CTX
));
107 Digests the input data and updates HMAC-SHA256 context.
109 This function performs HMAC-SHA256 digest on a data buffer of the specified size.
110 It can be called multiple times to compute the digest of long or discontinuous data streams.
111 HMAC-SHA256 context should be already correctly initialized by HmacSha256Init(), and should not
112 be finalized by HmacSha256Final(). Behavior with invalid context is undefined.
114 If HmacSha256Context is NULL, then return FALSE.
116 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
117 @param[in] Data Pointer to the buffer containing the data to be digested.
118 @param[in] DataSize Size of Data buffer in bytes.
120 @retval TRUE HMAC-SHA256 data digest succeeded.
121 @retval FALSE HMAC-SHA256 data digest failed.
127 IN OUT VOID
*HmacSha256Context
,
133 // Check input parameters.
135 if (HmacSha256Context
== NULL
) {
140 // Check invalid parameters, in case that only DataLength was checked in OpenSSL
142 if (Data
== NULL
&& DataSize
!= 0) {
147 // OpenSSL HMAC-SHA256 digest update
149 HMAC_Update (HmacSha256Context
, Data
, DataSize
);
155 Completes computation of the HMAC-SHA256 digest value.
157 This function completes HMAC-SHA256 hash computation and retrieves the digest value into
158 the specified memory. After this function has been called, the HMAC-SHA256 context cannot
160 HMAC-SHA256 context should be already correctly initialized by HmacSha256Init(), and should
161 not be finalized by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined.
163 If HmacSha256Context is NULL, then return FALSE.
164 If HashValue is NULL, then return FALSE.
166 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
167 @param[out] HashValue Pointer to a buffer that receives the HMAC-SHA256 digest
170 @retval TRUE HMAC-SHA256 digest computation succeeded.
171 @retval FALSE HMAC-SHA256 digest computation failed.
177 IN OUT VOID
*HmacSha256Context
,
184 // Check input parameters.
186 if (HmacSha256Context
== NULL
|| HmacValue
== NULL
) {
191 // OpenSSL HMAC-SHA256 digest finalization
193 HMAC_Final (HmacSha256Context
, HmacValue
, &Length
);
194 HMAC_CTX_cleanup (HmacSha256Context
);