public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Guo Dong" <guo.dong@intel.com>
To: "Rhodes, Sean" <sean@starlabs.systems>,
	"devel@edk2.groups.io" <devel@edk2.groups.io>
Cc: Patrick Rudolph <patrick.rudolph@9elements.com>
Subject: Re: [PATCH 14/18] UefiPayloadPkg: Add RNG support
Date: Wed, 16 Feb 2022 23:57:57 +0000	[thread overview]
Message-ID: <BYAPR11MB3622D01F5175FF8B2039C0869E359@BYAPR11MB3622.namprd11.prod.outlook.com> (raw)
In-Reply-To: <ed17ddb5ea99c1dd72c2ac5bc567fb2e01b9fdfc.1644527848.git.sean@starlabs.systems>


should we put BaseRngLib into SecurityPkg so that it could be used by other platforms?

Thanks,
Guo

-----Original Message-----
From: Sean Rhodes <sean@starlabs.systems> 
Sent: Thursday, February 10, 2022 2:28 PM
To: devel@edk2.groups.io
Cc: Dong, Guo <guo.dong@intel.com>; Patrick Rudolph <patrick.rudolph@9elements.com>
Subject: [PATCH 14/18] UefiPayloadPkg: Add RNG support

From: Patrick Rudolph <patrick.rudolph@9elements.com>

Uses the RDRAND instruction if available and install EfiRngProtocol.
The protocol may be used by iPXE or the Linux kernel to gather entropy.

Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
---
 UefiPayloadPkg/Library/BaseRngLib/BaseRng.c   | 199 ++++++++++++++++++
 .../Library/BaseRngLib/BaseRngLib.inf         |  32 +++
 .../Library/BaseRngLib/BaseRngLib.uni         |  17 ++
 UefiPayloadPkg/UefiPayloadPkg.dsc             |   8 +
 UefiPayloadPkg/UefiPayloadPkg.fdf             |   4 +
 5 files changed, 260 insertions(+)
 create mode 100644 UefiPayloadPkg/Library/BaseRngLib/BaseRng.c
 create mode 100644 UefiPayloadPkg/Library/BaseRngLib/BaseRngLib.inf
 create mode 100644 UefiPayloadPkg/Library/BaseRngLib/BaseRngLib.uni

diff --git a/UefiPayloadPkg/Library/BaseRngLib/BaseRng.c b/UefiPayloadPkg/Library/BaseRngLib/BaseRng.c
new file mode 100644
index 0000000000..c21e713cb0
--- /dev/null
+++ b/UefiPayloadPkg/Library/BaseRngLib/BaseRng.c
@@ -0,0 +1,199 @@
+/** @file

+  Random number generator services that uses RdRand instruction access

+  to provide high-quality random numbers.

+

+Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>

+SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+

+#include <Library/BaseLib.h>

+#include <Library/DebugLib.h>

+#include <Register/Intel/Cpuid.h>

+

+STATIC BOOLEAN  mHasRdRand;

+

+//

+// Bit mask used to determine if RdRand instruction is supported.

+//

+#define RDRAND_MASK  BIT30

+

+//

+// Limited retry number when valid random data is returned.

+// Uses the recommended value defined in Section 7.3.17 of "Intel 64 and IA-32

+// Architectures Software Developer's Mannual".

+//

+#define RDRAND_RETRY_LIMIT  10

+

+/**

+  The constructor function checks whether or not RDRAND instruction is supported

+  by the host hardware.

+

+  The constructor function checks whether or not RDRAND instruction is supported.

+  It will always return RETURN_SUCCESS.

+

+  @retval RETURN_SUCCESS   The constructor always returns EFI_SUCCESS.

+

+**/

+RETURN_STATUS

+EFIAPI

+BaseRngLibConstructor (

+  VOID

+  )

+{

+  UINT32  RegEax;

+  UINT32  RegEcx;

+

+  AsmCpuid (CPUID_SIGNATURE, &RegEax, NULL, NULL, NULL);

+  if (RegEax < 1) {

+    mHasRdRand = FALSE;

+    return RETURN_SUCCESS;

+  }

+

+  //

+  // Determine RDRAND support by examining bit 30 of the ECX register returned by

+  // CPUID. A value of 1 indicates that processor support RDRAND instruction.

+  //

+  AsmCpuid (CPUID_VERSION_INFO, 0, 0, &RegEcx, 0);

+

+  mHasRdRand = ((RegEcx & RDRAND_MASK) == RDRAND_MASK);

+

+  return RETURN_SUCCESS;

+}

