]> git.proxmox.com Git - mirror_edk2.git/commitdiff
SecurityPkg: Integrate new RngLib into RngDxe
authorThomas Palmer <thomas.palmer@hpe.com>
Fri, 9 Oct 2015 06:03:26 +0000 (06:03 +0000)
committerqlong <qlong@Edk2>
Fri, 9 Oct 2015 06:03:26 +0000 (06:03 +0000)
Use the new RngLib to provide the IA32/X64 random data for RngDxe.
Remove x86 specific functions from RdRand files.
Simplify RngDxe by using WriteUnaligned64 for all platforms.
Use GetRandomNumber128 in RngDxe to leverage 128 bit support provided
by some HW RNG devices.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Thomas Palmer <thomas.palmer@hpe.com>
Reviewed-by: Samer El-Haj-Mahmoud <elhaj@hpe.com>
Reviewed-by: Michael Kinney <michael.d.kinney@intel.com>
Reviewed-by: Chao Zhang <chao.b.zhang@intel.com>
Reviewed-by: Qin Long <qin.long@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@18591 6f19259b-4bc3-4df7-8a09-765794883524

SecurityPkg/RandomNumberGenerator/RngDxe/RdRand.c
SecurityPkg/RandomNumberGenerator/RngDxe/RdRand.h
SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c
SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
SecurityPkg/SecurityPkg.dsc

index 7e618dc91f2349b1da8c0b674c9dd844a940fabf..395b8867c83620e39c77ebf92cc7f41c01d7d2b6 100644 (file)
@@ -2,6 +2,7 @@
   Support routines for RDRAND instruction access.\r
 \r
 Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>\r
+(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -11,177 +12,11 @@ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
 **/\r
+#include <Library/RngLib.h>\r
 \r
 #include "RdRand.h"\r
 #include "AesCore.h"\r
 \r
-//\r
-// Bit mask used to determine if RdRand instruction is supported.\r
-//\r
-#define RDRAND_MASK    0x40000000\r
-\r
-/**\r
-  Determines whether or not RDRAND instruction is supported by the host hardware.\r
-\r
-  @retval EFI_SUCCESS          RDRAND instruction supported.\r
-  @retval EFI_UNSUPPORTED      RDRAND instruction not supported.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-IsRdRandSupported (\r
-  VOID\r
-  )\r
-{\r
-  EFI_STATUS  Status;\r
-  UINT32      RegEax;\r
-  UINT32      RegEbx;\r
-  UINT32      RegEcx;\r
-  UINT32      RegEdx;\r
-  BOOLEAN     IsIntelCpu;\r
-\r
-  Status     = EFI_UNSUPPORTED;\r
-  IsIntelCpu = FALSE;\r
-  \r
-  //\r
-  // Checks whether the current processor is an Intel product by CPUID.\r
-  //\r
-  AsmCpuid (0, &RegEax, &RegEbx, &RegEcx, &RegEdx);\r
-  if ((CompareMem ((CHAR8 *)(&RegEbx), "Genu", 4) == 0) &&\r
-      (CompareMem ((CHAR8 *)(&RegEdx), "ineI", 4) == 0) &&\r
-      (CompareMem ((CHAR8 *)(&RegEcx), "ntel", 4) == 0)) {\r
-    IsIntelCpu = TRUE;\r
-  }\r
-\r
-  if (IsIntelCpu) {\r
-    //\r
-    // Determine RDRAND support by examining bit 30 of the ECX register returned by CPUID.\r
-    // A value of 1 indicates that processor supports RDRAND instruction.\r
-    //\r
-    AsmCpuid (1, 0, 0, &RegEcx, 0);\r
-\r
-    if ((RegEcx & RDRAND_MASK) == RDRAND_MASK) {\r
-      Status = EFI_SUCCESS;\r
-    }\r
-  }\r
-\r
-  return Status;\r
-}\r
-\r
-/**\r
-  Calls RDRAND to obtain a 16-bit random number.\r
-\r
-  @param[out]  Rand          Buffer pointer to store the random result.\r
-  @param[in]   NeedRetry     Determine whether or not to loop retry.\r
-\r
-  @retval EFI_SUCCESS        RDRAND call was successful.\r
-  @retval EFI_NOT_READY      Failed attempts to call RDRAND.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-RdRand16 (\r
-  OUT UINT16       *Rand,\r
-  IN BOOLEAN       NeedRetry\r
-  )\r
-{\r
-  UINT32      Index;\r
-  UINT32      RetryCount;\r
-\r
-  if (NeedRetry) {\r
-    RetryCount = RETRY_LIMIT;\r
-  } else {\r
-    RetryCount = 1;\r
-  }\r
-\r
-  //\r
-  // Perform a single call to RDRAND, or enter a loop call until RDRAND succeeds.\r
-  //\r
-  for (Index = 0; Index < RetryCount; Index++) {\r
-    if (RdRand16Step (Rand)) {\r
-      return EFI_SUCCESS;\r
-    }\r
-  }\r
-  \r
-  return EFI_NOT_READY;\r
-}\r
-\r
-/**\r
-  Calls RDRAND to obtain a 32-bit random number.\r
-\r
-  @param[out]  Rand          Buffer pointer to store the random result.\r
-  @param[in]   NeedRetry     Determine whether or not to loop retry.\r
-\r
-  @retval EFI_SUCCESS        RDRAND call was successful.\r
-  @retval EFI_NOT_READY      Failed attempts to call RDRAND.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-RdRand32 (\r
-  OUT UINT32       *Rand,\r
-  IN BOOLEAN       NeedRetry\r
-  )\r
-{\r
-  UINT32      Index;\r
-  UINT32      RetryCount;\r
-\r
-  if (NeedRetry) {\r
-    RetryCount = RETRY_LIMIT;\r
-  } else {\r
-    RetryCount = 1;\r
-  }\r
-\r
-  //\r
-  // Perform a single call to RDRAND, or enter a loop call until RDRAND succeeds.\r
-  //\r
-  for (Index = 0; Index < RetryCount; Index++) {\r
-    if (RdRand32Step (Rand)) {\r
-      return EFI_SUCCESS;\r
-    }\r
-  }\r
-  \r
-  return EFI_NOT_READY;\r
-}\r
-\r
-/**\r
-  Calls RDRAND to obtain a 64-bit random number.\r
-\r
-  @param[out]  Rand          Buffer pointer to store the random result.\r
-  @param[in]   NeedRetry     Determine whether or not to loop retry.\r
-\r
-  @retval EFI_SUCCESS        RDRAND call was successful.\r
-  @retval EFI_NOT_READY      Failed attempts to call RDRAND.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-RdRand64 (\r
-  OUT UINT64       *Rand,\r
-  IN BOOLEAN       NeedRetry\r
-  )\r
-{\r
-  UINT32      Index;\r
-  UINT32      RetryCount;\r
-\r
-  if (NeedRetry) {\r
-    RetryCount = RETRY_LIMIT;\r
-  } else {\r
-    RetryCount = 1;\r
-  }\r
-\r
-  //\r
-  // Perform a single call to RDRAND, or enter a loop call until RDRAND succeeds.\r
-  //\r
-  for (Index = 0; Index < RetryCount; Index++) {\r
-    if (RdRand64Step (Rand)) {\r
-      return EFI_SUCCESS;\r
-    }\r
-  }\r
-  \r
-  return EFI_NOT_READY;\r
-}\r
-\r
 /**\r
   Calls RDRAND to fill a buffer of arbitrary size with random bytes.\r
 \r
@@ -199,80 +34,23 @@ RdRandGetBytes (
   OUT UINT8        *RandBuffer\r
   )\r
 {\r
-  EFI_STATUS  Status;\r
-  UINT8       *Start;\r
-  UINT8       *ResidualStart;\r
-  UINTN       *BlockStart;\r
-  UINTN       TempRand;\r
-  UINTN       Count;\r
-  UINTN       Residual;\r
-  UINTN       StartLen;\r
-  UINTN       BlockNum;\r
-  UINTN       Index;\r
-\r
-  ResidualStart = NULL;\r
-  TempRand      = 0;\r
+  BOOLEAN     IsRandom;\r
+  UINT64      TempRand[2];\r
 \r
-  //\r
-  // Compute the address of the first word aligned (32/64-bit) block in the \r
-  // destination buffer, depending on whether we are in 32- or 64-bit mode.\r
-  //\r
-  Start = RandBuffer;\r
-  if (((UINT32)(UINTN)Start % (UINT32)sizeof(UINTN)) == 0) {\r
-    BlockStart = (UINTN *)Start;\r
-    Count      = Length;\r
-    StartLen   = 0;\r
-  } else {\r
-    BlockStart = (UINTN *)(((UINTN)Start & ~(UINTN)(sizeof(UINTN) - 1)) + (UINTN)sizeof(UINTN));\r
-    Count      = Length - (sizeof (UINTN) - (UINT32)((UINTN)Start % sizeof (UINTN)));\r
-    StartLen   = (UINT32)((UINTN)BlockStart - (UINTN)Start);\r
-  }\r
-\r
-  //\r
-  // Compute the number of word blocks and the remaining number of bytes.\r
-  //\r
-  Residual = Count % sizeof (UINTN);\r
-  BlockNum = Count / sizeof (UINTN);\r
-  if (Residual != 0) {\r
-    ResidualStart = (UINT8 *) (BlockStart + BlockNum);\r
-  }\r
-\r
-  //\r
-  // Obtain a temporary random number for use in the residuals. Failout if retry fails.\r
-  //\r
-  if (StartLen > 0) {\r
-    Status = RdRandWord ((UINTN *) &TempRand, TRUE);\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-  }\r
-\r
-  //\r
-  // Populate the starting mis-aligned block.\r
-  //\r
-  for (Index = 0; Index < StartLen; Index++) {\r
-    Start[Index] = (UINT8)(TempRand & 0xff);\r
-    TempRand     = TempRand >> 8;\r
-  }\r
-\r
-  //\r
-  // Populate the central aligned block. Fail out if retry fails.\r
-  //\r
-  Status = RdRandGetWords (BlockNum, (UINTN *)(BlockStart));\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-  //\r
-  // Populate the final mis-aligned block.\r
-  //\r
-  if (Residual > 0) {\r
-    Status = RdRandWord ((UINTN *)&TempRand, TRUE);\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
+  while (Length > 0) {\r
+    IsRandom = GetRandomNumber128 (TempRand);\r
+    if (!IsRandom) {\r
+      return EFI_NOT_READY;\r
     }\r
-    for (Index = 0; Index < Residual; Index++) {\r
-      ResidualStart[Index] = (UINT8)(TempRand & 0xff);\r
-      TempRand             = TempRand >> 8;\r
+    if (Length >= sizeof (TempRand)) {\r
+      WriteUnaligned64 ((UINT64*)RandBuffer, TempRand[0]);\r
+      RandBuffer += sizeof (UINT64);\r
+      WriteUnaligned64 ((UINT64*)RandBuffer, TempRand[1]);\r
+      RandBuffer += sizeof (UINT64);\r
+      Length -= sizeof (TempRand);\r
+    } else {\r
+      CopyMem (RandBuffer, TempRand, Length);\r
+      Length = 0;\r
     }\r
   }\r
 \r
index 20fd9fbd3ffa0a653d8e639e84bc5ee81f892eca..d6378778cf15401eb6bb2f9533719480423f3f99 100644 (file)
@@ -9,6 +9,7 @@
   Secure Key technology.\r
 \r
 Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>\r
+(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -28,154 +29,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Library/TimerLib.h>\r
 #include <Protocol/Rng.h>\r
 \r
-//\r
-// The maximun number of retries to obtain one available random number. \r
-//\r
-#define RETRY_LIMIT    10\r
-\r
-/**\r
-  Determines whether or not RDRAND instruction is supported by the host hardware.\r
-\r
-  @retval EFI_SUCCESS          RDRAND instruction supported.\r
-  @retval EFI_UNSUPPORTED      RDRAND instruction not supported.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-IsRdRandSupported (\r
-  VOID\r
-  );\r
-\r
-/**\r
-  Generates a 16-bit random number through RDRAND instruction.\r
-\r
-  @param[out]  Rand          Buffer pointer to store the random result.\r
-\r
-  @retval TRUE               RDRAND call was successful.\r
-  @retval FALSE              Failed attempts to call RDRAND.\r
-\r
-**/\r
-BOOLEAN\r
-EFIAPI\r
-RdRand16Step (\r
-  OUT UINT16       *Rand\r
-  );\r
-\r
-/**\r
-  Generates a 32-bit random number through RDRAND instruction.\r
-\r
-  @param[out]  Rand          Buffer pointer to store the random result.\r
-\r
-  @retval TRUE               RDRAND call was successful.\r
-  @retval FALSE              Failed attempts to call RDRAND.\r
-\r
-**/\r
-BOOLEAN\r
-EFIAPI\r
-RdRand32Step (\r
-  OUT UINT32       *Rand\r
-  );\r
-\r
-/**\r
-  Generates a 64-bit random number through RDRAND instruction.\r
-\r
-  @param[out]  Rand          Buffer pointer to store the random result.\r
-\r
-  @retval TRUE               RDRAND call was successful.\r
-  @retval FALSE              Failed attempts to call RDRAND.\r
-\r
-**/\r
-BOOLEAN\r
-EFIAPI\r
-RdRand64Step  (\r
-  OUT UINT64   *Rand\r
-  );\r
-\r
-/**\r
-  Calls RDRAND to obtain a 16-bit random number.\r
-\r
-  @param[out]  Rand          Buffer pointer to store the random result.\r
-  @param[in]   NeedRetry     Determine whether or not to loop retry.\r
-\r
-  @retval EFI_SUCCESS        RDRAND call was successful.\r
-  @retval EFI_NOT_READY      Failed attempts to call RDRAND.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-RdRand16 (\r
-  OUT UINT16       *Rand,\r
-  IN BOOLEAN       NeedRetry\r
-  );\r
-\r
-/**\r
-  Calls RDRAND to obtain a 32-bit random number.\r
-\r
-  @param[out]  Rand          Buffer pointer to store the random result.\r
-  @param[in]   NeedRetry     Determine whether or not to loop retry.\r
-\r
-  @retval EFI_SUCCESS        RDRAND call was successful.\r
-  @retval EFI_NOT_READY      Failed attempts to call RDRAND.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-RdRand32 (\r
-  OUT UINT32       *Rand,\r
-  IN BOOLEAN       NeedRetry\r
-  );\r
-\r
-/**\r
-  Calls RDRAND to obtain a 64-bit random number.\r
-\r
-  @param[out]  Rand          Buffer pointer to store the random result.\r
-  @param[in]   NeedRetry     Determine whether or not to loop retry.\r
-\r
-  @retval EFI_SUCCESS        RDRAND call was successful.\r
-  @retval EFI_NOT_READY      Failed attempts to call RDRAND.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-RdRand64 (\r
-  OUT UINT64       *Rand,\r
-  IN BOOLEAN       NeedRetry\r
-  );\r
-  \r
-/**\r
-  Calls RDRAND to request a word-length random number.\r
-\r
-  @param[out]  Rand          Buffer pointer to store the random number.\r
-  @param[in]   NeedRetry     Determine whether or not to loop retry.\r
-\r
-  @retval EFI_SUCCESS        Random word generation succeeded.\r
-  @retval EFI_NOT_READY      Failed to request random word.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-RdRandWord (\r
-  OUT UINTN        *Rand,\r
-  IN BOOLEAN       NeedRetry\r
-  );\r
-\r
-/**\r
-  Calls RDRAND to request multiple word-length random numbers.\r
-\r
-  @param[in]   Length        Size of the buffer, in words, to fill with.\r
-  @param[out]  RandBuffer    Pointer to the buffer to store the random result.\r
-\r
-  @retval EFI_SUCCESS        Random words generation succeeded.\r
-  @retval EFI_NOT_READY      Failed to request random words.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-RdRandGetWords (\r
-  IN UINTN         Length,\r
-  OUT UINTN        *RandBuffer\r
-  );\r
-\r
 /**\r
   Calls RDRAND to fill a buffer of arbitrary size with random bytes.\r
 \r
@@ -210,4 +63,4 @@ RdRandGenerateEntropy (
   OUT UINT8        *Entropy\r
   );\r
 \r
-#endif  // __RD_RAND_H__
\ No newline at end of file
+#endif  // __RD_RAND_H__\r
index 98ef3b357fa5f3a6d45903d8e2d54aeb2deb4a75..32c46ab45f127f58a15cfc472754e1e9a7f0e1c1 100644 (file)
@@ -15,6 +15,7 @@
    - EFI_RNG_ALGORITHM_X9_31_AES_GUID         - Unsupported\r
 \r
 Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>\r
+(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
 This program and the accompanying materials \r
 are licensed and made available under the terms and conditions of the BSD License \r
 which accompanies this distribution.  The full text of the license may be found at \r
@@ -156,7 +157,7 @@ RngGetRNG (
     if (RNGValueLength < 32) {\r
       return EFI_INVALID_PARAMETER;\r
     }\r
-    \r
+\r
     Status = RdRandGenerateEntropy (RNGValueLength, RNGValue);\r
     return Status;\r
   }\r
@@ -196,14 +197,6 @@ RngDriverEntry (
   EFI_STATUS    Status;\r
   EFI_HANDLE    Handle;\r
 \r
-  //\r
-  // Verify RdRand support on Platform.\r
-  //\r
-  Status = IsRdRandSupported ();\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
   //\r
   // Install UEFI RNG (Random Number Generator) Protocol\r
   //\r
@@ -214,6 +207,6 @@ RngDriverEntry (
                   &mRngRdRand,\r
                   NULL\r
                   );\r
-  \r
+\r
   return Status;\r
 }\r
index d57c2d8c6f3480f89211b625c4f7d257cf64e791..4d668a170ae172a061d45ced34f5c0cee8ad1d0a 100644 (file)
@@ -9,6 +9,7 @@
 #  Secure Key technology.\r
 #\r
 #  Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.<BR>\r
+#  (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
 #  This program and the accompanying materials\r
 #  are licensed and made available under the terms and conditions of the BSD License\r
 #  which accompanies this distribution. The full text of the license may be found at\r
   AesCore.c\r
   AesCore.h\r
 \r
-[Sources.IA32]\r
-  IA32/RdRandWord.c\r
-  IA32/AsmRdRand.asm\r
-  IA32/GccRdRand.c          | GCC\r
-\r
-[Sources.X64]\r
-  X64/RdRandWord.c\r
-  X64/AsmRdRand.asm\r
-  X64/GccRdRand.c           | GCC\r
-\r
 [Packages]\r
   MdePkg/MdePkg.dec\r
   SecurityPkg/SecurityPkg.dec\r
@@ -62,6 +53,7 @@
   DebugLib     \r
   UefiDriverEntryPoint\r
   TimerLib\r
+  RngLib\r
 \r
 [Guids]\r
   gEfiRngAlgorithmSp80090Ctr256Guid   ## SOMETIMES_PRODUCES    ## GUID        # Unique ID of the algorithm for RNG\r
@@ -77,4 +69,4 @@
   XCODE:*_*_*_CC_FLAGS = -mmmx -msse\r
 \r
 [UserExtensions.TianoCore."ExtraFiles"]\r
-  RngDxeExtra.uni
\ No newline at end of file
+  RngDxeExtra.uni\r
index 6e2ca5c0b7249be9175bf359ae815403ba9437b2..0908b26a7c2461d35ea40f2cdc48db6a491a4284 100644 (file)
@@ -2,6 +2,7 @@
 #  Security Module Package for All Architectures.\r
 #\r
 # Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
+# (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
 # This program and the accompanying materials\r
 # are licensed and made available under the terms and conditions of the BSD License\r
 # which accompanies this distribution. The full text of the license may be found at\r
@@ -61,6 +62,7 @@
   TcgPpVendorLib|SecurityPkg/Library/TcgPpVendorLibNull/TcgPpVendorLibNull.inf\r
   Tcg2PpVendorLib|SecurityPkg/Library/Tcg2PpVendorLibNull/Tcg2PpVendorLibNull.inf\r
   TrEEPpVendorLib|SecurityPkg/Library/TrEEPpVendorLibNull/TrEEPpVendorLibNull.inf\r
+  RngLib|MdePkg/Library/BaseRngLib/BaseRngLib.inf\r
 \r
 [LibraryClasses.common.PEIM]\r
   PeimEntryPoint|MdePkg/Library/PeimEntryPoint/PeimEntryPoint.inf\r
@@ -74,6 +76,7 @@
   Tpm12DeviceLib|SecurityPkg/Library/Tpm12DeviceLibDTpm/Tpm12DeviceLibDTpm.inf\r
   Tpm2DeviceLib|SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.inf\r
   Tcg2PhysicalPresenceLib|SecurityPkg/Library/PeiTcg2PhysicalPresenceLib/PeiTcg2PhysicalPresenceLib.inf\r
+  RngLib|MdePkg/Library/BaseRngLib/BaseRngLib.inf\r
 \r
 [LibraryClasses.common.DXE_DRIVER]\r
   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf\r