/edk2/CryptoPkg/Library/BaseCryptLib/Rand/CryptRand.c

https://gitlab.com/envieidoc/Clover · C · 110 lines · 42 code · 10 blank · 58 comment · 13 complexity · 9c213c41747dc3b8a6587d97665b65c1 MD5 · raw file

  1. /** @file
  2. Pseudorandom Number Generator Wrapper Implementation over OpenSSL.
  3. Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.<BR>
  4. This program and the accompanying materials
  5. are licensed and made available under the terms and conditions of the BSD License
  6. which accompanies this distribution. The full text of the license may be found at
  7. http://opensource.org/licenses/bsd-license.php
  8. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  10. **/
  11. #include "InternalCryptLib.h"
  12. #include <openssl/rand.h>
  13. #include <openssl/evp.h>
  14. //
  15. // Default seed for UEFI Crypto Library
  16. //
  17. CONST UINT8 DefaultSeed[] = "UEFI Crypto Library default seed";
  18. /**
  19. Sets up the seed value for the pseudorandom number generator.
  20. This function sets up the seed value for the pseudorandom number generator.
  21. If Seed is not NULL, then the seed passed in is used.
  22. If Seed is NULL, then default seed is used.
  23. @param[in] Seed Pointer to seed value.
  24. If NULL, default seed is used.
  25. @param[in] SeedSize Size of seed value.
  26. If Seed is NULL, this parameter is ignored.
  27. @retval TRUE Pseudorandom number generator has enough entropy for random generation.
  28. @retval FALSE Pseudorandom number generator does not have enough entropy for random generation.
  29. **/
  30. BOOLEAN
  31. EFIAPI
  32. RandomSeed (
  33. IN CONST UINT8 *Seed OPTIONAL,
  34. IN UINTN SeedSize
  35. )
  36. {
  37. if (SeedSize > INT_MAX) {
  38. return FALSE;
  39. }
  40. //
  41. // The software PRNG implementation built in OpenSSL depends on message digest algorithm.
  42. // Make sure SHA-1 digest algorithm is available here.
  43. //
  44. if (EVP_add_digest (EVP_sha1 ()) == 0) {
  45. return FALSE;
  46. }
  47. //
  48. // Seed the pseudorandom number generator with user-supplied value.
  49. // NOTE: A cryptographic PRNG must be seeded with unpredictable data.
  50. //
  51. if (Seed != NULL) {
  52. RAND_seed (Seed, (UINT32) SeedSize);
  53. } else {
  54. RAND_seed (DefaultSeed, sizeof (DefaultSeed));
  55. }
  56. if (RAND_status () == 1) {
  57. return TRUE;
  58. }
  59. return FALSE;
  60. }
  61. /**
  62. Generates a pseudorandom byte stream of the specified size.
  63. If Output is NULL, then return FALSE.
  64. @param[out] Output Pointer to buffer to receive random value.
  65. @param[in] Size Size of randome bytes to generate.
  66. @retval TRUE Pseudorandom byte stream generated successfully.
  67. @retval FALSE Pseudorandom number generator fails to generate due to lack of entropy.
  68. **/
  69. BOOLEAN
  70. EFIAPI
  71. RandomBytes (
  72. OUT UINT8 *Output,
  73. IN UINTN Size
  74. )
  75. {
  76. //
  77. // Check input parameters.
  78. //
  79. if (Output == NULL || Size > INT_MAX) {
  80. return FALSE;
  81. }
  82. //
  83. // Generate random data.
  84. //
  85. if (RAND_bytes (Output, (UINT32) Size) != 1) {
  86. return FALSE;
  87. }
  88. return TRUE;
  89. }