]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Rand/CryptRand.c
Fix several issues 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
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
60 return TRUE;\r
61}\r
62\r
63/**\r
64 Generates a pseudorandom byte stream of the specified size.\r
65\r
16d2c32c 66 If Output is NULL, then return FALSE.\r
a8c44645 67\r
68 @param[out] Output Pointer to buffer to receive random value.\r
69 @param[in] Size Size of randome bytes to generate.\r
70\r
71 @retval TRUE Pseudorandom byte stream generated successfully.\r
72 @retval FALSE Pseudorandom number generator fails to generate due to lack of entropy.\r
73\r
74**/\r
75BOOLEAN\r
76EFIAPI\r
77RandomBytes (\r
78 OUT UINT8 *Output,\r
79 IN UINTN Size\r
80 )\r
81{\r
16d2c32c 82 //\r
83 // Check input parameters.\r
84 //\r
dda39f3a 85 if (Output == NULL || Size > INT_MAX) {\r
16d2c32c 86 return FALSE;\r
87 }\r
a8c44645 88\r
89 //\r
90 // Generate random data.\r
91 //\r
92 if (RAND_bytes (Output, (UINT32) Size) != 1) {\r
93 return FALSE;\r
94 }\r
95\r
96 return TRUE;\r
97}\r