]> git.proxmox.com Git - mirror_edk2.git/commitdiff
CryptoPkg/BaseCryptLib: Wrap OpenSSL SM3 algorithm
authorLu, XiaoyuX <xiaoyux.lu@intel.com>
Fri, 31 May 2019 08:13:22 +0000 (16:13 +0800)
committerJian J Wang <jian.j.wang@intel.com>
Fri, 7 Jun 2019 21:18:41 +0000 (05:18 +0800)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1861

1. Implement OpenSSL SM3 wrapped functions in CryptSm3.c file.
2. Add wrapped SM3 functions declaration to BaseCryptLib.h file.
3. Add CryptSm3.c to each module information file.

Cc: Jian J Wang <jian.j.wang@intel.com>
Signed-off-by: Xiaoyu Lu <xiaoyux.lu@intel.com>
Reviewed-by: Jian J Wang <jian.j.wang@intel.com>
CryptoPkg/Include/Library/BaseCryptLib.h
CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
CryptoPkg/Library/BaseCryptLib/Hash/CryptSm3.c [new file with mode: 0644]
CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf

index 84374b283b7aea9295cafc120da41f4b4e712f86..19d1afe3c8c0b81330fb19fe3369c8e185c4d5bf 100644 (file)
@@ -44,6 +44,11 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 ///\r
 #define SHA512_DIGEST_SIZE  64\r
 \r
+///\r
+/// SM3 digest size in bytes\r
+///\r
+#define SM3_256_DIGEST_SIZE 32\r
+\r
 ///\r
 /// TDES block size in bytes\r
 ///\r
@@ -885,6 +890,137 @@ Sha512HashAll (
   OUT  UINT8       *HashValue\r
   );\r
 \r
+/**\r
+  Retrieves the size, in bytes, of the context buffer required for SM3 hash operations.\r
+\r
+  @return  The size, in bytes, of the context buffer required for SM3 hash operations.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+Sm3GetContextSize (\r
+  VOID\r
+  );\r
+\r
+/**\r
+  Initializes user-supplied memory pointed by Sm3Context as SM3 hash context for\r
+  subsequent use.\r
+\r
+  If Sm3Context is NULL, then return FALSE.\r
+\r
+  @param[out]  Sm3Context  Pointer to SM3 context being initialized.\r
+\r
+  @retval TRUE   SM3 context initialization succeeded.\r
+  @retval FALSE  SM3 context initialization failed.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sm3Init (\r
+  OUT  VOID  *Sm3Context\r
+  );\r
+\r
+/**\r
+  Makes a copy of an existing SM3 context.\r
+\r
+  If Sm3Context is NULL, then return FALSE.\r
+  If NewSm3Context is NULL, then return FALSE.\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in]  Sm3Context     Pointer to SM3 context being copied.\r
+  @param[out] NewSm3Context  Pointer to new SM3 context.\r
+\r
+  @retval TRUE   SM3 context copy succeeded.\r
+  @retval FALSE  SM3 context copy failed.\r
+  @retval FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sm3Duplicate (\r
+  IN   CONST VOID  *Sm3Context,\r
+  OUT  VOID        *NewSm3Context\r
+  );\r
+\r
+/**\r
+  Digests the input data and updates SM3 context.\r
+\r
+  This function performs SM3 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
+  SM3 context should be already correctly initialized by Sm3Init(), and should not be finalized\r
+  by Sm3Final(). Behavior with invalid context is undefined.\r
+\r
+  If Sm3Context is NULL, then return FALSE.\r
+\r
+  @param[in, out]  Sm3Context     Pointer to the SM3 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   SM3 data digest succeeded.\r
+  @retval FALSE  SM3 data digest failed.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sm3Update (\r
+  IN OUT  VOID        *Sm3Context,\r
+  IN      CONST VOID  *Data,\r
+  IN      UINTN       DataSize\r
+  );\r
+\r
+/**\r
+  Completes computation of the SM3 digest value.\r
+\r
+  This function completes SM3 hash computation and retrieves the digest value into\r
+  the specified memory. After this function has been called, the SM3 context cannot\r
+  be used again.\r
+  SM3 context should be already correctly initialized by Sm3Init(), and should not be\r
+  finalized by Sm3Final(). Behavior with invalid SM3 context is undefined.\r
+\r
+  If Sm3Context is NULL, then return FALSE.\r
+  If HashValue is NULL, then return FALSE.\r
+\r
+  @param[in, out]  Sm3Context     Pointer to the SM3 context.\r
+  @param[out]      HashValue      Pointer to a buffer that receives the SM3 digest\r
+                                  value (32 bytes).\r
+\r
+  @retval TRUE   SM3 digest computation succeeded.\r
+  @retval FALSE  SM3 digest computation failed.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sm3Final (\r
+  IN OUT  VOID   *Sm3Context,\r
+  OUT     UINT8  *HashValue\r
+  );\r
+\r
+/**\r
+  Computes the SM3 message digest of a input data buffer.\r
+\r
+  This function performs the SM3 message digest of a given data buffer, and places\r
+  the digest value into the specified memory.\r
+\r
+  If this interface is not supported, then return FALSE.\r
+\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
+  @param[out]  HashValue   Pointer to a buffer that receives the SM3 digest\r
+                           value (32 bytes).\r
+\r
+  @retval TRUE   SM3 digest computation succeeded.\r
+  @retval FALSE  SM3 digest computation failed.\r
+  @retval FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sm3HashAll (\r
+  IN   CONST VOID  *Data,\r
+  IN   UINTN       DataSize,\r
+  OUT  UINT8       *HashValue\r
+  );\r
+\r
 //=====================================================================================\r
 //    MAC (Message Authentication Code) Primitive\r
 //=====================================================================================\r
index 26ce11d0c2e4d395d319cede9b7783a9bd5b4b8a..020df3c19b3c143fd5f2f480ed7d02719ecef58c 100644 (file)
@@ -33,6 +33,7 @@
   Hash/CryptSha1.c\r
   Hash/CryptSha256.c\r
   Hash/CryptSha512.c\r
+  Hash/CryptSm3.c\r
   Hmac/CryptHmacMd5.c\r
   Hmac/CryptHmacSha1.c\r
   Hmac/CryptHmacSha256.c\r
diff --git a/CryptoPkg/Library/BaseCryptLib/Hash/CryptSm3.c b/CryptoPkg/Library/BaseCryptLib/Hash/CryptSm3.c
new file mode 100644 (file)
index 0000000..eacf482
--- /dev/null
@@ -0,0 +1,234 @@
+/** @file\r
+  SM3 Digest Wrapper Implementations over openssl.\r
+\r
+Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include "InternalCryptLib.h"\r
+#include "internal/sm3.h"\r
+\r
+/**\r
+  Retrieves the size, in bytes, of the context buffer required for SM3 hash operations.\r
+\r
+  @return  The size, in bytes, of the context buffer required for SM3 hash operations.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+Sm3GetContextSize (\r
+  VOID\r
+  )\r
+{\r
+  //\r
+  // Retrieves Openssl SM3 Context Size\r
+  //\r
+  return (UINTN) (sizeof (SM3_CTX));\r
+}\r
+\r
+/**\r
+  Initializes user-supplied memory pointed by Sm3Context as SM3 hash context for\r
+  subsequent use.\r
+\r
+  If Sm3Context is NULL, then return FALSE.\r
+\r
+  @param[out]  Sm3Context  Pointer to SM3 context being initialized.\r
+\r
+  @retval TRUE   SM3 context initialization succeeded.\r
+  @retval FALSE  SM3 context initialization failed.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sm3Init (\r
+  OUT  VOID  *Sm3Context\r
+  )\r
+{\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (Sm3Context == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // Openssl SM3 Context Initialization\r
+  //\r
+  sm3_init ((SM3_CTX *) Sm3Context);\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Makes a copy of an existing SM3 context.\r
+\r
+  If Sm3Context is NULL, then return FALSE.\r
+  If NewSm3Context is NULL, then return FALSE.\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in]  Sm3Context     Pointer to SM3 context being copied.\r
+  @param[out] NewSm3Context  Pointer to new SM3 context.\r
+\r
+  @retval TRUE   SM3 context copy succeeded.\r
+  @retval FALSE  SM3 context copy failed.\r
+  @retval FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sm3Duplicate (\r
+  IN   CONST VOID  *Sm3Context,\r
+  OUT  VOID        *NewSm3Context\r
+  )\r
+{\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (Sm3Context == NULL || NewSm3Context == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  CopyMem (NewSm3Context, Sm3Context, sizeof (SM3_CTX));\r
+\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Digests the input data and updates SM3 context.\r
+\r
+  This function performs SM3 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
+  SM3 context should be already correctly initialized by Sm3Init(), and should not be finalized\r
+  by Sm3Final(). Behavior with invalid context is undefined.\r
+\r
+  If Sm3Context is NULL, then return FALSE.\r
+\r
+  @param[in, out]  Sm3Context     Pointer to the SM3 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   SM3 data digest succeeded.\r
+  @retval FALSE  SM3 data digest failed.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sm3Update (\r
+  IN OUT  VOID        *Sm3Context,\r
+  IN      CONST VOID  *Data,\r
+  IN      UINTN       DataSize\r
+  )\r
+{\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (Sm3Context == 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 SM3 Hash Update\r
+  //\r
+  sm3_update ((SM3_CTX *) Sm3Context, Data, DataSize);\r
+\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Completes computation of the SM3 digest value.\r
+\r
+  This function completes SM3 hash computation and retrieves the digest value into\r
+  the specified memory. After this function has been called, the SM3 context cannot\r
+  be used again.\r
+  SM3 context should be already correctly initialized by Sm3Init(), and should not be\r
+  finalized by Sm3Final(). Behavior with invalid SM3 context is undefined.\r
+\r
+  If Sm3Context is NULL, then return FALSE.\r
+  If HashValue is NULL, then return FALSE.\r
+\r
+  @param[in, out]  Sm3Context     Pointer to the SM3 context.\r
+  @param[out]      HashValue      Pointer to a buffer that receives the SM3 digest\r
+                                  value (32 bytes).\r
+\r
+  @retval TRUE   SM3 digest computation succeeded.\r
+  @retval FALSE  SM3 digest computation failed.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sm3Final (\r
+  IN OUT  VOID   *Sm3Context,\r
+  OUT     UINT8  *HashValue\r
+  )\r
+{\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (Sm3Context == NULL || HashValue == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // Openssl SM3 Hash Finalization\r
+  //\r
+  sm3_final (HashValue, (SM3_CTX *) Sm3Context);\r
+\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Computes the SM3 message digest of a input data buffer.\r
+\r
+  This function performs the SM3 message digest of a given data buffer, and places\r
+  the digest value into the specified memory.\r
+\r
+  If this interface is not supported, then return FALSE.\r
+\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
+  @param[out]  HashValue   Pointer to a buffer that receives the SM3 digest\r
+                           value (32 bytes).\r
+\r
+  @retval TRUE   SM3 digest computation succeeded.\r
+  @retval FALSE  SM3 digest computation failed.\r
+  @retval FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sm3HashAll (\r
+  IN   CONST VOID  *Data,\r
+  IN   UINTN       DataSize,\r
+  OUT  UINT8       *HashValue\r
+  )\r
+{\r
+  SM3_CTX Ctx;\r
+\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (HashValue == NULL) {\r
+    return FALSE;\r
+  }\r
+  if (Data == NULL && DataSize != 0) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // SM3 Hash Computation.\r
+  //\r
+  sm3_init(&Ctx);\r
+\r
+  sm3_update(&Ctx, Data, DataSize);\r
+\r
+  sm3_final(HashValue, &Ctx);\r
+\r
+  return TRUE;\r
+}\r
index 8b5278a0c1d52a3903034886de15bf90124254b5..4c43537476229989d5f57dadd0d0fce615188ded 100644 (file)
@@ -38,6 +38,7 @@
   Hash/CryptMd5.c\r
   Hash/CryptSha1.c\r
   Hash/CryptSha256.c\r
+  Hash/CryptSm3.c\r
   Hash/CryptSha512.c\r
   Hmac/CryptHmacMd5Null.c\r
   Hmac/CryptHmacSha1Null.c\r
index bc08f9d2ccd65bc4305e8112ed44ab217457b0d2..a59079d99e05501c53c4398cbb1bb3902ad3b5da 100644 (file)
@@ -37,6 +37,7 @@
   Hash/CryptMd5.c\r
   Hash/CryptSha1.c\r
   Hash/CryptSha256.c\r
+  Hash/CryptSm3.c\r
   Hash/CryptSha512Null.c\r
   Hmac/CryptHmacMd5Null.c\r
   Hmac/CryptHmacSha1Null.c\r
index d74ca3e2e41d12153148ef95c51eef58cac824d8..3fd7d65abfcaaec406b10c936e5d2dd610fdc122 100644 (file)
@@ -37,6 +37,7 @@
   Hash/CryptMd5.c\r
   Hash/CryptSha1.c\r
   Hash/CryptSha256.c\r
+  Hash/CryptSm3.c\r
   Hash/CryptSha512Null.c\r
   Hmac/CryptHmacMd5Null.c\r
   Hmac/CryptHmacSha1Null.c\r