///\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
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
--- /dev/null
+/** @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