]>
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>
12 #define HMAC_MD5_CTX_SIZE sizeof(void *) * 4 + sizeof(unsigned int) + \
13 sizeof(unsigned char) * HMAC_MAX_MD_CBLOCK
16 Retrieves the size, in bytes, of the context buffer required for HMAC-MD5 operations.
17 (NOTE: This API is deprecated.
18 Use HmacMd5New() / HmacMd5Free() for HMAC-MD5 Context operations.)
20 @return The size, in bytes, of the context buffer required for HMAC-MD5 operations.
25 HmacMd5GetContextSize (
30 // Retrieves the OpenSSL HMAC-MD5 Context Size
31 // NOTE: HMAC_CTX object was made opaque in openssl-1.1.x, here we just use the
32 // fixed size as a workaround to make this API work for compatibility.
33 // We should retire HmacMd5GetContextSize() in future, and use HmacMd5New()
34 // and HmacMd5Free() for context allocation and release.
36 return (UINTN
) HMAC_MD5_CTX_SIZE
;
40 Allocates and initializes one HMAC_CTX context for subsequent HMAC-MD5 use.
42 @return Pointer to the HMAC_CTX context that has been initialized.
43 If the allocations fails, HmacMd5New() returns NULL.
53 // Allocates & Initializes HMAC_CTX Context by OpenSSL HMAC_CTX_new()
55 return (VOID
*) HMAC_CTX_new ();
59 Release the specified HMAC_CTX context.
61 @param[in] HmacMd5Ctx Pointer to the HMAC_CTX context to be released.
71 // Free OpenSSL HMAC_CTX Context
73 HMAC_CTX_free ((HMAC_CTX
*)HmacMd5Ctx
);
77 Initializes user-supplied memory pointed by HmacMd5Context as HMAC-MD5 context for
80 If HmacMd5Context is NULL, then return FALSE.
82 @param[out] HmacMd5Context Pointer to HMAC-MD5 context being initialized.
83 @param[in] Key Pointer to the user-supplied key.
84 @param[in] KeySize Key size in bytes.
86 @retval TRUE HMAC-MD5 context initialization succeeded.
87 @retval FALSE HMAC-MD5 context initialization failed.
93 OUT VOID
*HmacMd5Context
,
99 // Check input parameters.
101 if (HmacMd5Context
== NULL
|| KeySize
> INT_MAX
) {
106 // OpenSSL HMAC-MD5 Context Initialization
108 memset(HmacMd5Context
, 0, HMAC_MD5_CTX_SIZE
);
109 if (HMAC_CTX_reset ((HMAC_CTX
*)HmacMd5Context
) != 1) {
112 if (HMAC_Init_ex ((HMAC_CTX
*)HmacMd5Context
, Key
, (UINT32
) KeySize
, EVP_md5(), NULL
) != 1) {
120 Makes a copy of an existing HMAC-MD5 context.
122 If HmacMd5Context is NULL, then return FALSE.
123 If NewHmacMd5Context is NULL, then return FALSE.
125 @param[in] HmacMd5Context Pointer to HMAC-MD5 context being copied.
126 @param[out] NewHmacMd5Context Pointer to new HMAC-MD5 context.
128 @retval TRUE HMAC-MD5 context copy succeeded.
129 @retval FALSE HMAC-MD5 context copy failed.
135 IN CONST VOID
*HmacMd5Context
,
136 OUT VOID
*NewHmacMd5Context
140 // Check input parameters.
142 if (HmacMd5Context
== NULL
|| NewHmacMd5Context
== NULL
) {
146 if (HMAC_CTX_copy ((HMAC_CTX
*)NewHmacMd5Context
, (HMAC_CTX
*)HmacMd5Context
) != 1) {
154 Digests the input data and updates HMAC-MD5 context.
156 This function performs HMAC-MD5 digest on a data buffer of the specified size.
157 It can be called multiple times to compute the digest of long or discontinuous data streams.
158 HMAC-MD5 context should be already correctly initialized by HmacMd5Init(), and should not be
159 finalized by HmacMd5Final(). Behavior with invalid context is undefined.
161 If HmacMd5Context is NULL, then return FALSE.
163 @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.
164 @param[in] Data Pointer to the buffer containing the data to be digested.
165 @param[in] DataSize Size of Data buffer in bytes.
167 @retval TRUE HMAC-MD5 data digest succeeded.
168 @retval FALSE HMAC-MD5 data digest failed.
174 IN OUT VOID
*HmacMd5Context
,
180 // Check input parameters.
182 if (HmacMd5Context
== NULL
) {
187 // Check invalid parameters, in case that only DataLength was checked in OpenSSL
189 if (Data
== NULL
&& DataSize
!= 0) {
194 // OpenSSL HMAC-MD5 digest update
196 if (HMAC_Update ((HMAC_CTX
*)HmacMd5Context
, Data
, DataSize
) != 1) {
204 Completes computation of the HMAC-MD5 digest value.
206 This function completes HMAC-MD5 digest computation and retrieves the digest value into
207 the specified memory. After this function has been called, the HMAC-MD5 context cannot
209 HMAC-MD5 context should be already correctly initialized by HmacMd5Init(), and should not be
210 finalized by HmacMd5Final(). Behavior with invalid HMAC-MD5 context is undefined.
212 If HmacMd5Context is NULL, then return FALSE.
213 If HmacValue is NULL, then return FALSE.
215 @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.
216 @param[out] HmacValue Pointer to a buffer that receives the HMAC-MD5 digest
219 @retval TRUE HMAC-MD5 digest computation succeeded.
220 @retval FALSE HMAC-MD5 digest computation failed.
226 IN OUT VOID
*HmacMd5Context
,
233 // Check input parameters.
235 if (HmacMd5Context
== NULL
|| HmacValue
== NULL
) {
240 // OpenSSL HMAC-MD5 digest finalization
242 if (HMAC_Final ((HMAC_CTX
*)HmacMd5Context
, HmacValue
, &Length
) != 1) {
245 if (HMAC_CTX_reset ((HMAC_CTX
*)HmacMd5Context
) != 1) {