* [PATCH 1/2] SecurityPkg: Add RNG support
@ 2022-02-23 22:43 Sean Rhodes
2022-02-23 22:43 ` [PATCH 2/2] UefiPayloadPkg: Hook up " Sean Rhodes
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Sean Rhodes @ 2022-02-23 22:43 UTC (permalink / raw)
To: devel; +Cc: guo.dong, Patrick Rudolph, Jiewen Yao, Jian J Wang
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.
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Guo Dong <guo.dong@intel.com>
Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
---
SecurityPkg/Library/BaseRngLib/BaseRng.c | 199 ++++++++++++++++++
SecurityPkg/Library/BaseRngLib/BaseRngLib.inf | 32 +++
SecurityPkg/Library/BaseRngLib/BaseRngLib.uni | 17 ++
3 files changed, 248 insertions(+)
create mode 100644 SecurityPkg/Library/BaseRngLib/BaseRng.c
create mode 100644 SecurityPkg/Library/BaseRngLib/BaseRngLib.inf
create mode 100644 SecurityPkg/Library/BaseRngLib/BaseRngLib.uni
diff --git a/SecurityPkg/Library/BaseRngLib/BaseRng.c b/SecurityPkg/Library/BaseRngLib/BaseRng.c
new file mode 100644
index 0000000000..c21e713cb0
--- /dev/null
+++ b/SecurityPkg/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/SecurityPkg/Library/BaseRngLib/BaseRngLib.inf b/SecurityPkg/Library/BaseRngLib/BaseRngLib.inf
new file mode 100644
index 0000000000..67a91ccfff
--- /dev/null
+++ b/SecurityPkg/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/SecurityPkg/Library/BaseRngLib/BaseRngLib.uni b/SecurityPkg/Library/BaseRngLib/BaseRngLib.uni
new file mode 100644
index 0000000000..f3ed954c52
--- /dev/null
+++ b/SecurityPkg/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"
+
--
2.32.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 2/2] UefiPayloadPkg: Hook up RNG support
2022-02-23 22:43 [PATCH 1/2] SecurityPkg: Add RNG support Sean Rhodes
@ 2022-02-23 22:43 ` Sean Rhodes
2022-02-23 23:18 ` [edk2-devel] " Ma, Maurice
2022-03-21 9:37 ` [edk2-devel] [PATCH 1/2] SecurityPkg: Add " Sean Rhodes
2022-05-17 12:51 ` Yao, Jiewen
2 siblings, 1 reply; 8+ messages in thread
From: Sean Rhodes @ 2022-02-23 22:43 UTC (permalink / raw)
To: devel
Cc: guo.dong, Patrick Rudolph, Jiewen Yao, Jian J Wang, Ray Ni,
Maurice Ma, Benjamin You
From: Patrick Rudolph <patrick.rudolph@9elements.com>
Hoop Up RNG from SecurityPkg.
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Guo Dong <guo.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Maurice Ma <maurice.ma@intel.com>
Cc: Benjamin You <benjamin.you@intel.com>
Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
---
UefiPayloadPkg/UefiPayloadPkg.dsc | 8 ++++++++
UefiPayloadPkg/UefiPayloadPkg.fdf | 4 ++++
2 files changed, 12 insertions(+)
diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc b/UefiPayloadPkg/UefiPayloadPkg.dsc
index 1ce96a51c1..0d4b4da24f 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.dsc
+++ b/UefiPayloadPkg/UefiPayloadPkg.dsc
@@ -703,6 +703,14 @@
MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf
!endif
+ #
+ # Random Number Generator
+ #
+ SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf {
+ <LibraryClasses>
+ RngLib|SecurityPkg/Library/BaseRngLib/BaseRngLib.inf
+ }
+
#------------------------------
# Build the shell
#------------------------------
diff --git a/UefiPayloadPkg/UefiPayloadPkg.fdf b/UefiPayloadPkg/UefiPayloadPkg.fdf
index c7b04978ad..6af1a8c8aa 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.fdf
+++ b/UefiPayloadPkg/UefiPayloadPkg.fdf
@@ -229,6 +229,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
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [edk2-devel] [PATCH 2/2] UefiPayloadPkg: Hook up RNG support
2022-02-23 22:43 ` [PATCH 2/2] UefiPayloadPkg: Hook up " Sean Rhodes
@ 2022-02-23 23:18 ` Ma, Maurice
2022-05-17 12:24 ` Sheng Lean Tan
0 siblings, 1 reply; 8+ messages in thread
From: Ma, Maurice @ 2022-02-23 23:18 UTC (permalink / raw)
To: Rhodes, Sean
Cc: Dong, Guo, Patrick Rudolph, Yao, Jiewen, Wang, Jian J, Ni, Ray,
You, Benjamin, devel@edk2.groups.io
Reviewed-by: Maurice Ma <maurice.ma@intel.com>
> -----Original Message-----
> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Sean
> Rhodes
> Sent: Wednesday, February 23, 2022 14:44
> To: devel@edk2.groups.io
> Cc: Dong, Guo <guo.dong@intel.com>; Patrick Rudolph
> <patrick.rudolph@9elements.com>; Yao, Jiewen <jiewen.yao@intel.com>;
> Wang, Jian J <jian.j.wang@intel.com>; Ni, Ray <ray.ni@intel.com>; Ma,
> Maurice <maurice.ma@intel.com>; You, Benjamin
> <benjamin.you@intel.com>
> Subject: [edk2-devel] [PATCH 2/2] UefiPayloadPkg: Hook up RNG support
>
> From: Patrick Rudolph <patrick.rudolph@9elements.com>
>
> Hoop Up RNG from SecurityPkg.
>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Guo Dong <guo.dong@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Maurice Ma <maurice.ma@intel.com>
> Cc: Benjamin You <benjamin.you@intel.com>
> Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
> ---
> UefiPayloadPkg/UefiPayloadPkg.dsc | 8 ++++++++
> UefiPayloadPkg/UefiPayloadPkg.fdf | 4 ++++
> 2 files changed, 12 insertions(+)
>
> diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc
> b/UefiPayloadPkg/UefiPayloadPkg.dsc
> index 1ce96a51c1..0d4b4da24f 100644
> --- a/UefiPayloadPkg/UefiPayloadPkg.dsc
> +++ b/UefiPayloadPkg/UefiPayloadPkg.dsc
> @@ -703,6 +703,14 @@
>
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.i
> nf !endif + #+ # Random Number Generator+ #+
> SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf {+
> <LibraryClasses>+
> RngLib|SecurityPkg/Library/BaseRngLib/BaseRngLib.inf+ }+ #------------------
> ------------ # Build the shell #------------------------------diff --git
> a/UefiPayloadPkg/UefiPayloadPkg.fdf b/UefiPayloadPkg/UefiPayloadPkg.fdf
> index c7b04978ad..6af1a8c8aa 100644
> --- a/UefiPayloadPkg/UefiPayloadPkg.fdf
> +++ b/UefiPayloadPkg/UefiPayloadPkg.fdf
> @@ -229,6 +229,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
>
>
>
> -=-=-=-=-=-=
> Groups.io Links: You receive all messages sent to this group.
> View/Reply Online (#86923): https://edk2.groups.io/g/devel/message/86923
> Mute This Topic: https://groups.io/mt/89353219/1773972
> Group Owner: devel+owner@edk2.groups.io
> Unsubscribe: https://edk2.groups.io/g/devel/unsub [maurice.ma@intel.com]
> -=-=-=-=-=-=
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [edk2-devel] [PATCH 1/2] SecurityPkg: Add RNG support
2022-02-23 22:43 [PATCH 1/2] SecurityPkg: Add RNG support Sean Rhodes
2022-02-23 22:43 ` [PATCH 2/2] UefiPayloadPkg: Hook up " Sean Rhodes
@ 2022-03-21 9:37 ` Sean Rhodes
2022-05-17 12:25 ` Sheng Lean Tan
2022-05-17 12:51 ` Yao, Jiewen
2 siblings, 1 reply; 8+ messages in thread
From: Sean Rhodes @ 2022-03-21 9:37 UTC (permalink / raw)
To: Sean Rhodes, devel
[-- Attachment #1: Type: text/plain, Size: 71 bytes --]
Hi Jiewen/Jian
Would you be able to review please?
Thanks
Sean
[-- Attachment #2: Type: text/html, Size: 91 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [edk2-devel] [PATCH 2/2] UefiPayloadPkg: Hook up RNG support
2022-02-23 23:18 ` [edk2-devel] " Ma, Maurice
@ 2022-05-17 12:24 ` Sheng Lean Tan
0 siblings, 0 replies; 8+ messages in thread
From: Sheng Lean Tan @ 2022-05-17 12:24 UTC (permalink / raw)
To: Ma, Maurice, devel
[-- Attachment #1: Type: text/plain, Size: 71 bytes --]
Hi Ray/Dong Guo,
Could you help to get this merge?
Thanks,
Sheng
[-- Attachment #2: Type: text/html, Size: 87 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [edk2-devel] [PATCH 1/2] SecurityPkg: Add RNG support
2022-03-21 9:37 ` [edk2-devel] [PATCH 1/2] SecurityPkg: Add " Sean Rhodes
@ 2022-05-17 12:25 ` Sheng Lean Tan
0 siblings, 0 replies; 8+ messages in thread
From: Sheng Lean Tan @ 2022-05-17 12:25 UTC (permalink / raw)
To: Sean Rhodes, devel
[-- Attachment #1: Type: text/plain, Size: 84 bytes --]
Hi Jiewen/ Jian J,
This patch has been hanging around for months, could you help?
[-- Attachment #2: Type: text/html, Size: 88 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [edk2-devel] [PATCH 1/2] SecurityPkg: Add RNG support
2022-02-23 22:43 [PATCH 1/2] SecurityPkg: Add RNG support Sean Rhodes
2022-02-23 22:43 ` [PATCH 2/2] UefiPayloadPkg: Hook up " Sean Rhodes
2022-03-21 9:37 ` [edk2-devel] [PATCH 1/2] SecurityPkg: Add " Sean Rhodes
@ 2022-05-17 12:51 ` Yao, Jiewen
2023-04-25 17:11 ` Benjamin Doron
2 siblings, 1 reply; 8+ messages in thread
From: Yao, Jiewen @ 2022-05-17 12:51 UTC (permalink / raw)
To: devel@edk2.groups.io, Rhodes, Sean
Cc: Dong, Guo, Rudolph, Patrick, Wang, Jian J
Some comment:
1) Please note that global variable is not allowed in BaseLib.
+STATIC BOOLEAN mHasRdRand;
2) We already have https://github.com/tianocore/edk2/tree/master/MdePkg/Library/BaseRngLib
Why we need another BaseRngLib ?
> -----Original Message-----
> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Sean
> Rhodes
> Sent: Thursday, February 24, 2022 6:44 AM
> To: devel@edk2.groups.io
> Cc: Dong, Guo <guo.dong@intel.com>; Patrick Rudolph
> <patrick.rudolph@9elements.com>; Yao, Jiewen <jiewen.yao@intel.com>;
> Wang, Jian J <jian.j.wang@intel.com>
> Subject: [edk2-devel] [PATCH 1/2] SecurityPkg: 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.
>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Guo Dong <guo.dong@intel.com>
> Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
> ---
> SecurityPkg/Library/BaseRngLib/BaseRng.c | 199 ++++++++++++++++++
> SecurityPkg/Library/BaseRngLib/BaseRngLib.inf | 32 +++
> SecurityPkg/Library/BaseRngLib/BaseRngLib.uni | 17 ++
> 3 files changed, 248 insertions(+)
> create mode 100644 SecurityPkg/Library/BaseRngLib/BaseRng.c
> create mode 100644 SecurityPkg/Library/BaseRngLib/BaseRngLib.inf
> create mode 100644 SecurityPkg/Library/BaseRngLib/BaseRngLib.uni
>
> diff --git a/SecurityPkg/Library/BaseRngLib/BaseRng.c
> b/SecurityPkg/Library/BaseRngLib/BaseRng.c
> new file mode 100644
> index 0000000000..c21e713cb0
> --- /dev/null
> +++ b/SecurityPkg/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/SecurityPkg/Library/BaseRngLib/BaseRngLib.inf
> b/SecurityPkg/Library/BaseRngLib/BaseRngLib.inf
> new file mode 100644
> index 0000000000..67a91ccfff
> --- /dev/null
> +++ b/SecurityPkg/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/SecurityPkg/Library/BaseRngLib/BaseRngLib.uni
> b/SecurityPkg/Library/BaseRngLib/BaseRngLib.uni
> new file mode 100644
> index 0000000000..f3ed954c52
> --- /dev/null
> +++ b/SecurityPkg/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"
>
> +
>
> --
> 2.32.0
>
>
>
> -=-=-=-=-=-=
> Groups.io Links: You receive all messages sent to this group.
> View/Reply Online (#86924): https://edk2.groups.io/g/devel/message/86924
> Mute This Topic: https://groups.io/mt/89353220/1772286
> Group Owner: devel+owner@edk2.groups.io
> Unsubscribe: https://edk2.groups.io/g/devel/unsub [jiewen.yao@intel.com]
> -=-=-=-=-=-=
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [edk2-devel] [PATCH 1/2] SecurityPkg: Add RNG support
2022-05-17 12:51 ` Yao, Jiewen
@ 2023-04-25 17:11 ` Benjamin Doron
0 siblings, 0 replies; 8+ messages in thread
From: Benjamin Doron @ 2023-04-25 17:11 UTC (permalink / raw)
To: Yao, Jiewen, devel
[-- Attachment #1: Type: text/plain, Size: 117 bytes --]
Please see https://edk2.groups.io/g/devel/message/103583 and https://edk2.groups.io/g/devel/message/103584. Thanks.
[-- Attachment #2: Type: text/html, Size: 314 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2023-04-25 17:11 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-02-23 22:43 [PATCH 1/2] SecurityPkg: Add RNG support Sean Rhodes
2022-02-23 22:43 ` [PATCH 2/2] UefiPayloadPkg: Hook up " Sean Rhodes
2022-02-23 23:18 ` [edk2-devel] " Ma, Maurice
2022-05-17 12:24 ` Sheng Lean Tan
2022-03-21 9:37 ` [edk2-devel] [PATCH 1/2] SecurityPkg: Add " Sean Rhodes
2022-05-17 12:25 ` Sheng Lean Tan
2022-05-17 12:51 ` Yao, Jiewen
2023-04-25 17:11 ` Benjamin Doron
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox