This is the HashApiInstance implementation for SM3 which registers the SM3 hash library in CryptoPkg with BaseHashLib based on whether a platform supports SM3 hash algorithm.
Signed-off-by: Subash Lakkimsetti <subashx.lakkimse...@intel.com> Signed-off-by: Sukerkar, Amol N <amol.n.suker...@intel.com> --- SecurityPkg/Library/HashApiInstanceSm3/HashApiInstanceSm3.c | 128 ++++++++++++++++++++ SecurityPkg/Library/HashApiInstanceSm3/HashApiInstanceSm3.inf | 40 ++++++ SecurityPkg/Library/HashApiInstanceSm3/HashApiInstanceSm3.uni | 16 +++ 3 files changed, 184 insertions(+) diff --git a/SecurityPkg/Library/HashApiInstanceSm3/HashApiInstanceSm3.c b/SecurityPkg/Library/HashApiInstanceSm3/HashApiInstanceSm3.c new file mode 100644 index 000000000000..391f482c1520 --- /dev/null +++ b/SecurityPkg/Library/HashApiInstanceSm3/HashApiInstanceSm3.c @@ -0,0 +1,128 @@ +/** @file + This library is BaseCrypto Sm3 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/BaseHashLib.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 +Sm3_Init ( + OUT HASH_HANDLE *HashHandle + ) +{ + VOID *Sm3Ctx; + UINTN CtxSize; + + CtxSize = Sm3GetContextSize (); + Sm3Ctx = AllocatePool (CtxSize); + ASSERT (Sm3Ctx != NULL); + + Sm3Init (Sm3Ctx); + + *HashHandle = (HASH_HANDLE)Sm3Ctx; + + 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 +Sm3_Update ( + IN HASH_HANDLE HashHandle, + IN VOID *DataToHash, + IN UINTN DataToHashLen + ) +{ + VOID *Sm3Ctx; + + Sm3Ctx = (VOID *)HashHandle; + Sm3Update (Sm3Ctx, 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 +Sm3_Final ( + IN HASH_HANDLE HashHandle, + OUT UINT8 **Digest + ) +{ + UINT8 Sm3Digest[SM3_256_DIGEST_SIZE]; + VOID *Sm3Ctx; + + Sm3Ctx = (VOID *)HashHandle; + Sm3Final (Sm3Ctx, Sm3Digest); + + CopyMem (*Digest, Sm3Digest, SM3_256_DIGEST_SIZE); + + FreePool (Sm3Ctx); + + return EFI_SUCCESS; +} + +HASH_INTERFACE_UNIFIED_API mSm3InternalHashApiInstance = { + HASH_ALGORITHM_SM3_256_GUID, + Sm3_Init, + Sm3_Update, + Sm3_Final, +}; + +/** + The function register Sm3 instance. + + @retval EFI_SUCCESS Sm3 instance is registered, or system dose not surpport registr Sm3 instance +**/ +EFI_STATUS +EFIAPI +HashApiInstanceSm3Constructor ( + VOID + ) +{ + EFI_STATUS Status; + + Status = RegisterHashApiLib (&mSm3InternalHashApiInstance); + if ((Status == EFI_SUCCESS) || (Status == EFI_UNSUPPORTED)) { + // + // Unsupported means platform policy does not need this instance enabled. + // + DEBUG ((DEBUG_ERROR, "[ansukerk]: Hash Interface Sm3 is registered\n")); + return EFI_SUCCESS; + } + return Status; +} diff --git a/SecurityPkg/Library/HashApiInstanceSm3/HashApiInstanceSm3.inf b/SecurityPkg/Library/HashApiInstanceSm3/HashApiInstanceSm3.inf new file mode 100644 index 000000000000..ae328bfbf41f --- /dev/null +++ b/SecurityPkg/Library/HashApiInstanceSm3/HashApiInstanceSm3.inf @@ -0,0 +1,40 @@ +## @file +# Provides BaseCrypto SM3 hash service +# +# This library 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 +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = HashApiInstanceSm3 + MODULE_UNI_FILE = HashApiInstanceSm3.uni + FILE_GUID = 9A7A6AB4-9DA6-4aa4-90CB-6D4B79EDA7B9 + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = NULL + CONSTRUCTOR = HashApiInstanceSm3Constructor + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + HashApiInstanceSm3.c + +[Packages] + MdePkg/MdePkg.dec + SecurityPkg/SecurityPkg.dec + CryptoPkg/CryptoPkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib + MemoryAllocationLib + BaseCryptLib diff --git a/SecurityPkg/Library/HashApiInstanceSm3/HashApiInstanceSm3.uni b/SecurityPkg/Library/HashApiInstanceSm3/HashApiInstanceSm3.uni new file mode 100644 index 000000000000..8f9712b1520e --- /dev/null +++ b/SecurityPkg/Library/HashApiInstanceSm3/HashApiInstanceSm3.uni @@ -0,0 +1,16 @@ +// /** @file +// Provides BaseCrypto SM3 hash service +// +// This library can be registered to BaseCrypto router, to serve as hash engine. +// +// Copyright (c) 2019, Intel Corporation. All rights reserved.<BR> +// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Provides BaseCrypto SM3 hash service API" + +#string STR_MODULE_DESCRIPTION #language en-US "This library can be registered to Base Hash API, to serve as hash engine." + -- 2.16.2.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#52380): https://edk2.groups.io/g/devel/message/52380 Mute This Topic: https://groups.io/mt/68808201/21656 Group Owner: devel+ow...@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com] -=-=-=-=-=-=-=-=-=-=-=-