]> git.proxmox.com Git - mirror_edk2.git/commitdiff
CryptoPkg: OpensslLib: Use RngLib to generate entropy in rand_pool
authorMatthew Carlson <macarl@microsoft.com>
Thu, 23 Jul 2020 21:59:23 +0000 (14:59 -0700)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Fri, 18 Sep 2020 02:19:21 +0000 (02:19 +0000)
Ref: https://github.com/tianocore/edk2/pull/845
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=1871

Changes OpenSSL to no longer depend on TimerLib and instead use RngLib.
This allows platforms to decide for themsevles what sort of entropy source
they provide to OpenSSL and TlsLib.

Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Xiaoyu Lu <xiaoyux.lu@intel.com>
Acked-by: Ard Biesheuvel <ard.biesheuvel@arm.com>
Reviewed-by: Jiewen Yao <Jiewen.yao@intel.com>
Signed-off-by: Matthew Carlson <matthewfcarlson@gmail.com>
CryptoPkg/CryptoPkg.ci.yaml
CryptoPkg/CryptoPkg.dsc
CryptoPkg/Library/OpensslLib/OpensslLib.inf
CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf
CryptoPkg/Library/OpensslLib/rand_pool.c
CryptoPkg/Library/OpensslLib/rand_pool_noise.c [deleted file]
CryptoPkg/Library/OpensslLib/rand_pool_noise.h [deleted file]
CryptoPkg/Library/OpensslLib/rand_pool_noise_tsc.c [deleted file]

index e2d190a90c51fde696d763bdd7f4cfc3dd352faf..2ade777aadc5ea09c2acebfe41da5d1b57453d01 100644 (file)
@@ -18,7 +18,9 @@
         ],\r
         ## Both file path and directory path are accepted.\r
         "IgnoreFiles": [\r
-            "Library/OpensslLib/openssl"\r
+            "Library/OpensslLib/openssl",\r
+            # this has OpenSSL interfaces that aren't UEFI spec compliant\r
+            "Library/OpensslLib/rand_pool.c"\r
         ]\r
     },\r
     "CompilerPlugin": {\r
index 1af78468a19c912102859c081f6d46547b8b13f0..0490eeb7e22fa82fb15a8028eaacd2c212f9cf83 100644 (file)
@@ -60,6 +60,7 @@
   BaseCryptLib|CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf\r
   TlsLib|CryptoPkg/Library/TlsLibNull/TlsLibNull.inf\r
   HashApiLib|CryptoPkg/Library/BaseHashApiLib/BaseHashApiLib.inf\r
+  RngLib|MdePkg/Library/BaseRngLibNull/BaseRngLibNull.inf\r
 \r
 [LibraryClasses.ARM, LibraryClasses.AARCH64]\r
   #\r
index cc27b8c57cb3a9556c3c3eb1ca6e656fb30d527c..b00bb74ce67ed3cf107c34679854669d4b68fa83 100644 (file)
   $(OPENSSL_PATH)/ssl/statem/statem_local.h\r
 # Autogenerated files list ends here\r
   buildinf.h\r
-  rand_pool_noise.h\r
   ossl_store.c\r
   rand_pool.c\r
 \r
-[Sources.Ia32]\r
-  rand_pool_noise_tsc.c\r
-\r
-[Sources.X64]\r
-  rand_pool_noise_tsc.c\r
-\r
-[Sources.ARM]\r
-  rand_pool_noise.c\r
-\r
-[Sources.AARCH64]\r
-  rand_pool_noise.c\r
-\r
 [Packages]\r
   MdePkg/MdePkg.dec\r
   CryptoPkg/CryptoPkg.dec\r
 [LibraryClasses]\r
   BaseLib\r
   DebugLib\r
-  TimerLib\r
+  RngLib\r
   PrintLib\r
 \r
 [LibraryClasses.ARM]\r
index 616ccd9f62d14273b7b0c9bf1e9336fbb7949c9e..3557711bd85a7493c92c441e89ad7b68cc8d1862 100644 (file)
   $(OPENSSL_PATH)/crypto/x509v3/v3_admis.h\r
 # Autogenerated files list ends here\r
   buildinf.h\r
-  rand_pool_noise.h\r
   ossl_store.c\r
   rand_pool.c\r
 \r
-[Sources.Ia32]\r
-  rand_pool_noise_tsc.c\r
-\r
-[Sources.X64]\r
-  rand_pool_noise_tsc.c\r
-\r
-[Sources.ARM]\r
-  rand_pool_noise.c\r
-\r
-[Sources.AARCH64]\r
-  rand_pool_noise.c\r
-\r
 [Packages]\r
   MdePkg/MdePkg.dec\r
   CryptoPkg/CryptoPkg.dec\r
 [LibraryClasses]\r
   BaseLib\r
   DebugLib\r
-  TimerLib\r
+  RngLib\r
   PrintLib\r
 \r
 [LibraryClasses.ARM]\r
index 9e0179b03490d961ae9fb57d97eabbfacd70cf12..6218ae0c1cd7f78faaeb0f188d2143b424dc5419 100644 (file)
@@ -2,8 +2,8 @@
   OpenSSL_1_1_1b doesn't implement rand_pool_* functions for UEFI.\r
   The file implement these functions.\r
 \r
-Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
-SPDX-License-Identifier: BSD-2-Clause-Patent\r
+  Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -11,53 +11,18 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include <openssl/aes.h>\r
 \r
 #include <Uefi.h>\r
-#include <Library/TimerLib.h>\r
-\r
-#include "rand_pool_noise.h"\r
-\r
-/**\r
-  Get some randomness from low-order bits of GetPerformanceCounter results.\r
-  And combine them to the 64-bit value\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.\r
-**/\r
-STATIC\r
-BOOLEAN\r
-EFIAPI\r
-GetRandNoise64FromPerformanceCounter(\r
-  OUT UINT64      *Rand\r
-  )\r
-{\r
-  UINT32 Index;\r
-  UINT32 *RandPtr;\r
-\r
-  if (NULL == Rand) {\r
-    return FALSE;\r
-  }\r
-\r
-  RandPtr = (UINT32 *) Rand;\r
-\r
-  for (Index = 0; Index < 2; Index ++) {\r
-    *RandPtr = (UINT32) (GetPerformanceCounter () & 0xFF);\r
-    MicroSecondDelay (10);\r
-    RandPtr++;\r
-  }\r
-\r
-  return TRUE;\r
-}\r
+#include <Library/RngLib.h>\r
 \r
 /**\r
   Calls RandomNumber64 to fill\r
   a buffer of arbitrary size with random bytes.\r
+  This is a shim layer to RngLib.\r
 \r
   @param[in]   Length        Size of the buffer, in bytes,  to fill with.\r
   @param[out]  RandBuffer    Pointer to the buffer to store the random result.\r
 \r
-  @retval EFI_SUCCESS        Random bytes generation succeeded.\r
-  @retval EFI_NOT_READY      Failed to request random bytes.\r
+  @retval TRUE        Random bytes generation succeeded.\r
+  @retval FALSE       Failed to request random bytes.\r
 \r
 **/\r
 STATIC\r
@@ -65,7 +30,7 @@ BOOLEAN
 EFIAPI\r
 RandGetBytes (\r
   IN UINTN         Length,\r
-  OUT UINT8        *RandBuffer\r
+  OUT UINT8       *RandBuffer\r
   )\r
 {\r
   BOOLEAN     Ret;\r
@@ -73,17 +38,17 @@ RandGetBytes (
 \r
   Ret = FALSE;\r
 \r
+  if (RandBuffer == NULL) {\r
+    DEBUG((DEBUG_ERROR, "[OPENSSL_RAND_POOL] NULL RandBuffer. No random numbers are generated and your system is not secure\n"));\r
+    ASSERT (RandBuffer != NULL); // Since we can't generate random numbers, we should assert. Otherwise we will just blow up later.\r
+    return Ret;\r
+  }\r
+\r
+\r
   while (Length > 0) {\r
-    //\r
-    // Get random noise from platform.\r
-    // If it failed, fallback to PerformanceCounter\r
-    // If you really care about security, you must override\r
-    // GetRandomNoise64FromPlatform.\r
-    //\r
-    Ret = GetRandomNoise64 (&TempRand);\r
-    if (Ret == FALSE) {\r
-      Ret = GetRandNoise64FromPerformanceCounter (&TempRand);\r
-    }\r
+    // Use RngLib to get random number\r
+    Ret = GetRandomNumber64 (&TempRand);\r
+\r
     if (!Ret) {\r
       return Ret;\r
     }\r
@@ -91,7 +56,8 @@ RandGetBytes (
       *((UINT64*) RandBuffer) = TempRand;\r
       RandBuffer += sizeof (UINT64);\r
       Length -= sizeof (TempRand);\r
-    } else {\r
+    }\r
+    else {\r
       CopyMem (RandBuffer, &TempRand, Length);\r
       Length = 0;\r
     }\r
@@ -100,125 +66,6 @@ RandGetBytes (
   return Ret;\r
 }\r
 \r
-/**\r
-  Creates a 128bit random value that is fully forward and backward prediction resistant,\r
-  suitable for seeding a NIST SP800-90 Compliant.\r
-  This function takes multiple random numbers from PerformanceCounter to ensure reseeding\r
-  and performs AES-CBC-MAC over the data to compute the seed value.\r
-\r
-  @param[out]  SeedBuffer    Pointer to a 128bit buffer to store the random seed.\r
-\r
-  @retval TRUE        Random seed generation succeeded.\r
-  @retval FALSE      Failed to request random bytes.\r
-\r
-**/\r
-STATIC\r
-BOOLEAN\r
-EFIAPI\r
-RandGetSeed128 (\r
-  OUT UINT8        *SeedBuffer\r
-  )\r
-{\r
-  BOOLEAN     Ret;\r
-  UINT8       RandByte[16];\r
-  UINT8       Key[16];\r
-  UINT8       Ffv[16];\r
-  UINT8       Xored[16];\r
-  UINT32      Index;\r
-  UINT32      Index2;\r
-  AES_KEY     AESKey;\r
-\r
-  //\r
-  // Chose an arbitrary key and zero the feed_forward_value (FFV)\r
-  //\r
-  for (Index = 0; Index < 16; Index++) {\r
-    Key[Index] = (UINT8) Index;\r
-    Ffv[Index] = 0;\r
-  }\r
-\r
-  AES_set_encrypt_key (Key, 16 * 8, &AESKey);\r
-\r
-  //\r
-  // Perform CBC_MAC over 32 * 128 bit values, with 10us gaps between 128 bit value\r
-  // The 10us gaps will ensure multiple reseeds within the system time with a large\r
-  // design margin.\r
-  //\r
-  for (Index = 0; Index < 32; Index++) {\r
-    MicroSecondDelay (10);\r
-    Ret = RandGetBytes (16, RandByte);\r
-    if (!Ret) {\r
-      return Ret;\r
-    }\r
-\r
-    //\r
-    // Perform XOR operations on two 128-bit value.\r
-    //\r
-    for (Index2 = 0; Index2 < 16; Index2++) {\r
-      Xored[Index2] = RandByte[Index2] ^ Ffv[Index2];\r
-    }\r
-\r
-    AES_encrypt (Xored, Ffv, &AESKey);\r
-  }\r
-\r
-  for (Index = 0; Index < 16; Index++) {\r
-    SeedBuffer[Index] = Ffv[Index];\r
-  }\r
-\r
-  return Ret;\r
-}\r
-\r
-/**\r
-  Generate high-quality entropy source.\r
-\r
-  @param[in]   Length        Size of the buffer, in bytes, to fill with.\r
-  @param[out]  Entropy       Pointer to the buffer to store the entropy data.\r
-\r
-  @retval EFI_SUCCESS        Entropy generation succeeded.\r
-  @retval EFI_NOT_READY      Failed to request random data.\r
-\r
-**/\r
-STATIC\r
-BOOLEAN\r
-EFIAPI\r
-RandGenerateEntropy (\r
-  IN UINTN         Length,\r
-  OUT UINT8        *Entropy\r
-  )\r
-{\r
-  BOOLEAN     Ret;\r
-  UINTN       BlockCount;\r
-  UINT8       Seed[16];\r
-  UINT8       *Ptr;\r
-\r
-  BlockCount = Length / 16;\r
-  Ptr        = (UINT8 *) Entropy;\r
-\r
-  //\r
-  // Generate high-quality seed for DRBG Entropy\r
-  //\r
-  while (BlockCount > 0) {\r
-    Ret = RandGetSeed128 (Seed);\r
-    if (!Ret) {\r
-      return Ret;\r
-    }\r
-    CopyMem (Ptr, Seed, 16);\r
-\r
-    BlockCount--;\r
-    Ptr = Ptr + 16;\r
-  }\r
-\r
-  //\r
-  // Populate the remained data as request.\r
-  //\r
-  Ret = RandGetSeed128 (Seed);\r
-  if (!Ret) {\r
-    return Ret;\r
-  }\r
-  CopyMem (Ptr, Seed, (Length % 16));\r
-\r
-  return Ret;\r
-}\r
-\r
 /*\r
  * Add random bytes to the pool to acquire requested amount of entropy\r
  *\r
@@ -227,27 +74,31 @@ RandGenerateEntropy (
  *\r
  * This is OpenSSL required interface.\r
  */\r
-size_t rand_pool_acquire_entropy(RAND_POOL *pool)\r
+size_t\r
+rand_pool_acquire_entropy (\r
+  RAND_POOL *pool\r
+  )\r
 {\r
-  BOOLEAN  Ret;\r
-  size_t bytes_needed;\r
-  unsigned char * buffer;\r
+  BOOLEAN        Ret;\r
+  size_t         Bytes_needed;\r
+  unsigned char *Buffer;\r
 \r
-  bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);\r
-  if (bytes_needed > 0) {\r
-    buffer = rand_pool_add_begin(pool, bytes_needed);\r
+  Bytes_needed = rand_pool_bytes_needed (pool, 1 /*entropy_factor*/);\r
+  if (Bytes_needed > 0) {\r
+    Buffer = rand_pool_add_begin (pool, Bytes_needed);\r
 \r
-    if (buffer != NULL) {\r
-      Ret = RandGenerateEntropy(bytes_needed, buffer);\r
+    if (Buffer != NULL) {\r
+      Ret = RandGetBytes (Bytes_needed, Buffer);\r
       if (FALSE == Ret) {\r
-        rand_pool_add_end(pool, 0, 0);\r
-      } else {\r
-        rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);\r
+        rand_pool_add_end (pool, 0, 0);\r
+      }\r
+      else {\r
+        rand_pool_add_end (pool, Bytes_needed, 8 * Bytes_needed);\r
       }\r
     }\r
   }\r
 \r
-  return rand_pool_entropy_available(pool);\r
+  return rand_pool_entropy_available (pool);\r
 }\r
 \r
 /*\r
@@ -255,17 +106,15 @@ size_t rand_pool_acquire_entropy(RAND_POOL *pool)
  *\r
  * This is OpenSSL required interface.\r
  */\r
-int rand_pool_add_nonce_data(RAND_POOL *pool)\r
+int\r
+rand_pool_add_nonce_data (\r
+  RAND_POOL *pool\r
+  )\r
 {\r
-  struct {\r
-    UINT64  Rand;\r
-    UINT64  TimerValue;\r
-  } data = { 0 };\r
-\r
-  RandGetBytes(8, (UINT8 *)&(data.Rand));\r
-  data.TimerValue = GetPerformanceCounter();\r
+  UINT8 data[16];\r
+  RandGetBytes (sizeof(data), data);\r
 \r
-  return rand_pool_add(pool, (unsigned char*)&data, sizeof(data), 0);\r
+  return rand_pool_add (pool, (unsigned char*)&data, sizeof(data), 0);\r
 }\r
 \r
 /*\r
@@ -273,17 +122,15 @@ int rand_pool_add_nonce_data(RAND_POOL *pool)
  *\r
  * This is OpenSSL required interface.\r
  */\r
-int rand_pool_add_additional_data(RAND_POOL *pool)\r
+int\r
+rand_pool_add_additional_data (\r
+  RAND_POOL *pool\r
+  )\r
 {\r
-  struct {\r
-    UINT64  Rand;\r
-    UINT64  TimerValue;\r
-  } data = { 0 };\r
+  UINT8 data[16];\r
+  RandGetBytes (sizeof(data), data);\r
 \r
-  RandGetBytes(8, (UINT8 *)&(data.Rand));\r
-  data.TimerValue = GetPerformanceCounter();\r
-\r
-  return rand_pool_add(pool, (unsigned char*)&data, sizeof(data), 0);\r
+  return rand_pool_add (pool, (unsigned char*)&data, sizeof(data), 0);\r
 }\r
 \r
 /*\r
@@ -291,7 +138,10 @@ int rand_pool_add_additional_data(RAND_POOL *pool)
  *\r
  * This is OpenSSL required interface.\r
  */\r
-int rand_pool_init(void)\r
+int\r
+rand_pool_init (\r
+  VOID\r
+  )\r
 {\r
   return 1;\r
 }\r
@@ -301,7 +151,10 @@ int rand_pool_init(void)
  *\r
  * This is OpenSSL required interface.\r
  */\r
-void rand_pool_cleanup(void)\r
+VOID\r
+rand_pool_cleanup(\r
+  VOID\r
+  )\r
 {\r
 }\r
 \r
@@ -310,7 +163,9 @@ void rand_pool_cleanup(void)
  *\r
  * This is OpenSSL required interface.\r
  */\r
-void rand_pool_keep_random_devices_open(int keep)\r
+VOID\r
+rand_pool_keep_random_devices_open (\r
+  int keep\r
+  )\r
 {\r
 }\r
-\r
diff --git a/CryptoPkg/Library/OpensslLib/rand_pool_noise.c b/CryptoPkg/Library/OpensslLib/rand_pool_noise.c
deleted file mode 100644 (file)
index 212834e..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-/** @file\r
-  Provide rand noise source.\r
-\r
-Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
-SPDX-License-Identifier: BSD-2-Clause-Patent\r
-\r
-**/\r
-\r
-#include <Library/BaseLib.h>\r
-\r
-/**\r
-  Get 64-bit noise source\r
-\r
-  @param[out] Rand         Buffer pointer to store 64-bit noise source\r
-\r
-  @retval FALSE            Failed to generate\r
-**/\r
-BOOLEAN\r
-EFIAPI\r
-GetRandomNoise64 (\r
-  OUT UINT64         *Rand\r
-  )\r
-{\r
-  //\r
-  // Return FALSE will fallback to use PerformanceCounter to\r
-  // generate noise.\r
-  //\r
-  return FALSE;\r
-}\r
diff --git a/CryptoPkg/Library/OpensslLib/rand_pool_noise.h b/CryptoPkg/Library/OpensslLib/rand_pool_noise.h
deleted file mode 100644 (file)
index 75acc68..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-/** @file\r
-  Provide rand noise source.\r
-\r
-Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
-SPDX-License-Identifier: BSD-2-Clause-Patent\r
-\r
-**/\r
-\r
-#ifndef __RAND_POOL_NOISE_H__\r
-#define __RAND_POOL_NOISE_H__\r
-\r
-#include <Uefi/UefiBaseType.h>\r
-\r
-/**\r
-   Get 64-bit noise source.\r
-\r
-   @param[out] Rand         Buffer pointer to store 64-bit noise source\r
-\r
-   @retval TRUE             Get randomness successfully.\r
-   @retval FALSE            Failed to generate\r
-**/\r
-BOOLEAN\r
-EFIAPI\r
-GetRandomNoise64 (\r
-  OUT UINT64         *Rand\r
-  );\r
-\r
-\r
-#endif // __RAND_POOL_NOISE_H__\r
diff --git a/CryptoPkg/Library/OpensslLib/rand_pool_noise_tsc.c b/CryptoPkg/Library/OpensslLib/rand_pool_noise_tsc.c
deleted file mode 100644 (file)
index 4158106..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-/** @file\r
-  Provide rand noise source.\r
-\r
-Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
-SPDX-License-Identifier: BSD-2-Clause-Patent\r
-\r
-**/\r
-\r
-#include <Library/BaseLib.h>\r
-#include <Library/DebugLib.h>\r
-#include <Library/TimerLib.h>\r
-\r
-/**\r
-  Get 64-bit noise source\r
-\r
-  @param[out] Rand         Buffer pointer to store 64-bit noise source\r
-\r
-  @retval TRUE             Get randomness successfully.\r
-  @retval FALSE            Failed to generate\r
-**/\r
-BOOLEAN\r
-EFIAPI\r
-GetRandomNoise64 (\r
-  OUT UINT64         *Rand\r
-  )\r
-{\r
-  UINT32 Index;\r
-  UINT32 *RandPtr;\r
-\r
-  if (NULL == Rand) {\r
-    return FALSE;\r
-  }\r
-\r
-  RandPtr = (UINT32 *)Rand;\r
-\r
-  for (Index = 0; Index < 2; Index ++) {\r
-    *RandPtr = (UINT32) ((AsmReadTsc ()) & 0xFF);\r
-    RandPtr++;\r
-    MicroSecondDelay (10);\r
-  }\r
-\r
-  return TRUE;\r
-}\r