Import upstream version 0.9+1465500757.14a59055

parent a14921c5
/** @file
SHA-384 and SHA-512 Digest Wrapper Implementations over OpenSSL.
Copyright (c) 2014, 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/sha.h>
/**
Retrieves the size, in bytes, of the context buffer required for SHA-384 hash operations.
@return The size, in bytes, of the context buffer required for SHA-384 hash operations.
**/
UINTN
EFIAPI
Sha384GetContextSize (
VOID
)
{
//
// Retrieves OpenSSL SHA-384 Context Size
//
return (UINTN) (sizeof (SHA512_CTX));
}
/**
Initializes user-supplied memory pointed by Sha384Context as SHA-384 hash context for
subsequent use.
If Sha384Context is NULL, then return FALSE.
@param[out] Sha384Context Pointer to SHA-384 context being initialized.
@retval TRUE SHA-384 context initialization succeeded.
@retval FALSE SHA-384 context initialization failed.
**/
BOOLEAN
EFIAPI
Sha384Init (
OUT VOID *Sha384Context
)
{
//
// Check input parameters.
//
if (Sha384Context == NULL) {
return FALSE;
}
//
// OpenSSL SHA-384 Context Initialization
//
return (BOOLEAN) (SHA384_Init ((SHA512_CTX *) Sha384Context));
}
/**
Makes a copy of an existing SHA-384 context.
If Sha384Context is NULL, then return FALSE.
If NewSha384Context is NULL, then return FALSE.
If this interface is not supported, then return FALSE.
@param[in] Sha384Context Pointer to SHA-384 context being copied.
@param[out] NewSha384Context Pointer to new SHA-384 context.
@retval TRUE SHA-384 context copy succeeded.
@retval FALSE SHA-384 context copy failed.
@retval FALSE This interface is not supported.
**/
BOOLEAN
EFIAPI
Sha384Duplicate (
IN CONST VOID *Sha384Context,
OUT VOID *NewSha384Context
)
{
//
// Check input parameters.
//
if (Sha384Context == NULL || NewSha384Context == NULL) {
return FALSE;
}
CopyMem (NewSha384Context, Sha384Context, sizeof (SHA512_CTX));
return TRUE;
}
/**
Digests the input data and updates SHA-384 context.
This function performs SHA-384 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.
SHA-384 context should be already correctly intialized by Sha384Init(), and should not be finalized
by Sha384Final(). Behavior with invalid context is undefined.
If Sha384Context is NULL, then return FALSE.
@param[in, out] Sha384Context Pointer to the SHA-384 context.
@param[in] Data Pointer to the buffer containing the data to be hashed.
@param[in] DataSize Size of Data buffer in bytes.
@retval TRUE SHA-384 data digest succeeded.
@retval FALSE SHA-384 data digest failed.
**/
BOOLEAN
EFIAPI
Sha384Update (
IN OUT VOID *Sha384Context,
IN CONST VOID *Data,
IN UINTN DataSize
)
{
//
// Check input parameters.
//
if (Sha384Context == NULL) {
return FALSE;
}
//
// Check invalid parameters, in case that only DataLength was checked in OpenSSL
//
if (Data == NULL && DataSize != 0) {
return FALSE;
}
//
// OpenSSL SHA-384 Hash Update
//
return (BOOLEAN) (SHA384_Update ((SHA512_CTX *) Sha384Context, Data, DataSize));
}
/**
Completes computation of the SHA-384 digest value.
This function completes SHA-384 hash computation and retrieves the digest value into
the specified memory. After this function has been called, the SHA-384 context cannot
be used again.
SHA-384 context should be already correctly intialized by Sha384Init(), and should not be
finalized by Sha384Final(). Behavior with invalid SHA-384 context is undefined.
If Sha384Context is NULL, then return FALSE.
If HashValue is NULL, then return FALSE.
@param[in, out] Sha384Context Pointer to the SHA-384 context.
@param[out] HashValue Pointer to a buffer that receives the SHA-384 digest
value (48 bytes).
@retval TRUE SHA-384 digest computation succeeded.
@retval FALSE SHA-384 digest computation failed.
**/
BOOLEAN
EFIAPI
Sha384Final (
IN OUT VOID *Sha384Context,
OUT UINT8 *HashValue
)
{
//
// Check input parameters.
//
if (Sha384Context == NULL || HashValue == NULL) {
return FALSE;
}
//
// OpenSSL SHA-384 Hash Finalization
//
return (BOOLEAN) (SHA384_Final (HashValue, (SHA512_CTX *) Sha384Context));
}
/**
Retrieves the size, in bytes, of the context buffer required for SHA-512 hash operations.
@return The size, in bytes, of the context buffer required for SHA-512 hash operations.
**/
UINTN
EFIAPI
Sha512GetContextSize (
VOID
)
{
//
// Retrieves OpenSSL SHA-512 Context Size
//
return (UINTN) (sizeof (SHA512_CTX));
}
/**
Initializes user-supplied memory pointed by Sha512Context as SHA-512 hash context for
subsequent use.
If Sha512Context is NULL, then return FALSE.
@param[out] Sha512Context Pointer to SHA-512 context being initialized.
@retval TRUE SHA-512 context initialization succeeded.
@retval FALSE SHA-512 context initialization failed.
**/
BOOLEAN
EFIAPI
Sha512Init (
OUT VOID *Sha512Context
)
{
//
// Check input parameters.
//
if (Sha512Context == NULL) {
return FALSE;
}
//
// OpenSSL SHA-512 Context Initialization
//
return (BOOLEAN) (SHA512_Init ((SHA512_CTX *) Sha512Context));
}
/**
Makes a copy of an existing SHA-512 context.
If Sha512Context is NULL, then return FALSE.
If NewSha512Context is NULL, then return FALSE.
If this interface is not supported, then return FALSE.
@param[in] Sha512Context Pointer to SHA-512 context being copied.
@param[out] NewSha512Context Pointer to new SHA-512 context.
@retval TRUE SHA-512 context copy succeeded.
@retval FALSE SHA-512 context copy failed.
@retval FALSE This interface is not supported.
**/
BOOLEAN
EFIAPI
Sha512Duplicate (
IN CONST VOID *Sha512Context,
OUT VOID *NewSha512Context
)
{
//
// Check input parameters.
//
if (Sha512Context == NULL || NewSha512Context == NULL) {
return FALSE;
}
CopyMem (NewSha512Context, Sha512Context, sizeof (SHA512_CTX));
return TRUE;
}
/**
Digests the input data and updates SHA-512 context.
This function performs SHA-512 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.
SHA-512 context should be already correctly intialized by Sha512Init(), and should not be finalized
by Sha512Final(). Behavior with invalid context is undefined.
If Sha512Context is NULL, then return FALSE.
@param[in, out] Sha512Context Pointer to the SHA-512 context.
@param[in] Data Pointer to the buffer containing the data to be hashed.
@param[in] DataSize Size of Data buffer in bytes.
@retval TRUE SHA-512 data digest succeeded.
@retval FALSE SHA-512 data digest failed.
**/
BOOLEAN
EFIAPI
Sha512Update (
IN OUT VOID *Sha512Context,
IN CONST VOID *Data,
IN UINTN DataSize
)
{
//
// Check input parameters.
//
if (Sha512Context == NULL) {
return FALSE;
}
//
// Check invalid parameters, in case that only DataLength was checked in OpenSSL
//
if (Data == NULL && DataSize != 0) {
return FALSE;
}
//
// OpenSSL SHA-512 Hash Update
//
return (BOOLEAN) (SHA512_Update ((SHA512_CTX *) Sha512Context, Data, DataSize));
}
/**
Completes computation of the SHA-512 digest value.
This function completes SHA-512 hash computation and retrieves the digest value into
the specified memory. After this function has been called, the SHA-512 context cannot
be used again.
SHA-512 context should be already correctly intialized by Sha512Init(), and should not be
finalized by Sha512Final(). Behavior with invalid SHA-512 context is undefined.
If Sha512Context is NULL, then return FALSE.
If HashValue is NULL, then return FALSE.
@param[in, out] Sha512Context Pointer to the SHA-512 context.
@param[out] HashValue Pointer to a buffer that receives the SHA-512 digest
value (64 bytes).
@retval TRUE SHA-512 digest computation succeeded.
@retval FALSE SHA-512 digest computation failed.
**/
BOOLEAN
EFIAPI
Sha512Final (
IN OUT VOID *Sha512Context,
OUT UINT8 *HashValue
)
{
//
// Check input parameters.
//
if (Sha512Context == NULL || HashValue == NULL) {
return FALSE;
}
//
// OpenSSL SHA-512 Hash Finalization
//
return (BOOLEAN) (SHA384_Final (HashValue, (SHA512_CTX *) Sha512Context));
}
......@@ -34,7 +34,7 @@ typedef VOID *FILE;
//
// Map all va_xxxx elements to VA_xxx defined in MdePkg/Include/Base.h
//
#if !defined(__CC_ARM) // if va_list is not already defined
#if !defined(__CC_ARM) || defined(_STDARG_H) // if va_list is not already defined
/*
* These are now unconditionally #defined by GNU_EFI's efistdarg.h,
* so we should #undef them here before providing a new definition.
......@@ -47,6 +47,9 @@ typedef VOID *FILE;
#define va_arg VA_ARG
#define va_start VA_START
#define va_end VA_END
# if !defined(NO_BUILTIN_VA_FUNCS)
typedef __builtin_va_list VA_LIST;
#define VA_START(Marker, Parameter) __builtin_va_start (Marker, Parameter)
......@@ -57,6 +60,78 @@ typedef __builtin_va_list VA_LIST;
#define VA_COPY(Dest, Start) __builtin_va_copy (Dest, Start)
# else
#define _INT_SIZE_OF(n) ((sizeof (n) + sizeof (UINTN) - 1) &~(sizeof (UINTN) - 1))
///
/// Variable used to traverse the list of arguments. This type can vary by
/// implementation and could be an array or structure.
///
typedef CHAR8 *VA_LIST;
/**
Retrieves a pointer to the beginning of a variable argument list, based on
the name of the parameter that immediately precedes the variable argument list.
This function initializes Marker to point to the beginning of the variable
argument list that immediately follows Parameter. The method for computing the
pointer to the next argument in the argument list is CPU-specific following the
EFIAPI ABI.
@param Marker The VA_LIST used to traverse the list of arguments.
@param Parameter The name of the parameter that immediately precedes
the variable argument list.
@return A pointer to the beginning of a variable argument list.
**/
#define VA_START(Marker, Parameter) (Marker = (VA_LIST) ((UINTN) & (Parameter) + _INT_SIZE_OF (Parameter)))
/**
Returns an argument of a specified type from a variable argument list and updates
the pointer to the variable argument list to point to the next argument.
This function returns an argument of the type specified by TYPE from the beginning
of the variable argument list specified by Marker. Marker is then updated to point
to the next argument in the variable argument list. The method for computing the
pointer to the next argument in the argument list is CPU-specific following the EFIAPI ABI.
@param Marker VA_LIST used to traverse the list of arguments.
@param TYPE The type of argument to retrieve from the beginning
of the variable argument list.
@return An argument of the type specified by TYPE.
**/
#define VA_ARG(Marker, TYPE) (*(TYPE *) ((Marker += _INT_SIZE_OF (TYPE)) - _INT_SIZE_OF (TYPE)))
/**
Terminates the use of a variable argument list.
This function initializes Marker so it can no longer be used with VA_ARG().
After this macro is used, the only way to access the variable argument list is
by using VA_START() again.
@param Marker VA_LIST used to traverse the list of arguments.