/src/openssl/crypto/rand/rand_unix.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #ifndef _GNU_SOURCE |
11 | | # define _GNU_SOURCE |
12 | | #endif |
13 | | #include "e_os.h" |
14 | | #include <stdio.h> |
15 | | #include "internal/cryptlib.h" |
16 | | #include <openssl/rand.h> |
17 | | #include "rand_lcl.h" |
18 | | #include "internal/rand_int.h" |
19 | | #include <stdio.h> |
20 | | #include "internal/dso.h" |
21 | | #if defined(__linux) |
22 | | # include <sys/syscall.h> |
23 | | #endif |
24 | | #if defined(__FreeBSD__) |
25 | | # include <sys/types.h> |
26 | | # include <sys/sysctl.h> |
27 | | # include <sys/param.h> |
28 | | #endif |
29 | | #if defined(__OpenBSD__) || defined(__NetBSD__) |
30 | | # include <sys/param.h> |
31 | | #endif |
32 | | |
33 | | #if defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__) |
34 | | # include <sys/types.h> |
35 | | # include <sys/stat.h> |
36 | | # include <fcntl.h> |
37 | | # include <unistd.h> |
38 | | # include <sys/time.h> |
39 | | |
40 | | static uint64_t get_time_stamp(void); |
41 | | static uint64_t get_timer_bits(void); |
42 | | |
43 | | /* Macro to convert two thirty two bit values into a sixty four bit one */ |
44 | 0 | # define TWO32TO64(a, b) ((((uint64_t)(a)) << 32) + (b)) |
45 | | |
46 | | /* |
47 | | * Check for the existence and support of POSIX timers. The standard |
48 | | * says that the _POSIX_TIMERS macro will have a positive value if they |
49 | | * are available. |
50 | | * |
51 | | * However, we want an additional constraint: that the timer support does |
52 | | * not require an extra library dependency. Early versions of glibc |
53 | | * require -lrt to be specified on the link line to access the timers, |
54 | | * so this needs to be checked for. |
55 | | * |
56 | | * It is worse because some libraries define __GLIBC__ but don't |
57 | | * support the version testing macro (e.g. uClibc). This means |
58 | | * an extra check is needed. |
59 | | * |
60 | | * The final condition is: |
61 | | * "have posix timers and either not glibc or glibc without -lrt" |
62 | | * |
63 | | * The nested #if sequences are required to avoid using a parameterised |
64 | | * macro that might be undefined. |
65 | | */ |
66 | | # undef OSSL_POSIX_TIMER_OKAY |
67 | | # if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 |
68 | | # if defined(__GLIBC__) |
69 | | # if defined(__GLIBC_PREREQ) |
70 | | # if __GLIBC_PREREQ(2, 17) |
71 | | # define OSSL_POSIX_TIMER_OKAY |
72 | | # endif |
73 | | # endif |
74 | | # else |
75 | | # define OSSL_POSIX_TIMER_OKAY |
76 | | # endif |
77 | | # endif |
78 | | #endif /* defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__) */ |
79 | | |
80 | | #if (defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)) && \ |
81 | | !defined(OPENSSL_RAND_SEED_NONE) |
82 | | # error "UEFI and VXWorks only support seeding NONE" |
83 | | #endif |
84 | | |
85 | | #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) \ |
86 | | || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) \ |
87 | | || defined(OPENSSL_SYS_UEFI)) |
88 | | |
89 | | static ssize_t syscall_random(void *buf, size_t buflen); |
90 | | |
91 | | # if defined(OPENSSL_SYS_VOS) |
92 | | |
93 | | # ifndef OPENSSL_RAND_SEED_OS |
94 | | # error "Unsupported seeding method configured; must be os" |
95 | | # endif |
96 | | |
97 | | # if defined(OPENSSL_SYS_VOS_HPPA) && defined(OPENSSL_SYS_VOS_IA32) |
98 | | # error "Unsupported HP-PA and IA32 at the same time." |
99 | | # endif |
100 | | # if !defined(OPENSSL_SYS_VOS_HPPA) && !defined(OPENSSL_SYS_VOS_IA32) |
101 | | # error "Must have one of HP-PA or IA32" |
102 | | # endif |
103 | | |
104 | | /* |
105 | | * The following algorithm repeatedly samples the real-time clock (RTC) to |
106 | | * generate a sequence of unpredictable data. The algorithm relies upon the |
107 | | * uneven execution speed of the code (due to factors such as cache misses, |
108 | | * interrupts, bus activity, and scheduling) and upon the rather large |
109 | | * relative difference between the speed of the clock and the rate at which |
110 | | * it can be read. If it is ported to an environment where execution speed |
111 | | * is more constant or where the RTC ticks at a much slower rate, or the |
112 | | * clock can be read with fewer instructions, it is likely that the results |
113 | | * would be far more predictable. This should only be used for legacy |
114 | | * platforms. |
115 | | * |
116 | | * As a precaution, we assume only 2 bits of entropy per byte. |
117 | | */ |
118 | | size_t rand_pool_acquire_entropy(RAND_POOL *pool) |
119 | | { |
120 | | short int code; |
121 | | int i, k; |
122 | | size_t bytes_needed; |
123 | | struct timespec ts; |
124 | | unsigned char v; |
125 | | # ifdef OPENSSL_SYS_VOS_HPPA |
126 | | long duration; |
127 | | extern void s$sleep(long *_duration, short int *_code); |
128 | | # else |
129 | | long long duration; |
130 | | extern void s$sleep2(long long *_duration, short int *_code); |
131 | | # endif |
132 | | |
133 | | bytes_needed = rand_pool_bytes_needed(pool, 4 /*entropy_factor*/); |
134 | | |
135 | | for (i = 0; i < bytes_needed; i++) { |
136 | | /* |
137 | | * burn some cpu; hope for interrupts, cache collisions, bus |
138 | | * interference, etc. |
139 | | */ |
140 | | for (k = 0; k < 99; k++) |
141 | | ts.tv_nsec = random(); |
142 | | |
143 | | # ifdef OPENSSL_SYS_VOS_HPPA |
144 | | /* sleep for 1/1024 of a second (976 us). */ |
145 | | duration = 1; |
146 | | s$sleep(&duration, &code); |
147 | | # else |
148 | | /* sleep for 1/65536 of a second (15 us). */ |
149 | | duration = 1; |
150 | | s$sleep2(&duration, &code); |
151 | | # endif |
152 | | |
153 | | /* Get wall clock time, take 8 bits. */ |
154 | | clock_gettime(CLOCK_REALTIME, &ts); |
155 | | v = (unsigned char)(ts.tv_nsec & 0xFF); |
156 | | rand_pool_add(pool, arg, &v, sizeof(v) , 2); |
157 | | } |
158 | | return rand_pool_entropy_available(pool); |
159 | | } |
160 | | |
161 | | void rand_pool_cleanup(void) |
162 | | { |
163 | | } |
164 | | |
165 | | void rand_pool_keep_random_devices_open(int keep) |
166 | | { |
167 | | } |
168 | | |
169 | | # else |
170 | | |
171 | | # if defined(OPENSSL_RAND_SEED_EGD) && \ |
172 | | (defined(OPENSSL_NO_EGD) || !defined(DEVRANDOM_EGD)) |
173 | | # error "Seeding uses EGD but EGD is turned off or no device given" |
174 | | # endif |
175 | | |
176 | | # if defined(OPENSSL_RAND_SEED_DEVRANDOM) && !defined(DEVRANDOM) |
177 | | # error "Seeding uses urandom but DEVRANDOM is not configured" |
178 | | # endif |
179 | | |
180 | | # if defined(OPENSSL_RAND_SEED_OS) |
181 | | # if !defined(DEVRANDOM) |
182 | | # error "OS seeding requires DEVRANDOM to be configured" |
183 | | # endif |
184 | | # define OPENSSL_RAND_SEED_GETRANDOM |
185 | | # define OPENSSL_RAND_SEED_DEVRANDOM |
186 | | # endif |
187 | | |
188 | | # if defined(OPENSSL_RAND_SEED_LIBRANDOM) |
189 | | # error "librandom not (yet) supported" |
190 | | # endif |
191 | | |
192 | | # if (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND) |
193 | | /* |
194 | | * sysctl_random(): Use sysctl() to read a random number from the kernel |
195 | | * Returns the number of bytes returned in buf on success, -1 on failure. |
196 | | */ |
197 | | static ssize_t sysctl_random(char *buf, size_t buflen) |
198 | | { |
199 | | int mib[2]; |
200 | | size_t done = 0; |
201 | | size_t len; |
202 | | |
203 | | /* |
204 | | * Note: sign conversion between size_t and ssize_t is safe even |
205 | | * without a range check, see comment in syscall_random() |
206 | | */ |
207 | | |
208 | | /* |
209 | | * On FreeBSD old implementations returned longs, newer versions support |
210 | | * variable sizes up to 256 byte. The code below would not work properly |
211 | | * when the sysctl returns long and we want to request something not a |
212 | | * multiple of longs, which should never be the case. |
213 | | */ |
214 | | if (!ossl_assert(buflen % sizeof(long) == 0)) { |
215 | | errno = EINVAL; |
216 | | return -1; |
217 | | } |
218 | | |
219 | | /* |
220 | | * On NetBSD before 4.0 KERN_ARND was an alias for KERN_URND, and only |
221 | | * filled in an int, leaving the rest uninitialized. Since NetBSD 4.0 |
222 | | * it returns a variable number of bytes with the current version supporting |
223 | | * up to 256 bytes. |
224 | | * Just return an error on older NetBSD versions. |
225 | | */ |
226 | | #if defined(__NetBSD__) && __NetBSD_Version__ < 400000000 |
227 | | errno = ENOSYS; |
228 | | return -1; |
229 | | #endif |
230 | | |
231 | | mib[0] = CTL_KERN; |
232 | | mib[1] = KERN_ARND; |
233 | | |
234 | | do { |
235 | | len = buflen; |
236 | | if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) |
237 | | return done > 0 ? done : -1; |
238 | | done += len; |
239 | | buf += len; |
240 | | buflen -= len; |
241 | | } while (buflen > 0); |
242 | | |
243 | | return done; |
244 | | } |
245 | | # endif |
246 | | |
247 | | /* |
248 | | * syscall_random(): Try to get random data using a system call |
249 | | * returns the number of bytes returned in buf, or < 0 on error. |
250 | | */ |
251 | | static ssize_t syscall_random(void *buf, size_t buflen) |
252 | 0 | { |
253 | 0 | /* |
254 | 0 | * Note: 'buflen' equals the size of the buffer which is used by the |
255 | 0 | * get_entropy() callback of the RAND_DRBG. It is roughly bounded by |
256 | 0 | * |
257 | 0 | * 2 * DRBG_MINMAX_FACTOR * (RAND_DRBG_STRENGTH / 8) = 2^13 |
258 | 0 | * |
259 | 0 | * which is way below the OSSL_SSIZE_MAX limit. Therefore sign conversion |
260 | 0 | * between size_t and ssize_t is safe even without a range check. |
261 | 0 | */ |
262 | 0 |
|
263 | 0 | /* |
264 | 0 | * Do runtime detection to find getentropy(). |
265 | 0 | * |
266 | 0 | * Known OSs that should support this: |
267 | 0 | * - Darwin since 16 (OSX 10.12, IOS 10.0). |
268 | 0 | * - Solaris since 11.3 |
269 | 0 | * - OpenBSD since 5.6 |
270 | 0 | * - Linux since 3.17 with glibc 2.25 |
271 | 0 | * - FreeBSD since 12.0 (1200061) |
272 | 0 | */ |
273 | 0 | # if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__) && !defined(__hpux) |
274 | 0 | extern int getentropy(void *buffer, size_t length) __attribute__((weak)); |
275 | 0 |
|
276 | 0 | if (getentropy != NULL) |
277 | 0 | return getentropy(buf, buflen) == 0 ? (ssize_t)buflen : -1; |
278 | | # else |
279 | | union { |
280 | | void *p; |
281 | | int (*f)(void *buffer, size_t length); |
282 | | } p_getentropy; |
283 | | |
284 | | /* |
285 | | * We could cache the result of the lookup, but we normally don't |
286 | | * call this function often. |
287 | | */ |
288 | | ERR_set_mark(); |
289 | | p_getentropy.p = DSO_global_lookup("getentropy"); |
290 | | ERR_pop_to_mark(); |
291 | | if (p_getentropy.p != NULL) |
292 | | return p_getentropy.f(buf, buflen) == 0 ? (ssize_t)buflen : -1; |
293 | | # endif |
294 | | |
295 | 0 | /* Linux supports this since version 3.17 */ |
296 | 0 | # if defined(__linux) && defined(SYS_getrandom) |
297 | 0 | return syscall(SYS_getrandom, buf, buflen, 0); |
298 | | # elif (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND) |
299 | | return sysctl_random(buf, buflen); |
300 | | # else |
301 | | errno = ENOSYS; |
302 | | return -1; |
303 | | # endif |
304 | | } |
305 | | |
306 | | #if !defined(OPENSSL_RAND_SEED_NONE) && defined(OPENSSL_RAND_SEED_DEVRANDOM) |
307 | | static const char *random_device_paths[] = { DEVRANDOM }; |
308 | | static struct random_device { |
309 | | int fd; |
310 | | dev_t dev; |
311 | | ino_t ino; |
312 | | mode_t mode; |
313 | | dev_t rdev; |
314 | | } random_devices[OSSL_NELEM(random_device_paths)]; |
315 | | static int keep_random_devices_open = 1; |
316 | | |
317 | | /* |
318 | | * Verify that the file descriptor associated with the random source is |
319 | | * still valid. The rationale for doing this is the fact that it is not |
320 | | * uncommon for daemons to close all open file handles when daemonizing. |
321 | | * So the handle might have been closed or even reused for opening |
322 | | * another file. |
323 | | */ |
324 | | static int check_random_device(struct random_device * rd) |
325 | 24 | { |
326 | 24 | struct stat st; |
327 | 24 | |
328 | 24 | return rd->fd != -1 |
329 | 24 | && fstat(rd->fd, &st) != -1 |
330 | 24 | && rd->dev == st.st_dev |
331 | 24 | && rd->ino == st.st_ino |
332 | 24 | && ((rd->mode ^ st.st_mode) & ~(S_IRWXU | S_IRWXG | S_IRWXO)) == 0 |
333 | 24 | && rd->rdev == st.st_rdev; |
334 | 24 | } |
335 | | |
336 | | /* |
337 | | * Open a random device if required and return its file descriptor or -1 on error |
338 | | */ |
339 | | static int get_random_device(size_t n) |
340 | 0 | { |
341 | 0 | struct stat st; |
342 | 0 | struct random_device * rd = &random_devices[n]; |
343 | 0 |
|
344 | 0 | /* reuse existing file descriptor if it is (still) valid */ |
345 | 0 | if (check_random_device(rd)) |
346 | 0 | return rd->fd; |
347 | 0 | |
348 | 0 | /* open the random device ... */ |
349 | 0 | if ((rd->fd = open(random_device_paths[n], O_RDONLY)) == -1) |
350 | 0 | return rd->fd; |
351 | 0 | |
352 | 0 | /* ... and cache its relevant stat(2) data */ |
353 | 0 | if (fstat(rd->fd, &st) != -1) { |
354 | 0 | rd->dev = st.st_dev; |
355 | 0 | rd->ino = st.st_ino; |
356 | 0 | rd->mode = st.st_mode; |
357 | 0 | rd->rdev = st.st_rdev; |
358 | 0 | } else { |
359 | 0 | close(rd->fd); |
360 | 0 | rd->fd = -1; |
361 | 0 | } |
362 | 0 |
|
363 | 0 | return rd->fd; |
364 | 0 | } |
365 | | |
366 | | /* |
367 | | * Close a random device making sure it is a random device |
368 | | */ |
369 | | static void close_random_device(size_t n) |
370 | 24 | { |
371 | 24 | struct random_device * rd = &random_devices[n]; |
372 | 24 | |
373 | 24 | if (check_random_device(rd)) |
374 | 0 | close(rd->fd); |
375 | 24 | rd->fd = -1; |
376 | 24 | } |
377 | | |
378 | | static void open_random_devices(void) |
379 | 0 | { |
380 | 0 | size_t i; |
381 | 0 |
|
382 | 0 | for (i = 0; i < OSSL_NELEM(random_devices); i++) |
383 | 0 | (void)get_random_device(i); |
384 | 0 | } |
385 | | |
386 | | int rand_pool_init(void) |
387 | 0 | { |
388 | 0 | size_t i; |
389 | 0 |
|
390 | 0 | for (i = 0; i < OSSL_NELEM(random_devices); i++) |
391 | 0 | random_devices[i].fd = -1; |
392 | 0 | open_random_devices(); |
393 | 0 | return 1; |
394 | 0 | } |
395 | | |
396 | | void rand_pool_cleanup(void) |
397 | 8 | { |
398 | 8 | size_t i; |
399 | 8 | |
400 | 32 | for (i = 0; i < OSSL_NELEM(random_devices); i++) |
401 | 24 | close_random_device(i); |
402 | 8 | } |
403 | | |
404 | | void rand_pool_keep_random_devices_open(int keep) |
405 | 0 | { |
406 | 0 | if (keep) |
407 | 0 | open_random_devices(); |
408 | 0 | else |
409 | 0 | rand_pool_cleanup(); |
410 | 0 | keep_random_devices_open = keep; |
411 | 0 | } |
412 | | |
413 | | # else /* defined(OPENSSL_RAND_SEED_NONE) |
414 | | * || !defined(OPENSSL_RAND_SEED_DEVRANDOM) |
415 | | */ |
416 | | |
417 | | int rand_pool_init(void) |
418 | | { |
419 | | return 1; |
420 | | } |
421 | | |
422 | | void rand_pool_cleanup(void) |
423 | | { |
424 | | } |
425 | | |
426 | | void rand_pool_keep_random_devices_open(int keep) |
427 | | { |
428 | | } |
429 | | |
430 | | # endif /* !defined(OPENSSL_RAND_SEED_NONE) |
431 | | * && defined(OPENSSL_RAND_SEED_DEVRANDOM) |
432 | | */ |
433 | | |
434 | | /* |
435 | | * Try the various seeding methods in turn, exit when successful. |
436 | | * |
437 | | * TODO(DRBG): If more than one entropy source is available, is it |
438 | | * preferable to stop as soon as enough entropy has been collected |
439 | | * (as favored by @rsalz) or should one rather be defensive and add |
440 | | * more entropy than requested and/or from different sources? |
441 | | * |
442 | | * Currently, the user can select multiple entropy sources in the |
443 | | * configure step, yet in practice only the first available source |
444 | | * will be used. A more flexible solution has been requested, but |
445 | | * currently it is not clear how this can be achieved without |
446 | | * overengineering the problem. There are many parameters which |
447 | | * could be taken into account when selecting the order and amount |
448 | | * of input from the different entropy sources (trust, quality, |
449 | | * possibility of blocking). |
450 | | */ |
451 | | size_t rand_pool_acquire_entropy(RAND_POOL *pool) |
452 | 0 | { |
453 | | # ifdef OPENSSL_RAND_SEED_NONE |
454 | | return rand_pool_entropy_available(pool); |
455 | | # else |
456 | | size_t bytes_needed; |
457 | 0 | size_t entropy_available = 0; |
458 | 0 | unsigned char *buffer; |
459 | 0 |
|
460 | 0 | # ifdef OPENSSL_RAND_SEED_GETRANDOM |
461 | 0 | { |
462 | 0 | ssize_t bytes; |
463 | 0 | /* Maximum allowed number of consecutive unsuccessful attempts */ |
464 | 0 | int attempts = 3; |
465 | 0 |
|
466 | 0 | bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/); |
467 | 0 | while (bytes_needed != 0 && attempts-- > 0) { |
468 | 0 | buffer = rand_pool_add_begin(pool, bytes_needed); |
469 | 0 | bytes = syscall_random(buffer, bytes_needed); |
470 | 0 | if (bytes > 0) { |
471 | 0 | rand_pool_add_end(pool, bytes, 8 * bytes); |
472 | 0 | bytes_needed -= bytes; |
473 | 0 | attempts = 3; /* reset counter after successful attempt */ |
474 | 0 | } else if (bytes < 0 && errno != EINTR) { |
475 | 0 | break; |
476 | 0 | } |
477 | 0 | } |
478 | 0 | } |
479 | 0 | entropy_available = rand_pool_entropy_available(pool); |
480 | 0 | if (entropy_available > 0) |
481 | 0 | return entropy_available; |
482 | 0 | # endif |
483 | 0 | |
484 | | # if defined(OPENSSL_RAND_SEED_LIBRANDOM) |
485 | | { |
486 | | /* Not yet implemented. */ |
487 | | } |
488 | | # endif |
489 | | |
490 | 0 | # ifdef OPENSSL_RAND_SEED_DEVRANDOM |
491 | 0 | bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/); |
492 | 0 | { |
493 | 0 | size_t i; |
494 | 0 |
|
495 | 0 | for (i = 0; bytes_needed > 0 && i < OSSL_NELEM(random_device_paths); i++) { |
496 | 0 | ssize_t bytes = 0; |
497 | 0 | /* Maximum allowed number of consecutive unsuccessful attempts */ |
498 | 0 | int attempts = 3; |
499 | 0 | const int fd = get_random_device(i); |
500 | 0 |
|
501 | 0 | if (fd == -1) |
502 | 0 | continue; |
503 | 0 | |
504 | 0 | while (bytes_needed != 0 && attempts-- > 0) { |
505 | 0 | buffer = rand_pool_add_begin(pool, bytes_needed); |
506 | 0 | bytes = read(fd, buffer, bytes_needed); |
507 | 0 |
|
508 | 0 | if (bytes > 0) { |
509 | 0 | rand_pool_add_end(pool, bytes, 8 * bytes); |
510 | 0 | bytes_needed -= bytes; |
511 | 0 | attempts = 3; /* reset counter after successful attempt */ |
512 | 0 | } else if (bytes < 0 && errno != EINTR) { |
513 | 0 | break; |
514 | 0 | } |
515 | 0 | } |
516 | 0 | if (bytes < 0 || !keep_random_devices_open) |
517 | 0 | close_random_device(i); |
518 | 0 |
|
519 | 0 | bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/); |
520 | 0 | } |
521 | 0 | entropy_available = rand_pool_entropy_available(pool); |
522 | 0 | if (entropy_available > 0) |
523 | 0 | return entropy_available; |
524 | 0 | } |
525 | 0 | # endif |
526 | 0 | |
527 | | # ifdef OPENSSL_RAND_SEED_RDTSC |
528 | | entropy_available = rand_acquire_entropy_from_tsc(pool); |
529 | | if (entropy_available > 0) |
530 | | return entropy_available; |
531 | | # endif |
532 | | |
533 | | # ifdef OPENSSL_RAND_SEED_RDCPU |
534 | | entropy_available = rand_acquire_entropy_from_cpu(pool); |
535 | | if (entropy_available > 0) |
536 | | return entropy_available; |
537 | | # endif |
538 | | |
539 | | # ifdef OPENSSL_RAND_SEED_EGD |
540 | | bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/); |
541 | | if (bytes_needed > 0) { |
542 | | static const char *paths[] = { DEVRANDOM_EGD, NULL }; |
543 | | int i; |
544 | | |
545 | | for (i = 0; paths[i] != NULL; i++) { |
546 | | buffer = rand_pool_add_begin(pool, bytes_needed); |
547 | | if (buffer != NULL) { |
548 | | size_t bytes = 0; |
549 | | int num = RAND_query_egd_bytes(paths[i], |
550 | | buffer, (int)bytes_needed); |
551 | | if (num == (int)bytes_needed) |
552 | | bytes = bytes_needed; |
553 | | |
554 | | rand_pool_add_end(pool, bytes, 8 * bytes); |
555 | | entropy_available = rand_pool_entropy_available(pool); |
556 | | } |
557 | | if (entropy_available > 0) |
558 | | return entropy_available; |
559 | | } |
560 | | } |
561 | | # endif |
562 | | |
563 | 0 | return rand_pool_entropy_available(pool); |
564 | 0 | # endif |
565 | 0 | } |
566 | | # endif |
567 | | #endif |
568 | | |
569 | | #if defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__) |
570 | | int rand_pool_add_nonce_data(RAND_POOL *pool) |
571 | 0 | { |
572 | 0 | struct { |
573 | 0 | pid_t pid; |
574 | 0 | CRYPTO_THREAD_ID tid; |
575 | 0 | uint64_t time; |
576 | 0 | } data = { 0 }; |
577 | 0 |
|
578 | 0 | /* |
579 | 0 | * Add process id, thread id, and a high resolution timestamp to |
580 | 0 | * ensure that the nonce is unique whith high probability for |
581 | 0 | * different process instances. |
582 | 0 | */ |
583 | 0 | data.pid = getpid(); |
584 | 0 | data.tid = CRYPTO_THREAD_get_current_id(); |
585 | 0 | data.time = get_time_stamp(); |
586 | 0 |
|
587 | 0 | return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0); |
588 | 0 | } |
589 | | |
590 | | int rand_pool_add_additional_data(RAND_POOL *pool) |
591 | 0 | { |
592 | 0 | struct { |
593 | 0 | CRYPTO_THREAD_ID tid; |
594 | 0 | uint64_t time; |
595 | 0 | } data = { 0 }; |
596 | 0 |
|
597 | 0 | /* |
598 | 0 | * Add some noise from the thread id and a high resolution timer. |
599 | 0 | * The thread id adds a little randomness if the drbg is accessed |
600 | 0 | * concurrently (which is the case for the <master> drbg). |
601 | 0 | */ |
602 | 0 | data.tid = CRYPTO_THREAD_get_current_id(); |
603 | 0 | data.time = get_timer_bits(); |
604 | 0 |
|
605 | 0 | return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0); |
606 | 0 | } |
607 | | |
608 | | |
609 | | /* |
610 | | * Get the current time with the highest possible resolution |
611 | | * |
612 | | * The time stamp is added to the nonce, so it is optimized for not repeating. |
613 | | * The current time is ideal for this purpose, provided the computer's clock |
614 | | * is synchronized. |
615 | | */ |
616 | | static uint64_t get_time_stamp(void) |
617 | 0 | { |
618 | 0 | # if defined(OSSL_POSIX_TIMER_OKAY) |
619 | 0 | { |
620 | 0 | struct timespec ts; |
621 | 0 |
|
622 | 0 | if (clock_gettime(CLOCK_REALTIME, &ts) == 0) |
623 | 0 | return TWO32TO64(ts.tv_sec, ts.tv_nsec); |
624 | 0 | } |
625 | 0 | # endif |
626 | 0 | # if defined(__unix__) \ |
627 | 0 | || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) |
628 | 0 | { |
629 | 0 | struct timeval tv; |
630 | 0 |
|
631 | 0 | if (gettimeofday(&tv, NULL) == 0) |
632 | 0 | return TWO32TO64(tv.tv_sec, tv.tv_usec); |
633 | 0 | } |
634 | 0 | # endif |
635 | 0 | return time(NULL); |
636 | 0 | } |
637 | | |
638 | | /* |
639 | | * Get an arbitrary timer value of the highest possible resolution |
640 | | * |
641 | | * The timer value is added as random noise to the additional data, |
642 | | * which is not considered a trusted entropy sourec, so any result |
643 | | * is acceptable. |
644 | | */ |
645 | | static uint64_t get_timer_bits(void) |
646 | 0 | { |
647 | 0 | uint64_t res = OPENSSL_rdtsc(); |
648 | 0 |
|
649 | 0 | if (res != 0) |
650 | 0 | return res; |
651 | 0 | |
652 | | # if defined(__sun) || defined(__hpux) |
653 | | return gethrtime(); |
654 | | # elif defined(_AIX) |
655 | | { |
656 | | timebasestruct_t t; |
657 | | |
658 | | read_wall_time(&t, TIMEBASE_SZ); |
659 | | return TWO32TO64(t.tb_high, t.tb_low); |
660 | | } |
661 | | # elif defined(OSSL_POSIX_TIMER_OKAY) |
662 | 0 | { |
663 | 0 | struct timespec ts; |
664 | 0 |
|
665 | 0 | # ifdef CLOCK_BOOTTIME |
666 | 0 | # define CLOCK_TYPE CLOCK_BOOTTIME |
667 | | # elif defined(_POSIX_MONOTONIC_CLOCK) |
668 | | # define CLOCK_TYPE CLOCK_MONOTONIC |
669 | | # else |
670 | | # define CLOCK_TYPE CLOCK_REALTIME |
671 | | # endif |
672 | |
|
673 | 0 | if (clock_gettime(CLOCK_TYPE, &ts) == 0) |
674 | 0 | return TWO32TO64(ts.tv_sec, ts.tv_nsec); |
675 | 0 | } |
676 | 0 | # endif |
677 | 0 | # if defined(__unix__) \ |
678 | 0 | || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) |
679 | 0 | { |
680 | 0 | struct timeval tv; |
681 | 0 |
|
682 | 0 | if (gettimeofday(&tv, NULL) == 0) |
683 | 0 | return TWO32TO64(tv.tv_sec, tv.tv_usec); |
684 | 0 | } |
685 | 0 | # endif |
686 | 0 | return time(NULL); |
687 | 0 | } |
688 | | #endif /* defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__) */ |