+

+/**

+  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;

+

+  ASSERT (Rand != NULL);

+

+  if (mHasRdRand) {

+    //

+    // A loop to fetch a 16 bit random value with a retry count limit.

+    //

+    for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) {

+      if (AsmRdRand16 (Rand)) {

+        return TRUE;

+      }

+    }

+  }

+

+  return FALSE;

+}

+

+/**

+  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;

+

+  ASSERT (Rand != NULL);

+

+  if (mHasRdRand) {

+    //

+    // A loop to fetch a 32 bit random value with a retry count limit.

+    //

+    for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) {

+      if (AsmRdRand32 (Rand)) {

+        return TRUE;

+      }

+    }

+  }

+

+  return FALSE;

+}

+

+/**

+  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;

+

+  ASSERT (Rand != NULL);

+

+  if (mHasRdRand) {

+    //

+    // A loop to fetch a 64 bit random value with a retry count limit.

+    //

+    for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) {

+      if (AsmRdRand64 (Rand)) {

+        return TRUE;

+      }

+    }

+  }

+

+  return FALSE;

+}

+

+/**

+  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/UefiPayloadPkg/Library/BaseRngLib/BaseRngLib.inf b/UefiPayloadPkg/Library/BaseRngLib/BaseRngLib.inf
new file mode 100644
index 0000000000..67a91ccfff
--- /dev/null
+++ b/UefiPayloadPkg/Library/BaseRngLib/BaseRngLib.inf
@@ -0,0 +1,32 @@
+## @file

+#  Instance of RNG (Random Number Generator) Library.

+#

+#  Copyright (c) 2020 9elements Agency GmbH.<BR>

+#

+#  SPDX-License-Identifier: BSD-2-Clause-Patent

+#

+##

+

+[Defines]

+  INF_VERSION                    = 0x00010005

+  BASE_NAME                      = BaseRngLib

+  MODULE_UNI_FILE                = BaseRngLib.uni

+  FILE_GUID                      = 05C48431-DE18-4550-931A-3350E8551498

+  MODULE_TYPE                    = BASE

+  VERSION_STRING                 = 1.0

+  LIBRARY_CLASS                  = RngLib

+  CONSTRUCTOR                    = BaseRngLibConstructor

+

+#

+#  VALID_ARCHITECTURES           = IA32 X64

+#

+

+[Sources.Ia32, Sources.X64]

+  BaseRng.c

+

+[Packages]

+  MdePkg/MdePkg.dec

+

+[LibraryClasses]

+  BaseLib

+  DebugLib

diff --git a/UefiPayloadPkg/Library/BaseRngLib/BaseRngLib.uni b/UefiPayloadPkg/Library/BaseRngLib/BaseRngLib.uni
new file mode 100644
index 0000000000..f3ed954c52
--- /dev/null
+++ b/UefiPayloadPkg/Library/BaseRngLib/BaseRngLib.uni
@@ -0,0 +1,17 @@
+// /** @file

+// Instance of RNG (Random Number Generator) Library.

+//

+// BaseRng Library that uses CPU RdRand instruction access to provide

+// high-quality random numbers.

+//

+// Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>

+//

+// SPDX-License-Identifier: BSD-2-Clause-Patent

+//

+// **/

+

+

+#string STR_MODULE_ABSTRACT             #language en-US "Instance of RNG Library"

+

+#string STR_MODULE_DESCRIPTION          #language en-US "BaseRng Library that uses CPU RdRand instruction access to provide high-quality random numbers"

+

diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc b/UefiPayloadPkg/UefiPayloadPkg.dsc
index 7b57310dfd..c8562d592b 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.dsc
+++ b/UefiPayloadPkg/UefiPayloadPkg.dsc
@@ -723,6 +723,14 @@
   MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf

 !endif

 

+  #

+  # Random Number Generator

+  #

+  SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf {

+      <LibraryClasses>

+      RngLib|UefiPayloadPkg/Library/BaseRngLib/BaseRngLib.inf

+  }

+

   #------------------------------

   #  Build the shell

   #------------------------------

