TianoCore EDK2 master
Loading...
Searching...
No Matches
BaseRng.c
Go to the documentation of this file.
1
12#include <Library/BaseLib.h>
13#include <Library/DebugLib.h>
14
15#include "BaseRngLibInternals.h"
16
17//
18// Limited retry number when valid random data is returned.
19// Uses the recommended value defined in Section 7.3.17 of "Intel 64 and IA-32
20// Architectures Software Developer's Manual".
21//
22#define GETRANDOM_RETRY_LIMIT 10
23
35BOOLEAN
36EFIAPI
38 OUT UINT16 *Rand
39 )
40{
41 UINT32 Index;
42
43 ASSERT (Rand != NULL);
44
45 if (Rand == NULL) {
46 return FALSE;
47 }
48
49 if (!ArchIsRngSupported ()) {
50 return FALSE;
51 }
52
53 //
54 // A loop to fetch a 16 bit random value with a retry count limit.
55 //
56 for (Index = 0; Index < GETRANDOM_RETRY_LIMIT; Index++) {
58 return TRUE;
59 }
60 }
61
62 return FALSE;
63}
64
76BOOLEAN
77EFIAPI
79 OUT UINT32 *Rand
80 )
81{
82 UINT32 Index;
83
84 ASSERT (Rand != NULL);
85
86 if (Rand == NULL) {
87 return FALSE;
88 }
89
90 if (!ArchIsRngSupported ()) {
91 return FALSE;
92 }
93
94 //
95 // A loop to fetch a 32 bit random value with a retry count limit.
96 //
97 for (Index = 0; Index < GETRANDOM_RETRY_LIMIT; Index++) {
99 return TRUE;
100 }
101 }
102
103 return FALSE;
104}
105
117BOOLEAN
118EFIAPI
120 OUT UINT64 *Rand
121 )
122{
123 UINT32 Index;
124
125 ASSERT (Rand != NULL);
126
127 if (Rand == NULL) {
128 return FALSE;
129 }
130
131 if (!ArchIsRngSupported ()) {
132 return FALSE;
133 }
134
135 //
136 // A loop to fetch a 64 bit random value with a retry count limit.
137 //
138 for (Index = 0; Index < GETRANDOM_RETRY_LIMIT; Index++) {
140 return TRUE;
141 }
142 }
143
144 return FALSE;
145}
146
158BOOLEAN
159EFIAPI
161 OUT UINT64 *Rand
162 )
163{
164 ASSERT (Rand != NULL);
165
166 if (Rand == NULL) {
167 return FALSE;
168 }
169
170 if (!ArchIsRngSupported ()) {
171 return FALSE;
172 }
173
174 //
175 // Read first 64 bits
176 //
177 if (!GetRandomNumber64 (Rand)) {
178 return FALSE;
179 }
180
181 //
182 // Read second 64 bits
183 //
184 return GetRandomNumber64 (++Rand);
185}
BOOLEAN EFIAPI GetRandomNumber16(OUT UINT16 *Rand)
Definition: BaseRng.c:37
BOOLEAN EFIAPI GetRandomNumber32(OUT UINT32 *Rand)
Definition: BaseRng.c:78
BOOLEAN EFIAPI GetRandomNumber128(OUT UINT64 *Rand)
Definition: BaseRng.c:160
BOOLEAN EFIAPI GetRandomNumber64(OUT UINT64 *Rand)
Definition: BaseRng.c:119
#define NULL
Definition: Base.h:319
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define OUT
Definition: Base.h:284
BOOLEAN EFIAPI ArchGetRandomNumber16(OUT UINT16 *Rand)
Definition: Rndr.c:64
BOOLEAN EFIAPI ArchGetRandomNumber64(OUT UINT64 *Rand)
Definition: Rndr.c:114
BOOLEAN EFIAPI ArchGetRandomNumber32(OUT UINT32 *Rand)
Definition: Rndr.c:89
BOOLEAN EFIAPI ArchIsRngSupported(VOID)
Definition: Rndr.c:130
UINTN Rand(VOID)
Definition: Support.c:39