-
Notifications
You must be signed in to change notification settings - Fork 707
/
s2n_random.c
359 lines (291 loc) · 9.65 KB
/
s2n_random.c
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include <openssl/engine.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <unistd.h>
#include <pthread.h>
#include <limits.h>
#include <fcntl.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <time.h>
#include "utils/s2n_compiler.h"
/* clang can define gcc version to be < 4.3, but cpuid.h exists for most releases */
#if ((defined(__x86_64__) || defined(__i386__)) && (defined(__clang__) || S2N_GCC_VERSION_AT_LEAST(4,3,0)))
#include <cpuid.h>
#endif
#include "stuffer/s2n_stuffer.h"
#include "crypto/s2n_drbg.h"
#include "error/s2n_errno.h"
#include "utils/s2n_safety.h"
#include "utils/s2n_random.h"
#include "utils/s2n_mem.h"
#include <openssl/rand.h>
#define ENTROPY_SOURCE "/dev/urandom"
/* See https://en.wikipedia.org/wiki/CPUID */
#define RDRAND_ECX_FLAG 0x40000000
/* One second in nanoseconds */
#define ONE_S INT64_C(1000000000)
static int entropy_fd = -1;
static __thread struct s2n_drbg per_thread_private_drbg = {0};
static __thread struct s2n_drbg per_thread_public_drbg = {0};
#if !defined(MAP_INHERIT_ZERO)
static __thread int zero_if_forked = 0;
void s2n_on_fork(void)
{
zero_if_forked = 0;
}
#else
static __thread int *zero_if_forked_ptr;
#define zero_if_forked (*zero_if_forked_ptr)
#endif
static inline int s2n_defend_if_forked(void)
{
uint8_t s2n_public_drbg[] = "s2n public drbg";
uint8_t s2n_private_drbg[] = "s2n private drbg";
struct s2n_blob public = {.data = s2n_public_drbg,.size = sizeof(s2n_public_drbg) };
struct s2n_blob private = {.data = s2n_private_drbg,.size = sizeof(s2n_private_drbg) };
if (zero_if_forked == 0) {
GUARD(s2n_drbg_instantiate(&per_thread_public_drbg, &public));
GUARD(s2n_drbg_instantiate(&per_thread_private_drbg, &private));
zero_if_forked = 1;
}
return 0;
}
int s2n_get_public_random_data(struct s2n_blob *blob)
{
GUARD(s2n_defend_if_forked());
GUARD(s2n_drbg_generate(&per_thread_public_drbg, blob));
return 0;
}
int s2n_get_private_random_data(struct s2n_blob *blob)
{
GUARD(s2n_defend_if_forked());
GUARD(s2n_drbg_generate(&per_thread_private_drbg, blob));
return 0;
}
int s2n_get_public_random_bytes_used(void)
{
return s2n_drbg_bytes_used(&per_thread_public_drbg);
}
int s2n_get_private_random_bytes_used(void)
{
return s2n_drbg_bytes_used(&per_thread_private_drbg);
}
int s2n_get_urandom_data(struct s2n_blob *blob)
{
uint32_t n = blob->size;
uint8_t *data = blob->data;
struct timespec sleep_time = {.tv_sec = 0, .tv_nsec = 0 };
long backoff = 1;
while (n) {
int r = read(entropy_fd, data, n);
if (r <= 0) {
/*
* A non-blocking read() on /dev/urandom should "never" fail,
* except for EINTR. If it does, briefly pause and use
* exponential backoff to avoid creating a tight spinning loop.
*
* iteration delay
* --------- -----------------
* 1 10 nsec
* 2 100 nsec
* 3 1,000 nsec
* 4 10,000 nsec
* 5 100,000 nsec
* 6 1,000,000 nsec
* 7 10,000,000 nsec
* 8 99,999,999 nsec
* 9 99,999,999 nsec
* ...
*/
if (errno != EINTR) {
backoff = MIN(backoff * 10, ONE_S - 1);
sleep_time.tv_nsec = backoff;
do {
r = nanosleep(&sleep_time, &sleep_time);
}
while (r != 0);
}
continue;
}
data += r;
n -= r;
}
return 0;
}
int64_t s2n_public_random(int64_t max)
{
uint64_t r;
gt_check(max, 0);
while (1) {
struct s2n_blob blob = {.data = (void *)&r, sizeof(r) };
GUARD(s2n_get_public_random_data(&blob));
/* Imagine an int was one byte and UINT_MAX was 256. If the
* caller asked for s2n_random(129, ...) we'd end up in
* trouble. Each number in the range 0...127 would be twice
* as likely as 128. That's because r == 0 % 129 -> 0, and
* r == 129 % 129 -> 0, but only r == 128 returns 128,
* r == 257 is out of range.
*
* To de-bias the dice, we discard values of r that are higher
* that the highest multiple of 'max' an int can support. If
* max is a uint, then in the worst case we discard 50% - 1 r's.
* But since 'max' is an int and INT_MAX is <= UINT_MAX / 2,
* in the worst case we discard 25% - 1 r's.
*/
if (r < (UINT64_MAX - (UINT64_MAX % max))) {
return r % max;
}
}
}
#if !defined(OPENSSL_IS_BORINGSSL) && !defined(OPENSSL_FIPS) && !defined(LIBRESSL_VERSION_NUMBER)
int s2n_openssl_compat_rand(unsigned char *buf, int num)
{
struct s2n_blob out = {.data = buf,.size = num };
if (s2n_get_private_random_data(&out) < 0) {
return 0;
}
return 1;
}
int s2n_openssl_compat_status(void)
{
return 1;
}
int s2n_openssl_compat_init(ENGINE * unused)
{
return 1;
}
RAND_METHOD s2n_openssl_rand_method = {
.seed = NULL,
.bytes = s2n_openssl_compat_rand,
.cleanup = NULL,
.add = NULL,
.pseudorand = s2n_openssl_compat_rand,
.status = s2n_openssl_compat_status
};
#endif
int s2n_rand_init(void)
{
OPEN:
entropy_fd = open(ENTROPY_SOURCE, O_RDONLY);
if (entropy_fd == -1) {
if (errno == EINTR) {
goto OPEN;
}
S2N_ERROR(S2N_ERR_OPEN_RANDOM);
}
#if defined(MAP_INHERIT_ZERO)
zero_if_forked_ptr = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
S2N_ERROR_IF(zero_if_forked_ptr == MAP_FAILED, S2N_ERR_OPEN_RANDOM);
S2N_ERROR_IF(minherit(zero_if_forked_ptr, sizeof(int), MAP_INHERIT_ZERO) == -1, S2N_ERR_OPEN_RANDOM);
#else
S2N_ERROR_IF(pthread_atfork(NULL, NULL, s2n_on_fork) != 0, S2N_ERR_OPEN_RANDOM);
#endif
GUARD(s2n_defend_if_forked());
#if !defined(OPENSSL_IS_BORINGSSL) && !defined(OPENSSL_FIPS) && !defined(LIBRESSL_VERSION_NUMBER)
/* Create an engine */
ENGINE *e = ENGINE_new();
if (e == NULL ||
ENGINE_set_id(e, "s2n_rand") != 1 ||
ENGINE_set_name(e, "s2n entropy generator") != 1 ||
ENGINE_set_flags(e, ENGINE_FLAGS_NO_REGISTER_ALL) != 1 ||
ENGINE_set_init_function(e, s2n_openssl_compat_init) != 1 || ENGINE_set_RAND(e, &s2n_openssl_rand_method) != 1 || ENGINE_add(e) != 1 || ENGINE_free(e) != 1) {
S2N_ERROR(S2N_ERR_OPEN_RANDOM);
}
/* Use that engine for rand() */
e = ENGINE_by_id("s2n_rand");
S2N_ERROR_IF(e == NULL || ENGINE_init(e) != 1 || ENGINE_set_default(e, ENGINE_METHOD_RAND) != 1 || ENGINE_free(e) != 1, S2N_ERR_OPEN_RANDOM);
#endif
return 0;
}
int s2n_rand_cleanup(void)
{
S2N_ERROR_IF(entropy_fd == -1, S2N_ERR_NOT_INITIALIZED);
GUARD(close(entropy_fd));
entropy_fd = -1;
#if !defined(OPENSSL_IS_BORINGSSL) && !defined(OPENSSL_FIPS) && !defined(LIBRESSL_VERSION_NUMBER)
/* Cleanup our rand ENGINE in libcrypto */
ENGINE *rand_engine = ENGINE_by_id("s2n_rand");
if (rand_engine) {
ENGINE_finish(rand_engine);
ENGINE_free(rand_engine);
ENGINE_cleanup();
}
#endif
return 0;
}
int s2n_rand_cleanup_thread(void)
{
GUARD(s2n_drbg_wipe(&per_thread_private_drbg));
GUARD(s2n_drbg_wipe(&per_thread_public_drbg));
return 0;
}
int s2n_cpu_supports_rdrand()
{
#if ((defined(__x86_64__) || defined(__i386__)) && (defined(__clang__) || S2N_GCC_VERSION_AT_LEAST(4,3,0)))
uint32_t eax, ebx, ecx, edx;
if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
return 0;
}
if (ecx & RDRAND_ECX_FLAG) {
return 1;
}
#endif
return 0;
}
/* Due to the need to support some older assemblers,
* we cannot use either the compiler intrinsics or
* the RDRAND assembly mnemonic. For this reason,
* we're using the opcode directly (0F C7/6). This
* stores the result in eax.
*
* volatile is important to prevent the compiler from
* re-ordering or optimizing the use of RDRAND.
*/
int s2n_get_rdrand_data(struct s2n_blob *out)
{
#if defined(__x86_64__) || defined(__i386__)
int space_remaining = 0;
struct s2n_stuffer stuffer;
union {
uint64_t u64;
uint8_t u8[8];
} output;
GUARD(s2n_stuffer_init(&stuffer, out));
while ((space_remaining = s2n_stuffer_space_remaining(&stuffer))) {
int success = 0;
for (int tries = 0; tries < 10; tries++) {
__asm__ __volatile__(".byte 0x48;\n" ".byte 0x0f;\n" ".byte 0xc7;\n" ".byte 0xf0;\n" "adcl $0x00, %%ebx;\n":"=b"(success), "=a"(output.u64)
:"b"(0)
:"cc");
if (success) {
break;
}
}
if (!success) {
return -1;
}
int data_to_fill = MIN(sizeof(output), space_remaining);
GUARD(s2n_stuffer_write_bytes(&stuffer, output.u8, data_to_fill));
}
return 0;
#else
return -1;
#endif
}