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