]>
Commit | Line | Data |
---|---|---|
a8c44645 | 1 | /** @file\r |
2 | Pseudorandom Number Generator Wrapper Implementation over OpenSSL.\r | |
3 | \r | |
16d2c32c | 4 | Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r |
a8c44645 | 5 | This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT 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 | |
21 | CONST 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 | |
39 | BOOLEAN\r | |
40 | EFIAPI\r | |
41 | RandomSeed (\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 | |
71 | BOOLEAN\r | |
72 | EFIAPI\r | |
73 | RandomBytes (\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 |