]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Rand/CryptRand.c
1. Remove conducting ASSERT in BaseCryptLib.
[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
46 //\r
47 // Seed the pseudorandom number generator with user-supplied value.\r
48 // NOTE: A cryptographic PRNG must be seeded with unpredictable data.\r
49 //\r
50 if (Seed != NULL) {\r
51 RAND_seed (Seed, (UINT32) SeedSize);\r
52 } else {\r
53 RAND_seed (DefaultSeed, sizeof (DefaultSeed));\r
54 }\r
55\r
56 return TRUE;\r
57}\r
58\r
59/**\r
60 Generates a pseudorandom byte stream of the specified size.\r
61\r
16d2c32c 62 If Output is NULL, then return FALSE.\r
a8c44645 63\r
64 @param[out] Output Pointer to buffer to receive random value.\r
65 @param[in] Size Size of randome bytes to generate.\r
66\r
67 @retval TRUE Pseudorandom byte stream generated successfully.\r
68 @retval FALSE Pseudorandom number generator fails to generate due to lack of entropy.\r
69\r
70**/\r
71BOOLEAN\r
72EFIAPI\r
73RandomBytes (\r
74 OUT UINT8 *Output,\r
75 IN UINTN Size\r
76 )\r
77{\r
16d2c32c 78 //\r
79 // Check input parameters.\r
80 //\r
81 if (Output == NULL) {\r
82 return FALSE;\r
83 }\r
a8c44645 84\r
85 //\r
86 // Generate random data.\r
87 //\r
88 if (RAND_bytes (Output, (UINT32) Size) != 1) {\r
89 return FALSE;\r
90 }\r
91\r
92 return TRUE;\r
93}\r