]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Library/HashInstanceLibSha256/HashInstanceLibSha256.c
Add TPM2 implementation.
[mirror_edk2.git] / SecurityPkg / Library / HashInstanceLibSha256 / HashInstanceLibSha256.c
diff --git a/SecurityPkg/Library/HashInstanceLibSha256/HashInstanceLibSha256.c b/SecurityPkg/Library/HashInstanceLibSha256/HashInstanceLibSha256.c
new file mode 100644 (file)
index 0000000..abc97b4
--- /dev/null
@@ -0,0 +1,155 @@
+/** @file\r
+  Ihis library is BaseCrypto SHA256 hash instance.\r
+  It can be registered to BaseCrypto router, to serve as hash engine.\r
+\r
+Copyright (c) 2013, Intel Corporation. All rights reserved. <BR>\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\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 SHA256 to digest list.\r
+\r
+  @param DigestList   digest list\r
+  @param Sha256Digest SHA256 digest\r
+**/\r
+VOID\r
+Tpm2SetSha256ToDigestList (\r
+  IN TPML_DIGEST_VALUES *DigestList,\r
+  IN UINT8              *Sha256Digest\r
+  )\r
+{\r
+  DigestList->count = 1;\r
+  DigestList->digests[0].hashAlg = TPM_ALG_SHA256;\r
+  CopyMem (\r
+    DigestList->digests[0].digest.sha256,\r
+    Sha256Digest,\r
+    SHA256_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
+Sha256HashInit (\r
+  OUT HASH_HANDLE    *HashHandle\r
+  )\r
+{\r
+  VOID     *Sha256Ctx;\r
+  UINTN    CtxSize;\r
+\r
+  CtxSize = Sha256GetContextSize ();\r
+  Sha256Ctx = AllocatePool (CtxSize);\r
+  ASSERT (Sha256Ctx != NULL);\r
+\r
+  Sha256Init (Sha256Ctx);\r
+\r
+  *HashHandle = (HASH_HANDLE)Sha256Ctx;\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
+Sha256HashUpdate (\r
+  IN HASH_HANDLE    HashHandle,\r
+  IN VOID           *DataToHash,\r
+  IN UINTN          DataToHashLen\r
+  )\r
+{\r
+  VOID     *Sha256Ctx;\r
+\r
+  Sha256Ctx = (VOID *)HashHandle;\r
+  Sha256Update (Sha256Ctx, 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
+Sha256HashFinal (\r
+  IN HASH_HANDLE         HashHandle,\r
+  OUT TPML_DIGEST_VALUES *DigestList\r
+  )\r
+{\r
+  UINT8         Digest[SHA256_DIGEST_SIZE];\r
+  VOID          *Sha256Ctx;\r
+\r
+  Sha256Ctx = (VOID *)HashHandle;\r
+  Sha256Final (Sha256Ctx, Digest);\r
+\r
+  FreePool (Sha256Ctx);\r
+  \r
+  Tpm2SetSha256ToDigestList (DigestList, Digest);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+HASH_INTERFACE  mSha256InternalHashInstance = {\r
+  HASH_ALGORITHM_SHA256_GUID,\r
+  Sha256HashInit,\r
+  Sha256HashUpdate,\r
+  Sha256HashFinal,\r
+};\r
+\r
+/**\r
+  The function register SHA256 instance.\r
+  \r
+  @retval EFI_SUCCESS   SHA256 instance is registered, or system dose not surpport registr SHA256 instance\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+HashInstanceLibSha256Constructor (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  Status = RegisterHashInterfaceLib (&mSha256InternalHashInstance);\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
+}
\ No newline at end of file