diff --git a/UefiPayloadPkg/UefiPayloadPkg.fdf b/UefiPayloadPkg/UefiPayloadPkg.fdf
index a71d655687..3462eded64 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.fdf
+++ b/UefiPayloadPkg/UefiPayloadPkg.fdf
@@ -232,6 +232,10 @@ INF MdeModulePkg/Bus/Usb/UsbMouseDxe/UsbMouseDxe.inf
 #

 INF  MdeModulePkg/Universal/Acpi/AcpiTableDxe/AcpiTableDxe.inf

 

+# Random Number Generator

+#

+INF SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf

+

 #

 # UEFI network modules

 #

-- 
2.32.0


  reply	other threads:[~2022-02-16 23:58 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <df21ef7edddfef8074ba94514bf73d7a5d0d6046.1644527848.git.sean@starlabs.systems>
2022-02-10 21:20 ` [PATCH 02/18] UefiPayloadPkg: Provide option to use Boot Splash Sean Rhodes
2022-02-10 21:23 ` [PATCH 03/18] MdeModulePackage: Add option to follow BGRT spec Sean Rhodes
2022-02-10 21:23 ` [PATCH 04/18] UefiPayloadPkg: Hookup BGRT build option Sean Rhodes
2022-02-10 21:23 ` [PATCH 05/18] UefiPayloadPkg: Make Boot Manager Key configurable Sean Rhodes
2022-02-10 21:24 ` [PATCH 06/18] UefiPayloadPkg: Add build option for Above 4G Memory Sean Rhodes
2022-02-10 21:24 ` [PATCH 08/18] UefiPayloadPkg: Fix case of protocol Sean Rhodes
2022-02-20 18:11   ` [edk2-devel] " Patrick Rudolph
2022-02-10 21:25 ` [PATCH 09/18] SdMmcPciDxe: Reduce timeout for SD card reset Sean Rhodes
2022-02-11  3:15   ` Wu, Hao A
2022-02-10 21:25 ` [PATCH 10/18] BlSupportSmm: fix definition of SetSmrr() Sean Rhodes
2022-02-20 18:09   ` [edk2-devel] " Patrick Rudolph
2022-02-10 21:25 ` [PATCH 11/18] UefiPayloadPkg: Make Boot Timeout configurable Sean Rhodes
2022-02-10 21:26 ` [PATCH 12/18] Ps2KbdCtrller: Make wait for SUCCESS after BAT non-fatal Sean Rhodes
2022-02-11  3:12   ` Wu, Hao A
2022-02-10 21:27 ` [PATCH 13/18] MdeModulePkg/Usb/Keyboard.c: don't request protocol before setting Sean Rhodes
2022-02-11  3:32   ` Wu, Hao A
2022-02-11 22:45     ` MrChromebox
2022-02-18  2:17       ` [edk2-devel] " Wu, Hao A
2022-02-10 21:27 ` [PATCH 14/18] UefiPayloadPkg: Add RNG support Sean Rhodes
2022-02-16 23:57   ` Guo Dong [this message]
2022-02-10 21:27 ` [PATCH 15/18] UefiPayloadPkg/Library/PlatformBootManagerLib: Remove broken VGA detection Sean Rhodes
2022-02-16 23:51   ` Guo Dong
2022-03-21  9:48     ` [edk2-devel] " Sean Rhodes
2022-03-29 17:32       ` Guo Dong
2022-03-29 17:34         ` Sean Rhodes
2022-03-29 18:01           ` Guo Dong
2022-03-29 18:04             ` Sean Rhodes
2022-03-29 18:22               ` Guo Dong
2022-02-10 21:28 ` [PATCH 16/18] UefiCpuPkg: Disable MTRR programming for UefiPayloadPkg Sean Rhodes
2022-02-10 22:06   ` [edk2-devel] " Michael D Kinney
2022-02-11  7:12     ` Patrick Rudolph
2022-02-10 21:29 ` [PATCH 17/18] BmBoot: prioritize internal boot devices over external Sean Rhodes
2022-02-10 21:29 ` [PATCH 18/18] UefiPayloadPkg: Hookup Prioritise Internal build option Sean Rhodes

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=BYAPR11MB3622D01F5175FF8B2039C0869E359@BYAPR11MB3622.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