X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=CryptoPkg%2FLibrary%2FBaseCryptLib%2FHmac%2FCryptHmacSha256.c;h=d6b3ae4883297f09d2e220487f1a736dd7493a51;hp=5d349dc0850bae5bf582b6ac0806f7df49181145;hb=4c270243995a4cba33955f6dfaa8ee93748ff843;hpb=72009c626d8b1c237c0de840e9bfe9d23b76b94c diff --git a/CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacSha256.c b/CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacSha256.c index 5d349dc085..d6b3ae4883 100644 --- a/CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacSha256.c +++ b/CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacSha256.c @@ -1,7 +1,7 @@ /** @file HMAC-SHA256 Wrapper Implementation over OpenSSL. -Copyright (c) 2016, Intel Corporation. All rights reserved.
+Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -15,8 +15,13 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "InternalCryptLib.h" #include +#define HMAC_SHA256_CTX_SIZE sizeof(void *) * 4 + sizeof(unsigned int) + \ + sizeof(unsigned char) * HMAC_MAX_MD_CBLOCK + /** Retrieves the size, in bytes, of the context buffer required for HMAC-SHA256 operations. + (NOTE: This API is deprecated. + Use HmacSha256New() / HmacSha256Free() for HMAC-SHA256 Context operations.) @return The size, in bytes, of the context buffer required for HMAC-SHA256 operations. @@ -29,8 +34,49 @@ HmacSha256GetContextSize ( { // // Retrieves the OpenSSL HMAC-SHA256 Context Size + // NOTE: HMAC_CTX object was made opaque in openssl-1.1.x, here we just use the + // fixed size as a workaround to make this API work for compatibility. + // We should retire HmacSha256GetContextSize() in future, and use HmacSha256New() + // and HmacSha256Free() for context allocation and release. + // + return (UINTN)HMAC_SHA256_CTX_SIZE; +} + +/** + Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA256 use. + + @return Pointer to the HMAC_CTX context that has been initialized. + If the allocations fails, HmacSha256New() returns NULL. + +**/ +VOID * +EFIAPI +HmacSha256New ( + VOID + ) +{ + // + // Allocates & Initializes HMAC_CTX Context by OpenSSL HMAC_CTX_new() + // + return (VOID *) HMAC_CTX_new (); +} + +/** + Release the specified HMAC_CTX context. + + @param[in] HmacSha256Ctx Pointer to the HMAC_CTX context to be released. + +**/ +VOID +EFIAPI +HmacSha256Free ( + IN VOID *HmacSha256Ctx + ) +{ // - return (UINTN) (sizeof (HMAC_CTX)); + // Free OpenSSL HMAC_CTX Context + // + HMAC_CTX_free ((HMAC_CTX *)HmacSha256Ctx); } /** @@ -65,8 +111,13 @@ HmacSha256Init ( // // OpenSSL HMAC-SHA256 Context Initialization // - HMAC_CTX_init (HmacSha256Context); - HMAC_Init_ex (HmacSha256Context, Key, (UINT32) KeySize, EVP_sha256(), NULL); + memset(HmacSha256Context, 0, HMAC_SHA256_CTX_SIZE); + if (HMAC_CTX_reset ((HMAC_CTX *)HmacSha256Context) != 1) { + return FALSE; + } + if (HMAC_Init_ex ((HMAC_CTX *)HmacSha256Context, Key, (UINT32) KeySize, EVP_sha256(), NULL) != 1) { + return FALSE; + } return TRUE; } @@ -98,7 +149,9 @@ HmacSha256Duplicate ( return FALSE; } - CopyMem (NewHmacSha256Context, HmacSha256Context, sizeof (HMAC_CTX)); + if (HMAC_CTX_copy ((HMAC_CTX *)NewHmacSha256Context, (HMAC_CTX *)HmacSha256Context) != 1) { + return FALSE; + } return TRUE; } @@ -146,7 +199,9 @@ HmacSha256Update ( // // OpenSSL HMAC-SHA256 digest update // - HMAC_Update (HmacSha256Context, Data, DataSize); + if (HMAC_Update ((HMAC_CTX *)HmacSha256Context, Data, DataSize) != 1) { + return FALSE; + } return TRUE; } @@ -161,10 +216,10 @@ HmacSha256Update ( not be finalized by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined. If HmacSha256Context is NULL, then return FALSE. - If HashValue is NULL, then return FALSE. + If HmacValue is NULL, then return FALSE. @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context. - @param[out] HashValue Pointer to a buffer that receives the HMAC-SHA256 digest + @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA256 digest value (32 bytes). @retval TRUE HMAC-SHA256 digest computation succeeded. @@ -190,8 +245,12 @@ HmacSha256Final ( // // OpenSSL HMAC-SHA256 digest finalization // - HMAC_Final (HmacSha256Context, HmacValue, &Length); - HMAC_CTX_cleanup (HmacSha256Context); + if (HMAC_Final ((HMAC_CTX *)HmacSha256Context, HmacValue, &Length) != 1) { + return FALSE; + } + if (HMAC_CTX_reset ((HMAC_CTX *)HmacSha256Context) != 1) { + return FALSE; + } return TRUE; }