From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 192.55.52.115, mailfrom: jian.j.wang@intel.com) Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by groups.io with SMTP; Fri, 07 Jun 2019 14:21:24 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 07 Jun 2019 14:21:23 -0700 X-ExtLoop1: 1 Received: from fmsmsx105.amr.corp.intel.com ([10.18.124.203]) by orsmga002.jf.intel.com with ESMTP; 07 Jun 2019 14:21:23 -0700 Received: from fmsmsx118.amr.corp.intel.com (10.18.116.18) by FMSMSX105.amr.corp.intel.com (10.18.124.203) with Microsoft SMTP Server (TLS) id 14.3.408.0; Fri, 7 Jun 2019 14:21:23 -0700 Received: from shsmsx103.ccr.corp.intel.com (10.239.4.69) by fmsmsx118.amr.corp.intel.com (10.18.116.18) with Microsoft SMTP Server (TLS) id 14.3.408.0; Fri, 7 Jun 2019 14:21:22 -0700 Received: from shsmsx107.ccr.corp.intel.com ([169.254.9.98]) by SHSMSX103.ccr.corp.intel.com ([169.254.4.120]) with mapi id 14.03.0415.000; Sat, 8 Jun 2019 05:21:21 +0800 From: "Wang, Jian J" To: "Lu, XiaoyuX" , "devel@edk2.groups.io" Subject: Re: [PATCH v2 1/1] CryptoPkg/BaseCryptLib: Wrap OpenSSL SM3 algorithm Thread-Topic: [PATCH v2 1/1] CryptoPkg/BaseCryptLib: Wrap OpenSSL SM3 algorithm Thread-Index: AQHVF4kQYU1PHTVfVkmcD5Wa5ohG+6aQvsJw Date: Fri, 7 Jun 2019 21:21:20 +0000 Message-ID: References: <1559290402-22919-1-git-send-email-xiaoyux.lu@intel.com> In-Reply-To: <1559290402-22919-1-git-send-email-xiaoyux.lu@intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-titus-metadata-40: eyJDYXRlZ29yeUxhYmVscyI6IiIsIk1ldGFkYXRhIjp7Im5zIjoiaHR0cDpcL1wvd3d3LnRpdHVzLmNvbVwvbnNcL0ludGVsMyIsImlkIjoiNjRjYjg3ZTEtNWEzNC00NzlkLWJkYTMtZjhiNDZmNDRhNDY1IiwicHJvcHMiOlt7Im4iOiJDVFBDbGFzc2lmaWNhdGlvbiIsInZhbHMiOlt7InZhbHVlIjoiQ1RQX05UIn1dfV19LCJTdWJqZWN0TGFiZWxzIjpbXSwiVE1DVmVyc2lvbiI6IjE3LjEwLjE4MDQuNDkiLCJUcnVzdGVkTGFiZWxIYXNoIjoiUjRmcGpaYmM0Z1RxMmNcL3M2QVpSbjVjNGZcLzVwczBZTEZ2SXAwVmlmN0hDNHBaVmRWSVZvanhvZVI2SlV0Z0hQIn0= x-ctpclassification: CTP_NT dlp-product: dlpe-windows dlp-version: 11.0.600.7 dlp-reaction: no-action x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Return-Path: jian.j.wang@intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Pushed at f0718d1d6b47745a4249f4006807a45f2245dba1 Regards, Jian > -----Original Message----- > From: Lu, XiaoyuX > Sent: Friday, May 31, 2019 4:13 PM > To: devel@edk2.groups.io > Cc: Lu, XiaoyuX ; Wang, Jian J > Subject: [PATCH v2 1/1] CryptoPkg/BaseCryptLib: Wrap OpenSSL SM3 algorith= m >=20 > REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D1861 >=20 > 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. >=20 > Cc: Jian J Wang > Signed-off-by: Xiaoyu Lu > Reviewed-by: Jian J Wang > --- > 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 | 234 > +++++++++++++++++++++ > 6 files changed, 374 insertions(+) > create mode 100644 CryptoPkg/Library/BaseCryptLib/Hash/CryptSm3.c >=20 > 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 >=20 > /// > +/// 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 > ); >=20 > +/** > + Retrieves the size, in bytes, of the context buffer required for SM3 h= ash > operations. > + > + @return The size, in bytes, of the context buffer required for SM3 ha= sh > operations. > + > +**/ > +UINTN > +EFIAPI > +Sm3GetContextSize ( > + VOID > + ); > + > +/** > + Initializes user-supplied memory pointed by Sm3Context as SM3 hash con= text > 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 si= ze. > + It can be called multiple times to compute the digest of long or disco= ntinuous > 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 d= ata to be > hashed. > + @param[in] DataSize Size of Data buffer in bytes. > + > + @retval TRUE SM3 data digest succeeded. > + @retval FALSE SM3 data digest failed. > + > +**/ > +BOOLEAN > +EFIAPI > +Sm3Update ( > + IN OUT VOID *Sm3Context, > + IN CONST VOID *Data, > + IN UINTN DataSize > + ); > + > +/** > + Completes computation of the SM3 digest value. > + > + This function completes SM3 hash computation and retrieves the digest = value > into > + the specified memory. After this function has been called, the SM3 con= text > cannot > + be used again. > + SM3 context should be already correctly initialized by Sm3Init(), and = should > not be > + finalized by Sm3Final(). Behavior with invalid SM3 context is undefine= d. > + > + If Sm3Context is NULL, then return FALSE. > + If HashValue is NULL, then return FALSE. > + > + @param[in, out] Sm3Context Pointer to the SM3 context. > + @param[out] HashValue Pointer to a buffer that receives the = SM3 digest > + value (32 bytes). > + > + @retval TRUE SM3 digest computation succeeded. > + @retval FALSE SM3 digest computation failed. > + > +**/ > +BOOLEAN > +EFIAPI > +Sm3Final ( > + IN OUT VOID *Sm3Context, > + OUT UINT8 *HashValue > + ); > + > +/** > + Computes the SM3 message digest of a input data buffer. > + > + This function performs the SM3 message digest of a given data buffer, = and > places > + the digest value into the specified memory. > + > + If this interface is not supported, then return FALSE. > + > + @param[in] Data Pointer to the buffer containing the data to = be hashed. > + @param[in] DataSize Size of Data buffer in bytes. > + @param[out] HashValue Pointer to a buffer that receives the SM3 dig= est > + value (32 bytes). > + > + @retval TRUE SM3 digest computation succeeded. > + @retval FALSE SM3 digest computation failed. > + @retval FALSE This interface is not supported. > + > +**/ > +BOOLEAN > +EFIAPI > +Sm3HashAll ( > + IN CONST VOID *Data, > + IN UINTN DataSize, > + OUT UINT8 *HashValue > + ); > + >=20 > //=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > // MAC (Message Authentication Code) Primitive >=20 > //=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > diff --git a/CryptoPkg/Library/BaseCryptLib/Hash/CryptSm3.c > b/CryptoPkg/Library/BaseCryptLib/Hash/CryptSm3.c > new file mode 100644 > index 000000000000..ef3e59ccd8f9 > --- /dev/null > +++ b/CryptoPkg/Library/BaseCryptLib/Hash/CryptSm3.c > @@ -0,0 +1,234 @@ > +/** @file > + SM3 Digest Wrapper Implementations over openssl. > + > +Copyright (c) 2019, Intel Corporation. All rights reserved.
> +SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > +#include "InternalCryptLib.h" > +#include "internal/sm3.h" > + > +/** > + Retrieves the size, in bytes, of the context buffer required for SM3 h= ash > operations. > + > + @return The size, in bytes, of the context buffer required for SM3 ha= sh > operations. > + > +**/ > +UINTN > +EFIAPI > +Sm3GetContextSize ( > + VOID > + ) > +{ > + // > + // Retrieves Openssl SM3 Context Size > + // > + return (UINTN) (sizeof (SM3_CTX)); > +} > + > +/** > + Initializes user-supplied memory pointed by Sm3Context as SM3 hash con= text > 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 > + ) > +{ > + // > + // Check input parameters. > + // > + if (Sm3Context =3D=3D NULL) { > + return FALSE; > + } > + > + // > + // Openssl SM3 Context Initialization > + // > + sm3_init ((SM3_CTX *) Sm3Context); > + return TRUE; > +} > + > +/** > + 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 > + ) > +{ > + // > + // Check input parameters. > + // > + if (Sm3Context =3D=3D NULL || NewSm3Context =3D=3D NULL) { > + return FALSE; > + } > + > + CopyMem (NewSm3Context, Sm3Context, sizeof (SM3_CTX)); > + > + return TRUE; > +} > + > +/** > + Digests the input data and updates SM3 context. > + > + This function performs SM3 digest on a data buffer of the specified si= ze. > + It can be called multiple times to compute the digest of long or disco= ntinuous > 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 d= ata to be > hashed. > + @param[in] DataSize Size of Data buffer in bytes. > + > + @retval TRUE SM3 data digest succeeded. > + @retval FALSE SM3 data digest failed. > + > +**/ > +BOOLEAN > +EFIAPI > +Sm3Update ( > + IN OUT VOID *Sm3Context, > + IN CONST VOID *Data, > + IN UINTN DataSize > + ) > +{ > + // > + // Check input parameters. > + // > + if (Sm3Context =3D=3D NULL) { > + return FALSE; > + } > + > + // > + // Check invalid parameters, in case that only DataLength was checked = in > Openssl > + // > + if (Data =3D=3D NULL && DataSize !=3D 0) { > + return FALSE; > + } > + > + // > + // Openssl SM3 Hash Update > + // > + sm3_update ((SM3_CTX *) Sm3Context, Data, DataSize); > + > + return TRUE; > +} > + > +/** > + Completes computation of the SM3 digest value. > + > + This function completes SM3 hash computation and retrieves the digest = value > into > + the specified memory. After this function has been called, the SM3 con= text > cannot > + be used again. > + SM3 context should be already correctly initialized by Sm3Init(), and = should > not be > + finalized by Sm3Final(). Behavior with invalid SM3 context is undefine= d. > + > + If Sm3Context is NULL, then return FALSE. > + If HashValue is NULL, then return FALSE. > + > + @param[in, out] Sm3Context Pointer to the SM3 context. > + @param[out] HashValue Pointer to a buffer that receives the = SM3 digest > + value (32 bytes). > + > + @retval TRUE SM3 digest computation succeeded. > + @retval FALSE SM3 digest computation failed. > + > +**/ > +BOOLEAN > +EFIAPI > +Sm3Final ( > + IN OUT VOID *Sm3Context, > + OUT UINT8 *HashValue > + ) > +{ > + // > + // Check input parameters. > + // > + if (Sm3Context =3D=3D NULL || HashValue =3D=3D NULL) { > + return FALSE; > + } > + > + // > + // Openssl SM3 Hash Finalization > + // > + sm3_final (HashValue, (SM3_CTX *) Sm3Context); > + > + return TRUE; > +} > + > +/** > + Computes the SM3 message digest of a input data buffer. > + > + This function performs the SM3 message digest of a given data buffer, = and > places > + the digest value into the specified memory. > + > + If this interface is not supported, then return FALSE. > + > + @param[in] Data Pointer to the buffer containing the data to = be hashed. > + @param[in] DataSize Size of Data buffer in bytes. > + @param[out] HashValue Pointer to a buffer that receives the SM3 dig= est > + value (32 bytes). > + > + @retval TRUE SM3 digest computation succeeded. > + @retval FALSE SM3 digest computation failed. > + @retval FALSE This interface is not supported. > + > +**/ > +BOOLEAN > +EFIAPI > +Sm3HashAll ( > + IN CONST VOID *Data, > + IN UINTN DataSize, > + OUT UINT8 *HashValue > + ) > +{ > + SM3_CTX Ctx; > + > + // > + // Check input parameters. > + // > + if (HashValue =3D=3D NULL) { > + return FALSE; > + } > + if (Data =3D=3D NULL && DataSize !=3D 0) { > + return FALSE; > + } > + > + // > + // SM3 Hash Computation. > + // > + sm3_init(&Ctx); > + > + sm3_update(&Ctx, Data, DataSize); > + > + sm3_final(HashValue, &Ctx); > + > + return TRUE; > +} > -- > 2.7.4