TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptRandTsc.c
Go to the documentation of this file.
1
9#include "InternalCryptLib.h"
10#include <openssl/rand.h>
11#include <openssl/evp.h>
12#include <Library/PrintLib.h>
13
30BOOLEAN
31EFIAPI
33 IN CONST UINT8 *Seed OPTIONAL,
34 IN UINTN SeedSize
35 )
36{
37 CHAR8 DefaultSeed[128];
38
39 if (SeedSize > INT_MAX) {
40 return FALSE;
41 }
42
43 //
44 // Seed the pseudorandom number generator with user-supplied value.
45 // NOTE: A cryptographic PRNG must be seeded with unpredictable data.
46 //
47 if (Seed != NULL) {
48 RAND_seed (Seed, (UINT32)SeedSize);
49 } else {
50 //
51 // Retrieve current time.
52 //
54 DefaultSeed,
55 sizeof (DefaultSeed),
56 "UEFI Crypto Library default seed (%ld)",
57 AsmReadTsc ()
58 );
59
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
UINT64 EFIAPI AsmReadTsc(VOID)
Definition: GccInline.c:555
UINTN EFIAPI AsciiSPrint(OUT CHAR8 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR8 *FormatString,...)
Definition: PrintLib.c:813
#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: CryptRandTsc.c:84
BOOLEAN EFIAPI RandomSeed(IN CONST UINT8 *Seed OPTIONAL, IN UINTN SeedSize)
Definition: CryptRandTsc.c:32