1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/** @file
OpenSSL_1_1_1b doesn't implement rand_pool_* functions for UEFI.
The file implement these functions.
Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "internal/rand_int.h"
#include <openssl/aes.h>
#include <Uefi.h>
#include <Library/TimerLib.h>
#include "rand_pool_noise.h"
/**
Get some randomness from low-order bits of GetPerformanceCounter results.
And combine them to the 64-bit value
@param[out] Rand Buffer pointer to store the 64-bit random value.
@retval TRUE Random number generated successfully.
@retval FALSE Failed to generate.
**/
STATIC
BOOLEAN
EFIAPI
GetRandNoise64FromPerformanceCounter(
OUT UINT64 *Rand
)
{
UINT32 Index;
UINT32 *RandPtr;
if (NULL == Rand) {
return FALSE;
}
RandPtr = (UINT32 *) Rand;
for (Index = 0; Index < 2; Index ++) {
*RandPtr = (UINT32) (GetPerformanceCounter () & 0xFF);
MicroSecondDelay (10);
RandPtr++;
}
return TRUE;
}
/**
Calls RandomNumber64 to fill
a buffer of arbitrary size with random bytes.
@param[in] Length Size of the buffer, in bytes, to fill with.
@param[out] RandBuffer Pointer to the buffer to store the random result.
@retval EFI_SUCCESS Random bytes generation succeeded.
@retval EFI_NOT_READY Failed to request random bytes.
**/
STATIC
BOOLEAN
EFIAPI
RandGetBytes (
IN UINTN Length,
OUT UINT8 *RandBuffer
)
{
BOOLEAN Ret;
UINT64 TempRand;
Ret = FALSE;
while (Length > 0) {
//
// Get random noise from platform.
// If it failed, fallback to PerformanceCounter
// If you really care about security, you must override
// GetRandomNoise64FromPlatform.
//
Ret = GetRandomNoise64 (&TempRand);
if (Ret == FALSE) {
Ret = GetRandNoise64FromPerformanceCounter (&TempRand);
}
if (!Ret) {
return Ret;
}
if (Length >= sizeof (TempRand)) {
*((UINT64*) RandBuffer) = TempRand;
RandBuffer += sizeof (UINT64);
Length -= sizeof (TempRand);
} else {
CopyMem (RandBuffer, &TempRand, Length);
Length = 0;
}
}
return Ret;
}
/**
Creates a 128bit random value that is fully forward and backward prediction resistant,
suitable for seeding a NIST SP800-90 Compliant.
This function takes multiple random numbers from PerformanceCounter to ensure reseeding
and performs AES-CBC-MAC over the data to compute the seed value.
@param[out] SeedBuffer Pointer to a 128bit buffer to store the random seed.
@retval TRUE Random seed generation succeeded.
@retval FALSE Failed to request random bytes.
**/
STATIC
BOOLEAN
EFIAPI
RandGetSeed128 (
OUT UINT8 *SeedBuffer
)
{
BOOLEAN Ret;
UINT8 RandByte[16];
UINT8 Key[16];
UINT8 Ffv[16];
UINT8 Xored[16];
UINT32 Index;
UINT32 Index2;
AES_KEY AESKey;
//
// Chose an arbitrary key and zero the feed_forward_value (FFV)
//
for (Index = 0; Index < 16; Index++) {
Key[Index] = (UINT8) Index;
Ffv[Index] = 0;
}
AES_set_encrypt_key (Key, 16 * 8, &AESKey);
//
// Perform CBC_MAC over 32 * 128 bit values, with 10us gaps between 128 bit value
// The 10us gaps will ensure multiple reseeds within the system time with a large
// design margin.
//
for (Index = 0; Index < 32; Index++) {
MicroSecondDelay (10);
Ret = RandGetBytes (16, RandByte);
if (!Ret) {
return Ret;
}
//
// Perform XOR operations on two 128-bit value.
//
for (Index2 = 0; Index2 < 16; Index2++) {
Xored[Index2] = RandByte[Index2] ^ Ffv[Index2];
}
AES_encrypt (Xored, Ffv, &AESKey);
}
for (Index = 0; Index < 16; Index++) {
SeedBuffer[Index] = Ffv[Index];
}
return Ret;
}
/**
Generate high-quality entropy source.
@param[in] Length Size of the buffer, in bytes, to fill with.
@param[out] Entropy Pointer to the buffer to store the entropy data.
@retval EFI_SUCCESS Entropy generation succeeded.
@retval EFI_NOT_READY Failed to request random data.
**/
STATIC
BOOLEAN
EFIAPI
RandGenerateEntropy (
IN UINTN Length,
OUT UINT8 *Entropy
)
{
BOOLEAN Ret;
UINTN BlockCount;
UINT8 Seed[16];
UINT8 *Ptr;
BlockCount = Length / 16;
Ptr = (UINT8 *) Entropy;
//
// Generate high-quality seed for DRBG Entropy
//
while (BlockCount > 0) {
Ret = RandGetSeed128 (Seed);
if (!Ret) {
return Ret;
}
CopyMem (Ptr, Seed, 16);
BlockCount--;
Ptr = Ptr + 16;
}
//
// Populate the remained data as request.
//
Ret = RandGetSeed128 (Seed);
if (!Ret) {
return Ret;
}
CopyMem (Ptr, Seed, (Length % 16));
return Ret;
}
/*
* Add random bytes to the pool to acquire requested amount of entropy
*
* This function is platform specific and tries to acquire the requested
* amount of entropy by polling platform specific entropy sources.
*
* This is OpenSSL required interface.
*/
size_t rand_pool_acquire_entropy(RAND_POOL *pool)
{
BOOLEAN Ret;
size_t bytes_needed;
unsigned char * buffer;
bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
if (bytes_needed > 0) {
buffer = rand_pool_add_begin(pool, bytes_needed);
if (buffer != NULL) {
Ret = RandGenerateEntropy(bytes_needed, buffer);
if (FALSE == Ret) {
rand_pool_add_end(pool, 0, 0);
} else {
rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
}
}
}
return rand_pool_entropy_available(pool);
}
/*
* Implementation for UEFI
*
* This is OpenSSL required interface.
*/
int rand_pool_add_nonce_data(RAND_POOL *pool)
{
struct {
UINT64 Rand;
UINT64 TimerValue;
} data = { 0 };
RandGetBytes(8, (UINT8 *)&(data.Rand));
data.TimerValue = GetPerformanceCounter();
return rand_pool_add(pool, (unsigned char*)&data, sizeof(data), 0);
}
/*
* Implementation for UEFI
*
* This is OpenSSL required interface.
*/
int rand_pool_add_additional_data(RAND_POOL *pool)
{
struct {
UINT64 Rand;
UINT64 TimerValue;
} data = { 0 };
RandGetBytes(8, (UINT8 *)&(data.Rand));
data.TimerValue = GetPerformanceCounter();
return rand_pool_add(pool, (unsigned char*)&data, sizeof(data), 0);
}
/*
* Dummy Implementation for UEFI
*
* This is OpenSSL required interface.
*/
int rand_pool_init(void)
{
return 1;
}
/*
* Dummy Implementation for UEFI
*
* This is OpenSSL required interface.
*/
void rand_pool_cleanup(void)
{
}
/*
* Dummy Implementation for UEFI
*
* This is OpenSSL required interface.
*/
void rand_pool_keep_random_devices_open(int keep)
{
}
|