public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "PierreGondois" <pierre.gondois@arm.com>
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>,
	Michael D Kinney <michael.d.kinney@intel.com>,
	Liming Gao <gaoliming@byosoft.com.cn>,
	Jiewen Yao <jiewen.yao@intel.com>,
	Jian J Wang <jian.j.wang@intel.com>
Subject: [PATCH v5 20/21] SecurityPkg/RngDxe: Add Arm support of RngDxe
Date: Mon, 19 Sep 2022 21:22:06 +0200	[thread overview]
Message-ID: <20220919192207.637786-21-Pierre.Gondois@arm.com> (raw)
In-Reply-To: <20220919192207.637786-1-Pierre.Gondois@arm.com>

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.

To re-use the RngGetRNG(), RngGetInfo() and FreeAvailableAlgorithms()
functions, create Arm/AArch64 files which implement the arch specific
function GetAvailableAlgorithms(). Indeed, FEAT_RNG instruction is not
supported on Arm.

Signed-off-by: Pierre Gondois <pierre.gondois@arm.com>
---
 .../RngDxe/AArch64/AArch64Algo.c              | 72 +++++++++++++++++++
 .../RngDxe/Arm/ArmAlgo.c                      | 51 +++++++++++++
 .../RandomNumberGenerator/RngDxe/ArmRngDxe.c  | 60 ----------------
 .../RandomNumberGenerator/RngDxe/RngDxe.inf   | 12 +++-
 SecurityPkg/SecurityPkg.dsc                   |  2 +-
 5 files changed, 133 insertions(+), 64 deletions(-)
 create mode 100644 SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/AArch64Algo.c
 create mode 100644 SecurityPkg/RandomNumberGenerator/RngDxe/Arm/ArmAlgo.c

diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/AArch64Algo.c b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/AArch64Algo.c
new file mode 100644
index 000000000000..d7e80a0d0d0d
--- /dev/null
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/AArch64Algo.c
@@ -0,0 +1,72 @@
+/** @file
+  Aarch64 specific code.
+
+  Copyright (c) 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/MemoryAllocationLib.h>
+#include <Library/TrngLib.h>
+
+#include "RngDxeInternals.h"
+
+// Maximum number of Rng algorithms.
+#define RNG_AVAILABLE_ALGO_MAX  2
+
+/** Allocate and initialize mAvailableAlgoArray with the available
+    Rng algorithms. Also update mAvailableAlgoArrayCount.
+
+  @retval EFI_SUCCESS             The function completed successfully.
+  @retval EFI_OUT_OF_RESOURCES    Could not allocate memory.
+**/
+EFI_STATUS
+EFIAPI
+GetAvailableAlgorithms (
+  VOID
+  )
+{
+  UINT64  DummyRand;
+  UINT16  MajorRevision;
+  UINT16  MinorRevision;
+
+  // Rng algorithms 2 times, one for the allocation, one to populate.
+  mAvailableAlgoArray = AllocateZeroPool (RNG_AVAILABLE_ALGO_MAX);
+  if (mAvailableAlgoArray == NULL) {
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  // Check RngGetBytes() before advertising PcdCpuRngSupportedAlgorithm.
+  if (!EFI_ERROR (RngGetBytes (sizeof (DummyRand), (UINT8 *)&DummyRand))) {
+    CopyMem (
+      &mAvailableAlgoArray[mAvailableAlgoArrayCount],
+      PcdGetPtr (PcdCpuRngSupportedAlgorithm),
+      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)
+  if (!EFI_ERROR (GetTrngVersion (&MajorRevision, &MinorRevision))) {
+    CopyMem (
+      &mAvailableAlgoArray[mAvailableAlgoArrayCount],
+      &gEfiRngAlgorithmRaw,
+      sizeof (EFI_RNG_ALGORITHM)
+      );
+    mAvailableAlgoArrayCount++;
+  }
+
+  return EFI_SUCCESS;
+}
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/Arm/ArmAlgo.c b/SecurityPkg/RandomNumberGenerator/RngDxe/Arm/ArmAlgo.c
new file mode 100644
index 000000000000..5acef91fe3b2
--- /dev/null
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/Arm/ArmAlgo.c
@@ -0,0 +1,51 @@
+/** @file
+  Arm specific code.
+
+  Copyright (c) 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/MemoryAllocationLib.h>
+#include <Library/TrngLib.h>
+
+#include "RngDxeInternals.h"
+
+// Maximum number of Rng algorithms.
+#define RNG_AVAILABLE_ALGO_MAX  1
+
+/** Allocate and initialize mAvailableAlgoArray with the available
+    Rng algorithms. Also update mAvailableAlgoArrayCount.
+
+  @retval EFI_SUCCESS             The function completed successfully.
+  @retval EFI_OUT_OF_RESOURCES    Could not allocate memory.
+**/
+EFI_STATUS
+EFIAPI
+GetAvailableAlgorithms (
+  VOID
+  )
+{
+  UINT16  MajorRevision;
+  UINT16  MinorRevision;
+
+  // Rng algorithms 2 times, one for the allocation, one to populate.
+  mAvailableAlgoArray = AllocateZeroPool (RNG_AVAILABLE_ALGO_MAX);
+  if (mAvailableAlgoArray == NULL) {
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  // Raw algorithm (Trng)
+  if (!EFI_ERROR (GetTrngVersion (&MajorRevision, &MinorRevision))) {
+    CopyMem (
+      &mAvailableAlgoArray[mAvailableAlgoArrayCount],
+      &gEfiRngAlgorithmRaw,
+      sizeof (EFI_RNG_ALGORITHM)
+      );
+    mAvailableAlgoArrayCount++;
+  }
+
+  return EFI_SUCCESS;
+}
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
index 0d853720ecb1..5ba319899ce9 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
@@ -28,70 +28,10 @@
 #include <Library/MemoryAllocationLib.h>
 #include <Library/UefiBootServicesTableLib.h>
 #include <Library/RngLib.h>
-#include <Library/DebugLib.h>
-#include <Library/TrngLib.h>
 #include <Protocol/Rng.h>
 
 #include "RngDxeInternals.h"
 
-// Maximum number of Rng algorithms.
-#define RNG_AVAILABLE_ALGO_MAX  2
-
-/** Allocate and initialize mAvailableAlgoArray with the available
-    Rng algorithms. Also update mAvailableAlgoArrayCount.
-
-  @retval EFI_SUCCESS             The function completed successfully.
-  @retval EFI_OUT_OF_RESOURCES    Could not allocate memory.
-**/
-EFI_STATUS
-EFIAPI
-GetAvailableAlgorithms (
-  VOID
-  )
-{
-  UINT64  DummyRand;
-  UINT16  MajorRevision;
-  UINT16  MinorRevision;
-
-  // Rng algorithms 2 times, one for the allocation, one to populate.
-  mAvailableAlgoArray = AllocateZeroPool (RNG_AVAILABLE_ALGO_MAX);
-  if (mAvailableAlgoArray == NULL) {
-    return EFI_OUT_OF_RESOURCES;
-  }
-
-  // Check RngGetBytes() before advertising PcdCpuRngSupportedAlgorithm.
-  if (!EFI_ERROR (RngGetBytes (sizeof (DummyRand), (UINT8 *)&DummyRand))) {
-    CopyMem (
-      &mAvailableAlgoArray[mAvailableAlgoArrayCount],
-      PcdGetPtr (PcdCpuRngSupportedAlgorithm),
-      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)
-  if (!EFI_ERROR (GetTrngVersion (&MajorRevision, &MinorRevision))) {
-    CopyMem (
-      &mAvailableAlgoArray[mAvailableAlgoArrayCount],
-      &gEfiRngAlgorithmRaw,
-      sizeof (EFI_RNG_ALGORITHM)
-      );
-    mAvailableAlgoArrayCount++;
-  }
-
-  return EFI_SUCCESS;
-}
-
 /** Free mAvailableAlgoArray.
 **/
 VOID
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
index 337becf5224f..9f1630161032 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
@@ -28,7 +28,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]
@@ -41,10 +41,16 @@ [Sources.IA32, Sources.X64]
   Rand/AesCore.c
   Rand/AesCore.h
 
-[Sources.AARCH64]
+[Sources.AARCH64, Sources.ARM]
   ArmRngDxe.c
   ArmTrng.c
 
+[Sources.AARCH64]
+  AArch64/AArch64Algo.c
+
+[Sources.ARM]
+  Arm/ArmAlgo.c
+
 [Packages]
   MdeModulePkg/MdeModulePkg.dec
   MdePkg/MdePkg.dec
@@ -59,7 +65,7 @@ [LibraryClasses]
   TimerLib
   RngLib
 
-[LibraryClasses.AARCH64]
+[LibraryClasses.AARCH64, LibraryClasses.ARM]
   TrngLib
 
 [Guids]
diff --git a/SecurityPkg/SecurityPkg.dsc b/SecurityPkg/SecurityPkg.dsc
index 690a45e89728..55d17e64efd8 100644
--- a/SecurityPkg/SecurityPkg.dsc
+++ b/SecurityPkg/SecurityPkg.dsc
@@ -290,7 +290,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


  parent reply	other threads:[~2022-09-19 19:23 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-19 19:21 [PATCH v5 00/21] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
2022-09-19 19:21 ` [PATCH v5 01/21] ArmPkg: PCD to select conduit for monitor calls PierreGondois
2022-09-27 18:59   ` Leif Lindholm
2022-09-19 19:21 ` [PATCH v5 02/21] ArmPkg/ArmMonitorLib: Definition for ArmMonitorLib library class PierreGondois
2022-09-19 19:21 ` [PATCH v5 03/21] ArmPkg/ArmMonitorLib: Add ArmMonitorLib PierreGondois
2022-09-27 18:51   ` Leif Lindholm
2022-09-27 19:00   ` [edk2-devel] " Rebecca Cran
2022-09-27 19:10   ` Rebecca Cran
2022-09-19 19:21 ` [PATCH v5 04/21] ArmPkg/ArmHvcNullLib: Add NULL instance of ArmHvcLib PierreGondois
2022-09-27 18:55   ` Leif Lindholm
2022-09-19 19:21 ` [PATCH v5 05/21] MdePkg/TrngLib: Definition for TRNG library class interface PierreGondois
2022-09-27 19:00   ` Leif Lindholm
2022-09-19 19:21 ` [PATCH v5 06/21] MdePkg/TrngLib: Add NULL instance of TRNG Library PierreGondois
2022-09-27 19:02   ` Leif Lindholm
2022-09-19 19:21 ` [PATCH v5 07/21] ArmPkg: Add FID definitions for Firmware TRNG PierreGondois
2022-09-27 19:04   ` Leif Lindholm
2022-09-19 19:21 ` [PATCH v5 08/21] ArmPkg/TrngLib: Add Arm Firmware TRNG library PierreGondois
2022-09-19 19:21 ` [PATCH v5 09/21] MdePkg/BaseRngLib: Rename ArmReadIdIsar0() to ArmGetFeatRng() PierreGondois
2022-09-27 21:44   ` [edk2-devel] " Rebecca Cran
2022-09-28 17:10   ` Leif Lindholm
2022-09-29 16:21     ` PierreGondois
2022-09-29 17:09       ` Leif Lindholm
2022-09-19 19:21 ` [PATCH v5 10/21] ArmPkg/ArmLib: Add ArmReadIdIsar0() helper PierreGondois
2022-09-19 19:21 ` [PATCH v5 11/21] ArmPkg/ArmLib: Add ArmHasRngExt() PierreGondois
2022-09-19 19:21 ` [PATCH v5 12/21] SecurityPkg/RngDxe: Rename RdRandGenerateEntropy to generic name PierreGondois
2022-09-19 19:21 ` [PATCH v5 13/21] SecurityPkg/RngDxe: Replace Pcd with Sp80090Ctr256Guid PierreGondois
2022-09-19 19:22 ` [PATCH v5 14/21] SecurityPkg/RngDxe: Remove ArchGetSupportedRngAlgorithms() PierreGondois
2022-09-19 19:22 ` [PATCH v5 15/21] SecurityPkg/RngDxe: Documentation/include/parameter cleanup PierreGondois
2022-09-19 19:22 ` [PATCH v5 16/21] SecurityPkg/RngDxe: Check before advertising Cpu Rng algo PierreGondois
2022-09-19 19:22 ` [PATCH v5 17/21] SecurityPkg/RngDxe: Add AArch64 RawAlgorithm support through TrngLib PierreGondois
2022-09-19 19:22 ` [PATCH v5 18/21] SecurityPkg/RngDxe: Add debug warning for NULL PcdCpuRngSupportedAlgorithm PierreGondois
2022-09-19 19:22 ` [PATCH v5 19/21] SecurityPkg/RngDxe: Rename AArch64/RngDxe.c PierreGondois
2022-09-19 19:22 ` PierreGondois [this message]
2022-09-19 19:22 ` [PATCH v5 21/21] ArmVirtPkg: Kvmtool: Add RNG support using FW-TRNG interface PierreGondois

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220919192207.637786-21-Pierre.Gondois@arm.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox