CryptoPkg: Add HMAC-SHA256 cipher support

Add new HMAC-SHA256 cipher support in CryptoPkg to meet more security
and industry requirements,

and update Cryptest utility to include new HMAC-SHA256 test case.

Cc: Ting Ye <ting.ye@intel.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>
This commit is contained in:
Qin Long 2016-10-31 15:43:48 +08:00
parent b7d1ba0a8a
commit 72009c626d
8 changed files with 510 additions and 9 deletions

View File

@ -1,7 +1,7 @@
/** @file
Application for HMAC Primitives Validation.
Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2010 - 2016, 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
@ -54,6 +54,22 @@ GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha1Digest[] = {
0xf1, 0x46, 0xbe, 0x00
};
//
// Key value for HMAC-SHA-256 validation. (From "4. Test Vectors" of IETF RFC4231)
//
GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha256Key[20] = {
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b
};
//
// Result for HMAC-SHA-256 ("Hi There"). (From "4. Test Vectors" of IETF RFC4231)
//
GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha256Digest[] = {
0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
};
/**
Validate UEFI-OpenSSL Message Authentication Codes Interfaces.
@ -73,7 +89,7 @@ ValidateCryptHmac (
Print (L" \nUEFI-OpenSSL HMAC Engine Testing:\n");
Print (L"- HMAC-MD5: ");
Print (L"- HMAC-MD5: ");
//
// HMAC-MD5 Digest Validation
@ -113,7 +129,7 @@ ValidateCryptHmac (
Print (L"[Pass]\n");
Print (L"- HMAC-SHA1: ");
Print (L"- HMAC-SHA1: ");
//
// HMAC-SHA1 Digest Validation
@ -153,5 +169,44 @@ ValidateCryptHmac (
Print (L"[Pass]\n");
Print (L"- HMAC-SHA256: ");
//
// HMAC-SHA-256 Digest Validation
//
ZeroMem (Digest, MAX_DIGEST_SIZE);
CtxSize = HmacSha256GetContextSize ();
HmacCtx = AllocatePool (CtxSize);
Print (L"Init... ");
Status = HmacSha256Init (HmacCtx, HmacSha256Key, sizeof (HmacSha256Key));
if (!Status) {
Print (L"[Fail]");
return EFI_ABORTED;
}
Print (L"Update... ");
Status = HmacSha256Update (HmacCtx, HmacData, 8);
if (!Status) {
Print (L"[Fail]");
return EFI_ABORTED;
}
Print (L"Finalize... ");
Status = HmacSha256Final (HmacCtx, Digest);
if (!Status) {
Print (L"[Fail]");
return EFI_ABORTED;
}
FreePool (HmacCtx);
Print (L"Check Value... ");
if (CompareMem (Digest, HmacSha256Digest, SHA256_DIGEST_SIZE) != 0) {
Print (L"[Fail]");
return EFI_ABORTED;
}
Print (L"[Pass]\n");
return EFI_SUCCESS;
}

View File

@ -1131,6 +1131,124 @@ HmacSha1Final (
OUT UINT8 *HmacValue
);
/**
Retrieves the size, in bytes, of the context buffer required for HMAC-SHA256 operations.
If this interface is not supported, then return zero.
@return The size, in bytes, of the context buffer required for HMAC-SHA256 operations.
@retval 0 This interface is not supported.
**/
UINTN
EFIAPI
HmacSha256GetContextSize (
VOID
);
/**
Initializes user-supplied memory pointed by HmacSha256Context as HMAC-SHA256 context for
subsequent use.
If HmacSha256Context is NULL, then return FALSE.
If this interface is not supported, then return FALSE.
@param[out] HmacSha256Context Pointer to HMAC-SHA256 context being initialized.
@param[in] Key Pointer to the user-supplied key.
@param[in] KeySize Key size in bytes.
@retval TRUE HMAC-SHA256 context initialization succeeded.
@retval FALSE HMAC-SHA256 context initialization failed.
@retval FALSE This interface is not supported.
**/
BOOLEAN
EFIAPI
HmacSha256Init (
OUT VOID *HmacSha256Context,
IN CONST UINT8 *Key,
IN UINTN KeySize
);
/**
Makes a copy of an existing HMAC-SHA256 context.
If HmacSha256Context is NULL, then return FALSE.
If NewHmacSha256Context is NULL, then return FALSE.
If this interface is not supported, then return FALSE.
@param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.
@param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.
@retval TRUE HMAC-SHA256 context copy succeeded.
@retval FALSE HMAC-SHA256 context copy failed.
@retval FALSE This interface is not supported.
**/
BOOLEAN
EFIAPI
HmacSha256Duplicate (
IN CONST VOID *HmacSha256Context,
OUT VOID *NewHmacSha256Context
);
/**
Digests the input data and updates HMAC-SHA256 context.
This function performs HMAC-SHA256 digest on a data buffer of the specified size.
It can be called multiple times to compute the digest of long or discontinuous data streams.
HMAC-SHA256 context should be already correctly initialized by HmacSha256Init(), and should not
be finalized by HmacSha256Final(). Behavior with invalid context is undefined.
If HmacSha256Context is NULL, then return FALSE.
If this interface is not supported, then return FALSE.
@param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
@param[in] Data Pointer to the buffer containing the data to be digested.
@param[in] DataSize Size of Data buffer in bytes.
@retval TRUE HMAC-SHA256 data digest succeeded.
@retval FALSE HMAC-SHA256 data digest failed.
@retval FALSE This interface is not supported.
**/
BOOLEAN
EFIAPI
HmacSha256Update (
IN OUT VOID *HmacSha256Context,
IN CONST VOID *Data,
IN UINTN DataSize
);
/**
Completes computation of the HMAC-SHA256 digest value.
This function completes HMAC-SHA256 hash computation and retrieves the digest value into
the specified memory. After this function has been called, the HMAC-SHA256 context cannot
be used again.
HMAC-SHA256 context should be already correctly initialized by HmacSha256Init(), and should
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 this interface is not supported, 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
value (32 bytes).
@retval TRUE HMAC-SHA256 digest computation succeeded.
@retval FALSE HMAC-SHA256 digest computation failed.
@retval FALSE This interface is not supported.
**/
BOOLEAN
EFIAPI
HmacSha256Final (
IN OUT VOID *HmacSha256Context,
OUT UINT8 *HmacValue
);
//=====================================================================================
// Symmetric Cryptography Primitive
//=====================================================================================

