]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacMd5.c
2 HMAC-MD5 Wrapper Implementation over OpenSSL.
4 Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "InternalCryptLib.h"
10 #include <openssl/hmac.h>
13 // NOTE: OpenSSL redefines the size of HMAC_CTX at crypto/hmac/hmac_lcl.h
14 // #define HMAC_MAX_MD_CBLOCK_SIZE 144
16 #define HMAC_MD5_CTX_SIZE (sizeof(void *) * 4 + sizeof(unsigned int) + \
17 sizeof(unsigned char) * 144)
20 Retrieves the size, in bytes, of the context buffer required for HMAC-MD5 operations.
21 (NOTE: This API is deprecated.
22 Use HmacMd5New() / HmacMd5Free() for HMAC-MD5 Context operations.)
24 @return The size, in bytes, of the context buffer required for HMAC-MD5 operations.
29 HmacMd5GetContextSize (
34 // Retrieves the OpenSSL HMAC-MD5 Context Size
35 // NOTE: HMAC_CTX object was made opaque in openssl-1.1.x, here we just use the
36 // fixed size as a workaround to make this API work for compatibility.
37 // We should retire HmacMd5GetContextSize() in future, and use HmacMd5New()
38 // and HmacMd5Free() for context allocation and release.
40 return (UINTN
) HMAC_MD5_CTX_SIZE
;
44 Allocates and initializes one HMAC_CTX context for subsequent HMAC-MD5 use.
46 @return Pointer to the HMAC_CTX context that has been initialized.
47 If the allocations fails, HmacMd5New() returns NULL.
57 // Allocates & Initializes HMAC_CTX Context by OpenSSL HMAC_CTX_new()
59 return (VOID
*) HMAC_CTX_new ();
63 Release the specified HMAC_CTX context.
65 @param[in] HmacMd5Ctx Pointer to the HMAC_CTX context to be released.
75 // Free OpenSSL HMAC_CTX Context
77 HMAC_CTX_free ((HMAC_CTX
*)HmacMd5Ctx
);
81 Set user-supplied key for subsequent use. It must be done before any
82 calling to HmacMd5Update().
84 If HmacMd5Context is NULL, then return FALSE.
86 @param[out] HmacMd5Context Pointer to HMAC-MD5 context.
87 @param[in] Key Pointer to the user-supplied key.
88 @param[in] KeySize Key size in bytes.
90 @retval TRUE Key is set successfully.
91 @retval FALSE Key is set unsuccessfully.
97 OUT VOID
*HmacMd5Context
,
103 // Check input parameters.
105 if (HmacMd5Context
== NULL
|| KeySize
> INT_MAX
) {
109 if (HMAC_Init_ex ((HMAC_CTX
*)HmacMd5Context
, Key
, (UINT32
) KeySize
, EVP_md5(), NULL
) != 1) {
117 Makes a copy of an existing HMAC-MD5 context.
119 If HmacMd5Context is NULL, then return FALSE.
120 If NewHmacMd5Context is NULL, then return FALSE.
122 @param[in] HmacMd5Context Pointer to HMAC-MD5 context being copied.
123 @param[out] NewHmacMd5Context Pointer to new HMAC-MD5 context.
125 @retval TRUE HMAC-MD5 context copy succeeded.
126 @retval FALSE HMAC-MD5 context copy failed.
132 IN CONST VOID
*HmacMd5Context
,
133 OUT VOID
*NewHmacMd5Context
137 // Check input parameters.
139 if (HmacMd5Context
== NULL
|| NewHmacMd5Context
== NULL
) {
143 if (HMAC_CTX_copy ((HMAC_CTX
*)NewHmacMd5Context
, (HMAC_CTX
*)HmacMd5Context
) != 1) {
151 Digests the input data and updates HMAC-MD5 context.
153 This function performs HMAC-MD5 digest on a data buffer of the specified size.
154 It can be called multiple times to compute the digest of long or discontinuous data streams.
155 HMAC-MD5 context should be initialized by HmacMd5New(), and should not be finalized by
156 HmacMd5Final(). Behavior with invalid context is undefined.
158 If HmacMd5Context is NULL, then return FALSE.
160 @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.
161 @param[in] Data Pointer to the buffer containing the data to be digested.
162 @param[in] DataSize Size of Data buffer in bytes.
164 @retval TRUE HMAC-MD5 data digest succeeded.
165 @retval FALSE HMAC-MD5 data digest failed.
171 IN OUT VOID
*HmacMd5Context
,
177 // Check input parameters.
179 if (HmacMd5Context
== NULL
) {
184 // Check invalid parameters, in case that only DataLength was checked in OpenSSL
186 if (Data
== NULL
&& DataSize
!= 0) {
191 // OpenSSL HMAC-MD5 digest update
193 if (HMAC_Update ((HMAC_CTX
*)HmacMd5Context
, Data
, DataSize
) != 1) {
201 Completes computation of the HMAC-MD5 digest value.
203 This function completes HMAC-MD5 digest computation and retrieves the digest value into
204 the specified memory. After this function has been called, the HMAC-MD5 context cannot
206 HMAC-MD5 context should be initialized by HmacMd5New(), and should not be finalized by
207 HmacMd5Final(). Behavior with invalid HMAC-MD5 context is undefined.
209 If HmacMd5Context is NULL, then return FALSE.
210 If HmacValue is NULL, then return FALSE.
212 @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.
213 @param[out] HmacValue Pointer to a buffer that receives the HMAC-MD5 digest
216 @retval TRUE HMAC-MD5 digest computation succeeded.
217 @retval FALSE HMAC-MD5 digest computation failed.
223 IN OUT VOID
*HmacMd5Context
,
230 // Check input parameters.
232 if (HmacMd5Context
== NULL
|| HmacValue
== NULL
) {
237 // OpenSSL HMAC-MD5 digest finalization
239 if (HMAC_Final ((HMAC_CTX
*)HmacMd5Context
, HmacValue
, &Length
) != 1) {
242 if (HMAC_CTX_reset ((HMAC_CTX
*)HmacMd5Context
) != 1) {