]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Rand/CryptRand.c
1. Remove conducting ASSERT in BaseCryptLib.
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Rand / CryptRand.c
1 /** @file
2 Pseudorandom Number Generator Wrapper Implementation over OpenSSL.
3
4 Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "InternalCryptLib.h"
16 #include <openssl/rand.h>
17
18 //
19 // Default seed for UEFI Crypto Library
20 //
21 CONST UINT8 DefaultSeed[] = "UEFI Crypto Library default seed";
22
23 /**
24 Sets up the seed value for the pseudorandom number generator.
25
26 This function sets up the seed value for the pseudorandom number generator.
27 If Seed is not NULL, then the seed passed in is used.
28 If Seed is NULL, then default seed is used.
29
30 @param[in] Seed Pointer to seed value.
31 If NULL, default seed is used.
32 @param[in] SeedSize Size of seed value.
33 If Seed is NULL, this parameter is ignored.
34
35 @retval TRUE Pseudorandom number generator has enough entropy for random generation.
36 @retval FALSE Pseudorandom number generator does not have enough entropy for random generation.
37
38 **/
39 BOOLEAN
40 EFIAPI
41 RandomSeed (
42 IN CONST UINT8 *Seed OPTIONAL,
43 IN UINTN SeedSize
44 )
45 {
46 //
47 // Seed the pseudorandom number generator with user-supplied value.
48 // NOTE: A cryptographic PRNG must be seeded with unpredictable data.
49 //
50 if (Seed != NULL) {
51 RAND_seed (Seed, (UINT32) SeedSize);
52 } else {
53 RAND_seed (DefaultSeed, sizeof (DefaultSeed));
54 }
55
56 return TRUE;
57 }
58
59 /**
60 Generates a pseudorandom byte stream of the specified size.
61
62 If Output is NULL, then return FALSE.
63
64 @param[out] Output Pointer to buffer to receive random value.
65 @param[in] Size Size of randome bytes to generate.
66
67 @retval TRUE Pseudorandom byte stream generated successfully.
68 @retval FALSE Pseudorandom number generator fails to generate due to lack of entropy.
69
70 **/
71 BOOLEAN
72 EFIAPI
73 RandomBytes (
74 OUT UINT8 *Output,
75 IN UINTN Size
76 )
77 {
78 //
79 // Check input parameters.
80 //
81 if (Output == NULL) {
82 return FALSE;
83 }
84
85 //
86 // Generate random data.
87 //
88 if (RAND_bytes (Output, (UINT32) Size) != 1) {
89 return FALSE;
90 }
91
92 return TRUE;
93 }