This is the HashApiInstance implementation for SHA256 which registers the SHA256 hash library in CryptoPkg with BaseHashLib based on whether a platform supports SHA256 hash algorithm (provided by PcdTpm2HashMask).
Signed-off-by: Sukerkar, Amol N <amol.n.suker...@intel.com> --- SecurityPkg/Library/HashApiInstanceSha256/HashApiInstanceSha256.c | 128 ++++++++++++++++++++ SecurityPkg/Library/HashApiInstanceSha256/HashApiInstanceSha256.inf | 40 ++++++ SecurityPkg/Library/HashApiInstanceSha256/HashApiInstanceSha256.uni | 16 +++ SecurityPkg/SecurityPkg.dsc | 1 + 4 files changed, 185 insertions(+) diff --git a/SecurityPkg/Library/HashApiInstanceSha256/HashApiInstanceSha256.c b/SecurityPkg/Library/HashApiInstanceSha256/HashApiInstanceSha256.c new file mode 100644 index 000000000000..129d60a387fd --- /dev/null +++ b/SecurityPkg/Library/HashApiInstanceSha256/HashApiInstanceSha256.c @@ -0,0 +1,128 @@ +/** @file + This library is BaseCrypto SHA256 hash instance. + It can be registered to BaseCrypto router, to serve as hash engine. + +Copyright (c) 2013 - 2019, Intel Corporation. All rights reserved. <BR> +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include <PiPei.h> +#include <Library/BaseLib.h> +#include <Library/BaseMemoryLib.h> +#include <Library/DebugLib.h> +#include <Library/BaseCryptLib.h> +#include <Library/MemoryAllocationLib.h> +#include <Library/HashLib.h> + +/** + Start hash sequence. + + @param HashHandle Hash handle. + + @retval EFI_SUCCESS Hash sequence start and HandleHandle returned. + @retval EFI_OUT_OF_RESOURCES No enough resource to start hash. +**/ +EFI_STATUS +EFIAPI +Sha256_Init ( + OUT HASH_HANDLE *HashHandle + ) +{ + VOID *Sha256Ctx; + UINTN CtxSize; + + CtxSize = Sha256GetContextSize (); + Sha256Ctx = AllocatePool (CtxSize); + ASSERT (Sha256Ctx != NULL); + + Sha256Init (Sha256Ctx); + + *HashHandle = (HASH_HANDLE)Sha256Ctx; + + return EFI_SUCCESS; +} + +/** + Update hash sequence data. + + @param HashHandle Hash handle. + @param DataToHash Data to be hashed. + @param DataToHashLen Data size. + + @retval EFI_SUCCESS Hash sequence updated. +**/ +EFI_STATUS +EFIAPI +Sha256_Update ( + IN HASH_HANDLE HashHandle, + IN VOID *DataToHash, + IN UINTN DataToHashLen + ) +{ + VOID *Sha256Ctx; + + Sha256Ctx = (VOID *)HashHandle; + Sha256Update (Sha256Ctx, DataToHash, DataToHashLen); + + return EFI_SUCCESS; +} + +/** + Complete hash sequence complete. + + @param HashHandle Hash handle. + @param DigestList Digest list. + + @retval EFI_SUCCESS Hash sequence complete and DigestList is returned. +**/ +EFI_STATUS +EFIAPI +Sha256_Final ( + IN HASH_HANDLE HashHandle, + OUT UINT8 **Digest + ) +{ + UINT8 Sha256Digest[SHA256_DIGEST_SIZE]; + VOID *Sha256Ctx; + + Sha256Ctx = (VOID *)HashHandle; + Sha256Final (Sha256Ctx, Sha256Digest); + + CopyMem (*Digest, Sha256Digest, SHA256_DIGEST_SIZE); + + FreePool (Sha256Ctx); + + return EFI_SUCCESS; +} + +HASH_INTERFACE_UNIFIED_API mSha256InternalHashApiInstance = { + HASH_ALGORITHM_SHA256_GUID, + Sha256_Init, + Sha256_Update, + Sha256_Final, +}; + +/** + The function register SHA256 instance. + + @retval EFI_SUCCESS SHA256 instance is registered, or system dose not surpport registr SHA256 instance +**/ +EFI_STATUS +EFIAPI +HashApiInstanceSha256Constructor ( + VOID + ) +{ + EFI_STATUS Status; + + Status = RegisterHashApiLib (&mSha256InternalHashApiInstance); + if ((Status == EFI_SUCCESS) || (Status == EFI_UNSUPPORTED)) { + // + // Unsupported means platform policy does not need this instance enabled. + // + DEBUG ((DEBUG_ERROR, "[ansukerk]: Hash Interface SHA256 is registered\n")); + return EFI_SUCCESS; + } + return Status; +} diff --git a/SecurityPkg/Library/HashApiInstanceSha256/HashApiInstanceSha256.inf b/SecurityPkg/Library/HashApiInstanceSha256/HashApiInstanceSha256.inf new file mode 100644 index 000000000000..08d802cd69f4 --- /dev/null +++ b/SecurityPkg/Library/HashApiInstanceSha256/HashApiInstanceSha256.inf @@ -0,0 +1,40 @@ +## @file +# Provides BaseCrypto SHA1 hash service +# +# This library can be registered to BaseCrypto router, to serve as hash engine. +# +# Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = HashApiInstanceSha256 + MODULE_UNI_FILE = HashApiInstanceSha256.uni + FILE_GUID = 9A7A6AB4-9DA6-4aa4-90CB-6D4B79EDA7B9 + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = NULL + CONSTRUCTOR = HashApiInstanceSha256Constructor + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + HashApiInstanceSha256.c + +[Packages] + MdePkg/MdePkg.dec + SecurityPkg/SecurityPkg.dec + CryptoPkg/CryptoPkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib + MemoryAllocationLib + BaseCryptLib diff --git a/SecurityPkg/Library/HashApiInstanceSha256/HashApiInstanceSha256.uni b/SecurityPkg/Library/HashApiInstanceSha256/HashApiInstanceSha256.uni new file mode 100644 index 000000000000..b2121464c552 --- /dev/null +++ b/SecurityPkg/Library/HashApiInstanceSha256/HashApiInstanceSha256.uni @@ -0,0 +1,16 @@ +// /** @file +// Provides BaseCrypto SHA256 hash service +// +// This library can be registered to BaseCrypto router, to serve as hash engine. +// +// Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.<BR> +// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Provides BaseCrypto SHA256 hash service API" + +#string STR_MODULE_DESCRIPTION #language en-US "This library can be registered to Base Hash API, to serve as hash engine." + diff --git a/SecurityPkg/SecurityPkg.dsc b/SecurityPkg/SecurityPkg.dsc index 6c0832d48e88..95a5710735e0 100644 --- a/SecurityPkg/SecurityPkg.dsc +++ b/SecurityPkg/SecurityPkg.dsc @@ -245,6 +245,7 @@ [Components.IA32, Components.X64] # Hash API # SecurityPkg/Library/HashApiInstanceSha1/HashApiInstanceSha1.inf + SecurityPkg/Library/HashApiInstanceSha256/HashApiInstanceSha256.inf # # TPM -- 2.16.2.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#52377): https://edk2.groups.io/g/devel/message/52377 Mute This Topic: https://groups.io/mt/68808198/21656 Group Owner: devel+ow...@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com] -=-=-=-=-=-=-=-=-=-=-=-