View File

@ -6,7 +6,7 @@
# This external input must be validated carefully to avoid security issues such as
# buffer overflow or integer overflow.
#
# Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
# Copyright (c) 2009 - 2016, 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
@ -41,6 +41,7 @@
Hash/CryptSha512.c
Hmac/CryptHmacMd5.c
Hmac/CryptHmacSha1.c
Hmac/CryptHmacSha256.c
Cipher/CryptAes.c
Cipher/CryptTdes.c
Cipher/CryptArc4.c

View File

@ -0,0 +1,197 @@
/** @file
HMAC-SHA256 Wrapper Implementation over OpenSSL.
Copyright (c) 2016, 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
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "InternalCryptLib.h"
#include <openssl/hmac.h>
/**
Retrieves the size, in bytes, of the context buffer required for HMAC-SHA256 operations.
@return The size, in bytes, of the context buffer required for HMAC-SHA256 operations.
**/
UINTN
EFIAPI
HmacSha256GetContextSize (
VOID
)
{
//
// Retrieves the OpenSSL HMAC-SHA256 Context Size
//
return (UINTN) (sizeof (HMAC_CTX));
}
/**
Initializes user-supplied memory pointed by HmacSha256Context as HMAC-SHA256 context for
subsequent use.
If HmacSha256Context is NULL, then return FALSE.
@param[out] HmacSha256Context Pointer to HMAC-SHA256 context being initialized.
@param[in] Key Pointer to the user-supplied key.
@param[in] KeySize Key size in bytes.
@retval TRUE HMAC-SHA256 context initialization succeeded.
@retval FALSE HMAC-SHA256 context initialization failed.
**/
BOOLEAN
EFIAPI
HmacSha256Init (
OUT VOID *HmacSha256Context,
IN CONST UINT8 *Key,
IN UINTN KeySize
)
{
//
// Check input parameters.
//
if (HmacSha256Context == NULL || KeySize > INT_MAX) {
return FALSE;
}
//
// OpenSSL HMAC-SHA256 Context Initialization
//
HMAC_CTX_init (HmacSha256Context);
HMAC_Init_ex (HmacSha256Context, Key, (UINT32) KeySize, EVP_sha256(), NULL);
return TRUE;
}
/**
Makes a copy of an existing HMAC-SHA256 context.
If HmacSha256Context is NULL, then return FALSE.
If NewHmacSha256Context is NULL, then return FALSE.
@param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.
@param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.
@retval TRUE HMAC-SHA256 context copy succeeded.
@retval FALSE HMAC-SHA256 context copy failed.
**/
BOOLEAN
EFIAPI
HmacSha256Duplicate (
IN CONST VOID *HmacSha256Context,
OUT VOID *NewHmacSha256Context
)
{
//
// Check input parameters.
//
if (HmacSha256Context == NULL || NewHmacSha256Context == NULL) {
return FALSE;
}
CopyMem (NewHmacSha256Context, HmacSha256Context, sizeof (HMAC_CTX));
return TRUE;
}
/**
Digests the input data and updates HMAC-SHA256 context.
This function performs HMAC-SHA256 digest on a data buffer of the specified size.
It can be called multiple times to compute the digest of long or discontinuous data streams.
HMAC-SHA256 context should be already correctly initialized by HmacSha256Init(), and should not
be finalized by HmacSha256Final(). Behavior with invalid context is undefined.
If HmacSha256Context is NULL, then return FALSE.
@param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
@param[in] Data Pointer to the buffer containing the data to be digested.
@param[in] DataSize Size of Data buffer in bytes.
@retval TRUE HMAC-SHA256 data digest succeeded.
@retval FALSE HMAC-SHA256 data digest failed.
**/
BOOLEAN
EFIAPI
HmacSha256Update (
IN OUT VOID *HmacSha256Context,
IN CONST VOID *Data,
IN UINTN DataSize
)
{
//
// Check input parameters.
//
if (HmacSha256Context == NULL) {
return FALSE;
}
//
// Check invalid parameters, in case that only DataLength was checked in OpenSSL
//
if (Data == NULL && DataSize != 0) {
return FALSE;
}
//
// OpenSSL HMAC-SHA256 digest update
//
HMAC_Update (HmacSha256Context, Data, DataSize);
return TRUE;
}
/**
Completes computation of the HMAC-SHA256 digest value.
This function completes HMAC-SHA256 hash computation and retrieves the digest value into
the specified memory. After this function has been called, the HMAC-SHA256 context cannot
be used again.
HMAC-SHA256 context should be already correctly initialized by HmacSha256Init(), and should
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.
@param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
@param[out] HashValue Pointer to a buffer that receives the HMAC-SHA256 digest
value (32 bytes).
@retval TRUE HMAC-SHA256 digest computation succeeded.
@retval FALSE HMAC-SHA256 digest computation failed.
**/
BOOLEAN
EFIAPI
HmacSha256Final (
IN OUT VOID *HmacSha256Context,
OUT UINT8 *HmacValue
)
{
UINT32 Length;
//
// Check input parameters.
//
if (HmacSha256Context == NULL || HmacValue == NULL) {
return FALSE;
}
//
// OpenSSL HMAC-SHA256 digest finalization
//
HMAC_Final (HmacSha256Context, HmacValue, &Length);
HMAC_CTX_cleanup (HmacSha256Context);
return TRUE;
}

