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