]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacSha1.c
2 HMAC-SHA1 Wrapper Implementation over OpenSSL.
4 Copyright (c) 2010 - 2017, 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>
18 #define HMAC_SHA1_CTX_SIZE sizeof(void *) * 4 + sizeof(unsigned int) + \
19 sizeof(unsigned char) * HMAC_MAX_MD_CBLOCK
22 Retrieves the size, in bytes, of the context buffer required for HMAC-SHA1 operations.
23 (NOTE: This API is deprecated.
24 Use HmacSha1New() / HmacSha1Free() for HMAC-SHA1 Context operations.)
26 @return The size, in bytes, of the context buffer required for HMAC-SHA1 operations.
31 HmacSha1GetContextSize (
36 // Retrieves the OpenSSL HMAC-SHA1 Context Size
37 // NOTE: HMAC_CTX object was made opaque in openssl-1.1.x, here we just use the
38 // fixed size as a workaround to make this API work for compatibility.
39 // We should retire HmacSha15GetContextSize() in future, and use HmacSha1New()
40 // and HmacSha1Free() for context allocation and release.
42 return (UINTN
) HMAC_SHA1_CTX_SIZE
;
46 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA1 use.
48 @return Pointer to the HMAC_CTX context that has been initialized.
49 If the allocations fails, HmacSha1New() returns NULL.
59 // Allocates & Initializes HMAC_CTX Context by OpenSSL HMAC_CTX_new()
61 return (VOID
*) HMAC_CTX_new ();
65 Release the specified HMAC_CTX context.
67 @param[in] HmacSha1Ctx Pointer to the HMAC_CTX context to be released.
77 // Free OpenSSL HMAC_CTX Context
79 HMAC_CTX_free ((HMAC_CTX
*)HmacSha1Ctx
);
83 Initializes user-supplied memory pointed by HmacSha1Context as HMAC-SHA1 context for
86 If HmacSha1Context is NULL, then return FALSE.
88 @param[out] HmacSha1Context Pointer to HMAC-SHA1 context being initialized.
89 @param[in] Key Pointer to the user-supplied key.
90 @param[in] KeySize Key size in bytes.
92 @retval TRUE HMAC-SHA1 context initialization succeeded.
93 @retval FALSE HMAC-SHA1 context initialization failed.
99 OUT VOID
*HmacSha1Context
,
105 // Check input parameters.
107 if (HmacSha1Context
== NULL
|| KeySize
> INT_MAX
) {
112 // OpenSSL HMAC-SHA1 Context Initialization
114 memset(HmacSha1Context
, 0, HMAC_SHA1_CTX_SIZE
);
115 if (HMAC_CTX_reset ((HMAC_CTX
*)HmacSha1Context
) != 1) {
118 if (HMAC_Init_ex ((HMAC_CTX
*)HmacSha1Context
, Key
, (UINT32
) KeySize
, EVP_sha1(), NULL
) != 1) {
126 Makes a copy of an existing HMAC-SHA1 context.
128 If HmacSha1Context is NULL, then return FALSE.
129 If NewHmacSha1Context is NULL, then return FALSE.
131 @param[in] HmacSha1Context Pointer to HMAC-SHA1 context being copied.
132 @param[out] NewHmacSha1Context Pointer to new HMAC-SHA1 context.
134 @retval TRUE HMAC-SHA1 context copy succeeded.
135 @retval FALSE HMAC-SHA1 context copy failed.
141 IN CONST VOID
*HmacSha1Context
,
142 OUT VOID
*NewHmacSha1Context
146 // Check input parameters.
148 if (HmacSha1Context
== NULL
|| NewHmacSha1Context
== NULL
) {
152 if (HMAC_CTX_copy ((HMAC_CTX
*)NewHmacSha1Context
, (HMAC_CTX
*)HmacSha1Context
) != 1) {
160 Digests the input data and updates HMAC-SHA1 context.
162 This function performs HMAC-SHA1 digest on a data buffer of the specified size.
163 It can be called multiple times to compute the digest of long or discontinuous data streams.
164 HMAC-SHA1 context should be already correctly initialized by HmacSha1Init(), and should not
165 be finalized by HmacSha1Final(). Behavior with invalid context is undefined.
167 If HmacSha1Context is NULL, then return FALSE.
169 @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.
170 @param[in] Data Pointer to the buffer containing the data to be digested.
171 @param[in] DataSize Size of Data buffer in bytes.
173 @retval TRUE HMAC-SHA1 data digest succeeded.
174 @retval FALSE HMAC-SHA1 data digest failed.
180 IN OUT VOID
*HmacSha1Context
,
186 // Check input parameters.
188 if (HmacSha1Context
== NULL
) {
193 // Check invalid parameters, in case that only DataLength was checked in OpenSSL
195 if (Data
== NULL
&& DataSize
!= 0) {
200 // OpenSSL HMAC-SHA1 digest update
202 if (HMAC_Update ((HMAC_CTX
*)HmacSha1Context
, Data
, DataSize
) != 1) {
210 Completes computation of the HMAC-SHA1 digest value.
212 This function completes HMAC-SHA1 digest computation and retrieves the digest value into
213 the specified memory. After this function has been called, the HMAC-SHA1 context cannot
215 HMAC-SHA1 context should be already correctly initialized by HmacSha1Init(), and should
216 not be finalized by HmacSha1Final(). Behavior with invalid HMAC-SHA1 context is undefined.
218 If HmacSha1Context is NULL, then return FALSE.
219 If HmacValue is NULL, then return FALSE.
221 @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.
222 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA1 digest
225 @retval TRUE HMAC-SHA1 digest computation succeeded.
226 @retval FALSE HMAC-SHA1 digest computation failed.
232 IN OUT VOID
*HmacSha1Context
,
239 // Check input parameters.
241 if (HmacSha1Context
== NULL
|| HmacValue
== NULL
) {
246 // OpenSSL HMAC-SHA1 digest finalization
248 if (HMAC_Final ((HMAC_CTX
*)HmacSha1Context
, HmacValue
, &Length
) != 1) {
251 if (HMAC_CTX_reset ((HMAC_CTX
*)HmacSha1Context
) != 1) {