TianoCore EDK2 master
Loading...
Searching...
No Matches
RngLibTimer.c
Go to the documentation of this file.
1
10#include <Base.h>
11#include <Uefi.h>
12#include <Library/BaseLib.h>
14#include <Library/DebugLib.h>
15#include <Library/TimerLib.h>
16#include <Guid/RngAlgorithm.h>
17
18#define DEFAULT_DELAY_TIME_IN_MICROSECONDS 10
19
28UINT32
30 VOID
31 )
32{
33 UINT64 CounterHz;
34
35 // Get the counter properties
37 // Make sure we won't divide by zero
38 if (CounterHz == 0) {
39 ASSERT (CounterHz != 0); // Assert so the developer knows something is wrong
40 return DEFAULT_DELAY_TIME_IN_MICROSECONDS;
41 }
42
43 // Calculate the minimum delay based on 1.5 microseconds divided by the hertz.
44 // We calculate the length of a cycle (1/CounterHz) and multiply it by 1.5 microseconds
45 // This ensures that the performance counter has increased by at least one
46 return (UINT32)(MAX (DivU64x64Remainder (1500000, CounterHz, NULL), 1));
47}
48
60BOOLEAN
61EFIAPI
63 OUT UINT16 *Rand
64 )
65{
66 UINT32 Index;
67 UINT8 *RandPtr;
68 UINT32 DelayInMicroSeconds;
69
70 ASSERT (Rand != NULL);
71
72 if (Rand == NULL) {
73 return FALSE;
74 }
75
76 DelayInMicroSeconds = CalculateMinimumDecentDelayInMicroseconds ();
77 RandPtr = (UINT8 *)Rand;
78 // Get 2 bytes of random ish data
79 for (Index = 0; Index < sizeof (UINT16); Index++) {
80 *RandPtr = (UINT8)(GetPerformanceCounter () & 0xFF);
81 // Delay to give the performance counter a chance to change
82 MicroSecondDelay (DelayInMicroSeconds);
83 RandPtr++;
84 }
85
86 return TRUE;
87}
88
100BOOLEAN
101EFIAPI
103 OUT UINT32 *Rand
104 )
105{
106 UINT32 Index;
107 UINT8 *RandPtr;
108 UINT32 DelayInMicroSeconds;
109
110 ASSERT (Rand != NULL);
111
112 if (NULL == Rand) {
113 return FALSE;
114 }
115
116 RandPtr = (UINT8 *)Rand;
117 DelayInMicroSeconds = CalculateMinimumDecentDelayInMicroseconds ();
118 // Get 4 bytes of random ish data
119 for (Index = 0; Index < sizeof (UINT32); Index++) {
120 *RandPtr = (UINT8)(GetPerformanceCounter () & 0xFF);
121 // Delay to give the performance counter a chance to change
122 MicroSecondDelay (DelayInMicroSeconds);
123 RandPtr++;
124 }
125
126 return TRUE;
127}
128
140BOOLEAN
141EFIAPI
143 OUT UINT64 *Rand
144 )
145{
146 UINT32 Index;
147 UINT8 *RandPtr;
148 UINT32 DelayInMicroSeconds;
149
150 ASSERT (Rand != NULL);
151
152 if (NULL == Rand) {
153 return FALSE;
154 }
155
156 RandPtr = (UINT8 *)Rand;
157 DelayInMicroSeconds = CalculateMinimumDecentDelayInMicroseconds ();
158 // Get 8 bytes of random ish data
159 for (Index = 0; Index < sizeof (UINT64); Index++) {
160 *RandPtr = (UINT8)(GetPerformanceCounter () & 0xFF);
161 // Delay to give the performance counter a chance to change
162 MicroSecondDelay (DelayInMicroSeconds);
163 RandPtr++;
164 }
165
166 return TRUE;
167}
168
180BOOLEAN
181EFIAPI
183 OUT UINT64 *Rand
184 )
185{
186 ASSERT (Rand != NULL);
187 // This should take around 80ms
188
189 // Read first 64 bits
190 if (!GetRandomNumber64 (Rand)) {
191 return FALSE;
192 }
193
194 // Read second 64 bits
195 return GetRandomNumber64 (++Rand);
196}
197
209EFIAPI
211 GUID *RngGuid
212 )
213{
214 if (RngGuid == NULL) {
215 return EFI_INVALID_PARAMETER;
216 }
217
218 CopyMem (RngGuid, &gEdkiiRngAlgorithmUnSafe, sizeof (*RngGuid));
219 return EFI_SUCCESS;
220}
UINT64 EFIAPI GetPerformanceCounterProperties(OUT UINT64 *StartValue OPTIONAL, OUT UINT64 *EndValue OPTIONAL)
UINT64 EFIAPI GetPerformanceCounter(VOID)
UINTN EFIAPI MicroSecondDelay(IN UINTN MicroSeconds)
UINT64 EFIAPI DivU64x64Remainder(IN UINT64 Dividend, IN UINT64 Divisor, OUT UINT64 *Remainder OPTIONAL)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
STATIC UINT32 CalculateMinimumDecentDelayInMicroseconds(VOID)
Definition: RngLibTimer.c:29
BOOLEAN EFIAPI GetRandomNumber16(OUT UINT16 *Rand)
Definition: RngLibTimer.c:62
EFI_STATUS EFIAPI GetRngGuid(GUID *RngGuid)
Definition: RngLibTimer.c:210
BOOLEAN EFIAPI GetRandomNumber32(OUT UINT32 *Rand)
Definition: RngLibTimer.c:102
BOOLEAN EFIAPI GetRandomNumber128(OUT UINT64 *Rand)
Definition: RngLibTimer.c:182
BOOLEAN EFIAPI GetRandomNumber64(OUT UINT64 *Rand)
Definition: RngLibTimer.c:142
#define NULL
Definition: Base.h:319
#define STATIC
Definition: Base.h:264
#define VOID
Definition: Base.h:269
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define OUT
Definition: Base.h:284
#define MAX(a, b)
Definition: Base.h:992
UINTN Rand(VOID)
Definition: Support.c:39
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
Definition: Base.h:213