]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Library/HashInstanceLibSm3/HashInstanceLibSm3.c
SecurityPkg: introduce the SM3 digest algorithm
[mirror_edk2.git] / SecurityPkg / Library / HashInstanceLibSm3 / HashInstanceLibSm3.c
diff --git a/SecurityPkg/Library/HashInstanceLibSm3/HashInstanceLibSm3.c b/SecurityPkg/Library/HashInstanceLibSm3/HashInstanceLibSm3.c
new file mode 100644 (file)
index 0000000..8fd9516
--- /dev/null
@@ -0,0 +1,150 @@
+/** @file\r
+  BaseCrypto SM3 hash instance library.\r
+  It can be registered to BaseCrypto router, to serve as hash engine.\r
+\r
+  Copyright (c) 2013 - 2019, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+\r
+#include <PiPei.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/Tpm2CommandLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/BaseCryptLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/HashLib.h>\r
+\r
+/**\r
+  The function set SM3 to digest list.\r
+\r
+  @param DigestList   digest list\r
+  @param Sm3Digest    SM3 digest\r
+**/\r
+VOID\r
+Tpm2SetSm3ToDigestList (\r
+  IN TPML_DIGEST_VALUES *DigestList,\r
+  IN UINT8              *Sm3Digest\r
+  )\r
+{\r
+  DigestList->count = 1;\r
+  DigestList->digests[0].hashAlg = TPM_ALG_SM3_256;\r
+  CopyMem (\r
+    DigestList->digests[0].digest.sm3_256,\r
+    Sm3Digest,\r
+    SM3_256_DIGEST_SIZE\r
+    );\r
+}\r
+\r
+/**\r
+  Start hash sequence.\r
+\r
+  @param HashHandle Hash handle.\r
+\r
+  @retval EFI_SUCCESS          Hash sequence start and HandleHandle returned.\r
+  @retval EFI_OUT_OF_RESOURCES No enough resource to start hash.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+Sm3HashInit (\r
+  OUT HASH_HANDLE    *HashHandle\r
+  )\r
+{\r
+  VOID     *Sm3Ctx;\r
+  UINTN    CtxSize;\r
+\r
+  CtxSize = Sm3GetContextSize ();\r
+  Sm3Ctx = AllocatePool (CtxSize);\r
+  if (Sm3Ctx == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  Sm3Init (Sm3Ctx);\r
+\r
+  *HashHandle = (HASH_HANDLE)Sm3Ctx;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Update hash sequence data.\r
+\r
+  @param HashHandle    Hash handle.\r
+  @param DataToHash    Data to be hashed.\r
+  @param DataToHashLen Data size.\r
+\r
+  @retval EFI_SUCCESS     Hash sequence updated.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+Sm3HashUpdate (\r
+  IN HASH_HANDLE    HashHandle,\r
+  IN VOID           *DataToHash,\r
+  IN UINTN          DataToHashLen\r
+  )\r
+{\r
+  VOID     *Sm3Ctx;\r
+\r
+  Sm3Ctx = (VOID *)HashHandle;\r
+  Sm3Update (Sm3Ctx, DataToHash, DataToHashLen);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Complete hash sequence complete.\r
+\r
+  @param HashHandle    Hash handle.\r
+  @param DigestList    Digest list.\r
+\r
+  @retval EFI_SUCCESS     Hash sequence complete and DigestList is returned.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+Sm3HashFinal (\r
+  IN HASH_HANDLE         HashHandle,\r
+  OUT TPML_DIGEST_VALUES *DigestList\r
+  )\r
+{\r
+  UINT8         Digest[SM3_256_DIGEST_SIZE];\r
+  VOID          *Sm3Ctx;\r
+\r
+  Sm3Ctx = (VOID *)HashHandle;\r
+  Sm3Final (Sm3Ctx, Digest);\r
+\r
+  FreePool (Sm3Ctx);\r
+\r
+  Tpm2SetSm3ToDigestList (DigestList, Digest);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+HASH_INTERFACE  mSm3InternalHashInstance = {\r
+  HASH_ALGORITHM_SM3_256_GUID,\r
+  Sm3HashInit,\r
+  Sm3HashUpdate,\r
+  Sm3HashFinal,\r
+};\r
+\r
+/**\r
+  The function register SM3 instance.\r
+\r
+  @retval EFI_SUCCESS   SM3 instance is registered, or system dose not support register SM3 instance\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+HashInstanceLibSm3Constructor (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  Status = RegisterHashInterfaceLib (&mSm3InternalHashInstance);\r
+  if ((Status == EFI_SUCCESS) || (Status == EFI_UNSUPPORTED)) {\r
+    //\r
+    // Unsupported means platform policy does not need this instance enabled.\r
+    //\r
+    return EFI_SUCCESS;\r
+  }\r
+  return Status;\r
+}\r