public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface
@ 2022-06-29 15:02 PierreGondois
  2022-06-29 15:02 ` [PATCH v3 01/22] ArmPkg: PCD to select conduit for monitor calls PierreGondois
                   ` (31 more replies)
  0 siblings, 32 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <pierre.gondois@arm.com>

Bugzilla: Bug 3668 (https://bugzilla.tianocore.org/show_bug.cgi?id=3668)

The Arm True Random Number Generator Firmware, Interface 1.0, specification
defines an interface between an Operating System (OS) executing at EL1 and
Firmware (FW) exposing a conditioned entropy source that is provided by a
TRNG back end.
This patch-set:
- defines a TRNG library class that provides an interface to access the
  entropy source on a platform.
- implements a TRNG library instance that uses the Arm FW-TRNG interface.
- Adds RawAlgorithm support to RngDxe for Arm architecture using the Arm
  FW-TRNG interface.
- Enables RNG support using FW-TRNG interface for Kvmtool Guest/Virtual
  firmware.

This patch-set is based on the v2 from Sami Mujawar:
[PATCH v2 0/8] Add Raw algorithm support using Arm FW-TRNG interface 
https://edk2.groups.io/g/devel/message/83775

This patch-set can seen at:
https://github.com/PierreARM/edk2/tree/Arm_Trng_v3

V3:
 - Address Leif's comment (moving definitions, optimizations, ...)
 - Add ArmMonitorLib to choose Hvc/Smc conduit depending on a Pcd.
 - Re-factor some parts of SecurityPkg/RngDxe/ to ease the addition
   of new algorithms.
 - Add ArmHasRngExt() function to check Arm's FEAT_RNG extension.
V2:
 - Updates TrngLib definitions to use RETURN_STATUS as the return type
   from the interface functions as TrngLib is base type library.
 - Drops the patch "MdePkg: Add definition for NULL GUID" as there is
   already an equivalent definition provided by gZeroGuid. Thus, the
   use of gNullGuid has been replaced with gZeroGuid.

Pierre Gondois (14):
  ArmPkg/ArmMonitorLib: Definition for ArmMonitorLib library class
  ArmPkg/ArmMonitorLib: Add ArmMonitorLib
  ArmPkg/ArmHvcNullLib: Add NULL instance of ArmHvcLib
  MdePkg/BaseRngLib: Rename ArmReadIdIsar0() to ArmGetFeatRng()
  ArmPkg/ArmLib: Add ArmReadIdIsar0() helper
  ArmPkg/ArmLib: Add ArmHasRngExt()
  SecurityPkg: Update Securitypkg.ci.yaml
  SecurityPkg/RngDxe: Replace Pcd with Sp80090Ctr256Guid
  SecurityPkg/RngDxe: Remove ArchGetSupportedRngAlgorithms()
  SecurityPkg/RngDxe: Documentation/include/parameter cleanup
  SecurityPkg/RngDxe: Check before advertising Cpu Rng algo
  SecurityPkg/RngDxe: Add debug warning for NULL
    PcdCpuRngSupportedAlgorithm
  SecurityPkg/RngDxe: Rename AArch64/RngDxe.c
  SecurityPkg/RngDxe: Add Arm support of RngDxe

Sami Mujawar (8):
  ArmPkg: PCD to select conduit for monitor calls
  MdePkg/TrngLib: Definition for TRNG library class interface
  MdePkg/TrngLib: Add NULL instance of TRNG Library
  ArmPkg: Add FID definitions for Firmware TRNG
  ArmPkg/TrngLib: Add Arm Firmware TRNG library
  SecurityPkg/RngDxe: Rename RdRandGenerateEntropy to generic name
  SecurityPkg/RngDxe: Add AArch64 RawAlgorithm support through TrngLib
  ArmVirtPkg: Kvmtool: Add RNG support using FW-TRNG interface

 ArmPkg/ArmPkg.dec                             |  12 +-
 ArmPkg/ArmPkg.dsc                             |   3 +
 ArmPkg/Include/IndustryStandard/ArmStdSmc.h   | 109 ++++-
 ArmPkg/Include/Library/ArmLib.h               |  12 +-
 ArmPkg/Include/Library/ArmMonitorLib.h        |  42 ++
 ArmPkg/Library/ArmFwTrngLib/ArmFwTrngDefs.h   |  50 +++
 ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.c    | 403 ++++++++++++++++++
 ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.inf  |  29 ++
 ArmPkg/Library/ArmHvcNullLib/ArmHvcNullLib.c  |  29 ++
 .../Library/ArmHvcNullLib/ArmHvcNullLib.inf   |  22 +
 ArmPkg/Library/ArmLib/AArch64/AArch64Lib.c    |  15 +-
 ArmPkg/Library/ArmLib/AArch64/AArch64Lib.h    |  14 +-
 .../Library/ArmLib/AArch64/AArch64Support.S   |   7 +-
 ArmPkg/Library/ArmLib/Arm/ArmV7Lib.c          |  16 +-
 ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.c  |  34 ++
 .../Library/ArmMonitorLib/ArmMonitorLib.inf   |  29 ++
 ArmVirtPkg/ArmVirtKvmTool.dsc                 |  10 +
 ArmVirtPkg/ArmVirtKvmTool.fdf                 |   5 +
 MdePkg/Include/Library/TrngLib.h              | 121 ++++++
 .../{ArmReadIdIsar0.S => ArmGetFeatRng.S}     |   8 +-
 .../{ArmReadIdIsar0.asm => ArmGetFeatRng.asm} |   8 +-
 MdePkg/Library/BaseRngLib/AArch64/ArmRng.h    |   2 +-
 MdePkg/Library/BaseRngLib/AArch64/Rndr.c      |   2 +-
 MdePkg/Library/BaseRngLib/BaseRngLib.inf      |   4 +-
 .../Library/BaseTrngLibNull/BaseTrngLibNull.c | 135 ++++++
 .../BaseTrngLibNull/BaseTrngLibNull.inf       |  30 ++
 .../BaseTrngLibNull/BaseTrngLibNull.uni       |  12 +
 MdePkg/MdePkg.dec                             |   5 +
 MdePkg/MdePkg.dsc                             |   1 +
 .../RngDxe/{AArch64/RngDxe.c => ArmRngDxe.c}  | 137 +++++-
 .../RandomNumberGenerator/RngDxe/ArmTrng.c    |  71 +++
 .../RngDxe/Rand/RdRand.c                      |  14 +-
 .../RngDxe/Rand/RdRand.h                      |  43 --
 .../RngDxe/Rand/RngDxe.c                      |  36 +-
 .../RandomNumberGenerator/RngDxe/RngDxe.c     |  52 +--
 .../RandomNumberGenerator/RngDxe/RngDxe.inf   |  17 +-
 .../RngDxe/RngDxeInternals.h                  |  44 +-
 SecurityPkg/SecurityPkg.ci.yaml               |   1 +
 SecurityPkg/SecurityPkg.dsc                   |  12 +-
 39 files changed, 1423 insertions(+), 173 deletions(-)
 create mode 100644 ArmPkg/Include/Library/ArmMonitorLib.h
 create mode 100644 ArmPkg/Library/ArmFwTrngLib/ArmFwTrngDefs.h
 create mode 100644 ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.c
 create mode 100644 ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.inf
 create mode 100644 ArmPkg/Library/ArmHvcNullLib/ArmHvcNullLib.c
 create mode 100644 ArmPkg/Library/ArmHvcNullLib/ArmHvcNullLib.inf
 create mode 100644 ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.c
 create mode 100644 ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.inf
 create mode 100644 MdePkg/Include/Library/TrngLib.h
 rename MdePkg/Library/BaseRngLib/AArch64/{ArmReadIdIsar0.S => ArmGetFeatRng.S} (78%)
 rename MdePkg/Library/BaseRngLib/AArch64/{ArmReadIdIsar0.asm => ArmGetFeatRng.asm} (81%)
 create mode 100644 MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.c
 create mode 100644 MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.inf
 create mode 100644 MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.uni
 rename SecurityPkg/RandomNumberGenerator/RngDxe/{AArch64/RngDxe.c => ArmRngDxe.c} (51%)
 create mode 100644 SecurityPkg/RandomNumberGenerator/RngDxe/ArmTrng.c
 delete mode 100644 SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RdRand.h

-- 
2.25.1


^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v3 01/22] ArmPkg: PCD to select conduit for monitor calls
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 02/22] ArmPkg/ArmMonitorLib: Definition for ArmMonitorLib library class PierreGondois
                   ` (30 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Sami Mujawar <sami.mujawar@arm.com>

Define a PCD 'PcdMonitorConduitHvc' to select the conduit to use for
monitor calls. PcdMonitorConduitHvc is defined as FALSE by default,
meaning the SMC conduit is enabled as default.

Adding PcdMonitorConduitHvc allows selection of HVC conduit to be used
by virtual firmware implementations.

Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
---
 ArmPkg/ArmPkg.dec | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/ArmPkg/ArmPkg.dec b/ArmPkg/ArmPkg.dec
index 9da1bbc9f216..bb5cbecbc228 100644
--- a/ArmPkg/ArmPkg.dec
+++ b/ArmPkg/ArmPkg.dec
@@ -2,7 +2,7 @@
 # ARM processor package.
 #
 # Copyright (c) 2009 - 2010, Apple Inc. All rights reserved.<BR>
-# Copyright (c) 2011 - 2021, ARM Limited. All rights reserved.
+# Copyright (c) 2011 - 2022, ARM Limited. All rights reserved.
 # Copyright (c) 2021, Ampere Computing LLC. All rights reserved.
 #
 #    SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -132,6 +132,11 @@ [PcdsFeatureFlag.common]
   # Define if the GICv3 controller should use the GICv2 legacy
   gArmTokenSpaceGuid.PcdArmGicV3WithV2Legacy|FALSE|BOOLEAN|0x00000042
 
+  ## Define the conduit to use for monitor calls.
+  # Default PcdMonitorConduitHvc = FALSE, conduit = SMC
+  # If PcdMonitorConduitHvc = TRUE, conduit = HVC
+  gArmTokenSpaceGuid.PcdMonitorConduitHvc|FALSE|BOOLEAN|0x00000047
+
 [PcdsFeatureFlag.ARM]
   # Whether to map normal memory as non-shareable. FALSE is the safe choice, but
   # TRUE may be appropriate to fix performance problems if you don't care about
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 02/22] ArmPkg/ArmMonitorLib: Definition for ArmMonitorLib library class
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
  2022-06-29 15:02 ` [PATCH v3 01/22] ArmPkg: PCD to select conduit for monitor calls PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 03/22] ArmPkg/ArmMonitorLib: Add ArmMonitorLib PierreGondois
                   ` (29 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <Pierre.Gondois@arm.com>

The ArmMonitorLib provides an abstract interface to issue
an HyperVisor Call (HVC) or System Monitor Call (SMC) depending
on the default conduit.
The PcdMonitorConduitHvc PCD allows to select the default conduit.

The new library relies on the ArmHvcLib and ArmSmcLib libraries.
A Null instance of these libraries can be used for the unused conduit.

Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
---
 ArmPkg/ArmPkg.dec                      |  5 +++
 ArmPkg/Include/Library/ArmMonitorLib.h | 42 ++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 ArmPkg/Include/Library/ArmMonitorLib.h

diff --git a/ArmPkg/ArmPkg.dec b/ArmPkg/ArmPkg.dec
index bb5cbecbc228..653942ff63c3 100644
--- a/ArmPkg/ArmPkg.dec
+++ b/ArmPkg/ArmPkg.dec
@@ -71,6 +71,11 @@ [LibraryClasses.common]
   #
   ArmSvcLib|Include/Library/ArmSvcLib.h
 
+  ##  @libraryclass  Provides a Monitor Call interface that will use the
+  #   default conduit (HVC or SMC).
+  #
+  ArmMonitorLib|Include/Library/ArmMonitorLib.h
+
   ##  @libraryclass  Provides a default exception handler.
   #
   DefaultExceptionHandlerLib|Include/Library/DefaultExceptionHandlerLib.h
diff --git a/ArmPkg/Include/Library/ArmMonitorLib.h b/ArmPkg/Include/Library/ArmMonitorLib.h
new file mode 100644
index 000000000000..8bc430f92036
--- /dev/null
+++ b/ArmPkg/Include/Library/ArmMonitorLib.h
@@ -0,0 +1,42 @@
+/** @file
+
+  Copyright (c) 2022, Arm Limited. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef ARM_MONITOR_LIB_H_
+#define ARM_MONITOR_LIB_H_
+
+/** The size of the SMC arguments is different between AArch64 and AArch32.
+
+  The native size is used for the arguments.
+  It will be casted to either HVC or SMC args.
+*/
+typedef struct {
+  UINTN    Arg0;
+  UINTN    Arg1;
+  UINTN    Arg2;
+  UINTN    Arg3;
+  UINTN    Arg4;
+  UINTN    Arg5;
+  UINTN    Arg6;
+  UINTN    Arg7;
+} ARM_MONITOR_ARGS;
+
+/** Monitor call.
+
+  An HyperVisor Call (HVC) or System Monitor Call (SMC) will be issued
+  depending on the default conduit. PcdMonitorConduitHvc determines the type
+  of the call: if true, do an HVC.
+
+  @param [in, out]  Args    Arguments for the HVC/SMC.
+**/
+VOID
+EFIAPI
+ArmMonitorCall (
+  IN OUT ARM_MONITOR_ARGS  *Args
+  );
+
+#endif // ARM_MONITOR_LIB_H_
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 03/22] ArmPkg/ArmMonitorLib: Add ArmMonitorLib
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
  2022-06-29 15:02 ` [PATCH v3 01/22] ArmPkg: PCD to select conduit for monitor calls PierreGondois
  2022-06-29 15:02 ` [PATCH v3 02/22] ArmPkg/ArmMonitorLib: Definition for ArmMonitorLib library class PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 04/22] ArmPkg/ArmHvcNullLib: Add NULL instance of ArmHvcLib PierreGondois
                   ` (28 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <Pierre.Gondois@arm.com>

The ArmMonitorLib provides an abstract interface to issue
an HyperVisor Call (HVC) or System Monitor Call (SMC) depending
on the default conduit.
The PcdMonitorConduitHvc PCD allows to select the default conduit.

Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
---
 ArmPkg/ArmPkg.dsc                             |  1 +
 ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.c  | 34 +++++++++++++++++++
 .../Library/ArmMonitorLib/ArmMonitorLib.inf   | 29 ++++++++++++++++
 3 files changed, 64 insertions(+)
 create mode 100644 ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.c
 create mode 100644 ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.inf

diff --git a/ArmPkg/ArmPkg.dsc b/ArmPkg/ArmPkg.dsc
index 59fd8f295d4f..3afd212f472b 100644
--- a/ArmPkg/ArmPkg.dsc
+++ b/ArmPkg/ArmPkg.dsc
@@ -135,6 +135,7 @@ [Components.common]
   ArmPkg/Library/ArmSmcLibNull/ArmSmcLibNull.inf
   ArmPkg/Library/ArmHvcLib/ArmHvcLib.inf
   ArmPkg/Library/ArmSvcLib/ArmSvcLib.inf
+  ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.inf
   ArmPkg/Library/OpteeLib/OpteeLib.inf
 
   ArmPkg/Filesystem/SemihostFs/SemihostFs.inf
diff --git a/ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.c b/ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.c
new file mode 100644
index 000000000000..5e91f2957325
--- /dev/null
+++ b/ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.c
@@ -0,0 +1,34 @@
+/** @file
+  Arm Monitor Library.
+
+  Copyright (c) 2022, Arm Limited. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Library/ArmHvcLib.h>
+#include <Library/ArmMonitorLib.h>
+#include <Library/ArmSmcLib.h>
+#include <Library/PcdLib.h>
+
+/** Monitor call.
+
+  An HyperVisor Call (HVC) or System Monitor Call (SMC) will be issued
+  depending on the default conduit. PcdMonitorConduitHvc determines the type
+  of the call: if true, do an HVC.
+
+  @param [in, out]  Args    Arguments for the HVC/SMC.
+**/
+VOID
+EFIAPI
+ArmMonitorCall (
+  IN OUT ARM_MONITOR_ARGS  *Args
+  )
+{
+  if (FeaturePcdGet (PcdMonitorConduitHvc)) {
+    ArmCallHvc ((ARM_HVC_ARGS *)Args);
+  } else {
+    ArmCallSmc ((ARM_SMC_ARGS *)Args);
+  }
+}
diff --git a/ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.inf b/ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.inf
new file mode 100644
index 000000000000..abaeb556d471
--- /dev/null
+++ b/ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.inf
@@ -0,0 +1,29 @@
+## @file
+#  Arm Monitor Library
+#
+#  Copyright (c) 2022, Arm Limited. All rights reserved.<BR>
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+##
+
+[Defines]
+  INF_VERSION                    = 0x0001001B
+  BASE_NAME                      = ArmMonitorLib
+  FILE_GUID                      = F918DACB-FBB8-4CB6-A61D-08E75AF0E7CD
+  MODULE_TYPE                    = BASE
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = ArmMonitorLib
+
+[Sources]
+  ArmMonitorLib.c
+
+[Packages]
+  ArmPkg/ArmPkg.dec
+  MdePkg/MdePkg.dec
+
+[LibraryClasses]
+  ArmHvcLib
+  ArmSmcLib
+
+[Pcd]
+  gArmTokenSpaceGuid.PcdMonitorConduitHvc
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 04/22] ArmPkg/ArmHvcNullLib: Add NULL instance of ArmHvcLib
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (2 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 03/22] ArmPkg/ArmMonitorLib: Add ArmMonitorLib PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 05/22] MdePkg/TrngLib: Definition for TRNG library class interface PierreGondois
                   ` (27 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <Pierre.Gondois@arm.com>

Add a Null instance of ArmHvcLib in case of library dependencies.

Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
---
 ArmPkg/ArmPkg.dsc                             |  1 +
 ArmPkg/Library/ArmHvcNullLib/ArmHvcNullLib.c  | 29 +++++++++++++++++++
 .../Library/ArmHvcNullLib/ArmHvcNullLib.inf   | 22 ++++++++++++++
 3 files changed, 52 insertions(+)
 create mode 100644 ArmPkg/Library/ArmHvcNullLib/ArmHvcNullLib.c
 create mode 100644 ArmPkg/Library/ArmHvcNullLib/ArmHvcNullLib.inf

diff --git a/ArmPkg/ArmPkg.dsc b/ArmPkg/ArmPkg.dsc
index 3afd212f472b..e33b40f2c215 100644
--- a/ArmPkg/ArmPkg.dsc
+++ b/ArmPkg/ArmPkg.dsc
@@ -134,6 +134,7 @@ [Components.common]
   ArmPkg/Library/ArmSmcLib/ArmSmcLib.inf
   ArmPkg/Library/ArmSmcLibNull/ArmSmcLibNull.inf
   ArmPkg/Library/ArmHvcLib/ArmHvcLib.inf
+  ArmPkg/Library/ArmHvcNullLib/ArmHvcNullLib.inf
   ArmPkg/Library/ArmSvcLib/ArmSvcLib.inf
   ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.inf
   ArmPkg/Library/OpteeLib/OpteeLib.inf
diff --git a/ArmPkg/Library/ArmHvcNullLib/ArmHvcNullLib.c b/ArmPkg/Library/ArmHvcNullLib/ArmHvcNullLib.c
new file mode 100644
index 000000000000..6905631ccb6c
--- /dev/null
+++ b/ArmPkg/Library/ArmHvcNullLib/ArmHvcNullLib.c
@@ -0,0 +1,29 @@
+/** @file
+  Arm HyperVisor Call (HVC) Null Library.
+
+  Copyright (c) 2022, Arm Limited. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Library/DebugLib.h>
+#include <Library/ArmHvcLib.h>
+
+/**
+  Trigger an HVC call
+
+  HVC calls can take up to 8 arguments and return up to 4 return values.
+  Therefore, the 4 first fields in the ARM_HVC_ARGS structure are used
+  for both input and output values.
+
+  @param [in, out]  Args    Arguments for the HVC call.
+**/
+VOID
+ArmCallHvc (
+  IN OUT ARM_HVC_ARGS  *Args
+  )
+{
+  ASSERT (FALSE);
+  return;
+}
diff --git a/ArmPkg/Library/ArmHvcNullLib/ArmHvcNullLib.inf b/ArmPkg/Library/ArmHvcNullLib/ArmHvcNullLib.inf
new file mode 100644
index 000000000000..c4665d34018b
--- /dev/null
+++ b/ArmPkg/Library/ArmHvcNullLib/ArmHvcNullLib.inf
@@ -0,0 +1,22 @@
+## @file
+#  Arm Hvc Null Library
+#
+#  Copyright (c) 2022, Arm Limited. All rights reserved.<BR>
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = ArmHvcNullLib
+  FILE_GUID                      = 02076A46-D6DB-48DD-8E5F-153172DD73A1
+  MODULE_TYPE                    = BASE
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = ArmHvcLib
+
+[Sources]
+  ArmHvcNullLib.c
+
+[Packages]
+  ArmPkg/ArmPkg.dec
+  MdePkg/MdePkg.dec
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 05/22] MdePkg/TrngLib: Definition for TRNG library class interface
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (3 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 04/22] ArmPkg/ArmHvcNullLib: Add NULL instance of ArmHvcLib PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 06/22] MdePkg/TrngLib: Add NULL instance of TRNG Library PierreGondois
                   ` (26 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Sami Mujawar <sami.mujawar@arm.com>

Bugzilla: 3668 (https://bugzilla.tianocore.org/show_bug.cgi?id=3668)

The NIST Special Publications 800-90A, 800-90B and 800-90C
provide recommendations for random number generation. The
NIST 800-90C, Recommendation for Random Bit Generator (RBG)
Constructions, defines the GetEntropy() interface that is
used to access the entropy source. The GetEntropy() interface
is further used by Deterministic Random Bit Generators (DRBG)
to generate random numbers.

The True Random Number Generator (TRNG) library defines an
interface to access the entropy source on a platform. Some
platforms/architectures may provide access to the entropy
using a firmware interface. In such cases the TRNG library
shall be used to provide an abstraction.

Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
---
 MdePkg/Include/Library/TrngLib.h | 121 +++++++++++++++++++++++++++++++
 MdePkg/MdePkg.dec                |   5 ++
 2 files changed, 126 insertions(+)
 create mode 100644 MdePkg/Include/Library/TrngLib.h

diff --git a/MdePkg/Include/Library/TrngLib.h b/MdePkg/Include/Library/TrngLib.h
new file mode 100644
index 000000000000..a6f165b1f918
--- /dev/null
+++ b/MdePkg/Include/Library/TrngLib.h
@@ -0,0 +1,121 @@
+/** @file
+  TRNG interface library definitions.
+
+  Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+  @par Reference(s):
+  - [1] Arm True Random Number Generator Firmware, Interface 1.0,
+        Platform Design Document.
+        (https://developer.arm.com/documentation/den0098/latest/)
+  - [2] NIST Special Publication 800-90A Revision 1, June 2015, Recommendation
+        for Random Number Generation Using Deterministic Random Bit Generators.
+        (https://csrc.nist.gov/publications/detail/sp/800-90a/rev-1/final)
+  - [3] NIST Special Publication 800-90B, Recommendation for the Entropy
+        Sources Used for Random Bit Generation.
+        (https://csrc.nist.gov/publications/detail/sp/800-90b/final)
+  - [4] (Second Draft) NIST Special Publication 800-90C, Recommendation for
+        Random Bit Generator (RBG) Constructions.
+        (https://csrc.nist.gov/publications/detail/sp/800-90c/draft)
+
+  @par Glossary:
+    - TRNG - True Random Number Generator
+**/
+
+#ifndef TRNG_LIB_H_
+#define TRNG_LIB_H_
+
+/** Get the version of the TRNG backend.
+
+  A TRNG may be implemented by the system firmware, in which case this
+  function shall return the version of the TRNG backend.
+  The implementation must return NOT_SUPPORTED if a Back end is not present.
+
+  @param [out]  MajorRevision     Major revision.
+  @param [out]  MinorRevision     Minor revision.
+
+  @retval  RETURN_SUCCESS            The function completed successfully.
+  @retval  RETURN_INVALID_PARAMETER  Invalid parameter.
+  @retval  RETURN_UNSUPPORTED        Backend not present.
+**/
+RETURN_STATUS
+EFIAPI
+GetTrngVersion (
+  OUT UINT16  *MajorRevision,
+  OUT UINT16  *MinorRevision
+  );
+
+/** Get the UUID of the TRNG backend.
+
+  A TRNG may be implemented by the system firmware, in which case this
+  function shall return the UUID of the TRNG backend.
+  Returning the TRNG UUID is optional and if not implemented, RETURN_UNSUPPORTED
+  shall be returned.
+
+  Note: The caller must not rely on the returned UUID as a trustworthy TRNG
+        Back end identity
+
+  @param [out]  Guid              UUID of the TRNG backend.
+
+  @retval  RETURN_SUCCESS            The function completed successfully.
+  @retval  RETURN_INVALID_PARAMETER  Invalid parameter.
+  @retval  RETURN_UNSUPPORTED        Function not implemented.
+**/
+RETURN_STATUS
+EFIAPI
+GetTrngUuid (
+  OUT GUID  *Guid
+  );
+
+/** Returns maximum number of entropy bits that can be returned in a single
+    call.
+
+  @return Returns the maximum number of Entropy bits that can be returned
+          in a single call to GetTrngEntropy().
+**/
+UINTN
+EFIAPI
+GetTrngMaxSupportedEntropyBits (
+  VOID
+  );
+
+/** Returns N bits of conditioned entropy.
+
+  See [3] Section 2.3.1 GetEntropy: An Interface to the Entropy Source
+    GetEntropy
+      Input:
+        bits_of_entropy: the requested amount of entropy
+      Output:
+        entropy_bitstring: The string that provides the requested entropy.
+      status: A Boolean value that is TRUE if the request has been satisfied,
+              and is FALSE otherwise.
+
+  Note: In this implementation this function returns a status code instead
+        of a boolean value.
+        This is also compatible with the definition of Get_Entropy, see [4]
+        Section 7.4 Entropy Source Calls.
+          (status, entropy_bitstring) = Get_Entropy (
+                                          requested_entropy,
+                                          max_length
+                                          )
+
+  @param  [in]   EntropyBits  Number of entropy bits requested.
+  @param  [in]   BufferSize   Size of the Buffer in bytes.
+  @param  [out]  Buffer       Buffer to return the entropy bits.
+
+  @retval  RETURN_SUCCESS            The function completed successfully.
+  @retval  RETURN_INVALID_PARAMETER  Invalid parameter.
+  @retval  RETURN_UNSUPPORTED        Function not implemented.
+  @retval  RETURN_BAD_BUFFER_SIZE    Buffer size is too small.
+  @retval  RETURN_NOT_READY          No Entropy available.
+**/
+RETURN_STATUS
+EFIAPI
+GetTrngEntropy (
+  IN  UINTN  EntropyBits,
+  IN  UINTN  BufferSize,
+  OUT UINT8  *Buffer
+  );
+
+#endif // TRNG_LIB_H_
diff --git a/MdePkg/MdePkg.dec b/MdePkg/MdePkg.dec
index f1ebf9e251c1..7ff26e22f915 100644
--- a/MdePkg/MdePkg.dec
+++ b/MdePkg/MdePkg.dec
@@ -7,6 +7,7 @@
 # Copyright (c) 2007 - 2022, Intel Corporation. All rights reserved.<BR>
 # Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
 # (C) Copyright 2016 - 2021 Hewlett Packard Enterprise Development LP<BR>
+#  Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
 #
 # SPDX-License-Identifier: BSD-2-Clause-Patent
 #
@@ -275,6 +276,10 @@ [LibraryClasses]
   ## @libraryclass  Provides function for SMM CPU Rendezvous Library.
   SmmCpuRendezvousLib|Include/Library/SmmCpuRendezvousLib.h
 
+  ##  @libraryclass  Provides services to generate Entropy using a TRNG.
+  #
+  TrngLib|Include/Library/TrngLib.h
+
 [LibraryClasses.IA32, LibraryClasses.X64, LibraryClasses.AARCH64]
   ##  @libraryclass  Provides services to generate random number.
   #
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 06/22] MdePkg/TrngLib: Add NULL instance of TRNG Library
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (4 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 05/22] MdePkg/TrngLib: Definition for TRNG library class interface PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 07/22] ArmPkg: Add FID definitions for Firmware TRNG PierreGondois
                   ` (25 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Sami Mujawar <sami.mujawar@arm.com>

Bugzilla: 3668 (https://bugzilla.tianocore.org/show_bug.cgi?id=3668)

The True Random Number Generator (TRNG) library defines an
interface to access the entropy source on a platform. On
platforms that do not have access to an entropy source, a
NULL instance of the TRNG library may be useful to satisfy
the build dependency.

Therefore, add a NULL instance of the TRNG library.

Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
---
 .../Library/BaseTrngLibNull/BaseTrngLibNull.c | 135 ++++++++++++++++++
 .../BaseTrngLibNull/BaseTrngLibNull.inf       |  30 ++++
 .../BaseTrngLibNull/BaseTrngLibNull.uni       |  12 ++
 MdePkg/MdePkg.dsc                             |   1 +
 4 files changed, 178 insertions(+)
 create mode 100644 MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.c
 create mode 100644 MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.inf
 create mode 100644 MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.uni

diff --git a/MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.c b/MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.c
new file mode 100644
index 000000000000..e383cd0cf1cc
--- /dev/null
+++ b/MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.c
@@ -0,0 +1,135 @@
+/** @file
+  Null version of TRNG (True Random Number Generator) services.
+
+  Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+  @par Reference(s):
+  - [1] Arm True Random Number Generator Firmware, Interface 1.0,
+        Platform Design Document.
+        (https://developer.arm.com/documentation/den0098/latest/)
+  - [2] NIST Special Publication 800-90A Revision 1, June 2015, Recommendation
+        for Random Number Generation Using Deterministic Random Bit Generators.
+        (https://csrc.nist.gov/publications/detail/sp/800-90a/rev-1/final)
+  - [3] NIST Special Publication 800-90B, Recommendation for the Entropy
+        Sources Used for Random Bit Generation.
+        (https://csrc.nist.gov/publications/detail/sp/800-90b/final)
+  - [4] (Second Draft) NIST Special Publication 800-90C, Recommendation for
+        Random Bit Generator (RBG) Constructions.
+        (https://csrc.nist.gov/publications/detail/sp/800-90c/draft)
+
+  @par Glossary:
+    - TRNG - True Random Number Generator
+**/
+
+#include <Library/DebugLib.h>
+#include <Library/TrngLib.h>
+
+/** Get the version of the TRNG backend.
+
+  A TRNG may be implemented by the system firmware, in which case this
+  function shall return the version of the TRNG backend.
+  The implementation must return NOT_SUPPORTED if a Back end is not present.
+
+  @param [out]  MajorRevision     Major revision.
+  @param [out]  MinorRevision     Minor revision.
+
+  @retval  RETURN_SUCCESS            The function completed successfully.
+  @retval  RETURN_INVALID_PARAMETER  Invalid parameter.
+  @retval  RETURN_UNSUPPORTED        Backend not present.
+**/
+RETURN_STATUS
+EFIAPI
+GetTrngVersion (
+  OUT UINT16  *MajorRevision,
+  OUT UINT16  *MinorRevision
+  )
+{
+  ASSERT (FALSE);
+  return RETURN_UNSUPPORTED;
+}
+
+/** Get the UUID of the TRNG backend.
+
+  A TRNG may be implemented by the system firmware, in which case this
+  function shall return the UUID of the TRNG backend.
+  Returning the TRNG UUID is optional and if not implemented, RETURN_UNSUPPORTED
+  shall be returned.
+
+  Note: The caller must not rely on the returned UUID as a trustworthy TRNG
+        Back end identity
+
+  @param [out]  Guid              UUID of the TRNG backend.
+
+  @retval  RETURN_SUCCESS            The function completed successfully.
+  @retval  RETURN_INVALID_PARAMETER  Invalid parameter.
+  @retval  RETURN_UNSUPPORTED        Function not implemented.
+**/
+RETURN_STATUS
+EFIAPI
+GetTrngUuid (
+  OUT GUID  *Guid
+  )
+{
+  ASSERT (FALSE);
+  return RETURN_UNSUPPORTED;
+}
+
+/** Returns maximum number of entropy bits that can be returned in a single
+    call.
+
+  @return Returns the maximum number of Entropy bits that can be returned
+          in a single call to GetTrngEntropy().
+**/
+UINTN
+EFIAPI
+GetTrngMaxSupportedEntropyBits (
+  VOID
+  )
+{
+  ASSERT (FALSE);
+  return 0;
+}
+
+/** Returns N bits of conditioned entropy.
+
+  See [3] Section 2.3.1 GetEntropy: An Interface to the Entropy Source
+    GetEntropy
+      Input:
+        bits_of_entropy: the requested amount of entropy
+      Output:
+        entropy_bitstring: The string that provides the requested entropy.
+      status: A Boolean value that is TRUE if the request has been satisfied,
+              and is FALSE otherwise.
+
+  Note: In this implementation this function returns a status code instead
+        of a boolean value.
+        This is also compatible with the definition of Get_Entropy, see [4]
+        Section 7.4 Entropy Source Calls.
+          (status, entropy_bitstring) = Get_Entropy (
+                                          requested_entropy,
+                                          max_length
+                                          )
+
+  @param  [in]   EntropyBits  Number of entropy bits requested.
+  @param  [in]   BufferSize   Size of the Buffer in bytes.
+  @param  [out]  Buffer       Buffer to return the entropy bits.
+
+  @retval  RETURN_SUCCESS            The function completed successfully.
+  @retval  RETURN_INVALID_PARAMETER  Invalid parameter.
+  @retval  RETURN_UNSUPPORTED        Function not implemented.
+  @retval  RETURN_BAD_BUFFER_SIZE    Buffer size is too small.
+  @retval  RETURN_NOT_READY          No Entropy available.
+**/
+RETURN_STATUS
+EFIAPI
+GetTrngEntropy (
+  IN  UINTN  EntropyBits,
+  IN  UINTN  BufferSize,
+  OUT UINT8  *Buffer
+  )
+{
+  ASSERT (FALSE);
+  return RETURN_UNSUPPORTED;
+}
diff --git a/MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.inf b/MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.inf
new file mode 100644
index 000000000000..004aa8445a25
--- /dev/null
+++ b/MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.inf
@@ -0,0 +1,30 @@
+## @file
+#  Null instance of TRNG (True Random Number Generator) Library.
+#
+#  Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x0001001B
+  BASE_NAME                      = BaseTrngLibNull
+  MODULE_UNI_FILE                = BaseTrngLibNull.uni
+  FILE_GUID                      = ABDE1C87-4F50-4B82-9133-7A79E13F69AB
+  MODULE_TYPE                    = BASE
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = TrngLib
+
+#
+#  VALID_ARCHITECTURES           = IA32 X64 ARM AARCH64
+#
+
+[Sources]
+  BaseTrngLibNull.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+
+[LibraryClasses]
+  DebugLib
diff --git a/MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.uni b/MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.uni
new file mode 100644
index 000000000000..952e4354c0cc
--- /dev/null
+++ b/MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.uni
@@ -0,0 +1,12 @@
+// /** @file
+// Null Instance of TRNG (True Random Number Generator) Library.
+//
+//  Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+// **/
+
+#string STR_MODULE_ABSTRACT             #language en-US "Null instance of TRNG Library"
+
+#string STR_MODULE_DESCRIPTION          #language en-US "This library instance should be used with modules that inherit an (indirect) dependency on the TrngLib class, but never actually call TrngLib APIs for consuming Entropy."
diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc
index 3d8874e64782..80e7233363d3 100644
--- a/MdePkg/MdePkg.dsc
+++ b/MdePkg/MdePkg.dsc
@@ -67,6 +67,7 @@ [Components]
   MdePkg/Library/DxeRngLib/DxeRngLib.inf
   MdePkg/Library/BaseRngLibNull/BaseRngLibNull.inf
   MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf
+  MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.inf
 
   MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf
   MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 07/22] ArmPkg: Add FID definitions for Firmware TRNG
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (5 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 06/22] MdePkg/TrngLib: Add NULL instance of TRNG Library PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 08/22] ArmPkg/TrngLib: Add Arm Firmware TRNG library PierreGondois
                   ` (24 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Sami Mujawar <sami.mujawar@arm.com>

Bugzilla: 3668 (https://bugzilla.tianocore.org/show_bug.cgi?id=3668)

The Arm True Random Number Generator Firmware, Interface 1.0,
Platform Design Document
(https://developer.arm.com/documentation/den0098/latest/)
defines an interface between an Operating System (OS) executing
at EL1 and Firmware (FW) exposing a conditioned entropy source
that is provided by a TRNG back end.

New function IDs have been defined by the specification for
accessing the TRNG services. Therefore, add these definitions
to the Arm standard SMC header.

Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
---
 ArmPkg/Include/IndustryStandard/ArmStdSmc.h | 109 +++++++++++++++++++-
 1 file changed, 107 insertions(+), 2 deletions(-)

diff --git a/ArmPkg/Include/IndustryStandard/ArmStdSmc.h b/ArmPkg/Include/IndustryStandard/ArmStdSmc.h
index 655edc21b205..4b2edcf0154c 100644
--- a/ArmPkg/Include/IndustryStandard/ArmStdSmc.h
+++ b/ArmPkg/Include/IndustryStandard/ArmStdSmc.h
@@ -1,13 +1,20 @@
 /** @file
 *
 *  Copyright (c) 2020, NUVIA Inc. All rights reserved.<BR>
-*  Copyright (c) 2012-2017, ARM Limited. All rights reserved.
+*  Copyright (c) 2012 - 2022, Arm Limited. All rights reserved.
 *
 *  SPDX-License-Identifier: BSD-2-Clause-Patent
 *
 * @par Revision Reference:
-*  - SMC Calling Convention version 1.2
+*  - [1] SMC Calling Convention version 1.2
 *    (https://developer.arm.com/documentation/den0028/c/?lang=en)
+*  - [2] Arm True Random Number Generator Firmware, Interface 1.0,
+*    Platform Design Document.
+*    (https://developer.arm.com/documentation/den0098/latest/)
+*
+*  @par Glossary:
+*    - TRNG - True Random Number Generator
+*
 **/
 
 #ifndef ARM_STD_SMC_H_
@@ -137,4 +144,102 @@
 /*                                    0xbf00ff02 is reserved */
 #define ARM_SMC_ID_TOS_REVISION  0xbf00ff03
 
+// Firmware TRNG interface Function IDs
+
+/*
+  SMC/HVC call to get the version of the TRNG backend,
+  Cf. [2], 2.1 TRNG_VERSION
+  Input values:
+    W0    0x8400_0050
+    W1-W7 Reserved (MBZ)
+  Return values:
+    Success (W0 > 0) W0[31] MBZ
+      W0[30:16] Major revision
+      W0[15:0] Minor revision
+      W1 - W3 Reserved (MBZ)
+    Error (W0 < 0)
+      NOT_SUPPORTED Function not implemented
+*/
+#define FID_TRNG_VERSION  0x84000050
+
+/*
+  SMC/HVC call to check if a TRNG function ID is implemented by the backend,
+  Cf. [2], Section 2.2 TRNG_FEATURES
+  Input Values
+    W0    0x8400_0051
+    W1    trng_func_id
+    W2-W7 Reserved (MBZ)
+  Return values:
+    Success (W0 >= 0):
+      SUCCESS Function is implemented.
+        > 0     Function is implemented and
+                has specific capabilities,
+                see function definition.
+    Error (W0 < 0)
+      NOT_SUPPORTED Function with FID=trng_func_id
+      is not implemented
+*/
+#define FID_TRNG_FEATURES  0x84000051
+
+/*
+  SMC/HVC call to get the UUID of the TRNG backend,
+  Cf. [2], Section 2.3 TRNG_GET_UUID
+  Input Values:
+    W0    0x8400_0052
+    W1-W7 Reserved (MBZ)
+  Return Values:
+    Success (W0 != -1)
+        W0 UUID[31:0]
+        W1 UUID[63:32]
+        W2 UUID[95:64]
+        W3 UUID[127:96]
+    Error (W0 = -1)
+        W0 NOT_SUPPORTED
+*/
+#define FID_TRNG_GET_UUID  0x84000052
+
+/*
+  AARCH32 SMC/HVC call to get entropy bits, Cf. [2], Section 2.4 TRNG_RND.
+  Input values:
+    W0    0x8400_0053
+    W2-W7 Reserved (MBZ)
+  Return values:
+    Success (W0 = 0):
+      W0 MBZ
+      W1 Entropy[95:64]
+      W2 Entropy[63:32]
+      W3 Entropy[31:0]
+    Error (W0 < 0)
+          W0 NOT_SUPPORTED
+          NO_ENTROPY
+          INVALID_PARAMETERS
+          W1 - W3 Reserved (MBZ)
+*/
+#define FID_TRNG_RND_AARCH32  0x84000053
+
+/*
+  AARCH64 SMC/HVC call to get entropy bits, Cf. [2], Section 2.4 TRNG_RND.
+  Input values:
+      X0    0xC400_0053
+      X2-X7 Reserved (MBZ)
+  Return values:
+    Success (X0 = 0):
+      X0 MBZ
+      X1 Entropy[191:128]
+      X2 Entropy[127:64]
+      X3 Entropy[63:0]
+    Error (X0 < 0)
+          X0 NOT_SUPPORTED
+          NO_ENTROPY
+          INVALID_PARAMETERS
+          X1 - X3 Reserved (MBZ)
+*/
+#define FID_TRNG_RND_AARCH64  0xC4000053
+
+// Firmware TRNG status codes
+#define TRNG_STATUS_SUCCESS            (INT32)(0)
+#define TRNG_STATUS_NOT_SUPPORTED      (INT32)(-1)
+#define TRNG_STATUS_INVALID_PARAMETER  (INT32)(-2)
+#define TRNG_STATUS_NO_ENTROPY         (INT32)(-3)
+
 #endif // ARM_STD_SMC_H_
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 08/22] ArmPkg/TrngLib: Add Arm Firmware TRNG library
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (6 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 07/22] ArmPkg: Add FID definitions for Firmware TRNG PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 09/22] MdePkg/BaseRngLib: Rename ArmReadIdIsar0() to ArmGetFeatRng() PierreGondois
                   ` (23 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Sami Mujawar <sami.mujawar@arm.com>

Bugzilla: 3668 (https://bugzilla.tianocore.org/show_bug.cgi?id=3668)

The Arm True Random Number Generator Firmware, Interface 1.0,
Platform Design Document
(https://developer.arm.com/documentation/den0098/latest/)
defines an interface between an Operating System (OS) executing
at EL1 and Firmware (FW) exposing a conditioned entropy source
that is provided by a TRNG back end.

The conditioned entropy, that is provided by the TRNG FW interface,
is commonly used to seed deterministic random number generators.

This patch adds a TrngLib library that implements the Arm TRNG
firmware interface.

Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
---
 ArmPkg/ArmPkg.dsc                            |   1 +
 ArmPkg/Library/ArmFwTrngLib/ArmFwTrngDefs.h  |  50 +++
 ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.c   | 403 +++++++++++++++++++
 ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.inf |  29 ++
 4 files changed, 483 insertions(+)
 create mode 100644 ArmPkg/Library/ArmFwTrngLib/ArmFwTrngDefs.h
 create mode 100644 ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.c
 create mode 100644 ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.inf

diff --git a/ArmPkg/ArmPkg.dsc b/ArmPkg/ArmPkg.dsc
index e33b40f2c215..02d1caa3ab40 100644
--- a/ArmPkg/ArmPkg.dsc
+++ b/ArmPkg/ArmPkg.dsc
@@ -138,6 +138,7 @@ [Components.common]
   ArmPkg/Library/ArmSvcLib/ArmSvcLib.inf
   ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.inf
   ArmPkg/Library/OpteeLib/OpteeLib.inf
+  ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.inf
 
   ArmPkg/Filesystem/SemihostFs/SemihostFs.inf
 
diff --git a/ArmPkg/Library/ArmFwTrngLib/ArmFwTrngDefs.h b/ArmPkg/Library/ArmFwTrngLib/ArmFwTrngDefs.h
new file mode 100644
index 000000000000..150c89fe7969
--- /dev/null
+++ b/ArmPkg/Library/ArmFwTrngLib/ArmFwTrngDefs.h
@@ -0,0 +1,50 @@
+/** @file
+  Arm Firmware TRNG definitions.
+
+  Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+  @par Reference(s):
+  - [1] Arm True Random Number Generator Firmware, Interface 1.0,
+        Platform Design Document.
+        (https://developer.arm.com/documentation/den0098/latest/)
+
+  @par Glossary:
+    - TRNG - True Random Number Generator
+    - FID  - Function ID
+**/
+
+#ifndef ARM_FW_TRNG_DEFS_H_
+#define ARM_FW_TRNG_DEFS_H_
+
+#include <IndustryStandard/ArmStdSmc.h>
+
+// Firmware TRNG revision mask and shift
+#define TRNG_REV_MAJOR_MASK   0x7FFF
+#define TRNG_REV_MINOR_MASK   0xFFFF
+#define TRNG_REV_MAJOR_SHIFT  16
+
+#if defined (MDE_CPU_ARM)
+
+/** FID to use on AArch32 platform to request entropy.
+*/
+#define FID_TRNG_RND  FID_TRNG_RND_AARCH32
+
+/** Maximum bits of entropy supported on AArch32.
+*/
+#define MAX_ENTROPY_BITS  96
+#elif defined (MDE_CPU_AARCH64)
+
+/** FID to use on AArch64 platform to request entropy.
+*/
+#define FID_TRNG_RND  FID_TRNG_RND_AARCH64
+
+/** Maximum bits of entropy supported on AArch64.
+*/
+#define MAX_ENTROPY_BITS  192
+#else
+  #error "Firmware TRNG not supported. Unknown chipset."
+#endif
+
+#endif // ARM_FW_TRNG_DEFS_H_
diff --git a/ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.c b/ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.c
new file mode 100644
index 000000000000..5cff23de0250
--- /dev/null
+++ b/ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.c
@@ -0,0 +1,403 @@
+/** @file
+  Arm Firmware TRNG interface library.
+
+  Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+  @par Reference(s):
+  - [1] Arm True Random Number Generator Firmware, Interface 1.0,
+        Platform Design Document.
+        (https://developer.arm.com/documentation/den0098/latest/)
+  - [2] NIST Special Publication 800-90A Revision 1, June 2015, Recommendation
+        for Random Number Generation Using Deterministic Random Bit Generators.
+        (https://csrc.nist.gov/publications/detail/sp/800-90a/rev-1/final)
+  - [3] NIST Special Publication 800-90B, Recommendation for the Entropy
+        Sources Used for Random Bit Generation.
+        (https://csrc.nist.gov/publications/detail/sp/800-90b/final)
+  - [4] (Second Draft) NIST Special Publication 800-90C, Recommendation for
+        Random Bit Generator (RBG) Constructions.
+        (https://csrc.nist.gov/publications/detail/sp/800-90c/draft)
+
+  @par Glossary:
+    - TRNG - True Random Number Generator
+    - FID  - Function ID
+**/
+
+#include <Base.h>
+#include <Library/ArmLib.h>
+#include <Library/ArmMonitorLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+
+#include "ArmFwTrngDefs.h"
+
+/** Convert TRNG status codes to RETURN status codes.
+
+  @param [in]  TrngStatus    TRNG status code.
+
+  @retval  RETURN_SUCCESS            Success.
+  @retval  RETURN_UNSUPPORTED        Function not implemented or
+                                     negative return code.
+  @retval  RETURN_INVALID_PARAMETER  A parameter is invalid.
+  @retval  RETURN_NOT_READY          No Entropy available.
+**/
+STATIC
+RETURN_STATUS
+TrngStatusToReturnStatus (
+  IN  INT32  TrngStatus
+  )
+{
+  switch (TrngStatus) {
+    case TRNG_STATUS_NOT_SUPPORTED:
+      return RETURN_UNSUPPORTED;
+
+    case TRNG_STATUS_INVALID_PARAMETER:
+      return RETURN_INVALID_PARAMETER;
+
+    case TRNG_STATUS_NO_ENTROPY:
+      return RETURN_NOT_READY;
+
+    case TRNG_STATUS_SUCCESS:
+      return RETURN_SUCCESS;
+
+    default:
+      if (TrngStatus < 0) {
+        return RETURN_UNSUPPORTED;
+      }
+
+      return RETURN_SUCCESS;
+  }
+}
+
+/** Get the version of the TRNG backend.
+
+  A TRNG may be implemented by the system firmware, in which case this
+  function shall return the version of the TRNG backend.
+  The implementation must return NOT_SUPPORTED if a Back end is not present.
+
+  @param [out]  MajorRevision     Major revision.
+  @param [out]  MinorRevision     Minor revision.
+
+  @retval  RETURN_SUCCESS            The function completed successfully.
+  @retval  RETURN_INVALID_PARAMETER  Invalid parameter.
+  @retval  RETURN_UNSUPPORTED        Backend not present.
+**/
+RETURN_STATUS
+EFIAPI
+GetTrngVersion (
+  OUT UINT16  *MajorRevision,
+  OUT UINT16  *MinorRevision
+  )
+{
+  RETURN_STATUS     Status;
+  ARM_MONITOR_ARGS  Parameters;
+  INT32             Revision;
+
+  if ((MajorRevision == NULL) || (MinorRevision == NULL)) {
+    return RETURN_INVALID_PARAMETER;
+  }
+
+  ZeroMem (&Parameters, sizeof (Parameters));
+
+  Parameters.Arg0 = FID_TRNG_VERSION;
+  ArmMonitorCall (&Parameters);
+
+  Revision = (INT32)Parameters.Arg0;
+  Status   = TrngStatusToReturnStatus (Revision);
+  if (RETURN_ERROR (Status)) {
+    return Status;
+  }
+
+  *MinorRevision = (Revision & TRNG_REV_MINOR_MASK);
+  *MajorRevision = ((Revision >> TRNG_REV_MAJOR_SHIFT) & TRNG_REV_MAJOR_MASK);
+  return RETURN_SUCCESS;
+}
+
+/** Get the features supported by the TRNG backend.
+
+  The caller can determine if functions defined in the TRNG ABI are
+  present in the ABI implementation.
+
+  @param [in]  FunctionId         Function Id.
+  @param [out] Capability         Function specific capability if present.
+
+  @retval  RETURN_SUCCESS            The function completed successfully.
+  @retval  RETURN_INVALID_PARAMETER  Invalid parameter.
+  @retval  RETURN_UNSUPPORTED        Function not implemented.
+**/
+STATIC
+RETURN_STATUS
+EFIAPI
+GetTrngFeatures (
+  IN  CONST UINT32  FunctionId,
+  OUT       UINT32  *Capability      OPTIONAL
+  )
+{
+  ARM_MONITOR_ARGS  Parameters;
+  RETURN_STATUS     Status;
+
+  ZeroMem (&Parameters, sizeof (Parameters));
+
+  Parameters.Arg0 = FID_TRNG_FEATURES;
+  Parameters.Arg1 = FunctionId;
+  ArmMonitorCall (&Parameters);
+
+  Status = TrngStatusToReturnStatus (Parameters.Arg0);
+  if (RETURN_ERROR (Status)) {
+    return Status;
+  }
+
+  if (Capability != NULL) {
+    *Capability = (UINT32)Parameters.Arg0;
+  }
+
+  return RETURN_SUCCESS;
+}
+
+/** Get the UUID of the TRNG backend.
+
+  A TRNG may be implemented by the system firmware, in which case this
+  function shall return the UUID of the TRNG backend.
+  Returning the TRNG UUID is optional and if not implemented, RETURN_UNSUPPORTED
+  shall be returned.
+
+  Note: The caller must not rely on the returned UUID as a trustworthy TRNG
+        Back end identity
+
+  @param [out]  Guid              UUID of the TRNG backend.
+
+  @retval  RETURN_SUCCESS            The function completed successfully.
+  @retval  RETURN_INVALID_PARAMETER  Invalid parameter.
+  @retval  RETURN_UNSUPPORTED        Function not implemented.
+**/
+RETURN_STATUS
+EFIAPI
+GetTrngUuid (
+  OUT GUID  *Guid
+  )
+{
+  ARM_MONITOR_ARGS  Parameters;
+
+  if (Guid == NULL) {
+    return RETURN_INVALID_PARAMETER;
+  }
+
+  ZeroMem (&Parameters, sizeof (Parameters));
+
+  Parameters.Arg0 = FID_TRNG_GET_UUID;
+  ArmMonitorCall (&Parameters);
+
+  // Only invalid value is TRNG_STATUS_NOT_SUPPORTED (-1).
+  if ((INT32)Parameters.Arg0 == TRNG_STATUS_NOT_SUPPORTED) {
+    return TrngStatusToReturnStatus ((INT32)Parameters.Arg0);
+  }
+
+  Guid->Data1 = (Parameters.Arg0 & MAX_UINT32);
+  Guid->Data2 = (Parameters.Arg1 & MAX_UINT16);
+  Guid->Data3 = ((Parameters.Arg1 >> 16) & MAX_UINT16);
+
+  Guid->Data4[0] = (Parameters.Arg2 & MAX_UINT8);
+  Guid->Data4[1] = ((Parameters.Arg2 >> 8) & MAX_UINT8);
+  Guid->Data4[2] = ((Parameters.Arg2 >> 16) & MAX_UINT8);
+  Guid->Data4[3] = ((Parameters.Arg2 >> 24) & MAX_UINT8);
+
+  Guid->Data4[4] = (Parameters.Arg3 & MAX_UINT8);
+  Guid->Data4[5] = ((Parameters.Arg3 >> 8) & MAX_UINT8);
+  Guid->Data4[6] = ((Parameters.Arg3 >> 16) & MAX_UINT8);
+  Guid->Data4[7] = ((Parameters.Arg3 >> 24) & MAX_UINT8);
+
+  DEBUG ((DEBUG_INFO, "FW-TRNG: UUID %g\n", Guid));
+
+  return RETURN_SUCCESS;
+}
+
+/** Returns maximum number of entropy bits that can be returned in a single
+    call.
+
+  @return Returns the maximum number of Entropy bits that can be returned
+          in a single call to GetTrngEntropy().
+**/
+UINTN
+EFIAPI
+GetTrngMaxSupportedEntropyBits (
+  VOID
+  )
+{
+  return MAX_ENTROPY_BITS;
+}
+
+/** Returns N bits of conditioned entropy.
+
+  See [3] Section 2.3.1 GetEntropy: An Interface to the Entropy Source
+    GetEntropy
+      Input:
+        bits_of_entropy: the requested amount of entropy
+      Output:
+        entropy_bitstring: The string that provides the requested entropy.
+      status: A Boolean value that is TRUE if the request has been satisfied,
+              and is FALSE otherwise.
+
+  Note: In this implementation this function returns a status code instead
+        of a boolean value.
+        This is also compatible with the definition of Get_Entropy, see [4]
+        Section 7.4 Entropy Source Calls.
+          (status, entropy_bitstring) = Get_Entropy (
+                                          requested_entropy,
+                                          max_length
+                                          )
+
+  @param  [in]   EntropyBits  Number of entropy bits requested.
+  @param  [in]   BufferSize   Size of the Buffer in bytes.
+  @param  [out]  Buffer       Buffer to return the entropy bits.
+
+  @retval  RETURN_SUCCESS            The function completed successfully.
+  @retval  RETURN_INVALID_PARAMETER  Invalid parameter.
+  @retval  RETURN_UNSUPPORTED        Function not implemented.
+  @retval  RETURN_BAD_BUFFER_SIZE    Buffer size is too small.
+  @retval  RETURN_NOT_READY          No Entropy available.
+**/
+RETURN_STATUS
+EFIAPI
+GetTrngEntropy (
+  IN  UINTN  EntropyBits,
+  IN  UINTN  BufferSize,
+  OUT UINT8  *Buffer
+  )
+{
+  RETURN_STATUS     Status;
+  ARM_MONITOR_ARGS  Parameters;
+  UINTN             EntropyBytes;
+  UINTN             LastValidBits;
+  UINTN             BytesToClear;
+  UINTN             EntropyData[3];
+
+  if ((EntropyBits == 0)                ||
+      (EntropyBits > MAX_ENTROPY_BITS)  ||
+      (Buffer == NULL))
+  {
+    return RETURN_INVALID_PARAMETER;
+  }
+
+  EntropyBytes = (EntropyBits + 7) >> 3;
+  if (EntropyBytes > BufferSize) {
+    return RETURN_BAD_BUFFER_SIZE;
+  }
+
+  ZeroMem (Buffer, BufferSize);
+  ZeroMem (&Parameters, sizeof (Parameters));
+
+  Parameters.Arg0 = FID_TRNG_RND;
+  Parameters.Arg1 = EntropyBits;
+  ArmMonitorCall (&Parameters);
+
+  Status = TrngStatusToReturnStatus ((INT32)Parameters.Arg0);
+  if (RETURN_ERROR (Status)) {
+    return Status;
+  }
+
+  // The entropy data is returned in the Parameters.Arg<3..1>
+  // With the lower order bytes in Parameters.Arg3 and the higher
+  // order bytes being stored in Parameters.Arg1.
+  EntropyData[0] = Parameters.Arg3;
+  EntropyData[1] = Parameters.Arg2;
+  EntropyData[2] = Parameters.Arg1;
+
+  CopyMem (Buffer, EntropyData, EntropyBytes);
+
+  // Mask off any unused top bytes, in accordance with specification.
+  BytesToClear = BufferSize - EntropyBytes;
+  if (BytesToClear != 0) {
+    ZeroMem (&Buffer[EntropyBytes], BytesToClear);
+  }
+
+  // Clear the unused MSB bits of the last byte.
+  LastValidBits = EntropyBits & 0x7;
+  if (LastValidBits != 0) {
+    Buffer[EntropyBytes - 1] &= (0xFF >> (8 - LastValidBits));
+  }
+
+  return Status;
+}
+
+/** The constructor checks that the FW-TRNG interface is supported
+    by the host firmware.
+
+  It will ASSERT() if FW-TRNG is not supported.
+  It will always return RETURN_SUCCESS.
+
+  @retval RETURN_SUCCESS   The constructor always returns RETURN_SUCCESS.
+**/
+RETURN_STATUS
+EFIAPI
+ArmFwTrngLibConstructor (
+  VOID
+  )
+{
+  ARM_MONITOR_ARGS  Parameters;
+  RETURN_STATUS     Status;
+  UINT16            MajorRev;
+  UINT16            MinorRev;
+  GUID              Guid;
+
+  ZeroMem (&Parameters, sizeof (Parameters));
+
+  Parameters.Arg0 = SMCCC_VERSION;
+  ArmMonitorCall (&Parameters);
+  Status = TrngStatusToReturnStatus ((INT32)Parameters.Arg0);
+  if (RETURN_ERROR (Status)) {
+    ASSERT_RETURN_ERROR (Status);
+    goto ErrorHandler;
+  }
+
+  // Cf [1] s2.1.3 'Caller responsibilities',
+  // SMCCC version must be greater or equal than 1.1
+  if ((INT32)Parameters.Arg0 < 0x10001) {
+    ASSERT_RETURN_ERROR (RETURN_UNSUPPORTED);
+    goto ErrorHandler;
+  }
+
+  Status = GetTrngVersion (&MajorRev, &MinorRev);
+  if (RETURN_ERROR (Status)) {
+    ASSERT_RETURN_ERROR (Status);
+    goto ErrorHandler;
+  }
+
+  // Check that the required features are present.
+  Status = GetTrngFeatures (FID_TRNG_RND, NULL);
+  if (RETURN_ERROR (Status)) {
+    ASSERT_RETURN_ERROR (Status);
+    goto ErrorHandler;
+  }
+
+  // Check if TRNG UUID is supported and if so trace the GUID.
+  Status = GetTrngFeatures (FID_TRNG_GET_UUID, NULL);
+  if (RETURN_ERROR (Status)) {
+    ASSERT_RETURN_ERROR (Status);
+    goto ErrorHandler;
+  }
+
+  DEBUG_CODE_BEGIN ();
+
+  Status = GetTrngUuid (&Guid);
+  if (RETURN_ERROR (Status)) {
+    ASSERT_RETURN_ERROR (Status);
+    goto ErrorHandler;
+  }
+
+  DEBUG ((
+    DEBUG_INFO,
+    "FW-TRNG: Version %d.%d, GUID {%g}\n",
+    MajorRev,
+    MinorRev,
+    Guid
+    ));
+
+  DEBUG_CODE_END ();
+
+  return RETURN_SUCCESS;
+
+ErrorHandler:
+  DEBUG ((DEBUG_ERROR, "ArmFwTrngLib could not be correctly initialized.\n"));
+  return RETURN_SUCCESS;
+}
diff --git a/ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.inf b/ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.inf
new file mode 100644
index 000000000000..baeb00d832fd
--- /dev/null
+++ b/ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.inf
@@ -0,0 +1,29 @@
+## @file
+#  Arm Firmware TRNG interface library.
+#
+#  Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+##
+
+[Defines]
+  INF_VERSION       = 0x0001001B
+  BASE_NAME         = ArmFwTrngLib
+  FILE_GUID         = 10DE97C9-28E4-4C9B-A53E-8D7D1B0DD4E0
+  VERSION_STRING    = 1.0
+  MODULE_TYPE       = BASE
+  LIBRARY_CLASS     = TrngLib
+  CONSTRUCTOR       = ArmFwTrngLibConstructor
+
+[Sources]
+  ArmFwTrngDefs.h
+  ArmFwTrngLib.c
+
+[Packages]
+  ArmPkg/ArmPkg.dec
+  MdePkg/MdePkg.dec
+
+[LibraryClasses]
+  ArmMonitorLib
+  BaseLib
+  BaseMemoryLib
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 09/22] MdePkg/BaseRngLib: Rename ArmReadIdIsar0() to ArmGetFeatRng()
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (7 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 08/22] ArmPkg/TrngLib: Add Arm Firmware TRNG library PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 10/22] ArmPkg/ArmLib: Add ArmReadIdIsar0() helper PierreGondois
                   ` (22 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <pierre.gondois@arm.com>

The MdePkg must be self contained and not have external dependencies.
ArmReadIdIsar0() is defined in MdePkg/Library/BaseRngLib and is
limited to the scope of this library.

The same function will be required to check the FEAT_AES and FEAT_RNG
extensions in other libraries. As this function is Arm specific, it
cannot be added to a library interface in MdePkg. It should be part of
ArmPkg/ArmLib.

To avoid having mutiple definitions/prototypes of ArmReadIdIsar0(),
and as BaseRngLib only requires to check the RNG capability bits,
rename the MdePkg/Library/BaseRngLib implementation to ArmGetFeatRng().

Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
---
 .../AArch64/{ArmReadIdIsar0.S => ArmGetFeatRng.S}         | 8 ++++----
 .../AArch64/{ArmReadIdIsar0.asm => ArmGetFeatRng.asm}     | 8 ++++----
 MdePkg/Library/BaseRngLib/AArch64/ArmRng.h                | 2 +-
 MdePkg/Library/BaseRngLib/AArch64/Rndr.c                  | 2 +-
 MdePkg/Library/BaseRngLib/BaseRngLib.inf                  | 4 ++--
 5 files changed, 12 insertions(+), 12 deletions(-)
 rename MdePkg/Library/BaseRngLib/AArch64/{ArmReadIdIsar0.S => ArmGetFeatRng.S} (78%)
 rename MdePkg/Library/BaseRngLib/AArch64/{ArmReadIdIsar0.asm => ArmGetFeatRng.asm} (81%)

diff --git a/MdePkg/Library/BaseRngLib/AArch64/ArmReadIdIsar0.S b/MdePkg/Library/BaseRngLib/AArch64/ArmGetFeatRng.S
similarity index 78%
rename from MdePkg/Library/BaseRngLib/AArch64/ArmReadIdIsar0.S
rename to MdePkg/Library/BaseRngLib/AArch64/ArmGetFeatRng.S
index 82a00d362212..c42d60513077 100644
--- a/MdePkg/Library/BaseRngLib/AArch64/ArmReadIdIsar0.S
+++ b/MdePkg/Library/BaseRngLib/AArch64/ArmGetFeatRng.S
@@ -1,6 +1,6 @@
 #------------------------------------------------------------------------------
 #
-# ArmReadIdIsar0() for AArch64
+# ArmGetFeatRng() for AArch64
 #
 # Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
 #
@@ -10,7 +10,7 @@
 
 .text
 .p2align 2
-GCC_ASM_EXPORT(ArmReadIdIsar0)
+GCC_ASM_EXPORT(ArmGetFeatRng)
 
 #/**
 #  Reads the ID_AA64ISAR0 Register.
@@ -20,11 +20,11 @@ GCC_ASM_EXPORT(ArmReadIdIsar0)
 #**/
 #UINT64
 #EFIAPI
-#ArmReadIdIsar0 (
+#ArmGetFeatRng (
 #  VOID
 #  );
 #
-ASM_PFX(ArmReadIdIsar0):
+ASM_PFX(ArmGetFeatRng):
   mrs  x0, id_aa64isar0_el1 // Read ID_AA64ISAR0 Register
   ret
 
diff --git a/MdePkg/Library/BaseRngLib/AArch64/ArmReadIdIsar0.asm b/MdePkg/Library/BaseRngLib/AArch64/ArmGetFeatRng.asm
similarity index 81%
rename from MdePkg/Library/BaseRngLib/AArch64/ArmReadIdIsar0.asm
rename to MdePkg/Library/BaseRngLib/AArch64/ArmGetFeatRng.asm
index 1d9f9a808c0c..947adfcd2749 100644
--- a/MdePkg/Library/BaseRngLib/AArch64/ArmReadIdIsar0.asm
+++ b/MdePkg/Library/BaseRngLib/AArch64/ArmGetFeatRng.asm
@@ -1,6 +1,6 @@
 ;------------------------------------------------------------------------------
 ;
-; ArmReadIdIsar0() for AArch64
+; ArmGetFeatRng() for AArch64
 ;
 ; Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
 ;
@@ -8,7 +8,7 @@
 ;
 ;------------------------------------------------------------------------------
 
-  EXPORT ArmReadIdIsar0
+  EXPORT ArmGetFeatRng
   AREA BaseLib_LowLevel, CODE, READONLY
 
 ;/**
@@ -19,11 +19,11 @@
 ;**/
 ;UINT64
 ;EFIAPI
-;ArmReadIdIsar0 (
+;ArmGetFeatRng (
 ;  VOID
 ;  );
 ;
-ArmReadIdIsar0
+ArmGetFeatRng
   mrs  x0, id_aa64isar0_el1 // Read ID_AA64ISAR0 Register
   ret
 
diff --git a/MdePkg/Library/BaseRngLib/AArch64/ArmRng.h b/MdePkg/Library/BaseRngLib/AArch64/ArmRng.h
index 2d6ef48ab941..b35cba3c063a 100644
--- a/MdePkg/Library/BaseRngLib/AArch64/ArmRng.h
+++ b/MdePkg/Library/BaseRngLib/AArch64/ArmRng.h
@@ -35,7 +35,7 @@ ArmRndr (
 **/
 UINT64
 EFIAPI
-ArmReadIdIsar0 (
+ArmGetFeatRng (
   VOID
   );
 
diff --git a/MdePkg/Library/BaseRngLib/AArch64/Rndr.c b/MdePkg/Library/BaseRngLib/AArch64/Rndr.c
index 20811bf3ebf3..0cfdf4c37149 100644
--- a/MdePkg/Library/BaseRngLib/AArch64/Rndr.c
+++ b/MdePkg/Library/BaseRngLib/AArch64/Rndr.c
@@ -47,7 +47,7 @@ BaseRngLibConstructor (
   // Determine RNDR support by examining bits 63:60 of the ISAR0 register returned by
   // MSR. A non-zero value indicates that the processor supports the RNDR instruction.
   //
-  Isar0 = ArmReadIdIsar0 ();
+  Isar0 = ArmGetFeatRng ();
   ASSERT ((Isar0 & RNDR_MASK) != 0);
 
   mRndrSupported = ((Isar0 & RNDR_MASK) != 0);
diff --git a/MdePkg/Library/BaseRngLib/BaseRngLib.inf b/MdePkg/Library/BaseRngLib/BaseRngLib.inf
index 1fcceb941495..d6eccb07d469 100644
--- a/MdePkg/Library/BaseRngLib/BaseRngLib.inf
+++ b/MdePkg/Library/BaseRngLib/BaseRngLib.inf
@@ -37,10 +37,10 @@ [Sources.AARCH64]
   AArch64/Rndr.c
   AArch64/ArmRng.h
 
-  AArch64/ArmReadIdIsar0.S   | GCC
+  AArch64/ArmGetFeatRng.S    | GCC
   AArch64/ArmRng.S           | GCC
 
-  AArch64/ArmReadIdIsar0.asm | MSFT
+  AArch64/ArmGetFeatRng.asm  | MSFT
   AArch64/ArmRng.asm         | MSFT
 
 [Packages]
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 10/22] ArmPkg/ArmLib: Add ArmReadIdIsar0() helper
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (8 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 09/22] MdePkg/BaseRngLib: Rename ArmReadIdIsar0() to ArmGetFeatRng() PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 11/22] ArmPkg/ArmLib: Add ArmHasRngExt() PierreGondois
                   ` (21 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <pierre.gondois@arm.com>

Add a ArmReadIdIsar0() helper function to access the AArch64
ID_ISAR0_EL1 register.

Signed-off-by: Pierre Gondois <pierre.gondois@arm.com>
---
 ArmPkg/Library/ArmLib/AArch64/AArch64Lib.h     | 12 +++++++++++-
 ArmPkg/Library/ArmLib/AArch64/AArch64Support.S |  7 ++++++-
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/ArmPkg/Library/ArmLib/AArch64/AArch64Lib.h b/ArmPkg/Library/ArmLib/AArch64/AArch64Lib.h
index 330481fc50db..105a52ee16fe 100644
--- a/ArmPkg/Library/ArmLib/AArch64/AArch64Lib.h
+++ b/ArmPkg/Library/ArmLib/AArch64/AArch64Lib.h
@@ -1,7 +1,7 @@
 /** @file
 
   Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
-  Portions Copyright (c) 2011 - 2013, ARM Ltd. All rights reserved.<BR>
+  Portions Copyright (c) 2011 - 2022, Arm Ltd. All rights reserved.<BR>
   Copyright (c) 2020, NUVIA Inc. All rights reserved.<BR>
 
   SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -54,4 +54,14 @@ ArmReadIdAA64Mmfr2 (
   VOID
   );
 
+/** Reads the ID_ISAR0_EL1 register.
+
+   @return The contents of the ID_ISAR0_EL1 register.
+**/
+UINTN
+EFIAPI
+ArmReadIdIsar0 (
+  VOID
+  );
+
 #endif // AARCH64_LIB_H_
diff --git a/ArmPkg/Library/ArmLib/AArch64/AArch64Support.S b/ArmPkg/Library/ArmLib/AArch64/AArch64Support.S
index d3cc1e86716b..baba283d01b9 100644
--- a/ArmPkg/Library/ArmLib/AArch64/AArch64Support.S
+++ b/ArmPkg/Library/ArmLib/AArch64/AArch64Support.S
@@ -1,7 +1,7 @@
 #------------------------------------------------------------------------------
 #
 # Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
-# Copyright (c) 2011 - 2017, ARM Limited. All rights reserved.
+# Copyright (c) 2011 - 2022, Arm Limited. All rights reserved.
 # Copyright (c) 2016, Linaro Limited. All rights reserved.
 # Copyright (c) 2020, NUVIA Inc. All rights reserved.
 #
@@ -482,4 +482,9 @@ ASM_FUNC(ArmWriteCntHctl)
   msr   cnthctl_el2, x0
   ret
 
+// UINTN ArmReadIdIsar0(VOID)
+ASM_FUNC(ArmReadIdIsar0)
+  mrs   x0, id_aa64isar0_el1
+  ret
+
 ASM_FUNCTION_REMOVE_IF_UNREFERENCED
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 11/22] ArmPkg/ArmLib: Add ArmHasRngExt()
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (9 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 10/22] ArmPkg/ArmLib: Add ArmReadIdIsar0() helper PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 12/21] SecurityPkg/RngDxe: Rename RdRandGenerateEntropy to generic name PierreGondois
                   ` (20 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <pierre.gondois@arm.com>

Add a ArmHasRngExt() to check for the FEAT_RNG extension.
Also add a mask for the RNDR bits.

Signed-off-by: Pierre Gondois <pierre.gondois@arm.com>
---
 ArmPkg/Include/Library/ArmLib.h            | 12 +++++++++++-
 ArmPkg/Library/ArmLib/AArch64/AArch64Lib.c | 15 ++++++++++++++-
 ArmPkg/Library/ArmLib/AArch64/AArch64Lib.h |  2 ++
 ArmPkg/Library/ArmLib/Arm/ArmV7Lib.c       | 16 +++++++++++++++-
 4 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/ArmPkg/Include/Library/ArmLib.h b/ArmPkg/Include/Library/ArmLib.h
index 6566deebdde2..8058634dbc53 100644
--- a/ArmPkg/Include/Library/ArmLib.h
+++ b/ArmPkg/Include/Library/ArmLib.h
@@ -1,7 +1,7 @@
 /** @file
 
   Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
-  Copyright (c) 2011 - 2016, ARM Ltd. All rights reserved.<BR>
+  Copyright (c) 2011 - 2022, Arm Limited. All rights reserved.<BR>
   Copyright (c) 2020 - 2021, NUVIA Inc. All rights reserved.<BR>
 
   SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -769,6 +769,16 @@ ArmHasCcidx (
   VOID
   );
 
+/** Check if FEAT_RNG extension is available.
+
+  @retval TRUE if FEAT_RNG extension is available.
+  @retval FALSE otherwise.
+**/
+BOOLEAN
+ArmHasRngExt (
+  VOID
+  );
+
 #ifdef MDE_CPU_ARM
 ///
 /// AArch32-only ID Register Helper functions
diff --git a/ArmPkg/Library/ArmLib/AArch64/AArch64Lib.c b/ArmPkg/Library/ArmLib/AArch64/AArch64Lib.c
index 7ab28e3e05fe..124b28e16874 100644
--- a/ArmPkg/Library/ArmLib/AArch64/AArch64Lib.c
+++ b/ArmPkg/Library/ArmLib/AArch64/AArch64Lib.c
@@ -1,7 +1,7 @@
 /** @file
 
   Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
-  Portions copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.<BR>
+  Portions copyright (c) 2011 - 2022, Arm Limited. All rights reserved.<BR>
   Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
 
   SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -104,3 +104,16 @@ ArmHasCcidx (
   Mmfr2 = ArmReadIdAA64Mmfr2 ();
   return (((Mmfr2 >> 20) & 0xF) == 1) ? TRUE : FALSE;
 }
+
+/** Check if FEAT_RNG extension is available.
+
+  @retval TRUE if FEAT_RNG extension is available.
+  @retval FALSE otherwise.
+**/
+BOOLEAN
+ArmHasRngExt (
+  VOID
+  )
+{
+  return ArmReadIdIsar0 () & ID_AA64ISAR0_EL1_RNDR_MASK;
+}
diff --git a/ArmPkg/Library/ArmLib/AArch64/AArch64Lib.h b/ArmPkg/Library/ArmLib/AArch64/AArch64Lib.h
index 105a52ee16fe..61a775ea27e8 100644
--- a/ArmPkg/Library/ArmLib/AArch64/AArch64Lib.h
+++ b/ArmPkg/Library/ArmLib/AArch64/AArch64Lib.h
@@ -11,6 +11,8 @@
 #ifndef AARCH64_LIB_H_
 #define AARCH64_LIB_H_
 
+#define ID_AA64ISAR0_EL1_RNDR_MASK  ((UINT64)0xF << 60U)
+
 typedef VOID (*AARCH64_CACHE_OPERATION)(
   UINTN
   );
diff --git a/ArmPkg/Library/ArmLib/Arm/ArmV7Lib.c b/ArmPkg/Library/ArmLib/Arm/ArmV7Lib.c
index 521d5be0de33..a4ec23c8f8d8 100644
--- a/ArmPkg/Library/ArmLib/Arm/ArmV7Lib.c
+++ b/ArmPkg/Library/ArmLib/Arm/ArmV7Lib.c
@@ -1,7 +1,7 @@
 /** @file
 
   Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
-  Copyright (c) 2011 - 2014, ARM Limited. All rights reserved.
+  Copyright (c) 2011 - 2022, Arm Limited. All rights reserved.
   Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
 
   SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -119,3 +119,17 @@ ArmHasCcidx (
   Mmfr4 = ArmReadIdMmfr4 ();
   return (((Mmfr4 >> 24) & 0xF) == 1) ? TRUE : FALSE;
 }
+
+/** Check if FEAT_RNG extension is available.
+
+  @retval TRUE if FEAT_RNG extension is available.
+  @retval FALSE otherwise.
+**/
+BOOLEAN
+ArmHasRngExt (
+  VOID
+  )
+{
+  // Not supported.
+  return FALSE;
+}
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 12/21] SecurityPkg/RngDxe: Rename RdRandGenerateEntropy to generic name
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (10 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 11/22] ArmPkg/ArmLib: Add ArmHasRngExt() PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 12/22] SecurityPkg: Update Securitypkg.ci.yaml PierreGondois
                   ` (19 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Sami Mujawar <sami.mujawar@arm.com>

Bugzilla: 3668 (https://bugzilla.tianocore.org/show_bug.cgi?id=3668)

Rename RdRandGenerateEntropy() to GenerateEntropy() to provide a
common interface to generate entropy on other architectures.
GenerateEntropy() is intended to generate high quality entropy.

Also move the definition to RngDxeInternals.h

Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
---
 .../RngDxe/Rand/RdRand.c                      | 14 ++++--
 .../RngDxe/Rand/RdRand.h                      | 43 -------------------
 .../RngDxe/Rand/RngDxe.c                      |  7 ++-
 .../RandomNumberGenerator/RngDxe/RngDxe.inf   |  2 +-
 .../RngDxe/RngDxeInternals.h                  | 19 ++++++++
 5 files changed, 36 insertions(+), 49 deletions(-)
 delete mode 100644 SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RdRand.h

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RdRand.c b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RdRand.c
index 5b6644138231..4b011c7e8e49 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RdRand.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RdRand.c
@@ -1,15 +1,23 @@
 /** @file
-  Support routines for RDRAND instruction access.
+  Support routines for RDRAND instruction access, which will leverage
+  Intel Secure Key technology to provide high-quality random numbers for use
+  in applications, or entropy for seeding other random number generators.
+  Refer to http://software.intel.com/en-us/articles/intel-digital-random-number
+  -generator-drng-software-implementation-guide/ for more information about Intel
+  Secure Key technology.
 
+Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
 #include <Library/RngLib.h>
+#include <Library/TimerLib.h>
 
 #include "AesCore.h"
-#include "RdRand.h"
 #include "RngDxeInternals.h"
 
 /**
@@ -87,7 +95,7 @@ RdRandGetSeed128 (
 **/
 EFI_STATUS
 EFIAPI
-RdRandGenerateEntropy (
+GenerateEntropy (
   IN UINTN   Length,
   OUT UINT8  *Entropy
   )
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RdRand.h b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RdRand.h
deleted file mode 100644
index 7fdb6891bd63..000000000000
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RdRand.h
+++ /dev/null
@@ -1,43 +0,0 @@
-/** @file
-  Header for the RDRAND APIs used by RNG DXE driver.
-
-  Support API definitions for RDRAND instruction access, which will leverage
-  Intel Secure Key technology to provide high-quality random numbers for use
-  in applications, or entropy for seeding other random number generators.
-  Refer to http://software.intel.com/en-us/articles/intel-digital-random-number
-  -generator-drng-software-implementation-guide/ for more information about Intel
-  Secure Key technology.
-
-Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>
-(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
-SPDX-License-Identifier: BSD-2-Clause-Patent
-
-**/
-
-#ifndef __RD_RAND_H__
-#define __RD_RAND_H__
-
-#include <Library/BaseLib.h>
-#include <Library/BaseMemoryLib.h>
-#include <Library/UefiBootServicesTableLib.h>
-#include <Library/TimerLib.h>
-#include <Protocol/Rng.h>
-
-/**
-  Generate high-quality entropy source through RDRAND.
-
-  @param[in]   Length        Size of the buffer, in bytes, to fill with.
-  @param[out]  Entropy       Pointer to the buffer to store the entropy data.
-
-  @retval EFI_SUCCESS        Entropy generation succeeded.
-  @retval EFI_NOT_READY      Failed to request random data.
-
-**/
-EFI_STATUS
-EFIAPI
-RdRandGenerateEntropy (
-  IN UINTN   Length,
-  OUT UINT8  *Entropy
-  );
-
-#endif // __RD_RAND_H__
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
index 2df4ed44329a..8d44f0636c3d 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
@@ -14,13 +14,16 @@
    - EFI_RNG_ALGORITHM_X9_31_3DES_GUID        - Unsupported
    - EFI_RNG_ALGORITHM_X9_31_AES_GUID         - Unsupported
 
+  Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
   Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
   (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
-#include "RdRand.h"
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+
 #include "RngDxeInternals.h"
 
 /**
@@ -88,7 +91,7 @@ RngGetRNG (
       return EFI_INVALID_PARAMETER;
     }
 
-    Status = RdRandGenerateEntropy (RNGValueLength, RNGValue);
+    Status = GenerateEntropy (RNGValueLength, RNGValue);
     return Status;
   }
 
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
index f3300971993f..60efb5562ee0 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
@@ -10,6 +10,7 @@
 #
 #  Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
 #  (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
+#  Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
 ##
@@ -36,7 +37,6 @@ [Sources.common]
 [Sources.IA32, Sources.X64]
   Rand/RngDxe.c
   Rand/RdRand.c
-  Rand/RdRand.h
   Rand/AesCore.c
   Rand/AesCore.h
 
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxeInternals.h b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxeInternals.h
index 224d8bd4ea5f..48d2d27c1608 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxeInternals.h
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxeInternals.h
@@ -10,6 +10,8 @@
 #ifndef RNGDXE_INTERNALS_H_
 #define RNGDXE_INTERNALS_H_
 
+#include <Protocol/Rng.h>
+
 /**
   Returns information about the random number generation implementation.
 
@@ -114,4 +116,21 @@ RngGetBytes (
   OUT UINT8  *RandBuffer
   );
 
+/**
+  Generate high-quality entropy source using a TRNG or through RDRAND.
+
+  @param[in]   Length        Size of the buffer, in bytes, to fill with.
+  @param[out]  Entropy       Pointer to the buffer to store the entropy data.
+
+  @retval EFI_SUCCESS        Entropy generation succeeded.
+  @retval EFI_NOT_READY      Failed to request random data.
+
+**/
+EFI_STATUS
+EFIAPI
+GenerateEntropy (
+  IN UINTN   Length,
+  OUT UINT8  *Entropy
+  );
+
 #endif // RNGDXE_INTERNALS_H_
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 12/22] SecurityPkg: Update Securitypkg.ci.yaml
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (11 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 12/21] SecurityPkg/RngDxe: Rename RdRandGenerateEntropy to generic name PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 13/22] SecurityPkg/RngDxe: Rename RdRandGenerateEntropy to generic name PierreGondois
                   ` (18 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <pierre.gondois@arm.com>

Add ArmPkg.dec as a valid dependency for the SecurityPkg.

Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
---
 SecurityPkg/SecurityPkg.ci.yaml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/SecurityPkg/SecurityPkg.ci.yaml b/SecurityPkg/SecurityPkg.ci.yaml
index 791214239899..08e98d713224 100644
--- a/SecurityPkg/SecurityPkg.ci.yaml
+++ b/SecurityPkg/SecurityPkg.ci.yaml
@@ -31,6 +31,7 @@
     },
     "DependencyCheck": {
         "AcceptableDependencies": [
+            "ArmPkg/ArmPkg.dec",
             "MdePkg/MdePkg.dec",
             "MdeModulePkg/MdeModulePkg.dec",
             "SecurityPkg/SecurityPkg.dec",
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 13/22] SecurityPkg/RngDxe: Rename RdRandGenerateEntropy to generic name
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (12 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 12/22] SecurityPkg: Update Securitypkg.ci.yaml PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 13/21] SecurityPkg/RngDxe: Replace Pcd with Sp80090Ctr256Guid PierreGondois
                   ` (17 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Sami Mujawar <sami.mujawar@arm.com>

Bugzilla: 3668 (https://bugzilla.tianocore.org/show_bug.cgi?id=3668)

Rename RdRandGenerateEntropy() to GenerateEntropy() to provide a
common interface to generate entropy on other architectures.
GenerateEntropy() is intended to generate high quality entropy.

Also move the definition to RngDxeInternals.h

Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
---
 .../RngDxe/Rand/RdRand.c                      | 14 ++++--
 .../RngDxe/Rand/RdRand.h                      | 43 -------------------
 .../RngDxe/Rand/RngDxe.c                      |  7 ++-
 .../RandomNumberGenerator/RngDxe/RngDxe.inf   |  2 +-
 .../RngDxe/RngDxeInternals.h                  | 19 ++++++++
 5 files changed, 36 insertions(+), 49 deletions(-)
 delete mode 100644 SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RdRand.h

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RdRand.c b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RdRand.c
index 5b6644138231..4b011c7e8e49 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RdRand.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RdRand.c
@@ -1,15 +1,23 @@
 /** @file
-  Support routines for RDRAND instruction access.
+  Support routines for RDRAND instruction access, which will leverage
+  Intel Secure Key technology to provide high-quality random numbers for use
+  in applications, or entropy for seeding other random number generators.
+  Refer to http://software.intel.com/en-us/articles/intel-digital-random-number
+  -generator-drng-software-implementation-guide/ for more information about Intel
+  Secure Key technology.
 
+Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
 #include <Library/RngLib.h>
+#include <Library/TimerLib.h>
 
 #include "AesCore.h"
-#include "RdRand.h"
 #include "RngDxeInternals.h"
 
 /**
@@ -87,7 +95,7 @@ RdRandGetSeed128 (
 **/
 EFI_STATUS
 EFIAPI
-RdRandGenerateEntropy (
+GenerateEntropy (
   IN UINTN   Length,
   OUT UINT8  *Entropy
   )
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RdRand.h b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RdRand.h
deleted file mode 100644
index 7fdb6891bd63..000000000000
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RdRand.h
+++ /dev/null
@@ -1,43 +0,0 @@
-/** @file
-  Header for the RDRAND APIs used by RNG DXE driver.
-
-  Support API definitions for RDRAND instruction access, which will leverage
-  Intel Secure Key technology to provide high-quality random numbers for use
-  in applications, or entropy for seeding other random number generators.
-  Refer to http://software.intel.com/en-us/articles/intel-digital-random-number
-  -generator-drng-software-implementation-guide/ for more information about Intel
-  Secure Key technology.
-
-Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>
-(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
-SPDX-License-Identifier: BSD-2-Clause-Patent
-
-**/
-
-#ifndef __RD_RAND_H__
-#define __RD_RAND_H__
-
-#include <Library/BaseLib.h>
-#include <Library/BaseMemoryLib.h>
-#include <Library/UefiBootServicesTableLib.h>
-#include <Library/TimerLib.h>
-#include <Protocol/Rng.h>
-
-/**
-  Generate high-quality entropy source through RDRAND.
-
-  @param[in]   Length        Size of the buffer, in bytes, to fill with.
-  @param[out]  Entropy       Pointer to the buffer to store the entropy data.
-
-  @retval EFI_SUCCESS        Entropy generation succeeded.
-  @retval EFI_NOT_READY      Failed to request random data.
-
-**/
-EFI_STATUS
-EFIAPI
-RdRandGenerateEntropy (
-  IN UINTN   Length,
-  OUT UINT8  *Entropy
-  );
-
-#endif // __RD_RAND_H__
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
index 2df4ed44329a..8d44f0636c3d 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
@@ -14,13 +14,16 @@
    - EFI_RNG_ALGORITHM_X9_31_3DES_GUID        - Unsupported
    - EFI_RNG_ALGORITHM_X9_31_AES_GUID         - Unsupported
 
+  Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
   Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
   (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
-#include "RdRand.h"
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+
 #include "RngDxeInternals.h"
 
 /**
@@ -88,7 +91,7 @@ RngGetRNG (
       return EFI_INVALID_PARAMETER;
     }
 
-    Status = RdRandGenerateEntropy (RNGValueLength, RNGValue);
+    Status = GenerateEntropy (RNGValueLength, RNGValue);
     return Status;
   }
 
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
index f3300971993f..60efb5562ee0 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
@@ -10,6 +10,7 @@
 #
 #  Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
 #  (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
+#  Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
 ##
@@ -36,7 +37,6 @@ [Sources.common]
 [Sources.IA32, Sources.X64]
   Rand/RngDxe.c
   Rand/RdRand.c
-  Rand/RdRand.h
   Rand/AesCore.c
   Rand/AesCore.h
 
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxeInternals.h b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxeInternals.h
index 224d8bd4ea5f..48d2d27c1608 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxeInternals.h
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxeInternals.h
@@ -10,6 +10,8 @@
 #ifndef RNGDXE_INTERNALS_H_
 #define RNGDXE_INTERNALS_H_
 
+#include <Protocol/Rng.h>
+
 /**
   Returns information about the random number generation implementation.
 
@@ -114,4 +116,21 @@ RngGetBytes (
   OUT UINT8  *RandBuffer
   );
 
+/**
+  Generate high-quality entropy source using a TRNG or through RDRAND.
+
+  @param[in]   Length        Size of the buffer, in bytes, to fill with.
+  @param[out]  Entropy       Pointer to the buffer to store the entropy data.
+
+  @retval EFI_SUCCESS        Entropy generation succeeded.
+  @retval EFI_NOT_READY      Failed to request random data.
+
+**/
+EFI_STATUS
+EFIAPI
+GenerateEntropy (
+  IN UINTN   Length,
+  OUT UINT8  *Entropy
+  );
+
 #endif // RNGDXE_INTERNALS_H_
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 13/21] SecurityPkg/RngDxe: Replace Pcd with Sp80090Ctr256Guid
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (13 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 13/22] SecurityPkg/RngDxe: Rename RdRandGenerateEntropy to generic name PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 14/21] SecurityPkg/RngDxe: Remove ArchGetSupportedRngAlgorithms() PierreGondois
                   ` (16 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <pierre.gondois@arm.com>

gEfiRngAlgorithmSp80090Ctr256Guid was used as the default algorithm
in RngGetRNG(). The commit below set the default algorithm to
PcdCpuRngSupportedAlgorithm, which is a zero GUID by default.

As the Pcd value is not defined for any platform in the edk2-platfoms
repository, assume it was an error and go back to the first version,
using gEfiRngAlgorithmSp80090Ctr256Guid.

Fixes 4e5ecdbac8bd ("SecurityPkg: Add support for RngDxe on AARCH64")
Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
---
 SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
index 8d44f0636c3d..8cfe6b471192 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
@@ -126,8 +126,7 @@ ArchGetSupportedRngAlgorithms (
   OUT    EFI_RNG_ALGORITHM  *RNGAlgorithmList
   )
 {
-  UINTN              RequiredSize;
-  EFI_RNG_ALGORITHM  *CpuRngSupportedAlgorithm;
+  UINTN  RequiredSize;
 
   RequiredSize = 2 * sizeof (EFI_RNG_ALGORITHM);
 
@@ -136,9 +135,7 @@ ArchGetSupportedRngAlgorithms (
     return EFI_BUFFER_TOO_SMALL;
   }
 
-  CpuRngSupportedAlgorithm = PcdGetPtr (PcdCpuRngSupportedAlgorithm);
-
-  CopyMem (&RNGAlgorithmList[0], CpuRngSupportedAlgorithm, sizeof (EFI_RNG_ALGORITHM));
+  CopyMem (&RNGAlgorithmList[0], gEfiRngAlgorithmSp80090Ctr256Guid, sizeof (EFI_RNG_ALGORITHM));
 
   // x86 platforms also support EFI_RNG_ALGORITHM_RAW via RDSEED
   CopyMem (&RNGAlgorithmList[1], &gEfiRngAlgorithmRaw, sizeof (EFI_RNG_ALGORITHM));
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 14/21] SecurityPkg/RngDxe: Remove ArchGetSupportedRngAlgorithms()
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (14 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 13/21] SecurityPkg/RngDxe: Replace Pcd with Sp80090Ctr256Guid PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 14/22] SecurityPkg/RngDxe: Replace Pcd with Sp80090Ctr256Guid PierreGondois
                   ` (15 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <Pierre.Gondois@arm.com>

RngGetInfo() is one of the 2 functions of the EFI_RNG_PROTOCOL.
RngGetInfo() is currently a mere wrapper around
ArchGetSupportedRngAlgorithms() which is implemented differently
depending on the architecture used.

RngGetInfo() does nothing more than calling
ArchGetSupportedRngAlgorithms(). So remove it, and let RngGetInfo()
be implemented differently according to the architecture.

This follows the implementation of the other function of the
EFI_RNG_PROTOCOL, RngGetRNG().

Signed-off-by: Pierre Gondois <pierre.gondois@arm.com>
---
 .../RngDxe/AArch64/RngDxe.c                   | 19 +++++--
 .../RngDxe/Rand/RngDxe.c                      | 24 +++++++--
 .../RandomNumberGenerator/RngDxe/RngDxe.c     | 49 -------------------
 .../RngDxe/RngDxeInternals.h                  | 25 ----------
 4 files changed, 34 insertions(+), 83 deletions(-)

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
index 3daf847d46d3..6d989f7ea376 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
@@ -14,6 +14,7 @@
   Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
   Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
   (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
+  Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
 
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
@@ -85,6 +86,7 @@ RngGetRNG (
 /**
   Returns information about the random number generation implementation.
 
+  @param[in]     This                 A pointer to the EFI_RNG_PROTOCOL instance.
   @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
                                       On output with a return code of EFI_SUCCESS, the size
                                       in bytes of the data returned in RNGAlgorithmList. On output
@@ -97,14 +99,19 @@ RngGetRNG (
                                       is the default algorithm for the driver.
 
   @retval EFI_SUCCESS                 The RNG algorithm list was returned successfully.
+  @retval EFI_UNSUPPORTED             The services is not supported by this driver.
+  @retval EFI_DEVICE_ERROR            The list of algorithms could not be retrieved due to a
+                                      hardware or firmware error.
+  @retval EFI_INVALID_PARAMETER       One or more of the parameters are incorrect.
   @retval EFI_BUFFER_TOO_SMALL        The buffer RNGAlgorithmList is too small to hold the result.
 
 **/
-UINTN
+EFI_STATUS
 EFIAPI
-ArchGetSupportedRngAlgorithms (
-  IN OUT UINTN              *RNGAlgorithmListSize,
-  OUT    EFI_RNG_ALGORITHM  *RNGAlgorithmList
+RngGetInfo (
+  IN EFI_RNG_PROTOCOL    *This,
+  IN OUT UINTN           *RNGAlgorithmListSize,
+  OUT EFI_RNG_ALGORITHM  *RNGAlgorithmList
   )
 {
   UINTN              RequiredSize;
@@ -112,6 +119,10 @@ ArchGetSupportedRngAlgorithms (
 
   RequiredSize = sizeof (EFI_RNG_ALGORITHM);
 
+  if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
   if (*RNGAlgorithmListSize < RequiredSize) {
     *RNGAlgorithmListSize = RequiredSize;
     return EFI_BUFFER_TOO_SMALL;
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
index 8cfe6b471192..b2d2236380fd 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
@@ -104,6 +104,7 @@ RngGetRNG (
 /**
   Returns information about the random number generation implementation.
 
+  @param[in]     This                 A pointer to the EFI_RNG_PROTOCOL instance.
   @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
                                       On output with a return code of EFI_SUCCESS, the size
                                       in bytes of the data returned in RNGAlgorithmList. On output
@@ -116,18 +117,27 @@ RngGetRNG (
                                       is the default algorithm for the driver.
 
   @retval EFI_SUCCESS                 The RNG algorithm list was returned successfully.
+  @retval EFI_UNSUPPORTED             No supported algorithms found.
+  @retval EFI_DEVICE_ERROR            The list of algorithms could not be retrieved due to a
+                                      hardware or firmware error.
+  @retval EFI_INVALID_PARAMETER       One or more of the parameters are incorrect.
   @retval EFI_BUFFER_TOO_SMALL        The buffer RNGAlgorithmList is too small to hold the result.
 
 **/
-UINTN
+EFI_STATUS
 EFIAPI
-ArchGetSupportedRngAlgorithms (
-  IN OUT UINTN              *RNGAlgorithmListSize,
-  OUT    EFI_RNG_ALGORITHM  *RNGAlgorithmList
+RngGetInfo (
+  IN EFI_RNG_PROTOCOL    *This,
+  IN OUT UINTN           *RNGAlgorithmListSize,
+  OUT EFI_RNG_ALGORITHM  *RNGAlgorithmList
   )
 {
   UINTN  RequiredSize;
 
+  if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
   RequiredSize = 2 * sizeof (EFI_RNG_ALGORITHM);
 
   if (*RNGAlgorithmListSize < RequiredSize) {
@@ -135,7 +145,11 @@ ArchGetSupportedRngAlgorithms (
     return EFI_BUFFER_TOO_SMALL;
   }
 
-  CopyMem (&RNGAlgorithmList[0], gEfiRngAlgorithmSp80090Ctr256Guid, sizeof (EFI_RNG_ALGORITHM));
+  if (RNGAlgorithmList == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  CopyMem (&RNGAlgorithmList[0], &gEfiRngAlgorithmSp80090Ctr256Guid, sizeof (EFI_RNG_ALGORITHM));
 
   // x86 platforms also support EFI_RNG_ALGORITHM_RAW via RDSEED
   CopyMem (&RNGAlgorithmList[1], &gEfiRngAlgorithmRaw, sizeof (EFI_RNG_ALGORITHM));
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c
index 6f52eeff4a09..6608ca8804a5 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c
@@ -28,55 +28,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 
 #include "RngDxeInternals.h"
 
-/**
-  Returns information about the random number generation implementation.
-
-  @param[in]     This                 A pointer to the EFI_RNG_PROTOCOL instance.
-  @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
-                                      On output with a return code of EFI_SUCCESS, the size
-                                      in bytes of the data returned in RNGAlgorithmList. On output
-                                      with a return code of EFI_BUFFER_TOO_SMALL,
-                                      the size of RNGAlgorithmList required to obtain the list.
-  @param[out] RNGAlgorithmList        A caller-allocated memory buffer filled by the driver
-                                      with one EFI_RNG_ALGORITHM element for each supported
-                                      RNG algorithm. The list must not change across multiple
-                                      calls to the same driver. The first algorithm in the list
-                                      is the default algorithm for the driver.
-
-  @retval EFI_SUCCESS                 The RNG algorithm list was returned successfully.
-  @retval EFI_UNSUPPORTED             The services is not supported by this driver.
-  @retval EFI_DEVICE_ERROR            The list of algorithms could not be retrieved due to a
-                                      hardware or firmware error.
-  @retval EFI_INVALID_PARAMETER       One or more of the parameters are incorrect.
-  @retval EFI_BUFFER_TOO_SMALL        The buffer RNGAlgorithmList is too small to hold the result.
-
-**/
-EFI_STATUS
-EFIAPI
-RngGetInfo (
-  IN EFI_RNG_PROTOCOL    *This,
-  IN OUT UINTN           *RNGAlgorithmListSize,
-  OUT EFI_RNG_ALGORITHM  *RNGAlgorithmList
-  )
-{
-  EFI_STATUS  Status;
-
-  if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {
-    return EFI_INVALID_PARAMETER;
-  }
-
-  //
-  // Return algorithm list supported by driver.
-  //
-  if (RNGAlgorithmList != NULL) {
-    Status = ArchGetSupportedRngAlgorithms (RNGAlgorithmListSize, RNGAlgorithmList);
-  } else {
-    Status = EFI_INVALID_PARAMETER;
-  }
-
-  return Status;
-}
-
 //
 // The Random Number Generator (RNG) protocol
 //
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxeInternals.h b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxeInternals.h
index 48d2d27c1608..7ecab140483d 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxeInternals.h
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxeInternals.h
@@ -74,31 +74,6 @@ RngGetRNG (
   OUT UINT8             *RNGValue
   );
 
-/**
-  Returns information about the random number generation implementation.
-
-  @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
-                                      On output with a return code of EFI_SUCCESS, the size
-                                      in bytes of the data returned in RNGAlgorithmList. On output
-                                      with a return code of EFI_BUFFER_TOO_SMALL,
-                                      the size of RNGAlgorithmList required to obtain the list.
-  @param[out] RNGAlgorithmList        A caller-allocated memory buffer filled by the driver
-                                      with one EFI_RNG_ALGORITHM element for each supported
-                                      RNG algorithm. The list must not change across multiple
-                                      calls to the same driver. The first algorithm in the list
-                                      is the default algorithm for the driver.
-
-  @retval EFI_SUCCESS                 The RNG algorithm list was returned successfully.
-  @retval EFI_BUFFER_TOO_SMALL        The buffer RNGAlgorithmList is too small to hold the result.
-
-**/
-UINTN
-EFIAPI
-ArchGetSupportedRngAlgorithms (
-  IN OUT UINTN              *RNGAlgorithmListSize,
-  OUT    EFI_RNG_ALGORITHM  *RNGAlgorithmList
-  );
-
 /**
   Runs CPU RNG instruction to fill a buffer of arbitrary size with random bytes.
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 14/22] SecurityPkg/RngDxe: Replace Pcd with Sp80090Ctr256Guid
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (15 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 14/21] SecurityPkg/RngDxe: Remove ArchGetSupportedRngAlgorithms() PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-07-21 10:51   ` Yao, Jiewen
  2022-06-29 15:02 ` [PATCH v3 15/21] SecurityPkg/RngDxe: Documentation/include/parameter cleanup PierreGondois
                   ` (14 subsequent siblings)
  31 siblings, 1 reply; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <pierre.gondois@arm.com>

gEfiRngAlgorithmSp80090Ctr256Guid was used as the default algorithm
in RngGetRNG(). The commit below set the default algorithm to
PcdCpuRngSupportedAlgorithm, which is a zero GUID by default.

As the Pcd value is not defined for any platform in the edk2-platfoms
repository, assume it was an error and go back to the first version,
using gEfiRngAlgorithmSp80090Ctr256Guid.

Fixes 4e5ecdbac8bd ("SecurityPkg: Add support for RngDxe on AARCH64")
Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
---
 SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
index 8d44f0636c3d..8cfe6b471192 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
@@ -126,8 +126,7 @@ ArchGetSupportedRngAlgorithms (
   OUT    EFI_RNG_ALGORITHM  *RNGAlgorithmList
   )
 {
-  UINTN              RequiredSize;
-  EFI_RNG_ALGORITHM  *CpuRngSupportedAlgorithm;
+  UINTN  RequiredSize;
 
   RequiredSize = 2 * sizeof (EFI_RNG_ALGORITHM);
 
@@ -136,9 +135,7 @@ ArchGetSupportedRngAlgorithms (
     return EFI_BUFFER_TOO_SMALL;
   }
 
-  CpuRngSupportedAlgorithm = PcdGetPtr (PcdCpuRngSupportedAlgorithm);
-
-  CopyMem (&RNGAlgorithmList[0], CpuRngSupportedAlgorithm, sizeof (EFI_RNG_ALGORITHM));
+  CopyMem (&RNGAlgorithmList[0], gEfiRngAlgorithmSp80090Ctr256Guid, sizeof (EFI_RNG_ALGORITHM));
 
   // x86 platforms also support EFI_RNG_ALGORITHM_RAW via RDSEED
   CopyMem (&RNGAlgorithmList[1], &gEfiRngAlgorithmRaw, sizeof (EFI_RNG_ALGORITHM));
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 15/21] SecurityPkg/RngDxe: Documentation/include/parameter cleanup
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (16 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 14/22] SecurityPkg/RngDxe: Replace Pcd with Sp80090Ctr256Guid PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 15/22] SecurityPkg/RngDxe: Remove ArchGetSupportedRngAlgorithms() PierreGondois
                   ` (13 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <Pierre.Gondois@arm.com>

This patch:
-Update RngGetBytes() documentation to align the function
 definition and declaration.
-Improve input parameter checking. Even though 'This'
 it is not used, the parameter should always point to the
 current EFI_RNG_PROTOCOL.
-Removes TimerLib inclusion as unused.

Signed-off-by: Pierre Gondois <pierre.gondois@arm.com>
---
 SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c | 3 +--
 SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c    | 2 +-
 SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c         | 3 +--
 3 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
index 6d989f7ea376..f9c740d761ff 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
@@ -23,7 +23,6 @@
 #include <Library/BaseLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/UefiBootServicesTableLib.h>
-#include <Library/TimerLib.h>
 #include <Protocol/Rng.h>
 
 #include "RngDxeInternals.h"
@@ -61,7 +60,7 @@ RngGetRNG (
 {
   EFI_STATUS  Status;
 
-  if ((RNGValueLength == 0) || (RNGValue == NULL)) {
+  if ((This == NULL) || (RNGValueLength == 0) || (RNGValue == NULL)) {
     return EFI_INVALID_PARAMETER;
   }
 
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
index b2d2236380fd..8f5d8e740f5e 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
@@ -59,7 +59,7 @@ RngGetRNG (
 {
   EFI_STATUS  Status;
 
-  if ((RNGValueLength == 0) || (RNGValue == NULL)) {
+  if ((This == NULL) || (RNGValueLength == 0) || (RNGValue == NULL)) {
     return EFI_INVALID_PARAMETER;
   }
 
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c
index 6608ca8804a5..d7905a7f4d72 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c
@@ -23,7 +23,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include <Library/BaseMemoryLib.h>
 #include <Library/UefiBootServicesTableLib.h>
 #include <Library/RngLib.h>
-#include <Library/TimerLib.h>
 #include <Protocol/Rng.h>
 
 #include "RngDxeInternals.h"
@@ -72,7 +71,7 @@ RngDriverEntry (
 }
 
 /**
-  Calls RDRAND to fill a buffer of arbitrary size with random bytes.
+  Runs CPU RNG instruction to fill a buffer of arbitrary size with random bytes.
 
   @param[in]   Length        Size of the buffer, in bytes,  to fill with.
   @param[out]  RandBuffer    Pointer to the buffer to store the random result.
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 15/22] SecurityPkg/RngDxe: Remove ArchGetSupportedRngAlgorithms()
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (17 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 15/21] SecurityPkg/RngDxe: Documentation/include/parameter cleanup PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 16/21] SecurityPkg/RngDxe: Check before advertising Cpu Rng algo PierreGondois
                   ` (12 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <Pierre.Gondois@arm.com>

RngGetInfo() is one of the 2 functions of the EFI_RNG_PROTOCOL.
RngGetInfo() is currently a mere wrapper around
ArchGetSupportedRngAlgorithms() which is implemented differently
depending on the architecture used.

RngGetInfo() does nothing more than calling
ArchGetSupportedRngAlgorithms(). So remove it, and let RngGetInfo()
be implemented differently according to the architecture.

This follows the implementation of the other function of the
EFI_RNG_PROTOCOL, RngGetRNG().

Signed-off-by: Pierre Gondois <pierre.gondois@arm.com>
---
 .../RngDxe/AArch64/RngDxe.c                   | 19 +++++--
 .../RngDxe/Rand/RngDxe.c                      | 24 +++++++--
 .../RandomNumberGenerator/RngDxe/RngDxe.c     | 49 -------------------
 .../RngDxe/RngDxeInternals.h                  | 25 ----------
 4 files changed, 34 insertions(+), 83 deletions(-)

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
index 3daf847d46d3..6d989f7ea376 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
@@ -14,6 +14,7 @@
   Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
   Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
   (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
+  Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
 
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
@@ -85,6 +86,7 @@ RngGetRNG (
 /**
   Returns information about the random number generation implementation.
 
+  @param[in]     This                 A pointer to the EFI_RNG_PROTOCOL instance.
   @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
                                       On output with a return code of EFI_SUCCESS, the size
                                       in bytes of the data returned in RNGAlgorithmList. On output
@@ -97,14 +99,19 @@ RngGetRNG (
                                       is the default algorithm for the driver.
 
   @retval EFI_SUCCESS                 The RNG algorithm list was returned successfully.
+  @retval EFI_UNSUPPORTED             The services is not supported by this driver.
+  @retval EFI_DEVICE_ERROR            The list of algorithms could not be retrieved due to a
+                                      hardware or firmware error.
+  @retval EFI_INVALID_PARAMETER       One or more of the parameters are incorrect.
   @retval EFI_BUFFER_TOO_SMALL        The buffer RNGAlgorithmList is too small to hold the result.
 
 **/
-UINTN
+EFI_STATUS
 EFIAPI
-ArchGetSupportedRngAlgorithms (
-  IN OUT UINTN              *RNGAlgorithmListSize,
-  OUT    EFI_RNG_ALGORITHM  *RNGAlgorithmList
+RngGetInfo (
+  IN EFI_RNG_PROTOCOL    *This,
+  IN OUT UINTN           *RNGAlgorithmListSize,
+  OUT EFI_RNG_ALGORITHM  *RNGAlgorithmList
   )
 {
   UINTN              RequiredSize;
@@ -112,6 +119,10 @@ ArchGetSupportedRngAlgorithms (
 
   RequiredSize = sizeof (EFI_RNG_ALGORITHM);
 
+  if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
   if (*RNGAlgorithmListSize < RequiredSize) {
     *RNGAlgorithmListSize = RequiredSize;
     return EFI_BUFFER_TOO_SMALL;
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
index 8cfe6b471192..b2d2236380fd 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
@@ -104,6 +104,7 @@ RngGetRNG (
 /**
   Returns information about the random number generation implementation.
 
+  @param[in]     This                 A pointer to the EFI_RNG_PROTOCOL instance.
   @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
                                       On output with a return code of EFI_SUCCESS, the size
                                       in bytes of the data returned in RNGAlgorithmList. On output
@@ -116,18 +117,27 @@ RngGetRNG (
                                       is the default algorithm for the driver.
 
   @retval EFI_SUCCESS                 The RNG algorithm list was returned successfully.
+  @retval EFI_UNSUPPORTED             No supported algorithms found.
+  @retval EFI_DEVICE_ERROR            The list of algorithms could not be retrieved due to a
+                                      hardware or firmware error.
+  @retval EFI_INVALID_PARAMETER       One or more of the parameters are incorrect.
   @retval EFI_BUFFER_TOO_SMALL        The buffer RNGAlgorithmList is too small to hold the result.
 
 **/
-UINTN
+EFI_STATUS
 EFIAPI
-ArchGetSupportedRngAlgorithms (
-  IN OUT UINTN              *RNGAlgorithmListSize,
-  OUT    EFI_RNG_ALGORITHM  *RNGAlgorithmList
+RngGetInfo (
+  IN EFI_RNG_PROTOCOL    *This,
+  IN OUT UINTN           *RNGAlgorithmListSize,
+  OUT EFI_RNG_ALGORITHM  *RNGAlgorithmList
   )
 {
   UINTN  RequiredSize;
 
+  if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
   RequiredSize = 2 * sizeof (EFI_RNG_ALGORITHM);
 
   if (*RNGAlgorithmListSize < RequiredSize) {
@@ -135,7 +145,11 @@ ArchGetSupportedRngAlgorithms (
     return EFI_BUFFER_TOO_SMALL;
   }
 
-  CopyMem (&RNGAlgorithmList[0], gEfiRngAlgorithmSp80090Ctr256Guid, sizeof (EFI_RNG_ALGORITHM));
+  if (RNGAlgorithmList == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  CopyMem (&RNGAlgorithmList[0], &gEfiRngAlgorithmSp80090Ctr256Guid, sizeof (EFI_RNG_ALGORITHM));
 
   // x86 platforms also support EFI_RNG_ALGORITHM_RAW via RDSEED
   CopyMem (&RNGAlgorithmList[1], &gEfiRngAlgorithmRaw, sizeof (EFI_RNG_ALGORITHM));
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c
index 6f52eeff4a09..6608ca8804a5 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c
@@ -28,55 +28,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 
 #include "RngDxeInternals.h"
 
-/**
-  Returns information about the random number generation implementation.
-
-  @param[in]     This                 A pointer to the EFI_RNG_PROTOCOL instance.
-  @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
-                                      On output with a return code of EFI_SUCCESS, the size
-                                      in bytes of the data returned in RNGAlgorithmList. On output
-                                      with a return code of EFI_BUFFER_TOO_SMALL,
-                                      the size of RNGAlgorithmList required to obtain the list.
-  @param[out] RNGAlgorithmList        A caller-allocated memory buffer filled by the driver
-                                      with one EFI_RNG_ALGORITHM element for each supported
-                                      RNG algorithm. The list must not change across multiple
-                                      calls to the same driver. The first algorithm in the list
-                                      is the default algorithm for the driver.
-
-  @retval EFI_SUCCESS                 The RNG algorithm list was returned successfully.
-  @retval EFI_UNSUPPORTED             The services is not supported by this driver.
-  @retval EFI_DEVICE_ERROR            The list of algorithms could not be retrieved due to a
-                                      hardware or firmware error.
-  @retval EFI_INVALID_PARAMETER       One or more of the parameters are incorrect.
-  @retval EFI_BUFFER_TOO_SMALL        The buffer RNGAlgorithmList is too small to hold the result.
-
-**/
-EFI_STATUS
-EFIAPI
-RngGetInfo (
-  IN EFI_RNG_PROTOCOL    *This,
-  IN OUT UINTN           *RNGAlgorithmListSize,
-  OUT EFI_RNG_ALGORITHM  *RNGAlgorithmList
-  )
-{
-  EFI_STATUS  Status;
-
-  if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {
-    return EFI_INVALID_PARAMETER;
-  }
-
-  //
-  // Return algorithm list supported by driver.
-  //
-  if (RNGAlgorithmList != NULL) {
-    Status = ArchGetSupportedRngAlgorithms (RNGAlgorithmListSize, RNGAlgorithmList);
-  } else {
-    Status = EFI_INVALID_PARAMETER;
-  }
-
-  return Status;
-}
-
 //
 // The Random Number Generator (RNG) protocol
 //
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxeInternals.h b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxeInternals.h
index 48d2d27c1608..7ecab140483d 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxeInternals.h
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxeInternals.h
@@ -74,31 +74,6 @@ RngGetRNG (
   OUT UINT8             *RNGValue
   );
 
-/**
-  Returns information about the random number generation implementation.
-
-  @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
-                                      On output with a return code of EFI_SUCCESS, the size
-                                      in bytes of the data returned in RNGAlgorithmList. On output
-                                      with a return code of EFI_BUFFER_TOO_SMALL,
-                                      the size of RNGAlgorithmList required to obtain the list.
-  @param[out] RNGAlgorithmList        A caller-allocated memory buffer filled by the driver
-                                      with one EFI_RNG_ALGORITHM element for each supported
-                                      RNG algorithm. The list must not change across multiple
-                                      calls to the same driver. The first algorithm in the list
-                                      is the default algorithm for the driver.
-
-  @retval EFI_SUCCESS                 The RNG algorithm list was returned successfully.
-  @retval EFI_BUFFER_TOO_SMALL        The buffer RNGAlgorithmList is too small to hold the result.
-
-**/
-UINTN
-EFIAPI
-ArchGetSupportedRngAlgorithms (
-  IN OUT UINTN              *RNGAlgorithmListSize,
-  OUT    EFI_RNG_ALGORITHM  *RNGAlgorithmList
-  );
-
 /**
   Runs CPU RNG instruction to fill a buffer of arbitrary size with random bytes.
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 16/21] SecurityPkg/RngDxe: Check before advertising Cpu Rng algo
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (18 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 15/22] SecurityPkg/RngDxe: Remove ArchGetSupportedRngAlgorithms() PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 16/22] SecurityPkg/RngDxe: Documentation/include/parameter cleanup PierreGondois
                   ` (11 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <pierre.gondois@arm.com>

RngGetBytes() relies on the RngLib. The RngLib might use the RNDR
instruction if the FEAT_RNG feature is present. Check RngGetBytes() is
working before advertising it via RngGetInfo().

To only check this one time, create a static array that is shared
between RngGetInfo and RngGetRNG. This array contains GUIDs.
The Rng algorithm with the lowest GUID and that has been checked
will be the default Rng algorithm.

This patch also prevents from having PcdCpuRngSupportedAlgorithm
let to a zero GUID, but let the possibility to have no valid Rng
algorithm in such case.

Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
---
 .../RngDxe/AArch64/RngDxe.c                   | 77 +++++++++++++++++--
 1 file changed, 69 insertions(+), 8 deletions(-)

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
index f9c740d761ff..d8b696bbea5f 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
@@ -23,10 +23,44 @@
 #include <Library/BaseLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/UefiBootServicesTableLib.h>
+#include <Library/RngLib.h>
 #include <Protocol/Rng.h>
 
 #include "RngDxeInternals.h"
 
+//
+// Static array containing the validated Rng algorithm.
+// This array is used by RngGetInfo and RngGetRNG and needs to be
+// populated only once.
+// The valid entry with the lowest index will be the default algorithm.
+//
+#define RNG_AVAILABLE_ALGO_MAX  1
+STATIC BOOLEAN            mAvailableAlgoArrayInit = FALSE;
+STATIC UINTN              mAvailableAlgoArrayCount;
+STATIC EFI_RNG_ALGORITHM  mAvailableAlgoArray[RNG_AVAILABLE_ALGO_MAX];
+
+/** Initialize mAvailableAlgoArray with the available Rng algorithms.
+**/
+STATIC
+VOID
+EFIAPI
+RngInitAvailableAlgoArray (
+  VOID
+  )
+{
+  // Check RngGetBytes() before advertising PcdCpuRngSupportedAlgorithm.
+  if (!EFI_ERROR (RngGetBytes (sizeof (Rand), (UINT8 *)&Rand))) {
+    CopyMem (
+      &mAvailableAlgoArray[mAvailableAlgoArrayCount],
+      PcdGetPtr (PcdCpuRngSupportedAlgorithm),
+      sizeof (EFI_RNG_ALGORITHM)
+      );
+    mAvailableAlgoArrayCount++;
+  }
+
+  mAvailableAlgoArrayInit = TRUE;
+}
+
 /**
   Produces and returns an RNG value using either the default or specified RNG algorithm.
 
@@ -59,18 +93,35 @@ RngGetRNG (
   )
 {
   EFI_STATUS  Status;
+  UINTN       Index;
 
   if ((This == NULL) || (RNGValueLength == 0) || (RNGValue == NULL)) {
     return EFI_INVALID_PARAMETER;
   }
 
+  if (!mAvailableAlgoArrayInit) {
+    RngInitAvailableAlgoArray ();
+  }
+
   if (RNGAlgorithm == NULL) {
     //
     // Use the default RNG algorithm if RNGAlgorithm is NULL.
     //
-    RNGAlgorithm = PcdGetPtr (PcdCpuRngSupportedAlgorithm);
+    for (Index = 0; Index < RNG_AVAILABLE_ALGO_MAX; Index++) {
+      if (!IsZeroGuid (&mAvailableAlgoArray[Index])) {
+        RNGAlgorithm = &mAvailableAlgoArray[Index];
+        goto FoundAlgo;
+      }
+    }
+
+    if (Index == RNG_AVAILABLE_ALGO_MAX) {
+      // No algorithm available.
+      ASSERT (Index != RNG_AVAILABLE_ALGO_MAX);
+      return EFI_DEVICE_ERROR;
+    }
   }
 
+FoundAlgo:
   if (CompareGuid (RNGAlgorithm, PcdGetPtr (PcdCpuRngSupportedAlgorithm))) {
     Status = RngGetBytes (RNGValueLength, RNGValue);
     return Status;
@@ -113,24 +164,34 @@ RngGetInfo (
   OUT EFI_RNG_ALGORITHM  *RNGAlgorithmList
   )
 {
-  UINTN              RequiredSize;
-  EFI_RNG_ALGORITHM  *CpuRngSupportedAlgorithm;
-
-  RequiredSize = sizeof (EFI_RNG_ALGORITHM);
+  UINTN  RequiredSize;
 
   if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {
     return EFI_INVALID_PARAMETER;
   }
 
+  if (!mAvailableAlgoArrayInit) {
+    RngInitAvailableAlgoArray ();
+  }
+
+  RequiredSize = mAvailableAlgoArrayCount * sizeof (EFI_RNG_ALGORITHM);
+
+  if (RequiredSize == 0) {
+    // No supported algorithms found.
+    return EFI_UNSUPPORTED;
+  }
+
   if (*RNGAlgorithmListSize < RequiredSize) {
     *RNGAlgorithmListSize = RequiredSize;
     return EFI_BUFFER_TOO_SMALL;
   }
 
-  CpuRngSupportedAlgorithm = PcdGetPtr (PcdCpuRngSupportedAlgorithm);
-
-  CopyMem (&RNGAlgorithmList[0], CpuRngSupportedAlgorithm, sizeof (EFI_RNG_ALGORITHM));
+  if (RNGAlgorithmList == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
 
+  // There is no gap in the array, so copy the block.
+  CopyMem (RNGAlgorithmList, mAvailableAlgoArray, RequiredSize);
   *RNGAlgorithmListSize = RequiredSize;
   return EFI_SUCCESS;
 }
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 16/22] SecurityPkg/RngDxe: Documentation/include/parameter cleanup
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (19 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 16/21] SecurityPkg/RngDxe: Check before advertising Cpu Rng algo PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 17/21] SecurityPkg/RngDxe: Add AArch64 RawAlgorithm support through TrngLib PierreGondois
                   ` (10 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <Pierre.Gondois@arm.com>

This patch:
-Update RngGetBytes() documentation to align the function
 definition and declaration.
-Improve input parameter checking. Even though 'This'
 it is not used, the parameter should always point to the
 current EFI_RNG_PROTOCOL.
-Removes TimerLib inclusion as unused.

Signed-off-by: Pierre Gondois <pierre.gondois@arm.com>
---
 SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c | 3 +--
 SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c    | 2 +-
 SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c         | 3 +--
 3 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
index 6d989f7ea376..f9c740d761ff 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
@@ -23,7 +23,6 @@
 #include <Library/BaseLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/UefiBootServicesTableLib.h>
-#include <Library/TimerLib.h>
 #include <Protocol/Rng.h>
 
 #include "RngDxeInternals.h"
@@ -61,7 +60,7 @@ RngGetRNG (
 {
   EFI_STATUS  Status;
 
-  if ((RNGValueLength == 0) || (RNGValue == NULL)) {
+  if ((This == NULL) || (RNGValueLength == 0) || (RNGValue == NULL)) {
     return EFI_INVALID_PARAMETER;
   }
 
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
index b2d2236380fd..8f5d8e740f5e 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
@@ -59,7 +59,7 @@ RngGetRNG (
 {
   EFI_STATUS  Status;
 
-  if ((RNGValueLength == 0) || (RNGValue == NULL)) {
+  if ((This == NULL) || (RNGValueLength == 0) || (RNGValue == NULL)) {
     return EFI_INVALID_PARAMETER;
   }
 
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c
index 6608ca8804a5..d7905a7f4d72 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c
@@ -23,7 +23,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include <Library/BaseMemoryLib.h>
 #include <Library/UefiBootServicesTableLib.h>
 #include <Library/RngLib.h>
-#include <Library/TimerLib.h>
 #include <Protocol/Rng.h>
 
 #include "RngDxeInternals.h"
@@ -72,7 +71,7 @@ RngDriverEntry (
 }
 
 /**
-  Calls RDRAND to fill a buffer of arbitrary size with random bytes.
+  Runs CPU RNG instruction to fill a buffer of arbitrary size with random bytes.
 
   @param[in]   Length        Size of the buffer, in bytes,  to fill with.
   @param[out]  RandBuffer    Pointer to the buffer to store the random result.
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 17/21] SecurityPkg/RngDxe: Add AArch64 RawAlgorithm support through TrngLib
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (20 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 16/22] SecurityPkg/RngDxe: Documentation/include/parameter cleanup PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 17/22] SecurityPkg/RngDxe: Check before advertising Cpu Rng algo PierreGondois
                   ` (9 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Sami Mujawar <sami.mujawar@arm.com>

Bugzilla: 3668 (https://bugzilla.tianocore.org/show_bug.cgi?id=3668)

RawAlgorithm is used to provide access to entropy that is suitable
for cryptographic applications. Therefore, add RawAlgorithm support
that provides access to entropy using the TrngLib.

Also remove unused UefiBootServicesTableLib library inclusion
and Status variable.

Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
---
 .../RngDxe/AArch64/RngDxe.c                   | 28 +++++++-
 .../RandomNumberGenerator/RngDxe/ArmTrng.c    | 71 +++++++++++++++++++
 .../RandomNumberGenerator/RngDxe/RngDxe.inf   |  5 ++
 SecurityPkg/SecurityPkg.dsc                   |  7 ++
 4 files changed, 108 insertions(+), 3 deletions(-)
 create mode 100644 SecurityPkg/RandomNumberGenerator/RngDxe/ArmTrng.c

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
index d8b696bbea5f..ee3f1ee78434 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
@@ -1,11 +1,13 @@
 /** @file
   RNG Driver to produce the UEFI Random Number Generator protocol.
 
-  The driver will use the RNDR instruction to produce random numbers.
+  The driver can use RNDR instruction (through the RngLib and if FEAT_RNG is
+  present) to produce random numbers. It also uses the Arm FW-TRNG interface
+  to implement EFI_RNG_ALGORITHM_RAW.
 
   RNG Algorithms defined in UEFI 2.4:
    - EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID
-   - EFI_RNG_ALGORITHM_RAW                    - Unsupported
+   - EFI_RNG_ALGORITHM_RAW
    - EFI_RNG_ALGORITHM_SP800_90_HMAC_256_GUID
    - EFI_RNG_ALGORITHM_SP800_90_HASH_256_GUID
    - EFI_RNG_ALGORITHM_X9_31_3DES_GUID        - Unsupported
@@ -24,6 +26,8 @@
 #include <Library/BaseMemoryLib.h>
 #include <Library/UefiBootServicesTableLib.h>
 #include <Library/RngLib.h>
+#include <Library/DebugLib.h>
+#include <Library/TrngLib.h>
 #include <Protocol/Rng.h>
 
 #include "RngDxeInternals.h"
@@ -34,7 +38,7 @@
 // populated only once.
 // The valid entry with the lowest index will be the default algorithm.
 //
-#define RNG_AVAILABLE_ALGO_MAX  1
+#define RNG_AVAILABLE_ALGO_MAX  2
 STATIC BOOLEAN            mAvailableAlgoArrayInit = FALSE;
 STATIC UINTN              mAvailableAlgoArrayCount;
 STATIC EFI_RNG_ALGORITHM  mAvailableAlgoArray[RNG_AVAILABLE_ALGO_MAX];
@@ -48,6 +52,9 @@ RngInitAvailableAlgoArray (
   VOID
   )
 {
+  UINT16  MajorRevision;
+  UINT16  MinorRevision;
+
   // Check RngGetBytes() before advertising PcdCpuRngSupportedAlgorithm.
   if (!EFI_ERROR (RngGetBytes (sizeof (Rand), (UINT8 *)&Rand))) {
     CopyMem (
@@ -58,6 +65,16 @@ RngInitAvailableAlgoArray (
     mAvailableAlgoArrayCount++;
   }
 
+  // Raw algorithm (Trng)
+  if (!EFI_ERROR (GetTrngVersion (&MajorRevision, &MinorRevision))) {
+    CopyMem (
+      &mAvailableAlgoArray[mAvailableAlgoArrayCount],
+      &gEfiRngAlgorithmRaw,
+      sizeof (EFI_RNG_ALGORITHM)
+      );
+    mAvailableAlgoArrayCount++;
+  }
+
   mAvailableAlgoArrayInit = TRUE;
 }
 
@@ -127,6 +144,11 @@ FoundAlgo:
     return Status;
   }
 
+  // Raw algorithm (Trng)
+  if (CompareGuid (RNGAlgorithm, &gEfiRngAlgorithmRaw)) {
+    return GenerateEntropy (RNGValueLength, RNGValue);
+  }
+
   //
   // Other algorithms are unsupported by this driver.
   //
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/ArmTrng.c b/SecurityPkg/RandomNumberGenerator/RngDxe/ArmTrng.c
new file mode 100644
index 000000000000..6100e02b32b0
--- /dev/null
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/ArmTrng.c
@@ -0,0 +1,71 @@
+/** @file
+  RNG Driver to produce the UEFI Random Number Generator protocol.
+
+  The driver implements the EFI_RNG_ALGORITHM_RAW using the FW-TRNG
+  interface to provide entropy.
+
+  Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/TrngLib.h>
+#include <Protocol/Rng.h>
+
+#include "RngDxeInternals.h"
+
+/**
+  Generate high-quality entropy source using a TRNG or through RDRAND.
+
+  @param[in]   Length        Size of the buffer, in bytes, to fill with.
+  @param[out]  Entropy       Pointer to the buffer to store the entropy data.
+
+  @retval  RETURN_SUCCESS            The function completed successfully.
+  @retval  RETURN_INVALID_PARAMETER  Invalid parameter.
+  @retval  RETURN_UNSUPPORTED        Function not implemented.
+  @retval  RETURN_BAD_BUFFER_SIZE    Buffer size is too small.
+  @retval  RETURN_NOT_READY          No Entropy available.
+**/
+EFI_STATUS
+EFIAPI
+GenerateEntropy (
+  IN  UINTN  Length,
+  OUT UINT8  *Entropy
+  )
+{
+  EFI_STATUS  Status;
+  UINTN       CollectedEntropyBits;
+  UINTN       RequiredEntropyBits;
+  UINTN       EntropyBits;
+  UINTN       Index;
+  UINTN       MaxBits;
+
+  ZeroMem (Entropy, Length);
+
+  RequiredEntropyBits  = (Length << 3);
+  Index                = 0;
+  CollectedEntropyBits = 0;
+  MaxBits              = GetTrngMaxSupportedEntropyBits ();
+  while (CollectedEntropyBits < RequiredEntropyBits) {
+    EntropyBits = MIN ((RequiredEntropyBits - CollectedEntropyBits), MaxBits);
+    Status      = GetTrngEntropy (
+                    EntropyBits,
+                    (Length - Index),
+                    &Entropy[Index]
+                    );
+    if (EFI_ERROR (Status)) {
+      // Discard the collected bits.
+      ZeroMem (Entropy, Length);
+      return Status;
+    }
+
+    CollectedEntropyBits += EntropyBits;
+    Index                += (EntropyBits >> 3);
+  } // while
+
+  return Status;
+}
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
index 60efb5562ee0..6c3d42066804 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
@@ -42,8 +42,10 @@ [Sources.IA32, Sources.X64]
 
 [Sources.AARCH64]
   AArch64/RngDxe.c
+  ArmTrng.c
 
 [Packages]
+  MdeModulePkg/MdeModulePkg.dec
   MdePkg/MdePkg.dec
   SecurityPkg/SecurityPkg.dec
 
@@ -56,6 +58,9 @@ [LibraryClasses]
   TimerLib
   RngLib
 
+[LibraryClasses.AARCH64]
+  TrngLib
+
 [Guids]
   gEfiRngAlgorithmSp80090Hash256Guid  ## SOMETIMES_PRODUCES    ## GUID        # Unique ID of the algorithm for RNG
   gEfiRngAlgorithmSp80090Hmac256Guid  ## SOMETIMES_PRODUCES    ## GUID        # Unique ID of the algorithm for RNG
diff --git a/SecurityPkg/SecurityPkg.dsc b/SecurityPkg/SecurityPkg.dsc
index d883747474e4..490076542a33 100644
--- a/SecurityPkg/SecurityPkg.dsc
+++ b/SecurityPkg/SecurityPkg.dsc
@@ -3,6 +3,7 @@
 #
 # Copyright (c) 2009 - 2021, Intel Corporation. All rights reserved.<BR>
 # (C) Copyright 2015-2020 Hewlett Packard Enterprise Development LP<BR>
+# Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
 # SPDX-License-Identifier: BSD-2-Clause-Patent
 #
 ##
@@ -87,6 +88,12 @@ [LibraryClasses.ARM, LibraryClasses.AARCH64]
 
   ArmSoftFloatLib|ArmPkg/Library/ArmSoftFloatLib/ArmSoftFloatLib.inf
 
+  # Arm FW-TRNG interface library.
+  TrngLib|ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.inf
+  ArmMonitorLib|ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.inf
+  ArmSmcLib|ArmPkg/Library/ArmSmcLib/ArmSmcLib.inf
+  ArmHvcLib|ArmPkg/Library/ArmHvcLib/ArmHvcLib.inf
+
 [LibraryClasses.ARM]
   RngLib|MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 17/22] SecurityPkg/RngDxe: Check before advertising Cpu Rng algo
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (21 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 17/21] SecurityPkg/RngDxe: Add AArch64 RawAlgorithm support through TrngLib PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 18/22] SecurityPkg/RngDxe: Add AArch64 RawAlgorithm support through TrngLib PierreGondois
                   ` (8 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <pierre.gondois@arm.com>

RngGetBytes() relies on the RngLib. The RngLib might use the RNDR
instruction if the FEAT_RNG feature is present. Check RngGetBytes() is
working before advertising it via RngGetInfo().

To only check this one time, create a static array that is shared
between RngGetInfo and RngGetRNG. This array contains GUIDs.
The Rng algorithm with the lowest GUID and that has been checked
will be the default Rng algorithm.

This patch also prevents from having PcdCpuRngSupportedAlgorithm
let to a zero GUID, but let the possibility to have no valid Rng
algorithm in such case.

Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
---
 .../RngDxe/AArch64/RngDxe.c                   | 77 +++++++++++++++++--
 1 file changed, 69 insertions(+), 8 deletions(-)

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
index f9c740d761ff..d8b696bbea5f 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
@@ -23,10 +23,44 @@
 #include <Library/BaseLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/UefiBootServicesTableLib.h>
+#include <Library/RngLib.h>
 #include <Protocol/Rng.h>
 
 #include "RngDxeInternals.h"
 
+//
+// Static array containing the validated Rng algorithm.
+// This array is used by RngGetInfo and RngGetRNG and needs to be
+// populated only once.
+// The valid entry with the lowest index will be the default algorithm.
+//
+#define RNG_AVAILABLE_ALGO_MAX  1
+STATIC BOOLEAN            mAvailableAlgoArrayInit = FALSE;
+STATIC UINTN              mAvailableAlgoArrayCount;
+STATIC EFI_RNG_ALGORITHM  mAvailableAlgoArray[RNG_AVAILABLE_ALGO_MAX];
+
+/** Initialize mAvailableAlgoArray with the available Rng algorithms.
+**/
+STATIC
+VOID
+EFIAPI
+RngInitAvailableAlgoArray (
+  VOID
+  )
+{
+  // Check RngGetBytes() before advertising PcdCpuRngSupportedAlgorithm.
+  if (!EFI_ERROR (RngGetBytes (sizeof (Rand), (UINT8 *)&Rand))) {
+    CopyMem (
+      &mAvailableAlgoArray[mAvailableAlgoArrayCount],
+      PcdGetPtr (PcdCpuRngSupportedAlgorithm),
+      sizeof (EFI_RNG_ALGORITHM)
+      );
+    mAvailableAlgoArrayCount++;
+  }
+
+  mAvailableAlgoArrayInit = TRUE;
+}
+
 /**
   Produces and returns an RNG value using either the default or specified RNG algorithm.
 
@@ -59,18 +93,35 @@ RngGetRNG (
   )
 {
   EFI_STATUS  Status;
+  UINTN       Index;
 
   if ((This == NULL) || (RNGValueLength == 0) || (RNGValue == NULL)) {
     return EFI_INVALID_PARAMETER;
   }
 
+  if (!mAvailableAlgoArrayInit) {
+    RngInitAvailableAlgoArray ();
+  }
+
   if (RNGAlgorithm == NULL) {
     //
     // Use the default RNG algorithm if RNGAlgorithm is NULL.
     //
-    RNGAlgorithm = PcdGetPtr (PcdCpuRngSupportedAlgorithm);
+    for (Index = 0; Index < RNG_AVAILABLE_ALGO_MAX; Index++) {
+      if (!IsZeroGuid (&mAvailableAlgoArray[Index])) {
+        RNGAlgorithm = &mAvailableAlgoArray[Index];
+        goto FoundAlgo;
+      }
+    }
+
+    if (Index == RNG_AVAILABLE_ALGO_MAX) {
+      // No algorithm available.
+      ASSERT (Index != RNG_AVAILABLE_ALGO_MAX);
+      return EFI_DEVICE_ERROR;
+    }
   }
 
+FoundAlgo:
   if (CompareGuid (RNGAlgorithm, PcdGetPtr (PcdCpuRngSupportedAlgorithm))) {
     Status = RngGetBytes (RNGValueLength, RNGValue);
     return Status;
@@ -113,24 +164,34 @@ RngGetInfo (
   OUT EFI_RNG_ALGORITHM  *RNGAlgorithmList
   )
 {
-  UINTN              RequiredSize;
-  EFI_RNG_ALGORITHM  *CpuRngSupportedAlgorithm;
-
-  RequiredSize = sizeof (EFI_RNG_ALGORITHM);
+  UINTN  RequiredSize;
 
   if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {
     return EFI_INVALID_PARAMETER;
   }
 
+  if (!mAvailableAlgoArrayInit) {
+    RngInitAvailableAlgoArray ();
+  }
+
+  RequiredSize = mAvailableAlgoArrayCount * sizeof (EFI_RNG_ALGORITHM);
+
+  if (RequiredSize == 0) {
+    // No supported algorithms found.
+    return EFI_UNSUPPORTED;
+  }
+
   if (*RNGAlgorithmListSize < RequiredSize) {
     *RNGAlgorithmListSize = RequiredSize;
     return EFI_BUFFER_TOO_SMALL;
   }
 
-  CpuRngSupportedAlgorithm = PcdGetPtr (PcdCpuRngSupportedAlgorithm);
-
-  CopyMem (&RNGAlgorithmList[0], CpuRngSupportedAlgorithm, sizeof (EFI_RNG_ALGORITHM));
+  if (RNGAlgorithmList == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
 
+  // There is no gap in the array, so copy the block.
+  CopyMem (RNGAlgorithmList, mAvailableAlgoArray, RequiredSize);
   *RNGAlgorithmListSize = RequiredSize;
   return EFI_SUCCESS;
 }
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 18/22] SecurityPkg/RngDxe: Add AArch64 RawAlgorithm support through TrngLib
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (22 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 17/22] SecurityPkg/RngDxe: Check before advertising Cpu Rng algo PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 18/21] SecurityPkg/RngDxe: Add debug warning for NULL PcdCpuRngSupportedAlgorithm PierreGondois
                   ` (7 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Sami Mujawar <sami.mujawar@arm.com>

Bugzilla: 3668 (https://bugzilla.tianocore.org/show_bug.cgi?id=3668)

RawAlgorithm is used to provide access to entropy that is suitable
for cryptographic applications. Therefore, add RawAlgorithm support
that provides access to entropy using the TrngLib.

Also remove unused UefiBootServicesTableLib library inclusion
and Status variable.

Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
---
 .../RngDxe/AArch64/RngDxe.c                   | 28 +++++++-
 .../RandomNumberGenerator/RngDxe/ArmTrng.c    | 71 +++++++++++++++++++
 .../RandomNumberGenerator/RngDxe/RngDxe.inf   |  5 ++
 SecurityPkg/SecurityPkg.dsc                   |  7 ++
 4 files changed, 108 insertions(+), 3 deletions(-)
 create mode 100644 SecurityPkg/RandomNumberGenerator/RngDxe/ArmTrng.c

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
index d8b696bbea5f..ee3f1ee78434 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
@@ -1,11 +1,13 @@
 /** @file
   RNG Driver to produce the UEFI Random Number Generator protocol.
 
-  The driver will use the RNDR instruction to produce random numbers.
+  The driver can use RNDR instruction (through the RngLib and if FEAT_RNG is
+  present) to produce random numbers. It also uses the Arm FW-TRNG interface
+  to implement EFI_RNG_ALGORITHM_RAW.
 
   RNG Algorithms defined in UEFI 2.4:
    - EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID
-   - EFI_RNG_ALGORITHM_RAW                    - Unsupported
+   - EFI_RNG_ALGORITHM_RAW
    - EFI_RNG_ALGORITHM_SP800_90_HMAC_256_GUID
    - EFI_RNG_ALGORITHM_SP800_90_HASH_256_GUID
    - EFI_RNG_ALGORITHM_X9_31_3DES_GUID        - Unsupported
@@ -24,6 +26,8 @@
 #include <Library/BaseMemoryLib.h>
 #include <Library/UefiBootServicesTableLib.h>
 #include <Library/RngLib.h>
+#include <Library/DebugLib.h>
+#include <Library/TrngLib.h>
 #include <Protocol/Rng.h>
 
 #include "RngDxeInternals.h"
@@ -34,7 +38,7 @@
 // populated only once.
 // The valid entry with the lowest index will be the default algorithm.
 //
-#define RNG_AVAILABLE_ALGO_MAX  1
+#define RNG_AVAILABLE_ALGO_MAX  2
 STATIC BOOLEAN            mAvailableAlgoArrayInit = FALSE;
 STATIC UINTN              mAvailableAlgoArrayCount;
 STATIC EFI_RNG_ALGORITHM  mAvailableAlgoArray[RNG_AVAILABLE_ALGO_MAX];
@@ -48,6 +52,9 @@ RngInitAvailableAlgoArray (
   VOID
   )
 {
+  UINT16  MajorRevision;
+  UINT16  MinorRevision;
+
   // Check RngGetBytes() before advertising PcdCpuRngSupportedAlgorithm.
   if (!EFI_ERROR (RngGetBytes (sizeof (Rand), (UINT8 *)&Rand))) {
     CopyMem (
@@ -58,6 +65,16 @@ RngInitAvailableAlgoArray (
     mAvailableAlgoArrayCount++;
   }
 
+  // Raw algorithm (Trng)
+  if (!EFI_ERROR (GetTrngVersion (&MajorRevision, &MinorRevision))) {
+    CopyMem (
+      &mAvailableAlgoArray[mAvailableAlgoArrayCount],
+      &gEfiRngAlgorithmRaw,
+      sizeof (EFI_RNG_ALGORITHM)
+      );
+    mAvailableAlgoArrayCount++;
+  }
+
   mAvailableAlgoArrayInit = TRUE;
 }
 
@@ -127,6 +144,11 @@ FoundAlgo:
     return Status;
   }
 
+  // Raw algorithm (Trng)
+  if (CompareGuid (RNGAlgorithm, &gEfiRngAlgorithmRaw)) {
+    return GenerateEntropy (RNGValueLength, RNGValue);
+  }
+
   //
   // Other algorithms are unsupported by this driver.
   //
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/ArmTrng.c b/SecurityPkg/RandomNumberGenerator/RngDxe/ArmTrng.c
new file mode 100644
index 000000000000..6100e02b32b0
--- /dev/null
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/ArmTrng.c
@@ -0,0 +1,71 @@
+/** @file
+  RNG Driver to produce the UEFI Random Number Generator protocol.
+
+  The driver implements the EFI_RNG_ALGORITHM_RAW using the FW-TRNG
+  interface to provide entropy.
+
+  Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/TrngLib.h>
+#include <Protocol/Rng.h>
+
+#include "RngDxeInternals.h"
+
+/**
+  Generate high-quality entropy source using a TRNG or through RDRAND.
+
+  @param[in]   Length        Size of the buffer, in bytes, to fill with.
+  @param[out]  Entropy       Pointer to the buffer to store the entropy data.
+
+  @retval  RETURN_SUCCESS            The function completed successfully.
+  @retval  RETURN_INVALID_PARAMETER  Invalid parameter.
+  @retval  RETURN_UNSUPPORTED        Function not implemented.
+  @retval  RETURN_BAD_BUFFER_SIZE    Buffer size is too small.
+  @retval  RETURN_NOT_READY          No Entropy available.
+**/
+EFI_STATUS
+EFIAPI
+GenerateEntropy (
+  IN  UINTN  Length,
+  OUT UINT8  *Entropy
+  )
+{
+  EFI_STATUS  Status;
+  UINTN       CollectedEntropyBits;
+  UINTN       RequiredEntropyBits;
+  UINTN       EntropyBits;
+  UINTN       Index;
+  UINTN       MaxBits;
+
+  ZeroMem (Entropy, Length);
+
+  RequiredEntropyBits  = (Length << 3);
+  Index                = 0;
+  CollectedEntropyBits = 0;
+  MaxBits              = GetTrngMaxSupportedEntropyBits ();
+  while (CollectedEntropyBits < RequiredEntropyBits) {
+    EntropyBits = MIN ((RequiredEntropyBits - CollectedEntropyBits), MaxBits);
+    Status      = GetTrngEntropy (
+                    EntropyBits,
+                    (Length - Index),
+                    &Entropy[Index]
+                    );
+    if (EFI_ERROR (Status)) {
+      // Discard the collected bits.
+      ZeroMem (Entropy, Length);
+      return Status;
+    }
+
+    CollectedEntropyBits += EntropyBits;
+    Index                += (EntropyBits >> 3);
+  } // while
+
+  return Status;
+}
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
index 60efb5562ee0..6c3d42066804 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
@@ -42,8 +42,10 @@ [Sources.IA32, Sources.X64]
 
 [Sources.AARCH64]
   AArch64/RngDxe.c
+  ArmTrng.c
 
 [Packages]
+  MdeModulePkg/MdeModulePkg.dec
   MdePkg/MdePkg.dec
   SecurityPkg/SecurityPkg.dec
 
@@ -56,6 +58,9 @@ [LibraryClasses]
   TimerLib
   RngLib
 
+[LibraryClasses.AARCH64]
+  TrngLib
+
 [Guids]
   gEfiRngAlgorithmSp80090Hash256Guid  ## SOMETIMES_PRODUCES    ## GUID        # Unique ID of the algorithm for RNG
   gEfiRngAlgorithmSp80090Hmac256Guid  ## SOMETIMES_PRODUCES    ## GUID        # Unique ID of the algorithm for RNG
diff --git a/SecurityPkg/SecurityPkg.dsc b/SecurityPkg/SecurityPkg.dsc
index d883747474e4..490076542a33 100644
--- a/SecurityPkg/SecurityPkg.dsc
+++ b/SecurityPkg/SecurityPkg.dsc
@@ -3,6 +3,7 @@
 #
 # Copyright (c) 2009 - 2021, Intel Corporation. All rights reserved.<BR>
 # (C) Copyright 2015-2020 Hewlett Packard Enterprise Development LP<BR>
+# Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
 # SPDX-License-Identifier: BSD-2-Clause-Patent
 #
 ##
@@ -87,6 +88,12 @@ [LibraryClasses.ARM, LibraryClasses.AARCH64]
 
   ArmSoftFloatLib|ArmPkg/Library/ArmSoftFloatLib/ArmSoftFloatLib.inf
 
+  # Arm FW-TRNG interface library.
+  TrngLib|ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.inf
+  ArmMonitorLib|ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.inf
+  ArmSmcLib|ArmPkg/Library/ArmSmcLib/ArmSmcLib.inf
+  ArmHvcLib|ArmPkg/Library/ArmHvcLib/ArmHvcLib.inf
+
 [LibraryClasses.ARM]
   RngLib|MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 18/21] SecurityPkg/RngDxe: Add debug warning for NULL PcdCpuRngSupportedAlgorithm
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (23 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 18/22] SecurityPkg/RngDxe: Add AArch64 RawAlgorithm support through TrngLib PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 19/22] " PierreGondois
                   ` (6 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <pierre.gondois@arm.com>

PcdCpuRngSupportedAlgorithm should allow to identify the the algorithm
used by the RNDR CPU instruction to generate a random number.
Add a debug warning if the Pcd is not set.

Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
---
 .../RandomNumberGenerator/RngDxe/AArch64/RngDxe.c      | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
index ee3f1ee78434..ffa32a29dc6a 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
@@ -63,6 +63,16 @@ RngInitAvailableAlgoArray (
       sizeof (EFI_RNG_ALGORITHM)
       );
     mAvailableAlgoArrayCount++;
+
+    DEBUG_CODE_BEGIN ();
+    if (IsZeroGuid (PcdGetPtr (PcdCpuRngSupportedAlgorithm))) {
+      DEBUG ((
+        DEBUG_WARN,
+        "PcdCpuRngSupportedAlgorithm should be a non-zero GUID\n"
+        ));
+    }
+
+    DEBUG_CODE_END ();
   }
 
   // Raw algorithm (Trng)
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 19/22] SecurityPkg/RngDxe: Add debug warning for NULL PcdCpuRngSupportedAlgorithm
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (24 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 18/21] SecurityPkg/RngDxe: Add debug warning for NULL PcdCpuRngSupportedAlgorithm PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 19/21] SecurityPkg/RngDxe: Rename AArch64/RngDxe.c PierreGondois
                   ` (5 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <pierre.gondois@arm.com>

PcdCpuRngSupportedAlgorithm should allow to identify the the algorithm
used by the RNDR CPU instruction to generate a random number.
Add a debug warning if the Pcd is not set.

Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
---
 .../RandomNumberGenerator/RngDxe/AArch64/RngDxe.c      | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
index ee3f1ee78434..ffa32a29dc6a 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
@@ -63,6 +63,16 @@ RngInitAvailableAlgoArray (
       sizeof (EFI_RNG_ALGORITHM)
       );
     mAvailableAlgoArrayCount++;
+
+    DEBUG_CODE_BEGIN ();
+    if (IsZeroGuid (PcdGetPtr (PcdCpuRngSupportedAlgorithm))) {
+      DEBUG ((
+        DEBUG_WARN,
+        "PcdCpuRngSupportedAlgorithm should be a non-zero GUID\n"
+        ));
+    }
+
+    DEBUG_CODE_END ();
   }
 
   // Raw algorithm (Trng)
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 19/21] SecurityPkg/RngDxe: Rename AArch64/RngDxe.c
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (25 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 19/22] " PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 20/21] SecurityPkg/RngDxe: Add Arm support of RngDxe PierreGondois
                   ` (4 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <pierre.gondois@arm.com>

To re-use the AArch64/RngDxe.c for an Arm implementation,
rename AArch64/RngDxe.c to ArmRngDxe.c.

Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
---
 .../RngDxe/{AArch64/RngDxe.c => ArmRngDxe.c}                    | 0
 SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf             | 2 +-
 2 files changed, 1 insertion(+), 1 deletion(-)
 rename SecurityPkg/RandomNumberGenerator/RngDxe/{AArch64/RngDxe.c => ArmRngDxe.c} (100%)

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
similarity index 100%
rename from SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
rename to SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
index 6c3d42066804..d2d0ff9ebb98 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
@@ -41,7 +41,7 @@ [Sources.IA32, Sources.X64]
   Rand/AesCore.h
 
 [Sources.AARCH64]
-  AArch64/RngDxe.c
+  ArmRngDxe.c
   ArmTrng.c
 
 [Packages]
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 20/21] SecurityPkg/RngDxe: Add Arm support of RngDxe
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (26 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 19/21] SecurityPkg/RngDxe: Rename AArch64/RngDxe.c PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-07-21 10:47   ` Yao, Jiewen
  2022-06-29 15:02 ` [PATCH v3 20/22] SecurityPkg/RngDxe: Rename AArch64/RngDxe.c PierreGondois
                   ` (3 subsequent siblings)
  31 siblings, 1 reply; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <pierre.gondois@arm.com>

Bugzilla: 3668 (https://bugzilla.tianocore.org/show_bug.cgi?id=3668)

Add RngDxe support for Arm. This implementation uses the TrngLib
to support the RawAlgorithm and doens't support the RNDR instruction.

Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
---
 SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c | 8 ++++++--
 SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf  | 9 ++++++---
 SecurityPkg/SecurityPkg.dsc                          | 2 +-
 3 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
index ffa32a29dc6a..4775252d30b6 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
@@ -22,6 +22,7 @@
 
 **/
 
+#include <Library/ArmLib.h>
 #include <Library/BaseLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/UefiBootServicesTableLib.h>
@@ -55,8 +56,9 @@ RngInitAvailableAlgoArray (
   UINT16  MajorRevision;
   UINT16  MinorRevision;
 
-  // Check RngGetBytes() before advertising PcdCpuRngSupportedAlgorithm.
-  if (!EFI_ERROR (RngGetBytes (sizeof (Rand), (UINT8 *)&Rand))) {
+ #ifdef MDE_CPU_AARCH64
+  // Check FEAT_RNG before advertising PcdCpuRngSupportedAlgorithm.
+  if (ArmHasRngExt ()) {
     CopyMem (
       &mAvailableAlgoArray[mAvailableAlgoArrayCount],
       PcdGetPtr (PcdCpuRngSupportedAlgorithm),
@@ -75,6 +77,8 @@ RngInitAvailableAlgoArray (
     DEBUG_CODE_END ();
   }
 
+ #endif
+
   // Raw algorithm (Trng)
   if (!EFI_ERROR (GetTrngVersion (&MajorRevision, &MinorRevision))) {
     CopyMem (
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
index d2d0ff9ebb98..599a3085102d 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
@@ -27,7 +27,7 @@ [Defines]
 #
 # The following information is for reference only and not required by the build tools.
 #
-#  VALID_ARCHITECTURES           = IA32 X64 AARCH64
+#  VALID_ARCHITECTURES           = IA32 X64 AARCH64 ARM
 #
 
 [Sources.common]
@@ -40,7 +40,7 @@ [Sources.IA32, Sources.X64]
   Rand/AesCore.c
   Rand/AesCore.h
 
-[Sources.AARCH64]
+[Sources.AARCH64, Sources.ARM]
   ArmRngDxe.c
   ArmTrng.c
 
@@ -49,6 +49,9 @@ [Packages]
   MdePkg/MdePkg.dec
   SecurityPkg/SecurityPkg.dec
 
+[Packages.AARCH64, Packages.ARM]
+  ArmPkg/ArmPkg.dec
+
 [LibraryClasses]
   UefiLib
   UefiBootServicesTableLib
@@ -58,7 +61,7 @@ [LibraryClasses]
   TimerLib
   RngLib
 
-[LibraryClasses.AARCH64]
+[LibraryClasses.AARCH64, LibraryClasses.ARM]
   TrngLib
 
 [Guids]
diff --git a/SecurityPkg/SecurityPkg.dsc b/SecurityPkg/SecurityPkg.dsc
index 490076542a33..882d639489ea 100644
--- a/SecurityPkg/SecurityPkg.dsc
+++ b/SecurityPkg/SecurityPkg.dsc
@@ -292,7 +292,7 @@ [Components.IA32, Components.X64, Components.ARM, Components.AARCH64]
   SecurityPkg/EnrollFromDefaultKeysApp/EnrollFromDefaultKeysApp.inf
   SecurityPkg/VariableAuthenticated/SecureBootDefaultKeysDxe/SecureBootDefaultKeysDxe.inf
 
-[Components.IA32, Components.X64, Components.AARCH64]
+[Components.IA32, Components.X64, Components.AARCH64, Components.ARM]
   #
   # Random Number Generator
   #
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 20/22] SecurityPkg/RngDxe: Rename AArch64/RngDxe.c
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (27 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 20/21] SecurityPkg/RngDxe: Add Arm support of RngDxe PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 21/21] ArmVirtPkg: Kvmtool: Add RNG support using FW-TRNG interface PierreGondois
                   ` (2 subsequent siblings)
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <pierre.gondois@arm.com>

To re-use the AArch64/RngDxe.c for an Arm implementation,
rename AArch64/RngDxe.c to ArmRngDxe.c.

Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
---
 .../RngDxe/{AArch64/RngDxe.c => ArmRngDxe.c}                    | 0
 SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf             | 2 +-
 2 files changed, 1 insertion(+), 1 deletion(-)
 rename SecurityPkg/RandomNumberGenerator/RngDxe/{AArch64/RngDxe.c => ArmRngDxe.c} (100%)

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
similarity index 100%
rename from SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
rename to SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
index 6c3d42066804..d2d0ff9ebb98 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
@@ -41,7 +41,7 @@ [Sources.IA32, Sources.X64]
   Rand/AesCore.h
 
 [Sources.AARCH64]
-  AArch64/RngDxe.c
+  ArmRngDxe.c
   ArmTrng.c
 
 [Packages]
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 21/21] ArmVirtPkg: Kvmtool: Add RNG support using FW-TRNG interface
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (28 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 20/22] SecurityPkg/RngDxe: Rename AArch64/RngDxe.c PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 21/22] SecurityPkg/RngDxe: Add Arm support of RngDxe PierreGondois
  2022-06-29 15:02 ` [PATCH v3 22/22] ArmVirtPkg: Kvmtool: Add RNG support using FW-TRNG interface PierreGondois
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Sami Mujawar <sami.mujawar@arm.com>

Bugzilla: 3668 (https://bugzilla.tianocore.org/show_bug.cgi?id=3668)

The EFI_RNG_PROTOCOL published by RngDxe has been updated to
implement the EFI_RNG_ALGORITHM_RAW using the Arm FW-TRNG
interface to provide access to entropy.

Therefore, enable EFI_RNG_PROTOCOL for the Kvmtool guest/virtual
firmware.

Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
---
 ArmVirtPkg/ArmVirtKvmTool.dsc | 10 ++++++++++
 ArmVirtPkg/ArmVirtKvmTool.fdf |  5 +++++
 2 files changed, 15 insertions(+)

diff --git a/ArmVirtPkg/ArmVirtKvmTool.dsc b/ArmVirtPkg/ArmVirtKvmTool.dsc
index 3bd3ebd6e0b3..847dbdd2af2b 100644
--- a/ArmVirtPkg/ArmVirtKvmTool.dsc
+++ b/ArmVirtPkg/ArmVirtKvmTool.dsc
@@ -81,6 +81,9 @@ [LibraryClasses.common]
   HwInfoParserLib|DynamicTablesPkg/Library/FdtHwInfoParserLib/FdtHwInfoParserLib.inf
   DynamicPlatRepoLib|DynamicTablesPkg/Library/Common/DynamicPlatRepoLib/DynamicPlatRepoLib.inf
 
+  ArmMonitorLib|ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.inf
+  TrngLib|ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.inf
+
 [LibraryClasses.common.SEC, LibraryClasses.common.PEI_CORE, LibraryClasses.common.PEIM]
   PciExpressLib|MdePkg/Library/BasePciExpressLib/BasePciExpressLib.inf
   PlatformHookLib|ArmVirtPkg/Library/Fdt16550SerialPortHookLib/EarlyFdt16550SerialPortHookLib.inf
@@ -112,6 +115,8 @@ [PcdsFeatureFlag.common]
   # Use MMIO for accessing RTC controller registers.
   gPcAtChipsetPkgTokenSpaceGuid.PcdRtcUseMmio|TRUE
 
+  gArmTokenSpaceGuid.PcdMonitorConduitHvc|TRUE
+
 [PcdsFixedAtBuild.common]
   gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x8000000F
 
@@ -362,6 +367,11 @@ [Components.common]
   OvmfPkg/VirtioPciDeviceDxe/VirtioPciDeviceDxe.inf
   OvmfPkg/Virtio10Dxe/Virtio10.inf
 
+  #
+  # Rng Support
+  #
+  SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
+
 !if $(ARCH) == AARCH64
   #
   # ACPI Support
diff --git a/ArmVirtPkg/ArmVirtKvmTool.fdf b/ArmVirtPkg/ArmVirtKvmTool.fdf
index 9e006e83ee5c..4b5c99ef6700 100644
--- a/ArmVirtPkg/ArmVirtKvmTool.fdf
+++ b/ArmVirtPkg/ArmVirtKvmTool.fdf
@@ -224,6 +224,11 @@ [FV.FvMain]
   #
   INF MdeModulePkg/Universal/Disk/RamDiskDxe/RamDiskDxe.inf
 
+  #
+  # Rng Support
+  #
+  INF SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
+
 [FV.FVMAIN_COMPACT]
 FvAlignment        = 16
 ERASE_POLARITY     = 1
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 21/22] SecurityPkg/RngDxe: Add Arm support of RngDxe
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (29 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 21/21] ArmVirtPkg: Kvmtool: Add RNG support using FW-TRNG interface PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  2022-06-29 15:02 ` [PATCH v3 22/22] ArmVirtPkg: Kvmtool: Add RNG support using FW-TRNG interface PierreGondois
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Pierre Gondois <pierre.gondois@arm.com>

Bugzilla: 3668 (https://bugzilla.tianocore.org/show_bug.cgi?id=3668)

Add RngDxe support for Arm. This implementation uses the TrngLib
to support the RawAlgorithm and doens't support the RNDR instruction.

Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
---
 SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c |  8 ++++++--
 SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf  | 10 +++++++---
 SecurityPkg/SecurityPkg.dsc                          |  5 ++++-
 3 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
index ffa32a29dc6a..4775252d30b6 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
@@ -22,6 +22,7 @@
 
 **/
 
+#include <Library/ArmLib.h>
 #include <Library/BaseLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/UefiBootServicesTableLib.h>
@@ -55,8 +56,9 @@ RngInitAvailableAlgoArray (
   UINT16  MajorRevision;
   UINT16  MinorRevision;
 
-  // Check RngGetBytes() before advertising PcdCpuRngSupportedAlgorithm.
-  if (!EFI_ERROR (RngGetBytes (sizeof (Rand), (UINT8 *)&Rand))) {
+ #ifdef MDE_CPU_AARCH64
+  // Check FEAT_RNG before advertising PcdCpuRngSupportedAlgorithm.
+  if (ArmHasRngExt ()) {
     CopyMem (
       &mAvailableAlgoArray[mAvailableAlgoArrayCount],
       PcdGetPtr (PcdCpuRngSupportedAlgorithm),
@@ -75,6 +77,8 @@ RngInitAvailableAlgoArray (
     DEBUG_CODE_END ();
   }
 
+ #endif
+
   // Raw algorithm (Trng)
   if (!EFI_ERROR (GetTrngVersion (&MajorRevision, &MinorRevision))) {
     CopyMem (
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
index d2d0ff9ebb98..20752e71ac4e 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
@@ -27,7 +27,7 @@ [Defines]
 #
 # The following information is for reference only and not required by the build tools.
 #
-#  VALID_ARCHITECTURES           = IA32 X64 AARCH64
+#  VALID_ARCHITECTURES           = IA32 X64 AARCH64 ARM
 #
 
 [Sources.common]
@@ -40,7 +40,7 @@ [Sources.IA32, Sources.X64]
   Rand/AesCore.c
   Rand/AesCore.h
 
-[Sources.AARCH64]
+[Sources.AARCH64, Sources.ARM]
   ArmRngDxe.c
   ArmTrng.c
 
@@ -49,6 +49,9 @@ [Packages]
   MdePkg/MdePkg.dec
   SecurityPkg/SecurityPkg.dec
 
+[Packages.AARCH64, Packages.ARM]
+  ArmPkg/ArmPkg.dec
+
 [LibraryClasses]
   UefiLib
   UefiBootServicesTableLib
@@ -58,7 +61,8 @@ [LibraryClasses]
   TimerLib
   RngLib
 
-[LibraryClasses.AARCH64]
+[LibraryClasses.AARCH64, LibraryClasses.ARM]
+  ArmLib
   TrngLib
 
 [Guids]
diff --git a/SecurityPkg/SecurityPkg.dsc b/SecurityPkg/SecurityPkg.dsc
index 490076542a33..779aa2a061a0 100644
--- a/SecurityPkg/SecurityPkg.dsc
+++ b/SecurityPkg/SecurityPkg.dsc
@@ -94,6 +94,9 @@ [LibraryClasses.ARM, LibraryClasses.AARCH64]
   ArmSmcLib|ArmPkg/Library/ArmSmcLib/ArmSmcLib.inf
   ArmHvcLib|ArmPkg/Library/ArmHvcLib/ArmHvcLib.inf
 
+  # RngDxe dependencies
+  ArmLib|ArmPkg/Library/ArmLib/ArmBaseLib.inf
+
 [LibraryClasses.ARM]
   RngLib|MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf
 
@@ -292,7 +295,7 @@ [Components.IA32, Components.X64, Components.ARM, Components.AARCH64]
   SecurityPkg/EnrollFromDefaultKeysApp/EnrollFromDefaultKeysApp.inf
   SecurityPkg/VariableAuthenticated/SecureBootDefaultKeysDxe/SecureBootDefaultKeysDxe.inf
 
-[Components.IA32, Components.X64, Components.AARCH64]
+[Components.IA32, Components.X64, Components.AARCH64, Components.ARM]
   #
   # Random Number Generator
   #
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v3 22/22] ArmVirtPkg: Kvmtool: Add RNG support using FW-TRNG interface
  2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
                   ` (30 preceding siblings ...)
  2022-06-29 15:02 ` [PATCH v3 21/22] SecurityPkg/RngDxe: Add Arm support of RngDxe PierreGondois
@ 2022-06-29 15:02 ` PierreGondois
  31 siblings, 0 replies; 35+ messages in thread
From: PierreGondois @ 2022-06-29 15:02 UTC (permalink / raw)
  To: devel
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Michael D Kinney, Liming Gao, Jiewen Yao, Jian J Wang

From: Sami Mujawar <sami.mujawar@arm.com>

Bugzilla: 3668 (https://bugzilla.tianocore.org/show_bug.cgi?id=3668)

The EFI_RNG_PROTOCOL published by RngDxe has been updated to
implement the EFI_RNG_ALGORITHM_RAW using the Arm FW-TRNG
interface to provide access to entropy.

Therefore, enable EFI_RNG_PROTOCOL for the Kvmtool guest/virtual
firmware.

Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
---
 ArmVirtPkg/ArmVirtKvmTool.dsc | 10 ++++++++++
 ArmVirtPkg/ArmVirtKvmTool.fdf |  5 +++++
 2 files changed, 15 insertions(+)

diff --git a/ArmVirtPkg/ArmVirtKvmTool.dsc b/ArmVirtPkg/ArmVirtKvmTool.dsc
index 3bd3ebd6e0b3..847dbdd2af2b 100644
--- a/ArmVirtPkg/ArmVirtKvmTool.dsc
+++ b/ArmVirtPkg/ArmVirtKvmTool.dsc
@@ -81,6 +81,9 @@ [LibraryClasses.common]
   HwInfoParserLib|DynamicTablesPkg/Library/FdtHwInfoParserLib/FdtHwInfoParserLib.inf
   DynamicPlatRepoLib|DynamicTablesPkg/Library/Common/DynamicPlatRepoLib/DynamicPlatRepoLib.inf
 
+  ArmMonitorLib|ArmPkg/Library/ArmMonitorLib/ArmMonitorLib.inf
+  TrngLib|ArmPkg/Library/ArmFwTrngLib/ArmFwTrngLib.inf
+
 [LibraryClasses.common.SEC, LibraryClasses.common.PEI_CORE, LibraryClasses.common.PEIM]
   PciExpressLib|MdePkg/Library/BasePciExpressLib/BasePciExpressLib.inf
   PlatformHookLib|ArmVirtPkg/Library/Fdt16550SerialPortHookLib/EarlyFdt16550SerialPortHookLib.inf
@@ -112,6 +115,8 @@ [PcdsFeatureFlag.common]
   # Use MMIO for accessing RTC controller registers.
   gPcAtChipsetPkgTokenSpaceGuid.PcdRtcUseMmio|TRUE
 
+  gArmTokenSpaceGuid.PcdMonitorConduitHvc|TRUE
+
 [PcdsFixedAtBuild.common]
   gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x8000000F
 
@@ -362,6 +367,11 @@ [Components.common]
   OvmfPkg/VirtioPciDeviceDxe/VirtioPciDeviceDxe.inf
   OvmfPkg/Virtio10Dxe/Virtio10.inf
 
+  #
+  # Rng Support
+  #
+  SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
+
 !if $(ARCH) == AARCH64
   #
   # ACPI Support
diff --git a/ArmVirtPkg/ArmVirtKvmTool.fdf b/ArmVirtPkg/ArmVirtKvmTool.fdf
index 9e006e83ee5c..4b5c99ef6700 100644
--- a/ArmVirtPkg/ArmVirtKvmTool.fdf
+++ b/ArmVirtPkg/ArmVirtKvmTool.fdf
@@ -224,6 +224,11 @@ [FV.FvMain]
   #
   INF MdeModulePkg/Universal/Disk/RamDiskDxe/RamDiskDxe.inf
 
+  #
+  # Rng Support
+  #
+  INF SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
+
 [FV.FVMAIN_COMPACT]
 FvAlignment        = 16
 ERASE_POLARITY     = 1
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 20/21] SecurityPkg/RngDxe: Add Arm support of RngDxe
  2022-06-29 15:02 ` [PATCH v3 20/21] SecurityPkg/RngDxe: Add Arm support of RngDxe PierreGondois
@ 2022-07-21 10:47   ` Yao, Jiewen
  0 siblings, 0 replies; 35+ messages in thread
From: Yao, Jiewen @ 2022-07-21 10:47 UTC (permalink / raw)
  To: Pierre.Gondois@arm.com, devel@edk2.groups.io
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Kinney, Michael D, Gao, Liming, Wang, Jian J

> + #ifdef MDE_CPU_AARCH64
> +  // Check FEAT_RNG before advertising PcdCpuRngSupportedAlgorithm.
> +  if (ArmHasRngExt ()) {

Usually, we don't prefer use "#ifdef" in a common code.

Instead, the general pattern is to define Architecture specific directory, and move architecture specific doe there.

Thank you
Yao Jiewen


> -----Original Message-----
> From: Pierre.Gondois@arm.com <Pierre.Gondois@arm.com>
> Sent: Wednesday, June 29, 2022 11:03 PM
> To: devel@edk2.groups.io
> Cc: Sami Mujawar <sami.mujawar@arm.com>; Leif Lindholm
> <quic_llindhol@quicinc.com>; Ard Biesheuvel <ardb+tianocore@kernel.org>;
> Rebecca Cran <rebecca@bsdio.com>; Kinney, Michael D
> <michael.d.kinney@intel.com>; Gao, Liming <gaoliming@byosoft.com.cn>; Yao,
> Jiewen <jiewen.yao@intel.com>; Wang, Jian J <jian.j.wang@intel.com>
> Subject: [PATCH v3 20/21] SecurityPkg/RngDxe: Add Arm support of RngDxe
> 
> From: Pierre Gondois <pierre.gondois@arm.com>
> 
> Bugzilla: 3668 (https://bugzilla.tianocore.org/show_bug.cgi?id=3668)
> 
> Add RngDxe support for Arm. This implementation uses the TrngLib
> to support the RawAlgorithm and doens't support the RNDR instruction.
> 
> Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
> ---
>  SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c | 8 ++++++--
>  SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf  | 9 ++++++---
>  SecurityPkg/SecurityPkg.dsc                          | 2 +-
>  3 files changed, 13 insertions(+), 6 deletions(-)
> 
> diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
> b/SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
> index ffa32a29dc6a..4775252d30b6 100644
> --- a/SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
> +++ b/SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
> @@ -22,6 +22,7 @@
> 
>  **/
> 
> +#include <Library/ArmLib.h>
>  #include <Library/BaseLib.h>
>  #include <Library/BaseMemoryLib.h>
>  #include <Library/UefiBootServicesTableLib.h>
> @@ -55,8 +56,9 @@ RngInitAvailableAlgoArray (
>    UINT16  MajorRevision;
>    UINT16  MinorRevision;
> 
> -  // Check RngGetBytes() before advertising PcdCpuRngSupportedAlgorithm.
> -  if (!EFI_ERROR (RngGetBytes (sizeof (Rand), (UINT8 *)&Rand))) {
> + #ifdef MDE_CPU_AARCH64
> +  // Check FEAT_RNG before advertising PcdCpuRngSupportedAlgorithm.
> +  if (ArmHasRngExt ()) {
>      CopyMem (
>        &mAvailableAlgoArray[mAvailableAlgoArrayCount],
>        PcdGetPtr (PcdCpuRngSupportedAlgorithm),
> @@ -75,6 +77,8 @@ RngInitAvailableAlgoArray (
>      DEBUG_CODE_END ();
>    }
> 
> + #endif
> +
>    // Raw algorithm (Trng)
>    if (!EFI_ERROR (GetTrngVersion (&MajorRevision, &MinorRevision))) {
>      CopyMem (
> diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
> b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
> index d2d0ff9ebb98..599a3085102d 100644
> --- a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
> +++ b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
> @@ -27,7 +27,7 @@ [Defines]
>  #
>  # The following information is for reference only and not required by the build
> tools.
>  #
> -#  VALID_ARCHITECTURES           = IA32 X64 AARCH64
> +#  VALID_ARCHITECTURES           = IA32 X64 AARCH64 ARM
>  #
> 
>  [Sources.common]
> @@ -40,7 +40,7 @@ [Sources.IA32, Sources.X64]
>    Rand/AesCore.c
>    Rand/AesCore.h
> 
> -[Sources.AARCH64]
> +[Sources.AARCH64, Sources.ARM]
>    ArmRngDxe.c
>    ArmTrng.c
> 
> @@ -49,6 +49,9 @@ [Packages]
>    MdePkg/MdePkg.dec
>    SecurityPkg/SecurityPkg.dec
> 
> +[Packages.AARCH64, Packages.ARM]
> +  ArmPkg/ArmPkg.dec
> +
>  [LibraryClasses]
>    UefiLib
>    UefiBootServicesTableLib
> @@ -58,7 +61,7 @@ [LibraryClasses]
>    TimerLib
>    RngLib
> 
> -[LibraryClasses.AARCH64]
> +[LibraryClasses.AARCH64, LibraryClasses.ARM]
>    TrngLib
> 
>  [Guids]
> diff --git a/SecurityPkg/SecurityPkg.dsc b/SecurityPkg/SecurityPkg.dsc
> index 490076542a33..882d639489ea 100644
> --- a/SecurityPkg/SecurityPkg.dsc
> +++ b/SecurityPkg/SecurityPkg.dsc
> @@ -292,7 +292,7 @@ [Components.IA32, Components.X64,
> Components.ARM, Components.AARCH64]
>    SecurityPkg/EnrollFromDefaultKeysApp/EnrollFromDefaultKeysApp.inf
> 
> SecurityPkg/VariableAuthenticated/SecureBootDefaultKeysDxe/SecureBootDefa
> ultKeysDxe.inf
> 
> -[Components.IA32, Components.X64, Components.AARCH64]
> +[Components.IA32, Components.X64, Components.AARCH64,
> Components.ARM]
>    #
>    # Random Number Generator
>    #
> --
> 2.25.1


^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 14/22] SecurityPkg/RngDxe: Replace Pcd with Sp80090Ctr256Guid
  2022-06-29 15:02 ` [PATCH v3 14/22] SecurityPkg/RngDxe: Replace Pcd with Sp80090Ctr256Guid PierreGondois
@ 2022-07-21 10:51   ` Yao, Jiewen
  0 siblings, 0 replies; 35+ messages in thread
From: Yao, Jiewen @ 2022-07-21 10:51 UTC (permalink / raw)
  To: Pierre.Gondois@arm.com, devel@edk2.groups.io
  Cc: Sami Mujawar, Leif Lindholm, Ard Biesheuvel, Rebecca Cran,
	Kinney, Michael D, Gao, Liming, Wang, Jian J

+  CopyMem (&RNGAlgorithmList[0], gEfiRngAlgorithmSp80090Ctr256Guid, sizeof (EFI_RNG_ALGORITHM));

Should it be &gEfiRngAlgorithmSp80090Ctr256Guid ? The dereference is missing.

Thank you
Yao Jiewen



> -----Original Message-----
> From: Pierre.Gondois@arm.com <Pierre.Gondois@arm.com>
> Sent: Wednesday, June 29, 2022 11:02 PM
> To: devel@edk2.groups.io
> Cc: Sami Mujawar <sami.mujawar@arm.com>; Leif Lindholm
> <quic_llindhol@quicinc.com>; Ard Biesheuvel <ardb+tianocore@kernel.org>;
> Rebecca Cran <rebecca@bsdio.com>; Kinney, Michael D
> <michael.d.kinney@intel.com>; Gao, Liming <gaoliming@byosoft.com.cn>; Yao,
> Jiewen <jiewen.yao@intel.com>; Wang, Jian J <jian.j.wang@intel.com>
> Subject: [PATCH v3 14/22] SecurityPkg/RngDxe: Replace Pcd with
> Sp80090Ctr256Guid
> 
> From: Pierre Gondois <pierre.gondois@arm.com>
> 
> gEfiRngAlgorithmSp80090Ctr256Guid was used as the default algorithm
> in RngGetRNG(). The commit below set the default algorithm to
> PcdCpuRngSupportedAlgorithm, which is a zero GUID by default.
> 
> As the Pcd value is not defined for any platform in the edk2-platfoms
> repository, assume it was an error and go back to the first version,
> using gEfiRngAlgorithmSp80090Ctr256Guid.
> 
> Fixes 4e5ecdbac8bd ("SecurityPkg: Add support for RngDxe on AARCH64")
> Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
> ---
>  SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c | 7 ++-----
>  1 file changed, 2 insertions(+), 5 deletions(-)
> 
> diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
> b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
> index 8d44f0636c3d..8cfe6b471192 100644
> --- a/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
> +++ b/SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
> @@ -126,8 +126,7 @@ ArchGetSupportedRngAlgorithms (
>    OUT    EFI_RNG_ALGORITHM  *RNGAlgorithmList
>    )
>  {
> -  UINTN              RequiredSize;
> -  EFI_RNG_ALGORITHM  *CpuRngSupportedAlgorithm;
> +  UINTN  RequiredSize;
> 
>    RequiredSize = 2 * sizeof (EFI_RNG_ALGORITHM);
> 
> @@ -136,9 +135,7 @@ ArchGetSupportedRngAlgorithms (
>      return EFI_BUFFER_TOO_SMALL;
>    }
> 
> -  CpuRngSupportedAlgorithm = PcdGetPtr (PcdCpuRngSupportedAlgorithm);
> -
> -  CopyMem (&RNGAlgorithmList[0], CpuRngSupportedAlgorithm, sizeof
> (EFI_RNG_ALGORITHM));
> +  CopyMem (&RNGAlgorithmList[0], gEfiRngAlgorithmSp80090Ctr256Guid,
> sizeof (EFI_RNG_ALGORITHM));
> 
>    // x86 platforms also support EFI_RNG_ALGORITHM_RAW via RDSEED
>    CopyMem (&RNGAlgorithmList[1], &gEfiRngAlgorithmRaw, sizeof
> (EFI_RNG_ALGORITHM));
> --
> 2.25.1


^ permalink raw reply	[flat|nested] 35+ messages in thread

end of thread, other threads:[~2022-07-21 10:51 UTC | newest]

Thread overview: 35+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
2022-06-29 15:02 ` [PATCH v3 01/22] ArmPkg: PCD to select conduit for monitor calls PierreGondois
2022-06-29 15:02 ` [PATCH v3 02/22] ArmPkg/ArmMonitorLib: Definition for ArmMonitorLib library class PierreGondois
2022-06-29 15:02 ` [PATCH v3 03/22] ArmPkg/ArmMonitorLib: Add ArmMonitorLib PierreGondois
2022-06-29 15:02 ` [PATCH v3 04/22] ArmPkg/ArmHvcNullLib: Add NULL instance of ArmHvcLib PierreGondois
2022-06-29 15:02 ` [PATCH v3 05/22] MdePkg/TrngLib: Definition for TRNG library class interface PierreGondois
2022-06-29 15:02 ` [PATCH v3 06/22] MdePkg/TrngLib: Add NULL instance of TRNG Library PierreGondois
2022-06-29 15:02 ` [PATCH v3 07/22] ArmPkg: Add FID definitions for Firmware TRNG PierreGondois
2022-06-29 15:02 ` [PATCH v3 08/22] ArmPkg/TrngLib: Add Arm Firmware TRNG library PierreGondois
2022-06-29 15:02 ` [PATCH v3 09/22] MdePkg/BaseRngLib: Rename ArmReadIdIsar0() to ArmGetFeatRng() PierreGondois
2022-06-29 15:02 ` [PATCH v3 10/22] ArmPkg/ArmLib: Add ArmReadIdIsar0() helper PierreGondois
2022-06-29 15:02 ` [PATCH v3 11/22] ArmPkg/ArmLib: Add ArmHasRngExt() PierreGondois
2022-06-29 15:02 ` [PATCH v3 12/21] SecurityPkg/RngDxe: Rename RdRandGenerateEntropy to generic name PierreGondois
2022-06-29 15:02 ` [PATCH v3 12/22] SecurityPkg: Update Securitypkg.ci.yaml PierreGondois
2022-06-29 15:02 ` [PATCH v3 13/22] SecurityPkg/RngDxe: Rename RdRandGenerateEntropy to generic name PierreGondois
2022-06-29 15:02 ` [PATCH v3 13/21] SecurityPkg/RngDxe: Replace Pcd with Sp80090Ctr256Guid PierreGondois
2022-06-29 15:02 ` [PATCH v3 14/21] SecurityPkg/RngDxe: Remove ArchGetSupportedRngAlgorithms() PierreGondois
2022-06-29 15:02 ` [PATCH v3 14/22] SecurityPkg/RngDxe: Replace Pcd with Sp80090Ctr256Guid PierreGondois
2022-07-21 10:51   ` Yao, Jiewen
2022-06-29 15:02 ` [PATCH v3 15/21] SecurityPkg/RngDxe: Documentation/include/parameter cleanup PierreGondois
2022-06-29 15:02 ` [PATCH v3 15/22] SecurityPkg/RngDxe: Remove ArchGetSupportedRngAlgorithms() PierreGondois
2022-06-29 15:02 ` [PATCH v3 16/21] SecurityPkg/RngDxe: Check before advertising Cpu Rng algo PierreGondois
2022-06-29 15:02 ` [PATCH v3 16/22] SecurityPkg/RngDxe: Documentation/include/parameter cleanup PierreGondois
2022-06-29 15:02 ` [PATCH v3 17/21] SecurityPkg/RngDxe: Add AArch64 RawAlgorithm support through TrngLib PierreGondois
2022-06-29 15:02 ` [PATCH v3 17/22] SecurityPkg/RngDxe: Check before advertising Cpu Rng algo PierreGondois
2022-06-29 15:02 ` [PATCH v3 18/22] SecurityPkg/RngDxe: Add AArch64 RawAlgorithm support through TrngLib PierreGondois
2022-06-29 15:02 ` [PATCH v3 18/21] SecurityPkg/RngDxe: Add debug warning for NULL PcdCpuRngSupportedAlgorithm PierreGondois
2022-06-29 15:02 ` [PATCH v3 19/22] " PierreGondois
2022-06-29 15:02 ` [PATCH v3 19/21] SecurityPkg/RngDxe: Rename AArch64/RngDxe.c PierreGondois
2022-06-29 15:02 ` [PATCH v3 20/21] SecurityPkg/RngDxe: Add Arm support of RngDxe PierreGondois
2022-07-21 10:47   ` Yao, Jiewen
2022-06-29 15:02 ` [PATCH v3 20/22] SecurityPkg/RngDxe: Rename AArch64/RngDxe.c PierreGondois
2022-06-29 15:02 ` [PATCH v3 21/21] ArmVirtPkg: Kvmtool: Add RNG support using FW-TRNG interface PierreGondois
2022-06-29 15:02 ` [PATCH v3 21/22] SecurityPkg/RngDxe: Add Arm support of RngDxe PierreGondois
2022-06-29 15:02 ` [PATCH v3 22/22] ArmVirtPkg: Kvmtool: Add RNG support using FW-TRNG interface PierreGondois

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox