]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Use current time value as default random seed in BaseCryptLib.
authortye1 <tye1@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 10 May 2012 07:58:02 +0000 (07:58 +0000)
committertye1 <tye1@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 10 May 2012 07:58:02 +0000 (07:58 +0000)
Signed-off by: Ye Ting <ting.ye@intel.com>
Reviewed-by: Yao Jiewen <jiewen.yao@intel.com>
Reviewed-by: Sun Rui <rui.sun@intel.com>
Reviewed-by: Long Qin <qin.long@intel.com>
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@13300 6f19259b-4bc3-4df7-8a09-765794883524

CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
CryptoPkg/Library/BaseCryptLib/Rand/CryptRandItc.c [new file with mode: 0644]
CryptoPkg/Library/BaseCryptLib/Rand/CryptRandTsc.c [new file with mode: 0644]
CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf

index c21ee01c4b853d82620ece12b5b61d34549f6676..84faecd873070c84a6e37d5d99f4b8f62bd80062 100644 (file)
@@ -1,7 +1,7 @@
 ## @file\r
 #  Cryptographic Library Instance for DXE_DRIVER.\r
 #\r
 ## @file\r
 #  Cryptographic Library Instance for DXE_DRIVER.\r
 #\r
-#  Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
+#  Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<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
 #  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
@@ -37,7 +37,6 @@
   Cipher/CryptAes.c\r
   Cipher/CryptTdes.c\r
   Cipher/CryptArc4.c\r
   Cipher/CryptAes.c\r
   Cipher/CryptTdes.c\r
   Cipher/CryptArc4.c\r
-  Rand/CryptRand.c\r
   Pk/CryptRsa.c\r
   Pk/CryptPkcs7.c\r
   Pk/CryptDh.c\r
   Pk/CryptRsa.c\r
   Pk/CryptPkcs7.c\r
   Pk/CryptDh.c\r
   SysCall/Ia32/MathLShiftS64.S      | GCC\r
   SysCall/Ia32/MathRShiftU64.S      | GCC\r
 \r
   SysCall/Ia32/MathLShiftS64.S      | GCC\r
   SysCall/Ia32/MathRShiftU64.S      | GCC\r
 \r
+  Rand/CryptRandTsc.c\r
+\r
+[Sources.X64]\r
+  Rand/CryptRandTsc.c\r
+\r
+[Sources.IPF]\r
+  Rand/CryptRandItc.c\r
+\r
+[Sources.ARM]\r
+  Rand/CryptRand.c\r
+\r
 [Packages]\r
   MdePkg/MdePkg.dec\r
   CryptoPkg/CryptoPkg.dec\r
 [Packages]\r
   MdePkg/MdePkg.dec\r
   CryptoPkg/CryptoPkg.dec\r
@@ -79,6 +89,7 @@
   DebugLib\r
   OpensslLib\r
   IntrinsicLib\r
   DebugLib\r
   OpensslLib\r
   IntrinsicLib\r
+  PrintLib\r
 \r
 #\r
 # Remove these [BuildOptions] after this library is cleaned up\r
 \r
 #\r
 # Remove these [BuildOptions] after this library is cleaned up\r
diff --git a/CryptoPkg/Library/BaseCryptLib/Rand/CryptRandItc.c b/CryptoPkg/Library/BaseCryptLib/Rand/CryptRandItc.c
new file mode 100644 (file)
index 0000000..881141c
--- /dev/null
@@ -0,0 +1,101 @@
+/** @file\r
+  Pseudorandom Number Generator Wrapper Implementation over OpenSSL.\r
+\r
+Copyright (c) 2012, Intel Corporation. All rights reserved.<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
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include "InternalCryptLib.h"\r
+#include <openssl/rand.h>\r
+#include <Library/PrintLib.h>\r
+\r
+/**\r
+  Sets up the seed value for the pseudorandom number generator.\r
+\r
+  This function sets up the seed value for the pseudorandom number generator.\r
+  If Seed is not NULL, then the seed passed in is used.\r
+  If Seed is NULL, then default seed is used.\r
+\r
+  @param[in]  Seed      Pointer to seed value.\r
+                        If NULL, default seed is used.\r
+  @param[in]  SeedSize  Size of seed value.\r
+                        If Seed is NULL, this parameter is ignored.\r
+\r
+  @retval TRUE   Pseudorandom number generator has enough entropy for random generation.\r
+  @retval FALSE  Pseudorandom number generator does not have enough entropy for random generation.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RandomSeed (\r
+  IN  CONST  UINT8  *Seed  OPTIONAL,\r
+  IN  UINTN         SeedSize\r
+  )\r
+{\r
+  CHAR8  DefaultSeed[128];\r
+\r
+  //\r
+  // Seed the pseudorandom number generator with user-supplied value.\r
+  // NOTE: A cryptographic PRNG must be seeded with unpredictable data.\r
+  //\r
+  if (Seed != NULL) {\r
+    RAND_seed (Seed, (UINT32) SeedSize);\r
+  } else {\r
+    //\r
+    // Retrieve current time.\r
+    //\r
+    AsciiSPrint (\r
+      DefaultSeed,\r
+      sizeof (DefaultSeed),\r
+      "UEFI Crypto Library default seed (%ld)",\r
+      AsmReadItc ()\r
+      ); \r
+\r
+    RAND_seed (DefaultSeed, sizeof (DefaultSeed));\r
+  }\r
+\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Generates a pseudorandom byte stream of the specified size.\r
+\r
+  If Output is NULL, then return FALSE.\r
+\r
+  @param[out]  Output  Pointer to buffer to receive random value.\r
+  @param[in]   Size    Size of randome bytes to generate.\r
+\r
+  @retval TRUE   Pseudorandom byte stream generated successfully.\r
+  @retval FALSE  Pseudorandom number generator fails to generate due to lack of entropy.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RandomBytes (\r
+  OUT  UINT8  *Output,\r
+  IN   UINTN  Size\r
+  )\r
+{\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (Output == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // Generate random data.\r
+  //\r
+  if (RAND_bytes (Output, (UINT32) Size) != 1) {\r
+    return FALSE;\r
+  }\r
+\r
+  return TRUE;\r
+}\r
diff --git a/CryptoPkg/Library/BaseCryptLib/Rand/CryptRandTsc.c b/CryptoPkg/Library/BaseCryptLib/Rand/CryptRandTsc.c
new file mode 100644 (file)
index 0000000..bb8783d
--- /dev/null
@@ -0,0 +1,101 @@
+/** @file\r
+  Pseudorandom Number Generator Wrapper Implementation over OpenSSL.\r
+\r
+Copyright (c) 2012, Intel Corporation. All rights reserved.<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
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include "InternalCryptLib.h"\r
+#include <openssl/rand.h>\r
+#include <Library/PrintLib.h>\r
+\r
+/**\r
+  Sets up the seed value for the pseudorandom number generator.\r
+\r
+  This function sets up the seed value for the pseudorandom number generator.\r
+  If Seed is not NULL, then the seed passed in is used.\r
+  If Seed is NULL, then default seed is used.\r
+\r
+  @param[in]  Seed      Pointer to seed value.\r
+                        If NULL, default seed is used.\r
+  @param[in]  SeedSize  Size of seed value.\r
+                        If Seed is NULL, this parameter is ignored.\r
+\r
+  @retval TRUE   Pseudorandom number generator has enough entropy for random generation.\r
+  @retval FALSE  Pseudorandom number generator does not have enough entropy for random generation.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RandomSeed (\r
+  IN  CONST  UINT8  *Seed  OPTIONAL,\r
+  IN  UINTN         SeedSize\r
+  )\r
+{\r
+  CHAR8  DefaultSeed[128];\r
+\r
+  //\r
+  // Seed the pseudorandom number generator with user-supplied value.\r
+  // NOTE: A cryptographic PRNG must be seeded with unpredictable data.\r
+  //\r
+  if (Seed != NULL) {\r
+    RAND_seed (Seed, (UINT32) SeedSize);\r
+  } else {\r
+    //\r
+    // Retrieve current time.\r
+    //\r
+    AsciiSPrint (\r
+      DefaultSeed,\r
+      sizeof (DefaultSeed),\r
+      "UEFI Crypto Library default seed (%ld)",\r
+      AsmReadTsc ()\r
+      ); \r
+\r
+    RAND_seed (DefaultSeed, sizeof (DefaultSeed));\r
+  }\r
+\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Generates a pseudorandom byte stream of the specified size.\r
+\r
+  If Output is NULL, then return FALSE.\r
+\r
+  @param[out]  Output  Pointer to buffer to receive random value.\r
+  @param[in]   Size    Size of randome bytes to generate.\r
+\r
+  @retval TRUE   Pseudorandom byte stream generated successfully.\r
+  @retval FALSE  Pseudorandom number generator fails to generate due to lack of entropy.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RandomBytes (\r
+  OUT  UINT8  *Output,\r
+  IN   UINTN  Size\r
+  )\r
+{\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (Output == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // Generate random data.\r
+  //\r
+  if (RAND_bytes (Output, (UINT32) Size) != 1) {\r
+    return FALSE;\r
+  }\r
+\r
+  return TRUE;\r
+}\r
index 99b4295c04e44ff49db44b3f6d548f275cca6466..87f3b93fb9ac77f0bdb12133d543c66ec4422fcc 100644 (file)
@@ -31,7 +31,6 @@
   Hash/CryptMd5.c\r
   Hash/CryptSha1.c\r
   Hash/CryptSha256.c\r
   Hash/CryptMd5.c\r
   Hash/CryptSha1.c\r
   Hash/CryptSha256.c\r
-  Rand/CryptRand.c\r
   Pk/CryptRsa.c\r
   Pk/CryptPkcs7.c\r
   Pk/CryptX509.c\r
   Pk/CryptRsa.c\r
   Pk/CryptPkcs7.c\r
   Pk/CryptX509.c\r
   SysCall/Ia32/MathLShiftS64.S      | GCC\r
   SysCall/Ia32/MathRShiftU64.S      | GCC\r
 \r
   SysCall/Ia32/MathLShiftS64.S      | GCC\r
   SysCall/Ia32/MathRShiftU64.S      | GCC\r
 \r
+  Rand/CryptRandTsc.c\r
+\r
+[Sources.X64]\r
+  Rand/CryptRandTsc.c\r
+\r
+[Sources.IPF]\r
+  Rand/CryptRandItc.c\r
+\r
+[Sources.ARM]\r
+  Rand/CryptRand.c\r
+\r
 [Packages]\r
   MdePkg/MdePkg.dec\r
   CryptoPkg/CryptoPkg.dec\r
 [Packages]\r
   MdePkg/MdePkg.dec\r
   CryptoPkg/CryptoPkg.dec\r
@@ -73,6 +83,7 @@
   DebugLib\r
   OpensslLib\r
   IntrinsicLib\r
   DebugLib\r
   OpensslLib\r
   IntrinsicLib\r
+  PrintLib\r
 \r
 #\r
 # Remove these [BuildOptions] after this library is cleaned up\r
 \r
 #\r
 # Remove these [BuildOptions] after this library is cleaned up\r
index a6eea164a149c73aab6553e17aa9cb570436a65d..03ed82f68703ef419366207620437f4eecdbe182 100644 (file)
@@ -31,7 +31,6 @@
   Hash/CryptMd5.c\r
   Hash/CryptSha1.c\r
   Hash/CryptSha256.c\r
   Hash/CryptMd5.c\r
   Hash/CryptSha1.c\r
   Hash/CryptSha256.c\r
-  Rand/CryptRand.c\r
   Pk/CryptRsa.c\r
   Pk/CryptPkcs7.c\r
   Pk/CryptX509.c\r
   Pk/CryptRsa.c\r
   Pk/CryptPkcs7.c\r
   Pk/CryptX509.c\r
   SysCall/Ia32/MathLShiftS64.S      | GCC\r
   SysCall/Ia32/MathRShiftU64.S      | GCC\r
 \r
   SysCall/Ia32/MathLShiftS64.S      | GCC\r
   SysCall/Ia32/MathRShiftU64.S      | GCC\r
 \r
+  Rand/CryptRandTsc.c\r
+\r
+[Sources.X64]\r
+  Rand/CryptRandTsc.c\r
+\r
+[Sources.IPF]\r
+  Rand/CryptRandItc.c\r
+\r
+[Sources.ARM]\r
+  Rand/CryptRand.c\r
+\r
 [Packages]\r
   MdePkg/MdePkg.dec\r
   CryptoPkg/CryptoPkg.dec\r
 [Packages]\r
   MdePkg/MdePkg.dec\r
   CryptoPkg/CryptoPkg.dec\r
@@ -71,6 +81,7 @@
   MemoryAllocationLib\r
   OpensslLib\r
   IntrinsicLib\r
   MemoryAllocationLib\r
   OpensslLib\r
   IntrinsicLib\r
+  PrintLib\r
 \r
 #\r
 # Remove these [BuildOptions] after this library is cleaned up\r
 \r
 #\r
 # Remove these [BuildOptions] after this library is cleaned up\r