2010-11-01 07:30:58 +01:00
|
|
|
/** @file
|
|
|
|
SHA-1 Digest Wrapper Implementation over OpenSSL.
|
|
|
|
|
2012-03-19 06:52:16 +01:00
|
|
|
Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
|
2010-11-01 07:30:58 +01:00
|
|
|
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.
|
|
|
|
|
|
|
|
**/
|
|
|
|
|
2010-11-02 07:06:38 +01:00
|
|
|
#include "InternalCryptLib.h"
|
2010-11-01 07:30:58 +01:00
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Retrieves the size, in bytes, of the context buffer required for SHA-1 hash operations.
|
|
|
|
|
|
|
|
@return The size, in bytes, of the context buffer required for SHA-1 hash operations.
|
|
|
|
|
|
|
|
**/
|
|
|
|
UINTN
|
|
|
|
EFIAPI
|
|
|
|
Sha1GetContextSize (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Retrieves OpenSSL SHA Context Size
|
|
|
|
//
|
|
|
|
return (UINTN)(sizeof (SHA_CTX));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-11-02 07:06:38 +01:00
|
|
|
Initializes user-supplied memory pointed by Sha1Context as SHA-1 hash context for
|
2010-11-01 07:30:58 +01:00
|
|
|
subsequent use.
|
|
|
|
|
2012-03-19 06:52:16 +01:00
|
|
|
If Sha1Context is NULL, then return FALSE.
|
2010-11-01 07:30:58 +01:00
|
|
|
|
2010-11-02 07:06:38 +01:00
|
|
|
@param[out] Sha1Context Pointer to SHA-1 context being initialized.
|
2010-11-01 07:30:58 +01:00
|
|
|
|
2010-11-02 07:06:38 +01:00
|
|
|
@retval TRUE SHA-1 context initialization succeeded.
|
|
|
|
@retval FALSE SHA-1 context initialization failed.
|
2010-11-01 07:30:58 +01:00
|
|
|
|
|
|
|
**/
|
|
|
|
BOOLEAN
|
|
|
|
EFIAPI
|
|
|
|
Sha1Init (
|
2010-11-02 07:06:38 +01:00
|
|
|
OUT VOID *Sha1Context
|
2010-11-01 07:30:58 +01:00
|
|
|
)
|
|
|
|
{
|
|
|
|
//
|
2012-03-19 06:52:16 +01:00
|
|
|
// Check input parameters.
|
2010-11-01 07:30:58 +01:00
|
|
|
//
|
2012-03-19 06:52:16 +01:00
|
|
|
if (Sha1Context == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-11-01 07:30:58 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// OpenSSL SHA-1 Context Initialization
|
|
|
|
//
|
|
|
|
return (BOOLEAN) (SHA1_Init ((SHA_CTX *)Sha1Context));
|
|
|
|
}
|
|
|
|
|
2010-11-02 07:06:38 +01:00
|
|
|
/**
|
|
|
|
Makes a copy of an existing SHA-1 context.
|
|
|
|
|
2012-03-19 06:52:16 +01:00
|
|
|
If Sha1Context is NULL, then return FALSE.
|
|
|
|
If NewSha1Context is NULL, then return FALSE.
|
2010-11-02 07:06:38 +01:00
|
|
|
|
|
|
|
@param[in] Sha1Context Pointer to SHA-1 context being copied.
|
|
|
|
@param[out] NewSha1Context Pointer to new SHA-1 context.
|
|
|
|
|
|
|
|
@retval TRUE SHA-1 context copy succeeded.
|
|
|
|
@retval FALSE SHA-1 context copy failed.
|
|
|
|
|
|
|
|
**/
|
|
|
|
BOOLEAN
|
|
|
|
EFIAPI
|
|
|
|
Sha1Duplicate (
|
|
|
|
IN CONST VOID *Sha1Context,
|
|
|
|
OUT VOID *NewSha1Context
|
|
|
|
)
|
|
|
|
{
|
2010-12-31 08:22:48 +01:00
|
|
|
//
|
2012-03-19 06:52:16 +01:00
|
|
|
// Check input parameters.
|
2010-12-31 08:22:48 +01:00
|
|
|
//
|
2012-03-19 06:52:16 +01:00
|
|
|
if (Sha1Context == NULL || NewSha1Context == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-12-31 08:22:48 +01:00
|
|
|
|
2010-11-02 07:06:38 +01:00
|
|
|
CopyMem (NewSha1Context, Sha1Context, sizeof (SHA_CTX));
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2010-11-01 07:30:58 +01:00
|
|
|
|
|
|
|
/**
|
2010-11-02 07:06:38 +01:00
|
|
|
Digests the input data and updates SHA-1 context.
|
|
|
|
|
|
|
|
This function performs SHA-1 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-1 context should be already correctly intialized by Sha1Init(), and should not be finalized
|
|
|
|
by Sha1Final(). Behavior with invalid context is undefined.
|
2010-11-01 07:30:58 +01:00
|
|
|
|
2012-03-19 06:52:16 +01:00
|
|
|
If Sha1Context is NULL, then return FALSE.
|
2010-11-01 07:30:58 +01:00
|
|
|
|
|
|
|
@param[in, out] Sha1Context Pointer to the SHA-1 context.
|
|
|
|
@param[in] Data Pointer to the buffer containing the data to be hashed.
|
2010-11-02 07:06:38 +01:00
|
|
|
@param[in] DataSize Size of Data buffer in bytes.
|
2010-11-01 07:30:58 +01:00
|
|
|
|
|
|
|
@retval TRUE SHA-1 data digest succeeded.
|
2010-11-02 07:06:38 +01:00
|
|
|
@retval FALSE SHA-1 data digest failed.
|
2010-11-01 07:30:58 +01:00
|
|
|
|
|
|
|
**/
|
|
|
|
BOOLEAN
|
|
|
|
EFIAPI
|
|
|
|
Sha1Update (
|
|
|
|
IN OUT VOID *Sha1Context,
|
|
|
|
IN CONST VOID *Data,
|
2010-11-02 07:06:38 +01:00
|
|
|
IN UINTN DataSize
|
2010-11-01 07:30:58 +01:00
|
|
|
)
|
|
|
|
{
|
|
|
|
//
|
2012-03-19 06:52:16 +01:00
|
|
|
// Check input parameters.
|
2010-11-01 07:30:58 +01:00
|
|
|
//
|
2012-03-19 06:52:16 +01:00
|
|
|
if (Sha1Context == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-11-01 07:30:58 +01:00
|
|
|
|
|
|
|
//
|
2012-03-19 06:52:16 +01:00
|
|
|
// Check invalid parameters, in case that only DataLength was checked in OpenSSL
|
2010-11-01 07:30:58 +01:00
|
|
|
//
|
2012-03-19 06:52:16 +01:00
|
|
|
if (Data == NULL && DataSize != 0) {
|
|
|
|
return FALSE;
|
2010-11-01 07:30:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// OpenSSL SHA-1 Hash Update
|
|
|
|
//
|
2010-11-02 07:06:38 +01:00
|
|
|
return (BOOLEAN) (SHA1_Update ((SHA_CTX *)Sha1Context, Data, DataSize));
|
2010-11-01 07:30:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-11-02 07:06:38 +01:00
|
|
|
Completes computation of the SHA-1 digest value.
|
|
|
|
|
|
|
|
This function completes SHA-1 hash computation and retrieves the digest value into
|
|
|
|
the specified memory. After this function has been called, the SHA-1 context cannot
|
|
|
|
be used again.
|
|
|
|
SHA-1 context should be already correctly intialized by Sha1Init(), and should not be
|
|
|
|
finalized by Sha1Final(). Behavior with invalid SHA-1 context is undefined.
|
2010-11-01 07:30:58 +01:00
|
|
|
|
2012-03-19 06:52:16 +01:00
|
|
|
If Sha1Context is NULL, then return FALSE.
|
|
|
|
If HashValue is NULL, then return FALSE.
|
2010-11-01 07:30:58 +01:00
|
|
|
|
2010-11-02 07:06:38 +01:00
|
|
|
@param[in, out] Sha1Context Pointer to the SHA-1 context.
|
2010-11-01 07:30:58 +01:00
|
|
|
@param[out] HashValue Pointer to a buffer that receives the SHA-1 digest
|
|
|
|
value (20 bytes).
|
|
|
|
|
|
|
|
@retval TRUE SHA-1 digest computation succeeded.
|
|
|
|
@retval FALSE SHA-1 digest computation failed.
|
|
|
|
|
|
|
|
**/
|
|
|
|
BOOLEAN
|
|
|
|
EFIAPI
|
|
|
|
Sha1Final (
|
|
|
|
IN OUT VOID *Sha1Context,
|
|
|
|
OUT UINT8 *HashValue
|
|
|
|
)
|
|
|
|
{
|
|
|
|
//
|
2012-03-19 06:52:16 +01:00
|
|
|
// Check input parameters.
|
2010-11-01 07:30:58 +01:00
|
|
|
//
|
2012-03-19 06:52:16 +01:00
|
|
|
if (Sha1Context == NULL || HashValue == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-11-01 07:30:58 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// OpenSSL SHA-1 Hash Finalization
|
|
|
|
//
|
|
|
|
return (BOOLEAN) (SHA1_Final (HashValue, (SHA_CTX *)Sha1Context));
|
|
|
|
}
|