View File

@ -0,0 +1,127 @@
/** @file
HMAC-SHA256 Wrapper Implementation which does not provide real capabilities.
Copyright (c) 2016, 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
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "InternalCryptLib.h"
/**
Retrieves the size, in bytes, of the context buffer required for HMAC-SHA256 operations.
Return zero to indicate this interface is not supported.
@retval 0 This interface is not supported.
**/
UINTN
EFIAPI
HmacSha256GetContextSize (
VOID
)
{
ASSERT (FALSE);
return 0;
}
/**
Initializes user-supplied memory pointed by HmacSha256Context as HMAC-SHA256 context for
subsequent use.
Return FALSE to indicate this interface is not supported.
@param[out] HmacSha256Context Pointer to HMAC-SHA256 context being initialized.
@param[in] Key Pointer to the user-supplied key.
@param[in] KeySize Key size in bytes.
@retval FALSE This interface is not supported.
**/
BOOLEAN
EFIAPI
HmacSha256Init (
OUT VOID *HmacSha256Context,
IN CONST UINT8 *Key,
IN UINTN KeySize
)
{
ASSERT (FALSE);
return FALSE;
}
/**
Makes a copy of an existing HMAC-SHA256 context.
Return FALSE to indicate this interface is not supported.
@param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.
@param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.
@retval FALSE This interface is not supported.
**/
BOOLEAN
EFIAPI
HmacSha256Duplicate (
IN CONST VOID *HmacSha256Context,
OUT VOID *NewHmacSha256Context
)
{
ASSERT (FALSE);
return FALSE;
}
/**
Digests the input data and updates HMAC-SHA256 context.
Return FALSE to indicate this interface is not supported.
@param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
@param[in] Data Pointer to the buffer containing the data to be digested.
@param[in] DataSize Size of Data buffer in bytes.
@retval FALSE This interface is not supported.
**/
BOOLEAN
EFIAPI
HmacSha256Update (
IN OUT VOID *HmacSha256Context,
IN CONST VOID *Data,
IN UINTN DataSize
)
{
ASSERT (FALSE);
return FALSE;
}
/**
Completes computation of the HMAC-SHA256 digest value.
Return FALSE to indicate this interface is not supported.
@param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
@param[out] HashValue Pointer to a buffer that receives the HMAC-SHA256 digest
value (32 bytes).
@retval FALSE This interface is not supported.
**/
BOOLEAN
EFIAPI
HmacSha256Final (
IN OUT VOID *HmacSha256Context,
OUT UINT8 *HmacValue
)
{
ASSERT (FALSE);
return FALSE;
}

