From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.byosoft.com.cn (mail.byosoft.com.cn [58.240.74.243]) by mx.groups.io with SMTP id smtpd.web10.9361.1598491634234900309 for ; Wed, 26 Aug 2020 18:27:15 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=none, err=permanent DNS error (domain: byosoft.com.cn, ip: 58.240.74.243, mailfrom: gaoliming@byosoft.com.cn) Received: from DESKTOPS6D0PVI ([58.246.60.130]) (envelope-sender ) by 192.168.6.13 with ESMTP for ; Thu, 27 Aug 2020 09:27:04 +0800 X-WM-Sender: gaoliming@byosoft.com.cn X-WM-AuthFlag: YES X-WM-AuthUser: gaoliming@byosoft.com.cn From: "gaoliming" To: , Cc: "'Ard Biesheuvel'" , "'Michael D Kinney'" , "'Liming Gao'" , "'Zhiguang Liu'" References: <20200826205501.1124-1-matthewfcarlson@gmail.com> <20200826205501.1124-2-matthewfcarlson@gmail.com> In-Reply-To: <20200826205501.1124-2-matthewfcarlson@gmail.com> Subject: =?UTF-8?B?5Zue5aSNOiBbZWRrMi1kZXZlbF0gW1BBVENIIHY5IDEvNV0gTWRlUGtnOiBUaW1lclJuZ0xpYjogQWRkZWQgUm5nTGliIHRoYXQgdXNlcyBUaW1lckxpYg==?= Date: Thu, 27 Aug 2020 09:27:06 +0800 Message-ID: <002e01d67c11$2d768a80$88639f80$@byosoft.com.cn> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQHyfPnFr5/2IqzimbBvrQYaUqkljwI9hCyMqQE7Z1A= Content-Type: text/plain; charset="gb2312" Content-Transfer-Encoding: quoted-printable Content-Language: zh-cn Matthew: > -----=D3=CA=BC=FE=D4=AD=BC=FE----- > =B7=A2=BC=FE=C8=CB: bounce+27952+64653+4905953+8761045@groups.io > =B4=FA=B1=ED Matthew > Carlson > =B7=A2=CB=CD=CA=B1=BC=E4: 2020=C4=EA8=D4=C227=C8=D5 4:55 > =CA=D5=BC=FE=C8=CB: devel@edk2.groups.io > =B3=AD=CB=CD: Ard Biesheuvel ; Michael D = Kinney > ; Liming Gao ; > Zhiguang Liu ; Matthew Carlson > > =D6=F7=CC=E2: [edk2-devel] [PATCH v9 1/5] MdePkg: TimerRngLib: Added = RngLib that > uses TimerLib >=20 > From: Matthew Carlson >=20 > Added a new RngLib that provides random numbers from the TimerLib > using the performance counter. This is meant to be used for OpenSSL > to replicate past behavior. This should not be used in production as > a real source of entropy. >=20 > Ref: https://github.com/tianocore/edk2/pull/845 > Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D1871 >=20 > Cc: Ard Biesheuvel > Cc: Michael D Kinney > Cc: Liming Gao > Cc: Zhiguang Liu > Signed-off-by: Matthew Carlson > --- > MdePkg/Library/BaseRngLibTimerLib/RngLibTimer.c | 187 > ++++++++++++++++++++ > MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf | 36 ++++ > MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.uni | 15 ++ > MdePkg/MdePkg.dsc | > 3 +- > 4 files changed, 240 insertions(+), 1 deletion(-) >=20 > diff --git a/MdePkg/Library/BaseRngLibTimerLib/RngLibTimer.c > b/MdePkg/Library/BaseRngLibTimerLib/RngLibTimer.c > new file mode 100644 > index 000000000000..aecaa427bb3f > --- /dev/null > +++ b/MdePkg/Library/BaseRngLibTimerLib/RngLibTimer.c > @@ -0,0 +1,187 @@ > +/** @file >=20 > + BaseRng Library that uses the TimerLib to provide reasonably random > numbers. >=20 > + Do not use this on a production system. >=20 > + >=20 > + Copyright (c) Microsoft Corporation. >=20 > + SPDX-License-Identifier: BSD-2-Clause-Patent >=20 > +**/ >=20 > + >=20 > +#include >=20 > +#include >=20 > +#include >=20 > +#include >=20 > + >=20 > +/** >=20 > + Using the TimerLib GetPerformanceCounterProperties() we delay >=20 > + for enough time for the PerformanceCounter to increment. >=20 > + >=20 > + If the return value from GetPerformanceCounterProperties (TimerLib) >=20 > + is zero, this function will return 10 and attempt to assert. >=20 > + **/ >=20 > +STATIC >=20 > +UINT32 >=20 > +CalculateMinimumDecentDelayInMicroseconds ( >=20 > + VOID >=20 > + ) >=20 > +{ >=20 > + UINT64 CounterHz; >=20 > + >=20 > + // Get the counter properties >=20 > + CounterHz =3D GetPerformanceCounterProperties (NULL, NULL); >=20 > + // Make sure we won't divide by zero >=20 > + if (CounterHz =3D=3D 0) { >=20 > + ASSERT(CounterHz !=3D 0); // Assert so the developer knows = something is > wrong >=20 > + return 10; // return 10 microseconds by default >=20 How about define one macro for the default value? > + } >=20 > + // Calculate the minimum delay based on 1.5 microseconds divided by = the > hertz. >=20 > + // We calculate the length of a cycle (1/CounterHz) and multiply it = by 1.5 > microseconds >=20 > + // This ensures that the performance counter has increased by at = least > one >=20 > + return (UINT32)(MAX (DivU64x64Remainder (1500000,CounterHz, NULL), > 1)); >=20 > +} >=20 > + >=20 > + >=20 > +/** >=20 > + Generates a 16-bit random number. >=20 > + >=20 > + if Rand is NULL, then ASSERT(). >=20 > + >=20 > + @param[out] Rand Buffer pointer to store the 16-bit random = value. >=20 > + >=20 > + @retval TRUE Random number generated successfully. >=20 > + @retval FALSE Failed to generate the random number. >=20 > + >=20 > +**/ >=20 > +BOOLEAN >=20 > +EFIAPI >=20 > +GetRandomNumber16 ( >=20 > + OUT UINT16 *Rand >=20 > + ) >=20 > +{ >=20 > + UINT32 Index; >=20 > + UINT8 *RandPtr; >=20 > + UINT32 DelayInMicroSeconds; >=20 > + >=20 > + ASSERT (Rand !=3D NULL); >=20 > + >=20 > + if (Rand =3D=3D NULL) { >=20 > + return FALSE; >=20 > + } >=20 > + DelayInMicroSeconds =3D CalculateMinimumDecentDelayInMicroseconds = (); >=20 > + RandPtr =3D (UINT8*)Rand; >=20 > + // Get 2 bytes of random ish data >=20 > + for (Index =3D 0; Index < 2; Index ++) { >=20 > + *RandPtr =3D (UINT8)(GetPerformanceCounter () & 0xFF); >=20 > + // Delay to give the performance counter a chance to change >=20 > + MicroSecondDelay (DelayInMicroSeconds); >=20 > + RandPtr++; >=20 > + } >=20 > + return TRUE; >=20 > +} >=20 > + >=20 > +/** >=20 > + Generates a 32-bit random number. >=20 > + >=20 > + if Rand is NULL, then ASSERT(). >=20 > + >=20 > + @param[out] Rand Buffer pointer to store the 32-bit random = value. >=20 > + >=20 > + @retval TRUE Random number generated successfully. >=20 > + @retval FALSE Failed to generate the random number. >=20 > + >=20 > +**/ >=20 > +BOOLEAN >=20 > +EFIAPI >=20 > +GetRandomNumber32 ( >=20 > + OUT UINT32 *Rand >=20 > + ) >=20 > +{ >=20 > + UINT32 Index; >=20 > + UINT8 *RandPtr; >=20 > + UINT32 DelayInMicroSeconds; >=20 > + >=20 > + ASSERT (Rand !=3D NULL); >=20 > + >=20 > + if (NULL =3D=3D Rand) { >=20 > + return FALSE; >=20 > + } >=20 > + >=20 > + RandPtr =3D (UINT8 *) Rand; >=20 > + DelayInMicroSeconds =3D CalculateMinimumDecentDelayInMicroseconds = (); >=20 > + // Get 4 bytes of random ish data >=20 > + for (Index =3D 0; Index < 4; Index ++) { >=20 > + *RandPtr =3D (UINT8)(GetPerformanceCounter () & 0xFF); >=20 > + // Delay to give the performance counter a chance to change >=20 > + MicroSecondDelay (DelayInMicroSeconds); >=20 > + RandPtr++; >=20 > + } >=20 > + return TRUE; >=20 > +} >=20 > + >=20 > +/** >=20 > + Generates a 64-bit random number. >=20 > + >=20 > + if Rand is NULL, then ASSERT(). >=20 > + >=20 > + @param[out] Rand Buffer pointer to store the 64-bit random = value. >=20 > + >=20 > + @retval TRUE Random number generated successfully. >=20 > + @retval FALSE Failed to generate the random number. >=20 > + >=20 > +**/ >=20 > +BOOLEAN >=20 > +EFIAPI >=20 > +GetRandomNumber64 ( >=20 > + OUT UINT64 *Rand >=20 > + ) >=20 > +{ >=20 > + UINT32 Index; >=20 > + UINT8 *RandPtr; >=20 > + UINT32 DelayInMicroSeconds; >=20 > + >=20 > + ASSERT (Rand !=3D NULL); >=20 > + >=20 > + if (NULL =3D=3D Rand) { >=20 > + return FALSE; >=20 > + } >=20 > + >=20 > + RandPtr =3D (UINT8 *)Rand; >=20 > + DelayInMicroSeconds =3D CalculateMinimumDecentDelayInMicroseconds = (); >=20 > + // Get 8 bytes of random ish data >=20 > + for (Index =3D 0; Index < 8; Index ++) { >=20 > + *RandPtr =3D (UINT8)(GetPerformanceCounter () & 0xFF); >=20 > + // Delay to give the performance counter a chance to change >=20 > + MicroSecondDelay (DelayInMicroSeconds); >=20 > + RandPtr++; >=20 > + } >=20 > + >=20 > + return TRUE; >=20 > +} >=20 > + >=20 > +/** >=20 > + Generates a 128-bit random number. >=20 > + >=20 > + if Rand is NULL, then ASSERT(). >=20 > + >=20 > + @param[out] Rand Buffer pointer to store the 128-bit random > value. >=20 > + >=20 > + @retval TRUE Random number generated successfully. >=20 > + @retval FALSE Failed to generate the random number. >=20 > + >=20 > +**/ >=20 > +BOOLEAN >=20 > +EFIAPI >=20 > +GetRandomNumber128 ( >=20 > + OUT UINT64 *Rand >=20 > + ) >=20 > +{ >=20 > + ASSERT (Rand !=3D NULL); >=20 > + // This should take around 80ms >=20 > + >=20 > + // Read first 64 bits >=20 > + if (!GetRandomNumber64 (Rand)) { >=20 > + return FALSE; >=20 > + } >=20 > + >=20 > + // Read second 64 bits >=20 > + return GetRandomNumber64 (++Rand); >=20 > +} >=20 > diff --git a/MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf > b/MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf > new file mode 100644 > index 000000000000..c499e5327351 > --- /dev/null > +++ b/MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf > @@ -0,0 +1,36 @@ > +## @file >=20 > +# Instance of RNG (Random Number Generator) Library. >=20 > +# >=20 > +# BaseRng Library that uses the TimerLib to provide reasonably = random > numbers. >=20 > +# Do NOT use this on a production system as this uses the system > performance >=20 > +# counter rather than a true source of random in addition to having = a weak >=20 > +# random algorithm. This is provided primarily as a source of = entropy for >=20 > +# OpenSSL for platforms that do not have a good built in RngLib as = this >=20 > +# emulates what was done before (though it isn't perfect). >=20 > +# >=20 > +# Copyright (c) Microsoft Corporation. All rights reserved.
>=20 > +# >=20 > +# SPDX-License-Identifier: BSD-2-Clause-Patent >=20 > +# >=20 > +# >=20 > +## >=20 > + >=20 > +[Defines] >=20 > + INF_VERSION =3D 1.27 >=20 > + BASE_NAME =3D BaseRngLibTimerLib >=20 > + MODULE_UNI_FILE =3D BaseRngLibTimerLib.uni >=20 > + FILE_GUID =3D > 74950C45-10FC-4AB5-B114-49C87C17409B >=20 > + MODULE_TYPE =3D BASE >=20 > + VERSION_STRING =3D 1.0 >=20 > + LIBRARY_CLASS =3D RngLib >=20 > + CONSTRUCTOR =3D BaseRngLibConstructor >=20 Please remove CONSTRUCTOR, this library instance has no constructor.=20 > + >=20 > +[Sources] >=20 > + RngLibTimer.c >=20 > + >=20 > +[Packages] >=20 > + MdePkg/MdePkg.dec >=20 > + >=20 > +[LibraryClasses] >=20 > + BaseLib >=20 > + TimerLib >=20 Please add DebugLib here, this library instance also depends on = DebugLib. Thanks Liming > diff --git a/MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.uni > b/MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.uni > new file mode 100644 > index 000000000000..fde24b9f0107 > --- /dev/null > +++ b/MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.uni > @@ -0,0 +1,15 @@ > +// @file >=20 > +// Instance of RNG (Random Number Generator) Library. >=20 > +// >=20 > +// RngLib that uses TimerLib's performance counter to provide random > numbers. >=20 > +// >=20 > +// Copyright (c) Microsoft Corporation. >=20 > +// >=20 > +// SPDX-License-Identifier: BSD-2-Clause-Patent >=20 > +// >=20 > + >=20 > + >=20 > +#string STR_MODULE_ABSTRACT #language en-US "Instance of RNG > Library" >=20 > + >=20 > +#string STR_MODULE_DESCRIPTION #language en-US "BaseRng Library > that uses the TimerLib to provide low-entropy random numbers" >=20 > + >=20 > diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc > index 472fa3777412..d7ba3a730909 100644 > --- a/MdePkg/MdePkg.dsc > +++ b/MdePkg/MdePkg.dsc > @@ -62,6 +62,8 @@ > MdePkg/Library/BasePostCodeLibPort80/BasePostCodeLibPort80.inf >=20 > MdePkg/Library/BasePrintLib/BasePrintLib.inf >=20 >=20 > MdePkg/Library/BaseReportStatusCodeLibNull/BaseReportStatusCodeLibNull > .inf >=20 > + MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf >=20 > + MdePkg/Library/BaseRngLibNull/BaseRngLibNull.inf >=20 > MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf >=20 > MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf >=20 >=20 > MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf >=20 > @@ -69,7 +71,6 @@ >=20 > MdePkg/Library/BaseUefiDecompressLib/BaseUefiTianoCustomDecompressL > ib.inf >=20 > MdePkg/Library/BaseSmbusLibNull/BaseSmbusLibNull.inf >=20 > MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf >=20 > - MdePkg/Library/BaseRngLibNull/BaseRngLibNull.inf >=20 >=20 >=20 > MdePkg/Library/DxeCoreEntryPoint/DxeCoreEntryPoint.inf >=20 > MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf >=20 > -- > 2.28.0.windows.1 >=20 >=20 > -=3D-=3D-=3D-=3D-=3D-=3D > Groups.io Links: You receive all messages sent to this group. >=20 > View/Reply Online (#64653): = https://edk2.groups.io/g/devel/message/64653 > Mute This Topic: https://groups.io/mt/76437900/4905953 > Group Owner: devel+owner@edk2.groups.io > Unsubscribe: https://edk2.groups.io/g/devel/unsub > [gaoliming@byosoft.com.cn] > -=3D-=3D-=3D-=3D-=3D-=3D