]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Hash/CryptSha512.c
CryptoPkg Updates to support RFC3161 timestamp signature verification.
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hash / CryptSha512.c
diff --git a/CryptoPkg/Library/BaseCryptLib/Hash/CryptSha512.c b/CryptoPkg/Library/BaseCryptLib/Hash/CryptSha512.c
new file mode 100644 (file)
index 0000000..491f45d
--- /dev/null
@@ -0,0 +1,354 @@
+/** @file\r
+  SHA-384 and SHA-512 Digest Wrapper Implementations over OpenSSL.\r
+\r
+Copyright (c) 2014, 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 "InternalCryptLib.h"\r
+#include <openssl/sha.h>\r
+\r
+/**\r
+  Retrieves the size, in bytes, of the context buffer required for SHA-384 hash operations.\r
+\r
+  @return  The size, in bytes, of the context buffer required for SHA-384 hash operations.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+Sha384GetContextSize (\r
+  VOID\r
+  )\r
+{\r
+  //\r
+  // Retrieves OpenSSL SHA-384 Context Size\r
+  //\r
+  return (UINTN) (sizeof (SHA512_CTX));\r
+}\r
+\r
+/**\r
+  Initializes user-supplied memory pointed by Sha384Context as SHA-384 hash context for\r
+  subsequent use.\r
+\r
+  If Sha384Context is NULL, then return FALSE.\r
+\r
+  @param[out]  Sha384Context  Pointer to SHA-384 context being initialized.\r
+\r
+  @retval TRUE   SHA-384 context initialization succeeded.\r
+  @retval FALSE  SHA-384 context initialization failed.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sha384Init (\r
+  OUT  VOID  *Sha384Context\r
+  )\r
+{\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (Sha384Context == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // OpenSSL SHA-384 Context Initialization\r
+  //\r
+  return (BOOLEAN) (SHA384_Init ((SHA512_CTX *) Sha384Context));\r
+}\r
+\r
+/**\r
+  Makes a copy of an existing SHA-384 context.\r
+\r
+  If Sha384Context is NULL, then return FALSE.\r
+  If NewSha384Context is NULL, then return FALSE.\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in]  Sha384Context     Pointer to SHA-384 context being copied.\r
+  @param[out] NewSha384Context  Pointer to new SHA-384 context.\r
+\r
+  @retval TRUE   SHA-384 context copy succeeded.\r
+  @retval FALSE  SHA-384 context copy failed.\r
+  @retval FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sha384Duplicate (\r
+  IN   CONST VOID  *Sha384Context,\r
+  OUT  VOID        *NewSha384Context\r
+  )\r
+{\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (Sha384Context == NULL || NewSha384Context == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  CopyMem (NewSha384Context, Sha384Context, sizeof (SHA512_CTX));\r
+\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Digests the input data and updates SHA-384 context.\r
+\r
+  This function performs SHA-384 digest on a data buffer of the specified size.\r
+  It can be called multiple times to compute the digest of long or discontinuous data streams.\r
+  SHA-384 context should be already correctly intialized by Sha384Init(), and should not be finalized\r
+  by Sha384Final(). Behavior with invalid context is undefined.\r
+\r
+  If Sha384Context is NULL, then return FALSE.\r
+\r
+  @param[in, out]  Sha384Context  Pointer to the SHA-384 context.\r
+  @param[in]       Data           Pointer to the buffer containing the data to be hashed.\r
+  @param[in]       DataSize       Size of Data buffer in bytes.\r
+\r
+  @retval TRUE   SHA-384 data digest succeeded.\r
+  @retval FALSE  SHA-384 data digest failed.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sha384Update (\r
+  IN OUT  VOID        *Sha384Context,\r
+  IN      CONST VOID  *Data,\r
+  IN      UINTN       DataSize\r
+  )\r
+{\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (Sha384Context == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r
+  //\r
+  if (Data == NULL && DataSize != 0) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // OpenSSL SHA-384 Hash Update\r
+  //\r
+  return (BOOLEAN) (SHA384_Update ((SHA512_CTX *) Sha384Context, Data, DataSize));\r
+}\r
+\r
+/**\r
+  Completes computation of the SHA-384 digest value.\r
+\r
+  This function completes SHA-384 hash computation and retrieves the digest value into\r
+  the specified memory. After this function has been called, the SHA-384 context cannot\r
+  be used again.\r
+  SHA-384 context should be already correctly intialized by Sha384Init(), and should not be\r
+  finalized by Sha384Final(). Behavior with invalid SHA-384 context is undefined.\r
+\r
+  If Sha384Context is NULL, then return FALSE.\r
+  If HashValue is NULL, then return FALSE.\r
+\r
+  @param[in, out]  Sha384Context  Pointer to the SHA-384 context.\r
+  @param[out]      HashValue      Pointer to a buffer that receives the SHA-384 digest\r
+                                  value (48 bytes).\r
+\r
+  @retval TRUE   SHA-384 digest computation succeeded.\r
+  @retval FALSE  SHA-384 digest computation failed.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sha384Final (\r
+  IN OUT  VOID   *Sha384Context,\r
+  OUT     UINT8  *HashValue\r
+  )\r
+{\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (Sha384Context == NULL || HashValue == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // OpenSSL SHA-384 Hash Finalization\r
+  //\r
+  return (BOOLEAN) (SHA384_Final (HashValue, (SHA512_CTX *) Sha384Context));\r
+}\r
+\r
+/**\r
+  Retrieves the size, in bytes, of the context buffer required for SHA-512 hash operations.\r
+\r
+  @return  The size, in bytes, of the context buffer required for SHA-512 hash operations.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+Sha512GetContextSize (\r
+  VOID\r
+  )\r
+{\r
+  //\r
+  // Retrieves OpenSSL SHA-512 Context Size\r
+  //\r
+  return (UINTN) (sizeof (SHA512_CTX));\r
+}\r
+\r
+/**\r
+  Initializes user-supplied memory pointed by Sha512Context as SHA-512 hash context for\r
+  subsequent use.\r
+\r
+  If Sha512Context is NULL, then return FALSE.\r
+\r
+  @param[out]  Sha512Context  Pointer to SHA-512 context being initialized.\r
+\r
+  @retval TRUE   SHA-512 context initialization succeeded.\r
+  @retval FALSE  SHA-512 context initialization failed.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sha512Init (\r
+  OUT  VOID  *Sha512Context\r
+  )\r
+{\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (Sha512Context == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // OpenSSL SHA-512 Context Initialization\r
+  //\r
+  return (BOOLEAN) (SHA512_Init ((SHA512_CTX *) Sha512Context));\r
+}\r
+\r
+/**\r
+  Makes a copy of an existing SHA-512 context.\r
+\r
+  If Sha512Context is NULL, then return FALSE.\r
+  If NewSha512Context is NULL, then return FALSE.\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in]  Sha512Context     Pointer to SHA-512 context being copied.\r
+  @param[out] NewSha512Context  Pointer to new SHA-512 context.\r
+\r
+  @retval TRUE   SHA-512 context copy succeeded.\r
+  @retval FALSE  SHA-512 context copy failed.\r
+  @retval FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sha512Duplicate (\r
+  IN   CONST VOID  *Sha512Context,\r
+  OUT  VOID        *NewSha512Context\r
+  )\r
+{\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (Sha512Context == NULL || NewSha512Context == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  CopyMem (NewSha512Context, Sha512Context, sizeof (SHA512_CTX));\r
+\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Digests the input data and updates SHA-512 context.\r
+\r
+  This function performs SHA-512 digest on a data buffer of the specified size.\r
+  It can be called multiple times to compute the digest of long or discontinuous data streams.\r
+  SHA-512 context should be already correctly intialized by Sha512Init(), and should not be finalized\r
+  by Sha512Final(). Behavior with invalid context is undefined.\r
+\r
+  If Sha512Context is NULL, then return FALSE.\r
+\r
+  @param[in, out]  Sha512Context  Pointer to the SHA-512 context.\r
+  @param[in]       Data           Pointer to the buffer containing the data to be hashed.\r
+  @param[in]       DataSize       Size of Data buffer in bytes.\r
+\r
+  @retval TRUE   SHA-512 data digest succeeded.\r
+  @retval FALSE  SHA-512 data digest failed.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sha512Update (\r
+  IN OUT  VOID        *Sha512Context,\r
+  IN      CONST VOID  *Data,\r
+  IN      UINTN       DataSize\r
+  )\r
+{\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (Sha512Context == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r
+  //\r
+  if (Data == NULL && DataSize != 0) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // OpenSSL SHA-512 Hash Update\r
+  //\r
+  return (BOOLEAN) (SHA512_Update ((SHA512_CTX *) Sha512Context, Data, DataSize));\r
+}\r
+\r
+/**\r
+  Completes computation of the SHA-512 digest value.\r
+\r
+  This function completes SHA-512 hash computation and retrieves the digest value into\r
+  the specified memory. After this function has been called, the SHA-512 context cannot\r
+  be used again.\r
+  SHA-512 context should be already correctly intialized by Sha512Init(), and should not be\r
+  finalized by Sha512Final(). Behavior with invalid SHA-512 context is undefined.\r
+\r
+  If Sha512Context is NULL, then return FALSE.\r
+  If HashValue is NULL, then return FALSE.\r
+\r
+  @param[in, out]  Sha512Context  Pointer to the SHA-512 context.\r
+  @param[out]      HashValue      Pointer to a buffer that receives the SHA-512 digest\r
+                                  value (64 bytes).\r
+\r
+  @retval TRUE   SHA-512 digest computation succeeded.\r
+  @retval FALSE  SHA-512 digest computation failed.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sha512Final (\r
+  IN OUT  VOID   *Sha512Context,\r
+  OUT     UINT8  *HashValue\r
+  )\r
+{\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (Sha512Context == NULL || HashValue == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // OpenSSL SHA-512 Hash Finalization\r
+  //\r
+  return (BOOLEAN) (SHA384_Final (HashValue, (SHA512_CTX *) Sha512Context));\r
+}\r