Re: [edk2-devel] [PATCH v1 1/1] CryptoPkg/BaseCryptLib: Wrap OpenSSL SM3 algorithm

2019-05-31 Thread Wang, Jian J
Xiaoyu,

Two small issues below. With them fixed,

Reviewed-by: Jian J Wang 

> -Original Message-
> From: Lu, XiaoyuX
> Sent: Thursday, May 30, 2019 4:24 PM
> To: devel@edk2.groups.io
> Cc: Wang, Jian J 
> Subject: [PATCH v1 1/1] CryptoPkg/BaseCryptLib: Wrap OpenSSL SM3 algorithm
> 
> 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 
> Signed-off-by: Xiaoyu Lu 
> ---
>  CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf|   1 +
>  CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf |   1 +
>  CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf |   1 +
>  CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf |   1 +
>  CryptoPkg/Include/Library/BaseCryptLib.h   | 136 
>  CryptoPkg/Library/BaseCryptLib/Hash/CryptSm3.c | 240
> +
>  6 files changed, 380 insertions(+)
>  create mode 100644 CryptoPkg/Library/BaseCryptLib/Hash/CryptSm3.c
> 
> diff --git a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> index 2a581ceac70c..964e6db73161 100644
> --- a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> +++ b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> @@ -33,6 +33,7 @@ [Sources]
>Hash/CryptSha1.c
>Hash/CryptSha256.c
>Hash/CryptSha512.c
> +  Hash/CryptSm3.c
>Hmac/CryptHmacMd5.c
>Hmac/CryptHmacSha1.c
>Hmac/CryptHmacSha256.c
> diff --git a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> index 8fdc6920ec2e..b469334b11d0 100644
> --- a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> +++ b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> @@ -38,6 +38,7 @@ [Sources]
>Hash/CryptMd5.c
>Hash/CryptSha1.c
>Hash/CryptSha256.c
> +  Hash/CryptSm3.c
>Hash/CryptSha512.c
>Hmac/CryptHmacMd5Null.c
>Hmac/CryptHmacSha1Null.c
> diff --git a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> index 9d639fd01eae..fdae33a1a52b 100644
> --- a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> +++ b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> @@ -37,6 +37,7 @@ [Sources]
>Hash/CryptMd5.c
>Hash/CryptSha1.c
>Hash/CryptSha256.c
> +  Hash/CryptSm3.c
>Hash/CryptSha512Null.c
>Hmac/CryptHmacMd5Null.c
>Hmac/CryptHmacSha1Null.c
> diff --git a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> index c9f4abb22aea..f3255ebc7e95 100644
> --- a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> +++ b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> @@ -37,6 +37,7 @@ [Sources]
>Hash/CryptMd5.c
>Hash/CryptSha1.c
>Hash/CryptSha256.c
> +  Hash/CryptSm3.c
>Hash/CryptSha512Null.c
>Hmac/CryptHmacMd5Null.c
>Hmac/CryptHmacSha1Null.c
> diff --git a/CryptoPkg/Include/Library/BaseCryptLib.h
> b/CryptoPkg/Include/Library/BaseCryptLib.h
> index 84374b283b7a..19d1afe3c8c0 100644
> --- a/CryptoPkg/Include/Library/BaseCryptLib.h
> +++ b/CryptoPkg/Include/Library/BaseCryptLib.h
> @@ -45,6 +45,11 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>  #define SHA512_DIGEST_SIZE  64
> 
>  ///
> +/// SM3 digest size in bytes
> +///
> +#define SM3_256_DIGEST_SIZE 32
> +
> +///
>  /// TDES block size in bytes
>  ///
>  #define TDES_BLOCK_SIZE 8
> @@ -885,6 +890,137 @@ Sha512HashAll (
>OUT  UINT8   *HashValue
>);
> 
> +/**
> +  Retrieves the size, in bytes, of the context buffer required for SM3 hash
> operations.
> +
> +  @return  The size, in bytes, of the context buffer required for SM3 hash
> operations.
> +
> +**/
> +UINTN
> +EFIAPI
> +Sm3GetContextSize (
> +  VOID
> +  );
> +
> +/**
> +  Initializes user-supplied memory pointed by Sm3Context as SM3 hash context
> for
> +  subsequent use.
> +
> +  If Sm3Context is NULL, then return FALSE.
> +
> +  @param[out]  Sm3Context  Pointer to SM3 context being initialized.
> +
> +  @retval TRUE   SM3 context initialization succeeded.
> +  @retval FALSE  SM3 context initialization failed.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +Sm3Init (
> +  OUT  VOID  *Sm3Context
> +  );
> +
> +/**
> +  Makes a copy of an existing SM3 context.
> +
> +  If Sm3Context is NULL, then return FALSE.
> +  If NewSm3Context is NULL, then return FALSE.
> +  If this interface is not supported, then return FALSE.
> +
> +  @param[in]  Sm3Context Pointer to SM3 context being copied.
> +  @param[out] NewSm3Context  Pointer to new SM3 context.
> +
> +  @retval TRUE   SM3 context copy succeeded.
> +  @retval FALSE  SM3 context copy failed.
> +  @retval FALSE  This interface is not supported.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +Sm3Duplicate (
> +  IN   CONST VOID  *Sm3Context,
> +  OUT  VOID*NewSm3Context
> +  );
> +
> +/**
> +  Digests the input data and updates SM3 

[edk2-devel] [PATCH v1 1/1] CryptoPkg/BaseCryptLib: Wrap OpenSSL SM3 algorithm

2019-05-30 Thread Xiaoyu Lu
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 
Signed-off-by: Xiaoyu Lu 
---
 CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf|   1 +
 CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf |   1 +
 CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf |   1 +
 CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf |   1 +
 CryptoPkg/Include/Library/BaseCryptLib.h   | 136 
 CryptoPkg/Library/BaseCryptLib/Hash/CryptSm3.c | 240 +
 6 files changed, 380 insertions(+)
 create mode 100644 CryptoPkg/Library/BaseCryptLib/Hash/CryptSm3.c

diff --git a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf 
b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
index 2a581ceac70c..964e6db73161 100644
--- a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
+++ b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
@@ -33,6 +33,7 @@ [Sources]
   Hash/CryptSha1.c
   Hash/CryptSha256.c
   Hash/CryptSha512.c
+  Hash/CryptSm3.c
   Hmac/CryptHmacMd5.c
   Hmac/CryptHmacSha1.c
   Hmac/CryptHmacSha256.c
diff --git a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf 
b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
index 8fdc6920ec2e..b469334b11d0 100644
--- a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
+++ b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
@@ -38,6 +38,7 @@ [Sources]
   Hash/CryptMd5.c
   Hash/CryptSha1.c
   Hash/CryptSha256.c
+  Hash/CryptSm3.c
   Hash/CryptSha512.c
   Hmac/CryptHmacMd5Null.c
   Hmac/CryptHmacSha1Null.c
diff --git a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf 
b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
index 9d639fd01eae..fdae33a1a52b 100644
--- a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
+++ b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
@@ -37,6 +37,7 @@ [Sources]
   Hash/CryptMd5.c
   Hash/CryptSha1.c
   Hash/CryptSha256.c
+  Hash/CryptSm3.c
   Hash/CryptSha512Null.c
   Hmac/CryptHmacMd5Null.c
   Hmac/CryptHmacSha1Null.c
diff --git a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf 
b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
index c9f4abb22aea..f3255ebc7e95 100644
--- a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
+++ b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
@@ -37,6 +37,7 @@ [Sources]
   Hash/CryptMd5.c
   Hash/CryptSha1.c
   Hash/CryptSha256.c
+  Hash/CryptSm3.c
   Hash/CryptSha512Null.c
   Hmac/CryptHmacMd5Null.c
   Hmac/CryptHmacSha1Null.c
diff --git a/CryptoPkg/Include/Library/BaseCryptLib.h 
b/CryptoPkg/Include/Library/BaseCryptLib.h
index 84374b283b7a..19d1afe3c8c0 100644
--- a/CryptoPkg/Include/Library/BaseCryptLib.h
+++ b/CryptoPkg/Include/Library/BaseCryptLib.h
@@ -45,6 +45,11 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #define SHA512_DIGEST_SIZE  64
 
 ///
+/// SM3 digest size in bytes
+///
+#define SM3_256_DIGEST_SIZE 32
+
+///
 /// TDES block size in bytes
 ///
 #define TDES_BLOCK_SIZE 8
@@ -885,6 +890,137 @@ Sha512HashAll (
   OUT  UINT8   *HashValue
   );
 
+/**
+  Retrieves the size, in bytes, of the context buffer required for SM3 hash 
operations.
+
+  @return  The size, in bytes, of the context buffer required for SM3 hash 
operations.
+
+**/
+UINTN
+EFIAPI
+Sm3GetContextSize (
+  VOID
+  );
+
+/**
+  Initializes user-supplied memory pointed by Sm3Context as SM3 hash context 
for
+  subsequent use.
+
+  If Sm3Context is NULL, then return FALSE.
+
+  @param[out]  Sm3Context  Pointer to SM3 context being initialized.
+
+  @retval TRUE   SM3 context initialization succeeded.
+  @retval FALSE  SM3 context initialization failed.
+
+**/
+BOOLEAN
+EFIAPI
+Sm3Init (
+  OUT  VOID  *Sm3Context
+  );
+
+/**
+  Makes a copy of an existing SM3 context.
+
+  If Sm3Context is NULL, then return FALSE.
+  If NewSm3Context is NULL, then return FALSE.
+  If this interface is not supported, then return FALSE.
+
+  @param[in]  Sm3Context Pointer to SM3 context being copied.
+  @param[out] NewSm3Context  Pointer to new SM3 context.
+
+  @retval TRUE   SM3 context copy succeeded.
+  @retval FALSE  SM3 context copy failed.
+  @retval FALSE  This interface is not supported.
+
+**/
+BOOLEAN
+EFIAPI
+Sm3Duplicate (
+  IN   CONST VOID  *Sm3Context,
+  OUT  VOID*NewSm3Context
+  );
+
+/**
+  Digests the input data and updates SM3 context.
+
+  This function performs SM3 digest on a data buffer of the specified size.
+  It can be called multiple times to compute the digest of long or 
discontinuous data streams.
+  SM3 context should be already correctly initialized by Sm3Init(), and should 
not be finalized
+  by Sm3Final(). Behavior with invalid context is undefined.
+
+  If Sm3Context is NULL, then return FALSE.
+
+  @param[in, out]  Sm3Context Pointer to the SM3 context.
+  @param[in]   Data   Pointer to the buffer containing the data to 
be hashed.
+  @param[in]   DataSize   Size