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