TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptRand.c
Go to the documentation of this file.
1
9#include "InternalCryptLib.h"
10#include <openssl/rand.h>
11#include <openssl/evp.h>
12
13//
14// Default seed for UEFI Crypto Library
15//
16CONST UINT8 DefaultSeed[] = "UEFI Crypto Library default seed";
17
34BOOLEAN
35EFIAPI
37 IN CONST UINT8 *Seed OPTIONAL,
38 IN UINTN SeedSize
39 )
40{
41 if (SeedSize > INT_MAX) {
42 return FALSE;
43 }
44
45 //
46 // The software PRNG implementation built in OpenSSL depends on message digest algorithm.
47 // Make sure SHA-1 digest algorithm is available here.
48 //
49 if (EVP_add_digest (EVP_sha1 ()) == 0) {
50 return FALSE;
51 }
52
53 //
54 // Seed the pseudorandom number generator with user-supplied value.
55 // NOTE: A cryptographic PRNG must be seeded with unpredictable data.
56 //
57 if (Seed != NULL) {
58 RAND_seed (Seed, (UINT32)SeedSize);
59 } else {
60 RAND_seed (DefaultSeed, sizeof (DefaultSeed));
61 }
62
63 if (RAND_status () == 1) {
64 return TRUE;
65 }
66
67 return FALSE;
68}
69
82BOOLEAN
83EFIAPI
85 OUT UINT8 *Output,
86 IN UINTN Size
87 )
88{
89 //
90 // Check input parameters.
91 //
92 if ((Output == NULL) || (Size > INT_MAX)) {
93 return FALSE;
94 }
95
96 //
97 // Generate random data.
98 //
99 if (RAND_bytes (Output, (UINT32)Size) != 1) {
100 return FALSE;
101 }
102
103 return TRUE;
104}
UINT64 UINTN
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
BOOLEAN EFIAPI RandomBytes(OUT UINT8 *Output, IN UINTN Size)
Definition: CryptRand.c:84
BOOLEAN EFIAPI RandomSeed(IN CONST UINT8 *Seed OPTIONAL, IN UINTN SeedSize)
Definition: CryptRand.c:36