]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Rand/CryptRandTsc.c
2 Pseudorandom Number Generator Wrapper Implementation over OpenSSL.
4 Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "InternalCryptLib.h"
10 #include <openssl/rand.h>
11 #include <openssl/evp.h>
12 #include <Library/PrintLib.h>
15 Sets up the seed value for the pseudorandom number generator.
17 This function sets up the seed value for the pseudorandom number generator.
18 If Seed is not NULL, then the seed passed in is used.
19 If Seed is NULL, then default seed is used.
21 @param[in] Seed Pointer to seed value.
22 If NULL, default seed is used.
23 @param[in] SeedSize Size of seed value.
24 If Seed is NULL, this parameter is ignored.
26 @retval TRUE Pseudorandom number generator has enough entropy for random generation.
27 @retval FALSE Pseudorandom number generator does not have enough entropy for random generation.
33 IN CONST UINT8
*Seed OPTIONAL
,
37 CHAR8 DefaultSeed
[128];
39 if (SeedSize
> INT_MAX
) {
44 // The software PRNG implementation built in OpenSSL depends on message digest algorithm.
45 // Make sure SHA-1 digest algorithm is available here.
47 if (EVP_add_digest (EVP_sha1 ()) == 0) {
52 // Seed the pseudorandom number generator with user-supplied value.
53 // NOTE: A cryptographic PRNG must be seeded with unpredictable data.
56 RAND_seed (Seed
, (UINT32
) SeedSize
);
59 // Retrieve current time.
64 "UEFI Crypto Library default seed (%ld)",
68 RAND_seed (DefaultSeed
, sizeof (DefaultSeed
));
71 if (RAND_status () == 1) {
79 Generates a pseudorandom byte stream of the specified size.
81 If Output is NULL, then return FALSE.
83 @param[out] Output Pointer to buffer to receive random value.
84 @param[in] Size Size of random bytes to generate.
86 @retval TRUE Pseudorandom byte stream generated successfully.
87 @retval FALSE Pseudorandom number generator fails to generate due to lack of entropy.
98 // Check input parameters.
100 if (Output
== NULL
|| Size
> INT_MAX
) {
105 // Generate random data.
107 if (RAND_bytes (Output
, (UINT32
) Size
) != 1) {