]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg: BaseRngLibDxe: Add RngLib that uses RngDxe
authorMatthew Carlson <macarl@microsoft.com>
Fri, 31 Jul 2020 20:07:17 +0000 (13:07 -0700)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Fri, 18 Sep 2020 02:19:21 +0000 (02:19 +0000)
This adds a RngLib that uses the RngProtocol to provide randomness.
This means that the RngLib is meant to be used with DXE_DRIVERS.

Ref: https://github.com/tianocore/edk2/pull/845
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=1871

Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Zhiguang Liu <zhiguang.liu@intel.com>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
Signed-off-by: Matthew Carlson <matthewfcarlson@gmail.com>
MdePkg/Library/DxeRngLib/DxeRngLib.c [new file with mode: 0644]
MdePkg/Library/DxeRngLib/DxeRngLib.inf [new file with mode: 0644]
MdePkg/Library/DxeRngLib/DxeRngLib.uni [new file with mode: 0644]
MdePkg/MdePkg.dsc

diff --git a/MdePkg/Library/DxeRngLib/DxeRngLib.c b/MdePkg/Library/DxeRngLib/DxeRngLib.c
new file mode 100644 (file)
index 0000000..9c3d67b
--- /dev/null
@@ -0,0 +1,199 @@
+/** @file\r
+ Provides an implementation of the library class RngLib that uses the Rng protocol.\r
+\r
+ Copyright (c) Microsoft Corporation. All rights reserved.\r
+ SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+#include <Uefi.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/RngLib.h>\r
+#include <Protocol/Rng.h>\r
+\r
+/**\r
+  Routine Description:\r
+\r
+  Generates a random number via the NIST\r
+  800-9A algorithm.  Refer to\r
+  http://csrc.nist.gov/groups/STM/cavp/documents/drbg/DRBGVS.pdf\r
+  for more information.\r
+\r
+  @param[out] Buffer      Buffer to receive the random number.\r
+  @param[in]  BufferSize  Number of bytes in Buffer.\r
+\r
+  @retval EFI_SUCCESS or underlying failure code.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+GenerateRandomNumberViaNist800Algorithm (\r
+  OUT UINT8  *Buffer,\r
+  IN  UINTN  BufferSize\r
+  )\r
+{\r
+  EFI_STATUS        Status;\r
+  EFI_RNG_PROTOCOL  *RngProtocol;\r
+\r
+  RngProtocol = NULL;\r
+\r
+  if (Buffer == NULL) {\r
+      DEBUG((DEBUG_ERROR, "%a: Buffer == NULL.\n", __FUNCTION__));\r
+      return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Status = gBS->LocateProtocol (&gEfiRngProtocolGuid, NULL, (VOID **)&RngProtocol);\r
+  if (EFI_ERROR (Status) || RngProtocol == NULL) {\r
+      DEBUG((DEBUG_ERROR, "%a: Could not locate RNG prototocol, Status = %r\n", __FUNCTION__, Status));\r
+      return Status;\r
+  }\r
+\r
+  Status = RngProtocol->GetRNG (RngProtocol, &gEfiRngAlgorithmSp80090Ctr256Guid, BufferSize, Buffer);\r
+  DEBUG((DEBUG_INFO, "%a: GetRNG algorithm CTR-256 - Status = %r\n", __FUNCTION__, Status));\r
+  if (!EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Status = RngProtocol->GetRNG (RngProtocol, &gEfiRngAlgorithmSp80090Hmac256Guid, BufferSize, Buffer);\r
+  DEBUG((DEBUG_INFO, "%a: GetRNG algorithm HMAC-256 - Status = %r\n", __FUNCTION__, Status));\r
+  if (!EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Status = RngProtocol->GetRNG (RngProtocol, &gEfiRngAlgorithmSp80090Hash256Guid, BufferSize, Buffer);\r
+  DEBUG((DEBUG_INFO, "%a: GetRNG algorithm Hash-256 - Status = %r\n", __FUNCTION__, Status));\r
+  if (!EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  // If all the other methods have failed, use the default method from the RngProtocol\r
+  Status = RngProtocol->GetRNG (RngProtocol, NULL, BufferSize, Buffer);\r
+  DEBUG((DEBUG_INFO, "%a: GetRNG algorithm Hash-256 - Status = %r\n", __FUNCTION__, Status));\r
+  if (!EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  // If we get to this point, we have failed\r
+  DEBUG((DEBUG_ERROR, "%a: GetRNG() failed, staus = %r\n", __FUNCTION__, Status));\r
+\r
+  return Status;\r
+}// GenerateRandomNumberViaNist800Algorithm()\r
+\r
+\r
+/**\r
+  Generates a 16-bit random number.\r
+\r
+  if Rand is NULL, return FALSE.\r
+\r
+  @param[out] Rand     Buffer pointer to store the 16-bit random value.\r
+\r
+  @retval TRUE         Random number generated successfully.\r
+  @retval FALSE        Failed to generate the random number.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+GetRandomNumber16 (\r
+  OUT UINT16  *Rand\r
+  )\r
+{\r
+  EFI_STATUS Status;\r
+\r
+  if (Rand == NULL)\r
+  {\r
+    return FALSE;\r
+  }\r
+\r
+  Status = GenerateRandomNumberViaNist800Algorithm ((UINT8 *)Rand, sizeof(UINT16));\r
+  if (EFI_ERROR (Status)) {\r
+    return FALSE;\r
+  }\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Generates a 32-bit random number.\r
+\r
+  if Rand is NULL, return FALSE.\r
+\r
+  @param[out] Rand     Buffer pointer to store the 32-bit random value.\r
+\r
+  @retval TRUE         Random number generated successfully.\r
+  @retval FALSE        Failed to generate the random number.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+GetRandomNumber32 (\r
+  OUT UINT32 *Rand\r
+  )\r
+{\r
+  EFI_STATUS Status;\r
+\r
+  if (Rand == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  Status = GenerateRandomNumberViaNist800Algorithm ((UINT8*)Rand, sizeof(UINT32));\r
+  if (EFI_ERROR (Status)) {\r
+    return FALSE;\r
+  }\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Generates a 64-bit random number.\r
+\r
+  if Rand is NULL, return FALSE.\r
+\r
+  @param[out] Rand     Buffer pointer to store the 64-bit random value.\r
+\r
+  @retval TRUE         Random number generated successfully.\r
+  @retval FALSE        Failed to generate the random number.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+GetRandomNumber64 (\r
+  OUT UINT64 *Rand\r
+  )\r
+{\r
+  EFI_STATUS Status;\r
+\r
+  if (Rand == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  Status = GenerateRandomNumberViaNist800Algorithm ((UINT8*)Rand, sizeof(UINT64));\r
+  if (EFI_ERROR (Status)) {\r
+    return FALSE;\r
+  }\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Generates a 128-bit random number.\r
+\r
+  if Rand is NULL, return FALSE.\r
+\r
+  @param[out] Rand     Buffer pointer to store the 128-bit random value.\r
+\r
+  @retval TRUE         Random number generated successfully.\r
+  @retval FALSE        Failed to generate the random number.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+GetRandomNumber128 (\r
+  OUT UINT64 *Rand\r
+  )\r
+{\r
+  EFI_STATUS Status;\r
+\r
+  if (Rand == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  Status = GenerateRandomNumberViaNist800Algorithm ((UINT8*)Rand, 2 * sizeof(UINT64));\r
+  if (EFI_ERROR (Status)) {\r
+    return FALSE;\r
+  }\r
+  return TRUE;\r
+}\r
diff --git a/MdePkg/Library/DxeRngLib/DxeRngLib.inf b/MdePkg/Library/DxeRngLib/DxeRngLib.inf
new file mode 100644 (file)
index 0000000..68554ad
--- /dev/null
@@ -0,0 +1,38 @@
+# @file\r
+# Provides implementation of the library class RngLib that uses the RngProtocol\r
+#\r
+# @copyright\r
+# Copyright (c) Microsoft Corporation. All rights reserved.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION     = 1.27\r
+  BASE_NAME       = DxeRngLib\r
+  MODULE_UNI_FILE = DxeRngLib.uni\r
+  FILE_GUID       = FF9F84C5-A33E-44E3-9BB5-0D654B2D4149\r
+  MODULE_TYPE     = DXE_DRIVER\r
+  VERSION_STRING  = 1.0\r
+  LIBRARY_CLASS   = RngLib|DXE_DRIVER UEFI_APPLICATION UEFI_DRIVER\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+\r
+[Sources]\r
+  DxeRngLib.c\r
+\r
+[LibraryClasses]\r
+  DebugLib\r
+  UefiBootServicesTableLib\r
+\r
+[Protocols]\r
+  gEfiRngProtocolGuid                 ## CONSUMES\r
+\r
+[Depex]\r
+  gEfiRngProtocolGuid\r
+\r
+[Guids]\r
+  gEfiRngAlgorithmSp80090Ctr256Guid\r
+  gEfiRngAlgorithmSp80090Hash256Guid\r
+  gEfiRngAlgorithmSp80090Hmac256Guid\r
diff --git a/MdePkg/Library/DxeRngLib/DxeRngLib.uni b/MdePkg/Library/DxeRngLib/DxeRngLib.uni
new file mode 100644 (file)
index 0000000..c904e54
--- /dev/null
@@ -0,0 +1,15 @@
+// @file\r
+// Instance of RNG (Random Number Generator) Library.\r
+//\r
+// RngLib that uses the Rng Protocol to provide random numbers.\r
+//\r
+// Copyright (c) Microsoft Corporation.\r
+//\r
+// SPDX-License-Identifier: BSD-2-Clause-Patent\r
+//\r
+\r
+\r
+#string STR_MODULE_ABSTRACT     #language en-US "Instance of RNG Library"\r
+\r
+#string STR_MODULE_DESCRIPTION  #language en-US "BaseRng Library that uses the Rng Protocol to provide random numbers"\r
+\r
index d7ba3a730909bd77295aac0f807faf5fbf7c1e55..2c3b7966b086dbb2b6467c6f246ba1a2005fe57d 100644 (file)
   MdePkg/Library/BasePostCodeLibPort80/BasePostCodeLibPort80.inf\r
   MdePkg/Library/BasePrintLib/BasePrintLib.inf\r
   MdePkg/Library/BaseReportStatusCodeLibNull/BaseReportStatusCodeLibNull.inf\r
-  MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf\r
+  MdePkg/Library/DxeRngLib/DxeRngLib.inf\r
   MdePkg/Library/BaseRngLibNull/BaseRngLibNull.inf\r
+  MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf\r
+\r
   MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf\r
   MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf\r
   MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf\r