]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Hash/CryptSha256.c
Add CryptoPkg (from UDK2010.UP3)
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hash / CryptSha256.c
diff --git a/CryptoPkg/Library/BaseCryptLib/Hash/CryptSha256.c b/CryptoPkg/Library/BaseCryptLib/Hash/CryptSha256.c
new file mode 100644 (file)
index 0000000..9b566a4
--- /dev/null
@@ -0,0 +1,145 @@
+/** @file\r
+  SHA-256 Digest Wrapper Implementation over OpenSSL.\r
+\r
+Copyright (c) 2009 - 2010, 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 <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
+\r
+#include <Library/BaseCryptLib.h>\r
+#include <openssl/sha.h>\r
+\r
+\r
+/**\r
+  Retrieves the size, in bytes, of the context buffer required for SHA-256 operations.\r
+\r
+  @return  The size, in bytes, of the context buffer required for SHA-256 operations.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+Sha256GetContextSize (\r
+  VOID\r
+  )\r
+{\r
+  //\r
+  // Retrieves OpenSSL SHA-256 Context Size\r
+  //\r
+  return (UINTN)(sizeof (SHA256_CTX));\r
+}\r
+\r
+\r
+/**\r
+  Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for\r
+  subsequent use.\r
+\r
+  If Sha256Context is NULL, then ASSERT().\r
+\r
+  @param[in, out]  Sha256Context  Pointer to SHA-256 Context being initialized.\r
+\r
+  @retval TRUE   SHA-256 context initialization succeeded.\r
+  @retval FALSE  SHA-256 context initialization failed.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sha256Init (\r
+  IN OUT  VOID  *Sha256Context\r
+  )\r
+{\r
+  //\r
+  // ASSERT if Sha256Context is NULL\r
+  //\r
+  ASSERT (Sha256Context != NULL);\r
+\r
+  //\r
+  // OpenSSL SHA-256 Context Initialization\r
+  //\r
+  return (BOOLEAN) (SHA256_Init ((SHA256_CTX *)Sha256Context));\r
+}\r
+\r
+\r
+/**\r
+  Performs SHA-256 digest on a data buffer of the specified length. This function can\r
+  be called multiple times to compute the digest of long or discontinuous data streams.\r
+\r
+  If Sha256Context is NULL, then ASSERT().\r
+\r
+  @param[in, out]  Sha256Context  Pointer to the SHA-256 context.\r
+  @param[in]       Data           Pointer to the buffer containing the data to be hashed.\r
+  @param[in]       DataLength     Length of Data buffer in bytes.\r
+\r
+  @retval TRUE   SHA-256 data digest succeeded.\r
+  @retval FALSE  Invalid SHA-256 context. After Sha256Final function has been called, the\r
+                 SHA-256 context cannot be reused.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sha256Update (\r
+  IN OUT  VOID        *Sha256Context,\r
+  IN      CONST VOID  *Data,\r
+  IN      UINTN       DataLength\r
+  )\r
+{\r
+  //\r
+  // ASSERT if Sha256Context is NULL\r
+  //\r
+  ASSERT (Sha256Context != NULL);\r
+\r
+  //\r
+  // ASSERT if invalid parameters, in case that only DataLength was checked in OpenSSL\r
+  //\r
+  if (Data == NULL) {\r
+    ASSERT (DataLength == 0);\r
+  }\r
+\r
+  //\r
+  // OpenSSL SHA-256 Hash Update\r
+  //\r
+  return (BOOLEAN) (SHA256_Update ((SHA256_CTX *)Sha256Context, Data, DataLength));\r
+}\r
+\r
+\r
+/**\r
+  Completes SHA-256 hash computation and retrieves the digest value into the specified\r
+  memory. After this function has been called, the SHA-256 context cannot be used again.\r
+\r
+  If Sha256Context is NULL, then ASSERT().\r
+  If HashValue is NULL, then ASSERT().\r
+\r
+  @param[in, out]  Sha256Context  Pointer to SHA-256 context\r
+  @param[out]      HashValue      Pointer to a buffer that receives the SHA-256 digest\r
+                                  value (32 bytes).\r
+\r
+  @retval TRUE   SHA-256 digest computation succeeded.\r
+  @retval FALSE  SHA-256 digest computation failed.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sha256Final (\r
+  IN OUT  VOID   *Sha256Context,\r
+  OUT     UINT8  *HashValue\r
+  )\r
+{\r
+  //\r
+  // ASSERT if Sha256Context is NULL or HashValue is NULL\r
+  //\r
+  ASSERT (Sha256Context != NULL);\r
+  ASSERT (HashValue     != NULL);\r
+\r
+  //\r
+  // OpenSSL SHA-256 Hash Finalization\r
+  //\r
+  return (BOOLEAN) (SHA256_Final (HashValue, (SHA256_CTX *)Sha256Context));\r
+}\r