From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail05.groups.io (mail05.groups.io [45.79.224.7]) by spool.mail.gandi.net (Postfix) with ESMTPS id 8710B941378 for ; Fri, 17 May 2024 10:27:08 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=Xg9naRphvCsdYTms3Q77UGnSU2Wr/NfQDyAtOHrClHM=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Resent-Date:Resent-From:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20240206; t=1715941627; v=1; b=WZ9EtfGAT14a3iYZOkvOgs3TA4kpxLh80S0ACKUX/AfWCL/lvoa8l21nzLpPMcZWdnNpFh/I JloI30ho6GVJ8/BXzw+xDvkTXmXECpYBDY754d6qiQP4AZVFfVunA1uuUNDbC+uGK2pRP4gsEDq fkZxEYcrQA+dvqNEEheUIDECefBvVVOqY3bzon81MYp/yWbsMpls8SHr5Q9o1OXkptv3rWeXmjK 5tD004izHEkXcAmQdAGzdxGD2u87BYhoQBcGHIzGEgzDZgciq1soxm4bQjsyoNDNiHl7nCfQ5Ie eRlSLq8zDDzEVg171SeIJF8rIhsMixahhXO4ij7czw/0g== X-Received: by 127.0.0.2 with SMTP id 6miLYY7687511xV8xiCGokDW; Fri, 17 May 2024 03:27:07 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by mx.groups.io with SMTP id smtpd.web10.36833.1715941605520178194 for ; Fri, 17 May 2024 03:27:06 -0700 X-CSE-ConnectionGUID: hK6z7IWRT6mEAJ6Ae/ldDQ== X-CSE-MsgGUID: c+ClRS+8RHCgIXg3KMpDnQ== X-IronPort-AV: E=McAfee;i="6600,9927,11074"; a="37488648" X-IronPort-AV: E=Sophos;i="6.08,167,1712646000"; d="scan'208";a="37488648" X-Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 May 2024 03:27:06 -0700 X-CSE-ConnectionGUID: Yw1ssagQQBWS0eBSKnxq0A== X-CSE-MsgGUID: MXU7Oz+XQQGPRgFZ2SdV2g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,167,1712646000"; d="scan'208";a="32171556" X-Received: from shwdejointd777.ccr.corp.intel.com ([10.239.58.116]) by orviesa006.jf.intel.com with ESMTP; 17 May 2024 03:27:04 -0700 From: "Wenxing Hou" To: devel@edk2.groups.io Cc: Jiewen Yao , Yi Li , Jiewen Yao Subject: [edk2-devel] [PATCH v4 11/11] Add SM3 functions with openssl for Mbedtls Date: Fri, 17 May 2024 18:26:41 +0800 Message-Id: <20240517102641.4586-12-wenxing.hou@intel.com> In-Reply-To: <20240517102641.4586-1-wenxing.hou@intel.com> References: <20240517102641.4586-1-wenxing.hou@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Resent-Date: Fri, 17 May 2024 03:27:06 -0700 Resent-From: wenxing.hou@intel.com Reply-To: devel@edk2.groups.io,wenxing.hou@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: cxkLuucI7NqSEKYD8WSsvf3rx7686176AA= Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=WZ9EtfGA; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=intel.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 45.79.224.7 as permitted sender) smtp.mailfrom=bounce@groups.io REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4177 Because the Mbedlts 3.3.0 doesn't have Sm3, the Sm3 implementaion is based on Openssl. Cc: Jiewen Yao Cc: Yi Li Signed-off-by: Wenxing Hou Reviewed-by: Yi Li Acked-by: Jiewen Yao --- CryptoPkg/CryptoPkgMbedTls.dsc | 1 + .../BaseCryptLibMbedTls/BaseCryptLib.inf | 7 +- .../BaseCryptLibMbedTls/Hash/CryptSm3.c | 235 +++++++ .../BaseCryptLibMbedTls/PeiCryptLib.inf | 5 +- .../BaseCryptLibMbedTls/RuntimeCryptLib.inf | 4 +- .../BaseCryptLibMbedTls/SecCryptLib.inf | 1 - .../BaseCryptLibMbedTls/SmmCryptLib.inf | 5 +- .../SysCall/BaseMemAllocation.c | 122 ++++ .../SysCall/DummyOpensslSupport.c | 571 ++++++++++++++++++ .../SysCall/UnitTestHostCrtWrapper.c | 63 ++ .../BaseCryptLibMbedTls/TestBaseCryptLib.inf | 4 +- 11 files changed, 1010 insertions(+), 8 deletions(-) create mode 100644 CryptoPkg/Library/BaseCryptLibMbedTls/Hash/CryptSm3.c create mode 100644 CryptoPkg/Library/BaseCryptLibMbedTls/SysCall/BaseMemAl= location.c create mode 100644 CryptoPkg/Library/BaseCryptLibMbedTls/SysCall/DummyOpen= sslSupport.c create mode 100644 CryptoPkg/Library/BaseCryptLibMbedTls/SysCall/UnitTestH= ostCrtWrapper.c diff --git a/CryptoPkg/CryptoPkgMbedTls.dsc b/CryptoPkg/CryptoPkgMbedTls.dsc index 5d0ae6ff3f..c97b28c0b5 100644 --- a/CryptoPkg/CryptoPkgMbedTls.dsc +++ b/CryptoPkg/CryptoPkgMbedTls.dsc @@ -88,6 +88,7 @@ UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/U= efiRuntimeServicesTableLib.inf=0D IoLib|MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf=0D MbedTlsLib|CryptoPkg/Library/MbedTlsLib/MbedTlsLib.inf=0D + OpensslLib|CryptoPkg/Library/OpensslLib/OpensslLib.inf=0D IntrinsicLib|CryptoPkg/Library/IntrinsicLib/IntrinsicLib.inf=0D SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf=0D =0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/BaseCryptLib.inf b/Crypt= oPkg/Library/BaseCryptLibMbedTls/BaseCryptLib.inf index cb282fe648..b76b4e6067 100644 --- a/CryptoPkg/Library/BaseCryptLibMbedTls/BaseCryptLib.inf +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/BaseCryptLib.inf @@ -31,10 +31,8 @@ Hash/CryptSha1.c=0D Hash/CryptSha256.c=0D Hash/CryptSha512.c=0D - Hash/CryptSm3Null.c=0D -=0D Hash/CryptParallelHashNull.c=0D -=0D + Hash/CryptSm3.c=0D Hmac/CryptHmac.c=0D Kdf/CryptHkdf.c=0D Cipher/CryptAes.c=0D @@ -59,6 +57,8 @@ Rand/CryptRand.c=0D =0D SysCall/CrtWrapper.c=0D + SysCall/DummyOpensslSupport.c=0D + SysCall/BaseMemAllocation.c=0D SysCall/TimerWrapper.c=0D =0D [Packages]=0D @@ -72,6 +72,7 @@ UefiRuntimeServicesTableLib=0D DebugLib=0D MbedTlsLib=0D + OpensslLib=0D PrintLib=0D IntrinsicLib=0D RngLib=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/Hash/CryptSm3.c b/Crypto= Pkg/Library/BaseCryptLibMbedTls/Hash/CryptSm3.c new file mode 100644 index 0000000000..1a442d714e --- /dev/null +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/Hash/CryptSm3.c @@ -0,0 +1,235 @@ +/** @file=0D + SM3 Digest Wrapper Implementations over openssl.=0D +=0D +Copyright (c) 2024, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include "InternalCryptLib.h"=0D +#include "internal/sm3.h"=0D +=0D +/**=0D + Retrieves the size, in bytes, of the context buffer required for SM3 has= h operations.=0D +=0D + @return The size, in bytes, of the context buffer required for SM3 hash= operations.=0D +=0D +**/=0D +UINTN=0D +EFIAPI=0D +Sm3GetContextSize (=0D + VOID=0D + )=0D +{=0D + //=0D + // Retrieves Openssl SM3 Context Size=0D + //=0D + return (UINTN)(sizeof (SM3_CTX));=0D +}=0D +=0D +/**=0D + Initializes user-supplied memory pointed by Sm3Context as SM3 hash conte= xt for=0D + subsequent use.=0D +=0D + If Sm3Context is NULL, then return FALSE.=0D +=0D + @param[out] Sm3Context Pointer to SM3 context being initialized.=0D +=0D + @retval TRUE SM3 context initialization succeeded.=0D + @retval FALSE SM3 context initialization failed.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +Sm3Init (=0D + OUT VOID *Sm3Context=0D + )=0D +{=0D + //=0D + // Check input parameters.=0D + //=0D + if (Sm3Context =3D=3D NULL) {=0D + return FALSE;=0D + }=0D +=0D + //=0D + // Openssl SM3 Context Initialization=0D + //=0D + ossl_sm3_init ((SM3_CTX *)Sm3Context);=0D + return TRUE;=0D +}=0D +=0D +/**=0D + Makes a copy of an existing SM3 context.=0D +=0D + If Sm3Context is NULL, then return FALSE.=0D + If NewSm3Context is NULL, then return FALSE.=0D + If this interface is not supported, then return FALSE.=0D +=0D + @param[in] Sm3Context Pointer to SM3 context being copied.=0D + @param[out] NewSm3Context Pointer to new SM3 context.=0D +=0D + @retval TRUE SM3 context copy succeeded.=0D + @retval FALSE SM3 context copy failed.=0D + @retval FALSE This interface is not supported.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +Sm3Duplicate (=0D + IN CONST VOID *Sm3Context,=0D + OUT VOID *NewSm3Context=0D + )=0D +{=0D + //=0D + // Check input parameters.=0D + //=0D + if ((Sm3Context =3D=3D NULL) || (NewSm3Context =3D=3D NULL)) {=0D + return FALSE;=0D + }=0D +=0D + CopyMem (NewSm3Context, Sm3Context, sizeof (SM3_CTX));=0D +=0D + return TRUE;=0D +}=0D +=0D +/**=0D + Digests the input data and updates SM3 context.=0D +=0D + This function performs SM3 digest on a data buffer of the specified size= .=0D + It can be called multiple times to compute the digest of long or discont= inuous data streams.=0D + SM3 context should be already correctly initialized by Sm3Init(), and sh= ould not be finalized=0D + by Sm3Final(). Behavior with invalid context is undefined.=0D +=0D + If Sm3Context is NULL, then return FALSE.=0D +=0D + @param[in, out] Sm3Context Pointer to the SM3 context.=0D + @param[in] Data Pointer to the buffer containing the dat= a to be hashed.=0D + @param[in] DataSize Size of Data buffer in bytes.=0D +=0D + @retval TRUE SM3 data digest succeeded.=0D + @retval FALSE SM3 data digest failed.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +Sm3Update (=0D + IN OUT VOID *Sm3Context,=0D + IN CONST VOID *Data,=0D + IN UINTN DataSize=0D + )=0D +{=0D + //=0D + // Check input parameters.=0D + //=0D + if (Sm3Context =3D=3D NULL) {=0D + return FALSE;=0D + }=0D +=0D + //=0D + // Check invalid parameters, in case that only DataLength was checked in= Openssl=0D + //=0D + if ((Data =3D=3D NULL) && (DataSize !=3D 0)) {=0D + return FALSE;=0D + }=0D +=0D + //=0D + // Openssl SM3 Hash Update=0D + //=0D + ossl_sm3_update ((SM3_CTX *)Sm3Context, Data, DataSize);=0D +=0D + return TRUE;=0D +}=0D +=0D +/**=0D + Completes computation of the SM3 digest value.=0D +=0D + This function completes SM3 hash computation and retrieves the digest va= lue into=0D + the specified memory. After this function has been called, the SM3 conte= xt cannot=0D + be used again.=0D + SM3 context should be already correctly initialized by Sm3Init(), and sh= ould not be=0D + finalized by Sm3Final(). Behavior with invalid SM3 context is undefined.= =0D +=0D + If Sm3Context is NULL, then return FALSE.=0D + If HashValue is NULL, then return FALSE.=0D +=0D + @param[in, out] Sm3Context Pointer to the SM3 context.=0D + @param[out] HashValue Pointer to a buffer that receives the SM= 3 digest=0D + value (32 bytes).=0D +=0D + @retval TRUE SM3 digest computation succeeded.=0D + @retval FALSE SM3 digest computation failed.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +Sm3Final (=0D + IN OUT VOID *Sm3Context,=0D + OUT UINT8 *HashValue=0D + )=0D +{=0D + //=0D + // Check input parameters.=0D + //=0D + if ((Sm3Context =3D=3D NULL) || (HashValue =3D=3D NULL)) {=0D + return FALSE;=0D + }=0D +=0D + //=0D + // Openssl SM3 Hash Finalization=0D + //=0D + ossl_sm3_final (HashValue, (SM3_CTX *)Sm3Context);=0D +=0D + return TRUE;=0D +}=0D +=0D +/**=0D + Computes the SM3 message digest of a input data buffer.=0D +=0D + This function performs the SM3 message digest of a given data buffer, an= d places=0D + the digest value into the specified memory.=0D +=0D + If this interface is not supported, then return FALSE.=0D +=0D + @param[in] Data Pointer to the buffer containing the data to be= hashed.=0D + @param[in] DataSize Size of Data buffer in bytes.=0D + @param[out] HashValue Pointer to a buffer that receives the SM3 diges= t=0D + value (32 bytes).=0D +=0D + @retval TRUE SM3 digest computation succeeded.=0D + @retval FALSE SM3 digest computation failed.=0D + @retval FALSE This interface is not supported.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +Sm3HashAll (=0D + IN CONST VOID *Data,=0D + IN UINTN DataSize,=0D + OUT UINT8 *HashValue=0D + )=0D +{=0D + SM3_CTX Ctx;=0D +=0D + //=0D + // Check input parameters.=0D + //=0D + if (HashValue =3D=3D NULL) {=0D + return FALSE;=0D + }=0D +=0D + if ((Data =3D=3D NULL) && (DataSize !=3D 0)) {=0D + return FALSE;=0D + }=0D +=0D + //=0D + // SM3 Hash Computation.=0D + //=0D + ossl_sm3_init (&Ctx);=0D +=0D + ossl_sm3_update (&Ctx, Data, DataSize);=0D +=0D + ossl_sm3_final (HashValue, &Ctx);=0D +=0D + return TRUE;=0D +}=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/PeiCryptLib.inf b/Crypto= Pkg/Library/BaseCryptLibMbedTls/PeiCryptLib.inf index 660e11a96e..3c057660d8 100644 --- a/CryptoPkg/Library/BaseCryptLibMbedTls/PeiCryptLib.inf +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/PeiCryptLib.inf @@ -38,9 +38,9 @@ Hash/CryptMd5.c=0D Hash/CryptSha1.c=0D Hash/CryptSha256.c=0D - Hash/CryptSm3Null.c=0D Hash/CryptSha512.c=0D Hash/CryptParallelHashNull.c=0D + Hash/CryptSm3.c=0D Hmac/CryptHmac.c=0D Kdf/CryptHkdf.c=0D Cipher/CryptAes.c=0D @@ -65,6 +65,8 @@ Bn/CryptBnNull.c=0D =0D SysCall/CrtWrapper.c=0D + SysCall/DummyOpensslSupport.c=0D + SysCall/BaseMemAllocation.c=0D SysCall/ConstantTimeClock.c=0D =0D [Packages]=0D @@ -77,6 +79,7 @@ MemoryAllocationLib=0D DebugLib=0D MbedTlsLib=0D + OpensslLib=0D IntrinsicLib=0D PrintLib=0D PeiServicesTablePointerLib=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/RuntimeCryptLib.inf b/Cr= yptoPkg/Library/BaseCryptLibMbedTls/RuntimeCryptLib.inf index 280b1a9c29..824aa4fa5d 100644 --- a/CryptoPkg/Library/BaseCryptLibMbedTls/RuntimeCryptLib.inf +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/RuntimeCryptLib.inf @@ -37,9 +37,9 @@ Hash/CryptMd5.c=0D Hash/CryptSha1.c=0D Hash/CryptSha256.c=0D - Hash/CryptSm3Null.c=0D Hash/CryptSha512.c=0D Hash/CryptParallelHashNull.c=0D + Hash/CryptSm3.c=0D Hmac/CryptHmac.c=0D Kdf/CryptHkdf.c=0D Cipher/CryptAes.c=0D @@ -65,6 +65,7 @@ =0D SysCall/CrtWrapper.c=0D SysCall/TimerWrapper.c=0D + SysCall/DummyOpensslSupport.c=0D SysCall/RuntimeMemAllocation.c=0D =0D [Packages]=0D @@ -77,6 +78,7 @@ UefiRuntimeServicesTableLib=0D DebugLib=0D MbedTlsLib=0D + OpensslLib=0D IntrinsicLib=0D PrintLib=0D RngLib=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/SecCryptLib.inf b/Crypto= Pkg/Library/BaseCryptLibMbedTls/SecCryptLib.inf index 6fc073aac6..e61e07dfa4 100644 --- a/CryptoPkg/Library/BaseCryptLibMbedTls/SecCryptLib.inf +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/SecCryptLib.inf @@ -29,7 +29,6 @@ [Sources]=0D InternalCryptLib.h=0D Hash/CryptSha512.c=0D -=0D Hash/CryptMd5Null.c=0D Hash/CryptSha1Null.c=0D Hash/CryptSha256Null.c=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/SmmCryptLib.inf b/Crypto= Pkg/Library/BaseCryptLibMbedTls/SmmCryptLib.inf index 4a519b06ee..649c2a3c0c 100644 --- a/CryptoPkg/Library/BaseCryptLibMbedTls/SmmCryptLib.inf +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/SmmCryptLib.inf @@ -36,9 +36,9 @@ Hash/CryptMd5.c=0D Hash/CryptSha1.c=0D Hash/CryptSha256.c=0D - Hash/CryptSm3Null.c=0D Hash/CryptSha512.c=0D Hash/CryptParallelHashNull.c=0D + Hash/CryptSm3.c=0D Hmac/CryptHmac.c=0D Kdf/CryptHkdf.c=0D Cipher/CryptAes.c=0D @@ -63,6 +63,8 @@ Rand/CryptRand.c=0D =0D SysCall/CrtWrapper.c=0D + SysCall/DummyOpensslSupport.c=0D + SysCall/BaseMemAllocation.c=0D SysCall/ConstantTimeClock.c=0D =0D [Packages]=0D @@ -74,6 +76,7 @@ BaseMemoryLib=0D MemoryAllocationLib=0D MbedTlsLib=0D + OpensslLib=0D IntrinsicLib=0D PrintLib=0D MmServicesTableLib=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/SysCall/BaseMemAllocatio= n.c b/CryptoPkg/Library/BaseCryptLibMbedTls/SysCall/BaseMemAllocation.c new file mode 100644 index 0000000000..10fb75cd98 --- /dev/null +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/SysCall/BaseMemAllocation.c @@ -0,0 +1,122 @@ +/** @file=0D + Base Memory Allocation Routines Wrapper for Crypto library over OpenSSL= =0D + during PEI & DXE phases.=0D +=0D +Copyright (c) 2024, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +#include =0D +=0D +//=0D +// Extra header to record the memory buffer size from malloc routine.=0D +//=0D +#define CRYPTMEM_HEAD_SIGNATURE SIGNATURE_32('c','m','h','d')=0D +typedef struct {=0D + UINT32 Signature;=0D + UINT32 Reserved;=0D + UINTN Size;=0D +} CRYPTMEM_HEAD;=0D +=0D +#define CRYPTMEM_OVERHEAD sizeof(CRYPTMEM_HEAD)=0D +=0D +//=0D +// -- Memory-Allocation Routines --=0D +//=0D +=0D +/* Allocates memory blocks */=0D +void *=0D +malloc (=0D + size_t size=0D + )=0D +{=0D + CRYPTMEM_HEAD *PoolHdr;=0D + UINTN NewSize;=0D + VOID *Data;=0D +=0D + //=0D + // Adjust the size by the buffer header overhead=0D + //=0D + NewSize =3D (UINTN)(size) + CRYPTMEM_OVERHEAD;=0D +=0D + Data =3D AllocatePool (NewSize);=0D + if (Data !=3D NULL) {=0D + PoolHdr =3D (CRYPTMEM_HEAD *)Data;=0D + //=0D + // Record the memory brief information=0D + //=0D + PoolHdr->Signature =3D CRYPTMEM_HEAD_SIGNATURE;=0D + PoolHdr->Size =3D size;=0D +=0D + return (VOID *)(PoolHdr + 1);=0D + } else {=0D + //=0D + // The buffer allocation failed.=0D + //=0D + return NULL;=0D + }=0D +}=0D +=0D +/* Reallocate memory blocks */=0D +void *=0D +realloc (=0D + void *ptr,=0D + size_t size=0D + )=0D +{=0D + CRYPTMEM_HEAD *OldPoolHdr;=0D + CRYPTMEM_HEAD *NewPoolHdr;=0D + UINTN OldSize;=0D + UINTN NewSize;=0D + VOID *Data;=0D +=0D + NewSize =3D (UINTN)size + CRYPTMEM_OVERHEAD;=0D + Data =3D AllocatePool (NewSize);=0D + if (Data !=3D NULL) {=0D + NewPoolHdr =3D (CRYPTMEM_HEAD *)Data;=0D + NewPoolHdr->Signature =3D CRYPTMEM_HEAD_SIGNATURE;=0D + NewPoolHdr->Size =3D size;=0D + if (ptr !=3D NULL) {=0D + //=0D + // Retrieve the original size from the buffer header.=0D + //=0D + OldPoolHdr =3D (CRYPTMEM_HEAD *)ptr - 1;=0D + ASSERT (OldPoolHdr->Signature =3D=3D CRYPTMEM_HEAD_SIGNATURE);=0D + OldSize =3D OldPoolHdr->Size;=0D +=0D + //=0D + // Duplicate the buffer content.=0D + //=0D + CopyMem ((VOID *)(NewPoolHdr + 1), ptr, MIN (OldSize, size));=0D + FreePool ((VOID *)OldPoolHdr);=0D + }=0D +=0D + return (VOID *)(NewPoolHdr + 1);=0D + } else {=0D + //=0D + // The buffer allocation failed.=0D + //=0D + return NULL;=0D + }=0D +}=0D +=0D +/* De-allocates or frees a memory block */=0D +void=0D +free (=0D + void *ptr=0D + )=0D +{=0D + CRYPTMEM_HEAD *PoolHdr;=0D +=0D + //=0D + // In Standard C, free() handles a null pointer argument transparently. = This=0D + // is not true of FreePool() below, so protect it.=0D + //=0D + if (ptr !=3D NULL) {=0D + PoolHdr =3D (CRYPTMEM_HEAD *)ptr - 1;=0D + ASSERT (PoolHdr->Signature =3D=3D CRYPTMEM_HEAD_SIGNATURE);=0D + FreePool (PoolHdr);=0D + }=0D +}=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/SysCall/DummyOpensslSupp= ort.c b/CryptoPkg/Library/BaseCryptLibMbedTls/SysCall/DummyOpensslSupport.c new file mode 100644 index 0000000000..d3786f0e2a --- /dev/null +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/SysCall/DummyOpensslSupport.c @@ -0,0 +1,571 @@ +/**=0D +Copyright (c) 2024, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +**/=0D +=0D +#include =0D +=0D +int errno =3D 0;=0D +=0D +FILE *stderr =3D NULL;=0D +FILE *stdin =3D NULL;=0D +FILE *stdout =3D NULL;=0D +=0D +typedef=0D + int=0D +(*SORT_COMPARE)(=0D + IN VOID *Buffer1,=0D + IN VOID *Buffer2=0D + );=0D +=0D +//=0D +// Duplicated from EDKII BaseSortLib for qsort() wrapper=0D +//=0D +STATIC=0D +VOID=0D +QuickSortWorker (=0D + IN OUT VOID *BufferToSort,=0D + IN CONST UINTN Count,=0D + IN CONST UINTN ElementSize,=0D + IN SORT_COMPARE CompareFunction,=0D + IN VOID *Buffer=0D + )=0D +{=0D + VOID *Pivot;=0D + UINTN LoopCount;=0D + UINTN NextSwapLocation;=0D +=0D + ASSERT (BufferToSort !=3D NULL);=0D + ASSERT (CompareFunction !=3D NULL);=0D + ASSERT (Buffer !=3D NULL);=0D +=0D + if ((Count < 2) || (ElementSize < 1)) {=0D + return;=0D + }=0D +=0D + NextSwapLocation =3D 0;=0D +=0D + //=0D + // Pick a pivot (we choose last element)=0D + //=0D + Pivot =3D ((UINT8 *)BufferToSort + ((Count - 1) * ElementSize));=0D +=0D + //=0D + // Now get the pivot such that all on "left" are below it=0D + // and everything "right" are above it=0D + //=0D + for (LoopCount =3D 0; LoopCount < Count - 1; LoopCount++) {=0D + //=0D + // If the element is less than the pivot=0D + //=0D + if (CompareFunction ((VOID *)((UINT8 *)BufferToSort + ((LoopCount) * E= lementSize)), Pivot) <=3D 0) {=0D + //=0D + // Swap=0D + //=0D + CopyMem (Buffer, (UINT8 *)BufferToSort + (NextSwapLocation * Element= Size), ElementSize);=0D + CopyMem ((UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), (= UINT8 *)BufferToSort + ((LoopCount) * ElementSize), ElementSize);=0D + CopyMem ((UINT8 *)BufferToSort + ((LoopCount) * ElementSize), Buffer= , ElementSize);=0D +=0D + //=0D + // Increment NextSwapLocation=0D + //=0D + NextSwapLocation++;=0D + }=0D + }=0D +=0D + //=0D + // Swap pivot to its final position (NextSwapLocation)=0D + //=0D + CopyMem (Buffer, Pivot, ElementSize);=0D + CopyMem (Pivot, (UINT8 *)BufferToSort + (NextSwapLocation * ElementSize)= , ElementSize);=0D + CopyMem ((UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), Buffe= r, ElementSize);=0D +=0D + //=0D + // Now recurse on 2 partial lists. Neither of these will have the 'pivo= t' element.=0D + // IE list is sorted left half, pivot element, sorted right half...=0D + //=0D + QuickSortWorker (=0D + BufferToSort,=0D + NextSwapLocation,=0D + ElementSize,=0D + CompareFunction,=0D + Buffer=0D + );=0D +=0D + QuickSortWorker (=0D + (UINT8 *)BufferToSort + (NextSwapLocation + 1) * ElementSize,=0D + Count - NextSwapLocation - 1,=0D + ElementSize,=0D + CompareFunction,=0D + Buffer=0D + );=0D +=0D + return;=0D +}=0D +=0D +// ---------------------------------------------------------=0D +// Standard C Run-time Library Interface Wrapper=0D +// ---------------------------------------------------------=0D +=0D +//=0D +// -- String Manipulation Routines --=0D +//=0D +=0D +/* Scan a string for the last occurrence of a character */=0D +char *=0D +strrchr (=0D + const char *str,=0D + int c=0D + )=0D +{=0D + char *save;=0D +=0D + for (save =3D NULL; ; ++str) {=0D + if (*str =3D=3D c) {=0D + save =3D (char *)str;=0D + }=0D +=0D + if (*str =3D=3D 0) {=0D + return (save);=0D + }=0D + }=0D +}=0D +=0D +/* Compare first n bytes of string s1 with string s2, ignoring case */=0D +int=0D +strncasecmp (=0D + const char *s1,=0D + const char *s2,=0D + size_t n=0D + )=0D +{=0D + int Val;=0D +=0D + ASSERT (s1 !=3D NULL);=0D + ASSERT (s2 !=3D NULL);=0D +=0D + if (n !=3D 0) {=0D + do {=0D + Val =3D tolower (*s1) - tolower (*s2);=0D + if (Val !=3D 0) {=0D + return Val;=0D + }=0D +=0D + ++s1;=0D + ++s2;=0D + if (*s1 =3D=3D '\0') {=0D + break;=0D + }=0D + } while (--n !=3D 0);=0D + }=0D +=0D + return 0;=0D +}=0D +=0D +/* Read formatted data from a string */=0D +int=0D +sscanf (=0D + const char *buffer,=0D + const char *format,=0D + ...=0D + )=0D +{=0D + //=0D + // Null sscanf() function implementation to satisfy the linker, since=0D + // no direct functionality logic dependency in present UEFI cases.=0D + //=0D + return 0;=0D +}=0D +=0D +/* Maps errnum to an error-message string */=0D +char *=0D +strerror (=0D + int errnum=0D + )=0D +{=0D + return NULL;=0D +}=0D +=0D +/* Computes the length of the maximum initial segment of the string pointe= d to by s1=0D + which consists entirely of characters from the string pointed to by s2.= */=0D +size_t=0D +strspn (=0D + const char *s1,=0D + const char *s2=0D + )=0D +{=0D + UINT8 Map[32];=0D + UINT32 Index;=0D + size_t Count;=0D +=0D + for (Index =3D 0; Index < 32; Index++) {=0D + Map[Index] =3D 0;=0D + }=0D +=0D + while (*s2) {=0D + Map[*s2 >> 3] |=3D (1 << (*s2 & 7));=0D + s2++;=0D + }=0D +=0D + if (*s1) {=0D + Count =3D 0;=0D + while (Map[*s1 >> 3] & (1 << (*s1 & 7))) {=0D + Count++;=0D + s1++;=0D + }=0D +=0D + return Count;=0D + }=0D +=0D + return 0;=0D +}=0D +=0D +/* Computes the length of the maximum initial segment of the string pointe= d to by s1=0D + which consists entirely of characters not from the string pointed to by= s2. */=0D +size_t=0D +strcspn (=0D + const char *s1,=0D + const char *s2=0D + )=0D +{=0D + UINT8 Map[32];=0D + UINT32 Index;=0D + size_t Count;=0D +=0D + for (Index =3D 0; Index < 32; Index++) {=0D + Map[Index] =3D 0;=0D + }=0D +=0D + while (*s2) {=0D + Map[*s2 >> 3] |=3D (1 << (*s2 & 7));=0D + s2++;=0D + }=0D +=0D + Map[0] |=3D 1;=0D +=0D + Count =3D 0;=0D + while (!(Map[*s1 >> 3] & (1 << (*s1 & 7)))) {=0D + Count++;=0D + s1++;=0D + }=0D +=0D + return Count;=0D +}=0D +=0D +char *=0D +strcpy (=0D + char *strDest,=0D + const char *strSource=0D + )=0D +{=0D + // AsciiStrCpyS (strDest, MAX_STRING_SIZE, strSource);=0D + // return strDest;=0D + return NULL;=0D +}=0D +=0D +//=0D +// -- Character Classification Routines --=0D +//=0D +=0D +/* Determines if a particular character is a decimal-digit character */=0D +int=0D +isdigit (=0D + int c=0D + )=0D +{=0D + //=0D + // ::=3D [0-9]=0D + //=0D + return (('0' <=3D (c)) && ((c) <=3D '9'));=0D +}=0D +=0D +/* Determine if an integer represents character that is a hex digit */=0D +int=0D +isxdigit (=0D + int c=0D + )=0D +{=0D + //=0D + // ::=3D [0-9] | [a-f] | [A-F]=0D + //=0D + return ((('0' <=3D (c)) && ((c) <=3D '9')) ||=0D + (('a' <=3D (c)) && ((c) <=3D 'f')) ||=0D + (('A' <=3D (c)) && ((c) <=3D 'F')));=0D +}=0D +=0D +/* Determines if a particular character represents a space character */=0D +int=0D +isspace (=0D + int c=0D + )=0D +{=0D + //=0D + // ::=3D [ ]=0D + //=0D + return ((c) =3D=3D ' ');=0D +}=0D +=0D +/* Determine if a particular character is an alphanumeric character */=0D +int=0D +isalnum (=0D + int c=0D + )=0D +{=0D + //=0D + // ::=3D [0-9] | [a-z] | [A-Z]=0D + //=0D + return ((('0' <=3D (c)) && ((c) <=3D '9')) ||=0D + (('a' <=3D (c)) && ((c) <=3D 'z')) ||=0D + (('A' <=3D (c)) && ((c) <=3D 'Z')));=0D +}=0D +=0D +/* Determines if a particular character is in upper case */=0D +int=0D +isupper (=0D + int c=0D + )=0D +{=0D + //=0D + // :=3D [A-Z]=0D + //=0D + return (('A' <=3D (c)) && ((c) <=3D 'Z'));=0D +}=0D +=0D +//=0D +// -- Data Conversion Routines --=0D +//=0D +=0D +/* Convert strings to a long-integer value */=0D +long=0D +strtol (=0D + const char *nptr,=0D + char **endptr,=0D + int base=0D + )=0D +{=0D + //=0D + // Null strtol() function implementation to satisfy the linker, since th= ere is=0D + // no direct functionality logic dependency in present UEFI cases.=0D + //=0D + return 0;=0D +}=0D +=0D +/* Convert strings to an unsigned long-integer value */=0D +unsigned long=0D +strtoul (=0D + const char *nptr,=0D + char **endptr,=0D + int base=0D + )=0D +{=0D + //=0D + // Null strtoul() function implementation to satisfy the linker, since t= here is=0D + // no direct functionality logic dependency in present UEFI cases.=0D + //=0D + return 0;=0D +}=0D +=0D +/* Convert character to lowercase */=0D +int=0D +tolower (=0D + int c=0D + )=0D +{=0D + if (('A' <=3D (c)) && ((c) <=3D 'Z')) {=0D + return (c - ('A' - 'a'));=0D + }=0D +=0D + return (c);=0D +}=0D +=0D +//=0D +// -- Searching and Sorting Routines --=0D +//=0D +=0D +/* Performs a quick sort */=0D +void=0D +qsort (=0D + void *base,=0D + size_t num,=0D + size_t width,=0D + int ( *compare )(const void *, const void *)=0D + )=0D +{=0D + VOID *Buffer;=0D +=0D + ASSERT (base !=3D NULL);=0D + ASSERT (compare !=3D NULL);=0D +=0D + //=0D + // Use CRT-style malloc to cover BS and RT memory allocation.=0D + //=0D + Buffer =3D malloc (width);=0D + ASSERT (Buffer !=3D NULL);=0D +=0D + //=0D + // Re-use PerformQuickSort() function Implementation in EDKII BaseSortLi= b.=0D + //=0D + QuickSortWorker (base, (UINTN)num, (UINTN)width, (SORT_COMPARE)compare, = Buffer);=0D +=0D + free (Buffer);=0D + return;=0D +}=0D +=0D +//=0D +// -- Process and Environment Control Routines --=0D +//=0D +=0D +/* Get a value from the current environment */=0D +char *=0D +getenv (=0D + const char *varname=0D + )=0D +{=0D + //=0D + // Null getenv() function implementation to satisfy the linker, since th= ere is=0D + // no direct functionality logic dependency in present UEFI cases.=0D + //=0D + return NULL;=0D +}=0D +=0D +/* Get a value from the current environment */=0D +char *=0D +secure_getenv (=0D + const char *varname=0D + )=0D +{=0D + //=0D + // Null secure_getenv() function implementation to satisfy the linker, s= ince=0D + // there is no direct functionality logic dependency in present UEFI cas= es.=0D + //=0D + // From the secure_getenv() manual: 'just like getenv() except that it=0D + // returns NULL in cases where "secure execution" is required'.=0D + //=0D + return NULL;=0D +}=0D +=0D +//=0D +// -- Stream I/O Routines --=0D +//=0D +=0D +/* Write data to a stream */=0D +size_t=0D +fwrite (=0D + const void *buffer,=0D + size_t size,=0D + size_t count,=0D + FILE *stream=0D + )=0D +{=0D + return 0;=0D +}=0D +=0D +#ifdef __GNUC__=0D +=0D +typedef=0D + VOID=0D +(EFIAPI *NoReturnFuncPtr)(=0D + VOID=0D + ) __attribute__ ((__noreturn__));=0D +=0D +STATIC=0D +VOID=0D +EFIAPI=0D +NopFunction (=0D + VOID=0D + )=0D +{=0D +}=0D +=0D +void=0D +abort (=0D + void=0D + )=0D +{=0D + NoReturnFuncPtr NoReturnFunc;=0D +=0D + NoReturnFunc =3D (NoReturnFuncPtr)NopFunction;=0D +=0D + NoReturnFunc ();=0D +}=0D +=0D +#else=0D +=0D +void=0D +abort (=0D + void=0D + )=0D +{=0D + // Do nothing=0D +}=0D +=0D +#endif=0D +=0D +int=0D +fclose (=0D + FILE *f=0D + )=0D +{=0D + return 0;=0D +}=0D +=0D +FILE *=0D +fopen (=0D + const char *c,=0D + const char *m=0D + )=0D +{=0D + return NULL;=0D +}=0D +=0D +size_t=0D +fread (=0D + void *b,=0D + size_t c,=0D + size_t i,=0D + FILE *f=0D + )=0D +{=0D + return 0;=0D +}=0D +=0D +uid_t=0D +getuid (=0D + void=0D + )=0D +{=0D + return 0;=0D +}=0D +=0D +uid_t=0D +geteuid (=0D + void=0D + )=0D +{=0D + return 0;=0D +}=0D +=0D +gid_t=0D +getgid (=0D + void=0D + )=0D +{=0D + return 0;=0D +}=0D +=0D +gid_t=0D +getegid (=0D + void=0D + )=0D +{=0D + return 0;=0D +}=0D +=0D +int=0D +printf (=0D + char const *fmt,=0D + ...=0D + )=0D +{=0D + return 0;=0D +}=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/SysCall/UnitTestHostCrtW= rapper.c b/CryptoPkg/Library/BaseCryptLibMbedTls/SysCall/UnitTestHostCrtWra= pper.c new file mode 100644 index 0000000000..cd20309a6d --- /dev/null +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/SysCall/UnitTestHostCrtWrapper.c @@ -0,0 +1,63 @@ +/** @file=0D + C Run-Time Libraries (CRT) Wrapper Implementation for OpenSSL-based=0D + Cryptographic Library.=0D +=0D +Copyright (c) 2024, Intel Corporation. All rights reserved.
=0D +Copyright (c) Microsoft Corporation=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +=0D +#include =0D +#include =0D +=0D +/* Read formatted data from a string */=0D +int=0D +sscanf (=0D + const char *buffer,=0D + const char *format,=0D + ...=0D + )=0D +{=0D + //=0D + // Null sscanf() function implementation to satisfy the linker, since=0D + // no direct functionality logic dependency in present UEFI cases.=0D + //=0D + return 0;=0D +}=0D +=0D +uid_t=0D +getuid (=0D + void=0D + )=0D +{=0D + return 0;=0D +}=0D +=0D +uid_t=0D +geteuid (=0D + void=0D + )=0D +{=0D + return 0;=0D +}=0D +=0D +gid_t=0D +getgid (=0D + void=0D + )=0D +{=0D + return 0;=0D +}=0D +=0D +gid_t=0D +getegid (=0D + void=0D + )=0D +{=0D + return 0;=0D +}=0D +=0D +int errno =3D 0;=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/TestBaseCryptLib.inf b/C= ryptoPkg/Library/BaseCryptLibMbedTls/TestBaseCryptLib.inf index 4e2fb73cd6..30d15a8eff 100644 --- a/CryptoPkg/Library/BaseCryptLibMbedTls/TestBaseCryptLib.inf +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/TestBaseCryptLib.inf @@ -31,7 +31,7 @@ Hash/CryptSha1.c=0D Hash/CryptSha256.c=0D Hash/CryptSha512.c=0D - Hash/CryptSm3Null.c=0D + Hash/CryptSm3.c=0D Hash/CryptParallelHashNull.c=0D Hmac/CryptHmac.c=0D Kdf/CryptHkdf.c=0D @@ -56,6 +56,7 @@ Pk/CryptEcNull.c=0D Rand/CryptRand.c=0D SysCall/CrtWrapper.c=0D + SysCall/UnitTestHostCrtWrapper.c=0D =0D [Packages]=0D MdePkg/MdePkg.dec=0D @@ -68,6 +69,7 @@ UefiRuntimeServicesTableLib=0D DebugLib=0D MbedTlsLib=0D + OpensslLib=0D PrintLib=0D RngLib=0D =0D --=20 2.26.2.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#119038): https://edk2.groups.io/g/devel/message/119038 Mute This Topic: https://groups.io/mt/106151229/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-