View File

@ -7,13 +7,13 @@
# buffer overflow or integer overflow.
#
# Note: MD4 Digest functions, SHA-384 Digest functions, SHA-512 Digest functions,
# HMAC-MD5 functions, HMAC-SHA1 functions, AES/TDES/ARC4 functions, RSA external
# HMAC-MD5 functions, HMAC-SHA1/SHA256 functions, AES/TDES/ARC4 functions, RSA external
# functions, PKCS#7 SignedData sign functions, Diffie-Hellman functions, X.509
# certificate handler functions, authenticode signature verification functions,
# PEM handler functions, and pseudorandom number generator functions are not
# supported in this instance.
#
# Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
# Copyright (c) 2010 - 2016, 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
@ -47,6 +47,7 @@
Hash/CryptSha512Null.c
Hmac/CryptHmacMd5Null.c
Hmac/CryptHmacSha1Null.c
Hmac/CryptHmacSha256Null.c
Cipher/CryptAesNull.c
Cipher/CryptTdesNull.c
Cipher/CryptArc4Null.c

View File

@ -7,11 +7,11 @@
# buffer overflow or integer overflow.
#
# Note: MD4 Digest functions, SHA-384 Digest functions, SHA-512 Digest functions,
# HMAC-MD5 functions, HMAC-SHA1 functions, AES/TDES/ARC4 functions, RSA external
# HMAC-MD5 functions, HMAC-SHA1/SHA256 functions, AES/TDES/ARC4 functions, RSA external
# functions, PKCS#7 SignedData sign functions, Diffie-Hellman functions, and
# authenticode signature verification functions are not supported in this instance.
#
# Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
# Copyright (c) 2009 - 2016, 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
@ -46,6 +46,7 @@
Hash/CryptSha512Null.c
Hmac/CryptHmacMd5Null.c
Hmac/CryptHmacSha1Null.c
Hmac/CryptHmacSha256Null.c
Cipher/CryptAesNull.c
Cipher/CryptTdesNull.c
Cipher/CryptArc4Null.c

View File

@ -7,7 +7,7 @@
# buffer overflow or integer overflow.
#
# Note: MD4 Digest functions, SHA-384 Digest functions, SHA-512 Digest functions,
# HMAC-MD5 functions, HMAC-SHA1 functions, TDES/ARC4 functions, RSA external
# HMAC-MD5 functions, HMAC-SHA1/SHA256 functions, TDES/ARC4 functions, RSA external
# functions, PKCS#7 SignedData sign functions, Diffie-Hellman functions, and
# authenticode signature verification functions are not supported in this instance.
#
@ -46,6 +46,7 @@
Hash/CryptSha512Null.c
Hmac/CryptHmacMd5Null.c
Hmac/CryptHmacSha1Null.c
Hmac/CryptHmacSha256Null.c
Cipher/CryptAes.c
Cipher/CryptTdesNull.c
Cipher/CryptArc4Null.c