From: "Yao, Jiewen" <jiewen.yao@intel.com>
To: "devel@edk2.groups.io" <devel@edk2.groups.io>,
"matthewfcarlson@gmail.com" <matthewfcarlson@gmail.com>
Cc: "Kinney, Michael D" <michael.d.kinney@intel.com>,
"Gao, Liming" <liming.gao@intel.com>,
"Liu, Zhiguang" <zhiguang.liu@intel.com>
Subject: Re: [edk2-devel] [PATCH v1 2/2] MdePkg: TimerRngLib: Added RngLib that uses TimerLib
Date: Tue, 28 Jul 2020 02:06:24 +0000 [thread overview]
Message-ID: <DM5PR11MB2026438C28BD99B9AB951B588C730@DM5PR11MB2026.namprd11.prod.outlook.com> (raw)
In-Reply-To: <20200728015312.1023-3-matthewfcarlson@gmail.com>
Hi Carlson
The naming conversion for a lib instance is <Phase><Class>Lib<Instance>. See examples in MdePkg\Library.
I feel that we had better name it to be BaseRngLibTimer
Thank you
Yao Jiewen
> -----Original Message-----
> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Matthew
> Carlson
> Sent: Tuesday, July 28, 2020 9:53 AM
> To: devel@edk2.groups.io
> Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming
> <liming.gao@intel.com>; Liu, Zhiguang <zhiguang.liu@intel.com>; Matthew
> Carlson <matthewfcarlson@gmail.com>
> Subject: [edk2-devel] [PATCH v1 2/2] MdePkg: TimerRngLib: Added RngLib that
> uses TimerLib
>
> From: Matthew Carlson <macarl@microsoft.com>
>
> 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.
>
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Zhiguang Liu <zhiguang.liu@intel.com>
> Signed-off-by: Matthew Carlson <matthewfcarlson@gmail.com>
> ---
> MdePkg/Library/TimerRngLib/TimerRng.c | 153 ++++++++++++++++++++
> MdePkg/Library/TimerRngLib/TimerRngLib.inf | 37 +++++
> MdePkg/MdePkg.dsc | 2 +
> 3 files changed, 192 insertions(+)
>
> diff --git a/MdePkg/Library/TimerRngLib/TimerRng.c
> b/MdePkg/Library/TimerRngLib/TimerRng.c
> new file mode 100644
> index 000000000000..1b0f7f04c01d
> --- /dev/null
> +++ b/MdePkg/Library/TimerRngLib/TimerRng.c
> @@ -0,0 +1,153 @@
> +/** @file
>
> + BaseRng Library that uses the TimerLib to provide reasonably random
> numbers.
>
> + Do not use this on a production system.
>
> +
>
> + Copyright (c) Microsoft Corporation.
>
> + SPDX-License-Identifier: BSD-2-Clause-Patent
>
> +**/
>
> +
>
> +#include <Library/BaseLib.h>
>
> +#include <Library/DebugLib.h>
>
> +#include <Library/TimerLib.h>
>
> +#include <Uefi.h>
>
> +
>
> +/**
>
> + Generates a 16-bit random number.
>
> +
>
> + if Rand is NULL, then ASSERT().
>
> +
>
> + @param[out] Rand Buffer pointer to store the 16-bit random value.
>
> +
>
> + @retval TRUE Random number generated successfully.
>
> + @retval FALSE Failed to generate the random number.
>
> +
>
> +**/
>
> +BOOLEAN
>
> +EFIAPI
>
> +GetRandomNumber16 (
>
> + OUT UINT16 *Rand
>
> + )
>
> +{
>
> + UINT32 Index;
>
> + UINT8* RandPtr;
>
> +
>
> + ASSERT (Rand != NULL);
>
> +
>
> + if (NULL == Rand) {
>
> + return FALSE;
>
> + }
>
> +
>
> + RandPtr = (UINT8 *) Rand;
>
> + // Get 2 bytes of random ish data
>
> + for (Index = 0; Index < 2; Index ++) {
>
> + *RandPtr = (UINT8) (GetPerformanceCounter () & 0xFF);
>
> + MicroSecondDelay (4);
>
> + RandPtr++;
>
> + }
>
> + return TRUE;
>
> +}
>
> +
>
> +/**
>
> + Generates a 32-bit random number.
>
> +
>
> + if Rand is NULL, then ASSERT().
>
> +
>
> + @param[out] Rand Buffer pointer to store the 32-bit random value.
>
> +
>
> + @retval TRUE Random number generated successfully.
>
> + @retval FALSE Failed to generate the random number.
>
> +
>
> +**/
>
> +BOOLEAN
>
> +EFIAPI
>
> +GetRandomNumber32 (
>
> + OUT UINT32 *Rand
>
> + )
>
> +{
>
> + UINT32 Index;
>
> + UINT8* RandPtr;
>
> +
>
> + ASSERT (Rand != NULL);
>
> +
>
> + if (NULL == Rand) {
>
> + return FALSE;
>
> + }
>
> +
>
> + RandPtr = (UINT8 *) Rand;
>
> + // Get 4 bytes of random ish data
>
> + for (Index = 0; Index < 4; Index ++) {
>
> + *RandPtr = (UINT8) (GetPerformanceCounter () & 0xFF);
>
> + MicroSecondDelay (2);
>
> + RandPtr++;
>
> + }
>
> + return TRUE;
>
> +}
>
> +
>
> +/**
>
> + Generates a 64-bit random number.
>
> +
>
> + if Rand is NULL, then ASSERT().
>
> +
>
> + @param[out] Rand Buffer pointer to store the 64-bit random value.
>
> +
>
> + @retval TRUE Random number generated successfully.
>
> + @retval FALSE Failed to generate the random number.
>
> +
>
> +**/
>
> +BOOLEAN
>
> +EFIAPI
>
> +GetRandomNumber64 (
>
> + OUT UINT64 *Rand
>
> + )
>
> +{
>
> + UINT32 Index;
>
> + UINT8* RandPtr;
>
> +
>
> + ASSERT (Rand != NULL);
>
> +
>
> + if (NULL == Rand) {
>
> + return FALSE;
>
> + }
>
> +
>
> + RandPtr = (UINT8 *) Rand;
>
> + // Get 8 bytes of random ish data
>
> + for (Index = 0; Index < 8; Index ++) {
>
> + *RandPtr = (UINT8) (GetPerformanceCounter () & 0xFF);
>
> + MicroSecondDelay (1);
>
> + RandPtr++;
>
> + }
>
> +
>
> + return TRUE;
>
> +}
>
> +
>
> +/**
>
> + Generates a 128-bit random number.
>
> +
>
> + if Rand is NULL, then ASSERT().
>
> +
>
> + @param[out] Rand Buffer pointer to store the 128-bit random value.
>
> +
>
> + @retval TRUE Random number generated successfully.
>
> + @retval FALSE Failed to generate the random number.
>
> +
>
> +**/
>
> +BOOLEAN
>
> +EFIAPI
>
> +GetRandomNumber128 (
>
> + OUT UINT64 *Rand
>
> + )
>
> +{
>
> + ASSERT (Rand != NULL);
>
> +
>
> + //
>
> + // Read first 64 bits
>
> + //
>
> + if (!GetRandomNumber64 (Rand)) {
>
> + return FALSE;
>
> + }
>
> +
>
> + //
>
> + // Read second 64 bits
>
> + //
>
> + return GetRandomNumber64 (++Rand);
>
> +}
>
> diff --git a/MdePkg/Library/TimerRngLib/TimerRngLib.inf
> b/MdePkg/Library/TimerRngLib/TimerRngLib.inf
> new file mode 100644
> index 000000000000..a80a89b77e72
> --- /dev/null
> +++ b/MdePkg/Library/TimerRngLib/TimerRngLib.inf
> @@ -0,0 +1,37 @@
> +## @file
>
> +# Instance of RNG (Random Number Generator) Library.
>
> +#
>
> +# BaseRng Library that uses the TimerLib to provide reasonably random
> numbers.
>
> +# Do not use this on a production system.
>
> +#
>
> +# Copyright (c) Microsoft Corporation. All rights reserved.<BR>
>
> +#
>
> +# SPDX-License-Identifier: BSD-2-Clause-Patent
>
> +#
>
> +#
>
> +##
>
> +
>
> +[Defines]
>
> + INF_VERSION = 0x00010005
>
> + BASE_NAME = BaseRngLib
>
> + MODULE_UNI_FILE = BaseRngLib.uni
>
> + FILE_GUID = 74950C45-10FC-4AB5-B114-49C87C17409B
>
> + MODULE_TYPE = BASE
>
> + VERSION_STRING = 1.0
>
> + LIBRARY_CLASS = RngLib
>
> + CONSTRUCTOR = BaseRngLibConstructor
>
> +
>
> +#
>
> +# VALID_ARCHITECTURES = IA32 X64
>
> +#
>
> +
>
> +[Sources]
>
> + TimerRng.c
>
> +
>
> +[Packages]
>
> + MdePkg/MdePkg.dec
>
> +
>
> +[LibraryClasses]
>
> + BaseLib
>
> + DebugLib
>
> + TimerLib
>
> diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc
> index 472fa3777412..c46a95e52e15 100644
> --- a/MdePkg/MdePkg.dsc
> +++ b/MdePkg/MdePkg.dsc
> @@ -120,6 +120,8 @@
> MdePkg/Library/SmmLibNull/SmmLibNull.inf
>
>
> MdePkg/Library/BaseExtractGuidedSectionLib/BaseExtractGuidedSectionLib.inf
>
>
>
> + MdePkg/Library/TimerRngLib/TimerRngLib.inf
>
> +
>
>
> MdePkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoin
> t.inf
>
>
> MdePkg/Library/StandaloneMmServicesTableLib/StandaloneMmServicesTableLi
> b.inf
>
>
>
> --
> 2.27.0.windows.1
>
>
> -=-=-=-=-=-=
> Groups.io Links: You receive all messages sent to this group.
>
> View/Reply Online (#63373): https://edk2.groups.io/g/devel/message/63373
> Mute This Topic: https://groups.io/mt/75836598/1772286
> Group Owner: devel+owner@edk2.groups.io
> Unsubscribe: https://edk2.groups.io/g/devel/unsub [jiewen.yao@intel.com]
> -=-=-=-=-=-=
next prev parent reply other threads:[~2020-07-28 2:06 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-28 1:53 [PATCH v1 0/2] Use RngLib instead of TimerLib for OpensslLib Matthew Carlson
2020-07-28 1:53 ` [PATCH v1 1/2] CryptoPkg: OpensslLib: Use RngLib to generate entropy in rand_pool Matthew Carlson
2020-07-29 1:08 ` [edk2-devel] " Guomin Jiang
2020-07-28 1:53 ` [PATCH v1 2/2] MdePkg: TimerRngLib: Added RngLib that uses TimerLib Matthew Carlson
2020-07-28 2:06 ` Yao, Jiewen [this message]
2020-07-28 2:42 ` [edk2-devel] " Ni, Ray
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DM5PR11MB2026438C28BD99B9AB951B588C730@DM5PR11MB2026.namprd11.prod.outlook.com \
--to=devel@edk2.groups.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox