CryptoPkg: Update HMAC Wrapper with opaque HMAC_CTX object.

OpenSSL-1.1.xx makes most data structures opaque.
This patch updated HMAC Wrapper implementation with opaque
HMAC_CTX object.
The HmacXXGetContextSize() is marked as deprecated, and updated
to use the fixed HMAC_CTX size, which is just kept for compatibility.
New APIs (HmacXXNew(), HmacXXFree()) were added  as the recommended
HMAC_CTX usage interfaces for HMAC-XXXX operations.

Cc: Ting Ye <ting.ye@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Gary Lin <glin@suse.com>
Cc: Ronald Cron <ronald.cron@arm.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Qin Long <qin.long@intel.com>
Reviewed-by: Ting Ye <ting.ye@intel.com>
Tested-by: Laszlo Ersek <lersek@redhat.com>
This commit is contained in:
Qin Long
2017-03-23 20:47:34 +08:00
parent ec7ad9e103
commit 4c27024399
7 changed files with 411 additions and 29 deletions

View File

@@ -1,7 +1,7 @@
/** @file
HMAC-MD5 Wrapper Implementation over OpenSSL.
Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>
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 <openssl/hmac.h>
#define HMAC_MD5_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-MD5 operations.
(NOTE: This API is deprecated.
Use HmacMd5New() / HmacMd5Free() for HMAC-MD5 Context operations.)
@return The size, in bytes, of the context buffer required for HMAC-MD5 operations.
@@ -29,8 +34,49 @@ HmacMd5GetContextSize (
{
//
// Retrieves the OpenSSL HMAC-MD5 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 HmacMd5GetContextSize() in future, and use HmacMd5New()
// and HmacMd5Free() for context allocation and release.
//
return (UINTN) (sizeof (HMAC_CTX));
return (UINTN) HMAC_MD5_CTX_SIZE;
}
/**
Allocates and initializes one HMAC_CTX context for subsequent HMAC-MD5 use.
@return Pointer to the HMAC_CTX context that has been initialized.
If the allocations fails, HmacMd5New() returns NULL.
**/
VOID *
EFIAPI
HmacMd5New (
VOID
)
{
//
// Allocates & Initializes HMAC_CTX Context by OpenSSL HMAC_CTX_new()
//
return (VOID *) HMAC_CTX_new ();
}
/**
Release the specified HMAC_CTX context.
@param[in] HmacMd5Ctx Pointer to the HMAC_CTX context to be released.
**/
VOID
EFIAPI
HmacMd5Free (
IN VOID *HmacMd5Ctx
)
{
//
// Free OpenSSL HMAC_CTX Context
//
HMAC_CTX_free ((HMAC_CTX *)HmacMd5Ctx);
}
/**
@@ -65,8 +111,13 @@ HmacMd5Init (
//
// OpenSSL HMAC-MD5 Context Initialization
//
HMAC_CTX_init (HmacMd5Context);
HMAC_Init_ex (HmacMd5Context, Key, (UINT32) KeySize, EVP_md5(), NULL);
memset(HmacMd5Context, 0, HMAC_MD5_CTX_SIZE);
if (HMAC_CTX_reset ((HMAC_CTX *)HmacMd5Context) != 1) {
return FALSE;
}
if (HMAC_Init_ex ((HMAC_CTX *)HmacMd5Context, Key, (UINT32) KeySize, EVP_md5(), NULL) != 1) {
return FALSE;
}
return TRUE;
}
@@ -97,8 +148,10 @@ HmacMd5Duplicate (
if (HmacMd5Context == NULL || NewHmacMd5Context == NULL) {
return FALSE;
}
CopyMem (NewHmacMd5Context, HmacMd5Context, sizeof (HMAC_CTX));
if (HMAC_CTX_copy ((HMAC_CTX *)NewHmacMd5Context, (HMAC_CTX *)HmacMd5Context) != 1) {
return FALSE;
}
return TRUE;
}
@@ -146,7 +199,9 @@ HmacMd5Update (
//
// OpenSSL HMAC-MD5 digest update
//
HMAC_Update (HmacMd5Context, Data, DataSize);
if (HMAC_Update ((HMAC_CTX *)HmacMd5Context, Data, DataSize) != 1) {
return FALSE;
}
return TRUE;
}
@@ -190,8 +245,12 @@ HmacMd5Final (
//
// OpenSSL HMAC-MD5 digest finalization
//
HMAC_Final (HmacMd5Context, HmacValue, &Length);
HMAC_CTX_cleanup (HmacMd5Context);
if (HMAC_Final ((HMAC_CTX *)HmacMd5Context, HmacValue, &Length) != 1) {
return FALSE;
}
if (HMAC_CTX_reset ((HMAC_CTX *)HmacMd5Context) != 1) {
return FALSE;
}
return TRUE;
}