]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Rand/CryptRandTsc.c
Fix several issues in BaseCryptLib:
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Rand / CryptRandTsc.c
CommitLineData
7ce960e7 1/** @file\r
2 Pseudorandom Number Generator Wrapper Implementation over OpenSSL.\r
3\r
4Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r
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#include <Library/PrintLib.h>\r
18\r
19/**\r
20 Sets up the seed value for the pseudorandom number generator.\r
21\r
22 This function sets up the seed value for the pseudorandom number generator.\r
23 If Seed is not NULL, then the seed passed in is used.\r
24 If Seed is NULL, then default seed is used.\r
25\r
26 @param[in] Seed Pointer to seed value.\r
27 If NULL, default seed is used.\r
28 @param[in] SeedSize Size of seed value.\r
29 If Seed is NULL, this parameter is ignored.\r
30\r
31 @retval TRUE Pseudorandom number generator has enough entropy for random generation.\r
32 @retval FALSE Pseudorandom number generator does not have enough entropy for random generation.\r
33\r
34**/\r
35BOOLEAN\r
36EFIAPI\r
37RandomSeed (\r
38 IN CONST UINT8 *Seed OPTIONAL,\r
39 IN UINTN SeedSize\r
40 )\r
41{\r
42 CHAR8 DefaultSeed[128];\r
43\r
dda39f3a 44 if (SeedSize > INT_MAX) {\r
45 return FALSE;\r
46 }\r
47\r
7ce960e7 48 //\r
49 // Seed the pseudorandom number generator with user-supplied value.\r
50 // NOTE: A cryptographic PRNG must be seeded with unpredictable data.\r
51 //\r
52 if (Seed != NULL) {\r
53 RAND_seed (Seed, (UINT32) SeedSize);\r
54 } else {\r
55 //\r
56 // Retrieve current time.\r
57 //\r
58 AsciiSPrint (\r
59 DefaultSeed,\r
60 sizeof (DefaultSeed),\r
61 "UEFI Crypto Library default seed (%ld)",\r
62 AsmReadTsc ()\r
63 ); \r
64\r
65 RAND_seed (DefaultSeed, sizeof (DefaultSeed));\r
66 }\r
67\r
68 return TRUE;\r
69}\r
70\r
71/**\r
72 Generates a pseudorandom byte stream of the specified size.\r
73\r
74 If Output is NULL, then return FALSE.\r
75\r
76 @param[out] Output Pointer to buffer to receive random value.\r
77 @param[in] Size Size of randome bytes to generate.\r
78\r
79 @retval TRUE Pseudorandom byte stream generated successfully.\r
80 @retval FALSE Pseudorandom number generator fails to generate due to lack of entropy.\r
81\r
82**/\r
83BOOLEAN\r
84EFIAPI\r
85RandomBytes (\r
86 OUT UINT8 *Output,\r
87 IN UINTN Size\r
88 )\r
89{\r
90 //\r
91 // Check input parameters.\r
92 //\r
dda39f3a 93 if (Output == NULL || Size > INT_MAX) {\r
7ce960e7 94 return FALSE;\r
95 }\r
96\r
97 //\r
98 // Generate random data.\r
99 //\r
100 if (RAND_bytes (Output, (UINT32) Size) != 1) {\r
101 return FALSE;\r
102 }\r
103\r
104 return TRUE;\r
105}\r