/src/openssl30/providers/implementations/rands/seeding/rand_unix.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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 <openssl/crypto.h> |
18 | | #include "crypto/rand_pool.h" |
19 | | #include "crypto/rand.h" |
20 | | #include <stdio.h> |
21 | | #include "internal/dso.h" |
22 | | #include "prov/seeding.h" |
23 | | |
24 | | #ifdef __linux |
25 | | #include <sys/syscall.h> |
26 | | #ifdef DEVRANDOM_WAIT |
27 | | #include <sys/shm.h> |
28 | | #include <sys/utsname.h> |
29 | | #endif |
30 | | #endif |
31 | | #if (defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(OPENSSL_SYS_UEFI) |
32 | | #include <sys/types.h> |
33 | | #include <sys/sysctl.h> |
34 | | #include <sys/param.h> |
35 | | #endif |
36 | | #if defined(__OpenBSD__) |
37 | | #include <sys/param.h> |
38 | | #endif |
39 | | #if defined(__DragonFly__) |
40 | | #include <sys/param.h> |
41 | | #include <sys/random.h> |
42 | | #endif |
43 | | |
44 | | #if (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS)) \ |
45 | | || defined(__DJGPP__) |
46 | | #include <sys/types.h> |
47 | | #include <sys/stat.h> |
48 | | #include <fcntl.h> |
49 | | #include <unistd.h> |
50 | | #include <sys/time.h> |
51 | | |
52 | | static uint64_t get_time_stamp(void); |
53 | | static uint64_t get_timer_bits(void); |
54 | | |
55 | | /* Macro to convert two thirty two bit values into a sixty four bit one */ |
56 | 435 | #define TWO32TO64(a, b) ((((uint64_t)(a)) << 32) + (b)) |
57 | | |
58 | | /* |
59 | | * Check for the existence and support of POSIX timers. The standard |
60 | | * says that the _POSIX_TIMERS macro will have a positive value if they |
61 | | * are available. |
62 | | * |
63 | | * However, we want an additional constraint: that the timer support does |
64 | | * not require an extra library dependency. Early versions of glibc |
65 | | * require -lrt to be specified on the link line to access the timers, |
66 | | * so this needs to be checked for. |
67 | | * |
68 | | * It is worse because some libraries define __GLIBC__ but don't |
69 | | * support the version testing macro (e.g. uClibc). This means |
70 | | * an extra check is needed. |
71 | | * |
72 | | * The final condition is: |
73 | | * "have posix timers and either not glibc or glibc without -lrt" |
74 | | * |
75 | | * The nested #if sequences are required to avoid using a parameterised |
76 | | * macro that might be undefined. |
77 | | */ |
78 | | #undef OSSL_POSIX_TIMER_OKAY |
79 | | /* On some systems, _POSIX_TIMERS is defined but empty. |
80 | | * Subtracting by 0 when comparing avoids an error in this case. */ |
81 | | #if defined(_POSIX_TIMERS) && _POSIX_TIMERS - 0 > 0 |
82 | | #if defined(__GLIBC__) |
83 | | #if defined(__GLIBC_PREREQ) |
84 | | #if __GLIBC_PREREQ(2, 17) |
85 | | #define OSSL_POSIX_TIMER_OKAY |
86 | | #endif |
87 | | #endif |
88 | | #else |
89 | | #define OSSL_POSIX_TIMER_OKAY |
90 | | #endif |
91 | | #endif |
92 | | #endif /* (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS)) \ |
93 | | || defined(__DJGPP__) */ |
94 | | |
95 | | #if defined(OPENSSL_RAND_SEED_NONE) |
96 | | /* none means none. this simplifies the following logic */ |
97 | | #undef OPENSSL_RAND_SEED_OS |
98 | | #undef OPENSSL_RAND_SEED_GETRANDOM |
99 | | #undef OPENSSL_RAND_SEED_LIBRANDOM |
100 | | #undef OPENSSL_RAND_SEED_DEVRANDOM |
101 | | #undef OPENSSL_RAND_SEED_RDTSC |
102 | | #undef OPENSSL_RAND_SEED_RDCPU |
103 | | #undef OPENSSL_RAND_SEED_EGD |
104 | | #endif |
105 | | |
106 | | #if defined(OPENSSL_SYS_UEFI) && !defined(OPENSSL_RAND_SEED_NONE) |
107 | | #error "UEFI only supports seeding NONE" |
108 | | #endif |
109 | | |
110 | | #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) \ |
111 | | || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) \ |
112 | | || defined(OPENSSL_SYS_UEFI)) |
113 | | |
114 | | #if defined(OPENSSL_SYS_VOS) |
115 | | |
116 | | #ifndef OPENSSL_RAND_SEED_OS |
117 | | #error "Unsupported seeding method configured; must be os" |
118 | | #endif |
119 | | |
120 | | #if defined(OPENSSL_SYS_VOS_HPPA) && defined(OPENSSL_SYS_VOS_IA32) |
121 | | #error "Unsupported HP-PA and IA32 at the same time." |
122 | | #endif |
123 | | #if !defined(OPENSSL_SYS_VOS_HPPA) && !defined(OPENSSL_SYS_VOS_IA32) |
124 | | #error "Must have one of HP-PA or IA32" |
125 | | #endif |
126 | | |
127 | | /* |
128 | | * The following algorithm repeatedly samples the real-time clock (RTC) to |
129 | | * generate a sequence of unpredictable data. The algorithm relies upon the |
130 | | * uneven execution speed of the code (due to factors such as cache misses, |
131 | | * interrupts, bus activity, and scheduling) and upon the rather large |
132 | | * relative difference between the speed of the clock and the rate at which |
133 | | * it can be read. If it is ported to an environment where execution speed |
134 | | * is more constant or where the RTC ticks at a much slower rate, or the |
135 | | * clock can be read with fewer instructions, it is likely that the results |
136 | | * would be far more predictable. This should only be used for legacy |
137 | | * platforms. |
138 | | * |
139 | | * As a precaution, we assume only 2 bits of entropy per byte. |
140 | | */ |
141 | | size_t ossl_pool_acquire_entropy(RAND_POOL *pool) |
142 | | { |
143 | | short int code; |
144 | | int i, k; |
145 | | size_t bytes_needed; |
146 | | struct timespec ts; |
147 | | unsigned char v; |
148 | | #ifdef OPENSSL_SYS_VOS_HPPA |
149 | | long duration; |
150 | | extern void s$sleep(long *_duration, short int *_code); |
151 | | #else |
152 | | long long duration; |
153 | | extern void s$sleep2(long long *_duration, short int *_code); |
154 | | #endif |
155 | | |
156 | | bytes_needed = ossl_rand_pool_bytes_needed(pool, 4 /*entropy_factor*/); |
157 | | |
158 | | for (i = 0; i < bytes_needed; i++) { |
159 | | /* |
160 | | * burn some cpu; hope for interrupts, cache collisions, bus |
161 | | * interference, etc. |
162 | | */ |
163 | | for (k = 0; k < 99; k++) |
164 | | ts.tv_nsec = random(); |
165 | | |
166 | | #ifdef OPENSSL_SYS_VOS_HPPA |
167 | | /* sleep for 1/1024 of a second (976 us). */ |
168 | | duration = 1; |
169 | | s$sleep(&duration, &code); |
170 | | #else |
171 | | /* sleep for 1/65536 of a second (15 us). */ |
172 | | duration = 1; |
173 | | s$sleep2(&duration, &code); |
174 | | #endif |
175 | | |
176 | | /* Get wall clock time, take 8 bits. */ |
177 | | clock_gettime(CLOCK_REALTIME, &ts); |
178 | | v = (unsigned char)(ts.tv_nsec & 0xFF); |
179 | | ossl_rand_pool_add(pool, arg, &v, sizeof(v), 2); |
180 | | } |
181 | | return ossl_rand_pool_entropy_available(pool); |
182 | | } |
183 | | |
184 | | void ossl_rand_pool_cleanup(void) |
185 | | { |
186 | | } |
187 | | |
188 | | void ossl_rand_pool_keep_random_devices_open(int keep) |
189 | | { |
190 | | } |
191 | | |
192 | | #else |
193 | | |
194 | | #if defined(OPENSSL_RAND_SEED_EGD) && (defined(OPENSSL_NO_EGD) || !defined(DEVRANDOM_EGD)) |
195 | | #error "Seeding uses EGD but EGD is turned off or no device given" |
196 | | #endif |
197 | | |
198 | | #if defined(OPENSSL_RAND_SEED_DEVRANDOM) && !defined(DEVRANDOM) |
199 | | #error "Seeding uses urandom but DEVRANDOM is not configured" |
200 | | #endif |
201 | | |
202 | | #if defined(OPENSSL_RAND_SEED_OS) |
203 | | #if !defined(DEVRANDOM) |
204 | | #error "OS seeding requires DEVRANDOM to be configured" |
205 | | #endif |
206 | | #define OPENSSL_RAND_SEED_GETRANDOM |
207 | | #define OPENSSL_RAND_SEED_DEVRANDOM |
208 | | #endif |
209 | | |
210 | | #if defined(OPENSSL_RAND_SEED_LIBRANDOM) |
211 | | #error "librandom not (yet) supported" |
212 | | #endif |
213 | | |
214 | | #if (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND) |
215 | | /* |
216 | | * sysctl_random(): Use sysctl() to read a random number from the kernel |
217 | | * Returns the number of bytes returned in buf on success, -1 on failure. |
218 | | */ |
219 | | static ssize_t sysctl_random(char *buf, size_t buflen) |
220 | | { |
221 | | int mib[2]; |
222 | | size_t done = 0; |
223 | | size_t len; |
224 | | |
225 | | /* |
226 | | * Note: sign conversion between size_t and ssize_t is safe even |
227 | | * without a range check, see comment in syscall_random() |
228 | | */ |
229 | | |
230 | | /* |
231 | | * On FreeBSD old implementations returned longs, newer versions support |
232 | | * variable sizes up to 256 byte. The code below would not work properly |
233 | | * when the sysctl returns long and we want to request something not a |
234 | | * multiple of longs, which should never be the case. |
235 | | */ |
236 | | #if defined(__FreeBSD__) |
237 | | if (!ossl_assert(buflen % sizeof(long) == 0)) { |
238 | | errno = EINVAL; |
239 | | return -1; |
240 | | } |
241 | | #endif |
242 | | |
243 | | /* |
244 | | * On NetBSD before 4.0 KERN_ARND was an alias for KERN_URND, and only |
245 | | * filled in an int, leaving the rest uninitialized. Since NetBSD 4.0 |
246 | | * it returns a variable number of bytes with the current version supporting |
247 | | * up to 256 bytes. |
248 | | * Just return an error on older NetBSD versions. |
249 | | */ |
250 | | #if defined(__NetBSD__) && __NetBSD_Version__ < 400000000 |
251 | | errno = ENOSYS; |
252 | | return -1; |
253 | | #endif |
254 | | |
255 | | mib[0] = CTL_KERN; |
256 | | mib[1] = KERN_ARND; |
257 | | |
258 | | do { |
259 | | len = buflen > 256 ? 256 : buflen; |
260 | | if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) |
261 | | return done > 0 ? done : -1; |
262 | | done += len; |
263 | | buf += len; |
264 | | buflen -= len; |
265 | | } while (buflen > 0); |
266 | | |
267 | | return done; |
268 | | } |
269 | | #endif |
270 | | |
271 | | #if defined(OPENSSL_RAND_SEED_GETRANDOM) |
272 | | |
273 | | #if defined(__linux) && !defined(__NR_getrandom) |
274 | | #if defined(__arm__) |
275 | | #define __NR_getrandom (__NR_SYSCALL_BASE + 384) |
276 | | #elif defined(__i386__) |
277 | | #define __NR_getrandom 355 |
278 | | #elif defined(__x86_64__) |
279 | | #if defined(__ILP32__) |
280 | | #define __NR_getrandom (__X32_SYSCALL_BIT + 318) |
281 | | #else |
282 | | #define __NR_getrandom 318 |
283 | | #endif |
284 | | #elif defined(__xtensa__) |
285 | | #define __NR_getrandom 338 |
286 | | #elif defined(__s390__) || defined(__s390x__) |
287 | | #define __NR_getrandom 349 |
288 | | #elif defined(__bfin__) |
289 | | #define __NR_getrandom 389 |
290 | | #elif defined(__powerpc__) |
291 | | #define __NR_getrandom 359 |
292 | | #elif defined(__mips__) || defined(__mips64) |
293 | | #if _MIPS_SIM == _MIPS_SIM_ABI32 |
294 | | #define __NR_getrandom (__NR_Linux + 353) |
295 | | #elif _MIPS_SIM == _MIPS_SIM_ABI64 |
296 | | #define __NR_getrandom (__NR_Linux + 313) |
297 | | #elif _MIPS_SIM == _MIPS_SIM_NABI32 |
298 | | #define __NR_getrandom (__NR_Linux + 317) |
299 | | #endif |
300 | | #elif defined(__hppa__) |
301 | | #define __NR_getrandom (__NR_Linux + 339) |
302 | | #elif defined(__sparc__) |
303 | | #define __NR_getrandom 347 |
304 | | #elif defined(__ia64__) |
305 | | #define __NR_getrandom 1339 |
306 | | #elif defined(__alpha__) |
307 | | #define __NR_getrandom 511 |
308 | | #elif defined(__sh__) |
309 | | #if defined(__SH5__) |
310 | | #define __NR_getrandom 373 |
311 | | #else |
312 | | #define __NR_getrandom 384 |
313 | | #endif |
314 | | #elif defined(__avr32__) |
315 | | #define __NR_getrandom 317 |
316 | | #elif defined(__microblaze__) |
317 | | #define __NR_getrandom 385 |
318 | | #elif defined(__m68k__) |
319 | | #define __NR_getrandom 352 |
320 | | #elif defined(__cris__) |
321 | | #define __NR_getrandom 356 |
322 | | #elif defined(__aarch64__) |
323 | | #define __NR_getrandom 278 |
324 | | #else /* generic */ |
325 | | #define __NR_getrandom 278 |
326 | | #endif |
327 | | #endif |
328 | | |
329 | | /* |
330 | | * syscall_random(): Try to get random data using a system call |
331 | | * returns the number of bytes returned in buf, or < 0 on error. |
332 | | */ |
333 | | static ssize_t syscall_random(void *buf, size_t buflen) |
334 | 1.17k | { |
335 | | /* |
336 | | * Note: 'buflen' equals the size of the buffer which is used by the |
337 | | * get_entropy() callback of the RAND_DRBG. It is roughly bounded by |
338 | | * |
339 | | * 2 * RAND_POOL_FACTOR * (RAND_DRBG_STRENGTH / 8) = 2^14 |
340 | | * |
341 | | * which is way below the OSSL_SSIZE_MAX limit. Therefore sign conversion |
342 | | * between size_t and ssize_t is safe even without a range check. |
343 | | */ |
344 | | |
345 | | /* |
346 | | * Do runtime detection to find getentropy(). |
347 | | * |
348 | | * Known OSs that should support this: |
349 | | * - Darwin since 16 (OSX 10.12, IOS 10.0). |
350 | | * - Solaris since 11.3 |
351 | | * - OpenBSD since 5.6 |
352 | | * - Linux since 3.17 with glibc 2.25 |
353 | | * - FreeBSD since 12.0 (1200061) |
354 | | * |
355 | | * Note: Sometimes getentropy() can be provided but not implemented |
356 | | * internally. So we need to check errno for ENOSYS |
357 | | */ |
358 | 1.17k | #if !defined(__DragonFly__) && !defined(__NetBSD__) |
359 | 1.17k | #if defined(__GNUC__) && __GNUC__ >= 2 && defined(__ELF__) && !defined(__hpux) |
360 | 1.17k | extern int getentropy(void *buffer, size_t length) __attribute__((weak)); |
361 | | |
362 | 1.17k | if (getentropy != NULL) { |
363 | 1.17k | if (getentropy(buf, buflen) == 0) |
364 | 1.17k | return (ssize_t)buflen; |
365 | 0 | if (errno != ENOSYS) |
366 | 0 | return -1; |
367 | 0 | } |
368 | | #elif defined(OPENSSL_APPLE_CRYPTO_RANDOM) |
369 | | |
370 | | if (CCRandomGenerateBytes(buf, buflen) == kCCSuccess) |
371 | | return (ssize_t)buflen; |
372 | | |
373 | | return -1; |
374 | | #else |
375 | | union { |
376 | | void *p; |
377 | | int (*f)(void *buffer, size_t length); |
378 | | } p_getentropy; |
379 | | |
380 | | /* |
381 | | * We could cache the result of the lookup, but we normally don't |
382 | | * call this function often. |
383 | | */ |
384 | | ERR_set_mark(); |
385 | | p_getentropy.p = DSO_global_lookup("getentropy"); |
386 | | ERR_pop_to_mark(); |
387 | | if (p_getentropy.p != NULL) |
388 | | return p_getentropy.f(buf, buflen) == 0 ? (ssize_t)buflen : -1; |
389 | | #endif |
390 | 0 | #endif /* !__DragonFly__ */ |
391 | | |
392 | | /* Linux supports this since version 3.17 */ |
393 | 0 | #if defined(__linux) && defined(__NR_getrandom) |
394 | 0 | return syscall(__NR_getrandom, buf, buflen, 0); |
395 | | #elif (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND) |
396 | | return sysctl_random(buf, buflen); |
397 | | #elif (defined(__DragonFly__) && __DragonFly_version >= 500700) \ |
398 | | || (defined(__NetBSD__) && __NetBSD_Version >= 1000000000) |
399 | | return getrandom(buf, buflen, 0); |
400 | | #else |
401 | | errno = ENOSYS; |
402 | | return -1; |
403 | | #endif |
404 | 1.17k | } |
405 | | #endif /* defined(OPENSSL_RAND_SEED_GETRANDOM) */ |
406 | | |
407 | | #if defined(OPENSSL_RAND_SEED_DEVRANDOM) |
408 | | static const char *random_device_paths[] = { DEVRANDOM }; |
409 | | static struct random_device { |
410 | | int fd; |
411 | | dev_t dev; |
412 | | ino_t ino; |
413 | | mode_t mode; |
414 | | dev_t rdev; |
415 | | } random_devices[OSSL_NELEM(random_device_paths)]; |
416 | | static int keep_random_devices_open = 1; |
417 | | |
418 | | #if defined(__linux) && defined(DEVRANDOM_WAIT) \ |
419 | | && defined(OPENSSL_RAND_SEED_GETRANDOM) |
420 | | static void *shm_addr; |
421 | | |
422 | | static void cleanup_shm(void) |
423 | 0 | { |
424 | 0 | shmdt(shm_addr); |
425 | 0 | } |
426 | | |
427 | | /* |
428 | | * Ensure that the system randomness source has been adequately seeded. |
429 | | * This is done by having the first start of libcrypto, wait until the device |
430 | | * /dev/random becomes able to supply a byte of entropy. Subsequent starts |
431 | | * of the library and later reseedings do not need to do this. |
432 | | */ |
433 | | static int wait_random_seeded(void) |
434 | 0 | { |
435 | 0 | static int seeded = OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID < 0; |
436 | 0 | static const int kernel_version[] = { DEVRANDOM_SAFE_KERNEL }; |
437 | 0 | int kernel[2]; |
438 | 0 | int shm_id, fd, r; |
439 | 0 | char c, *p; |
440 | 0 | struct utsname un; |
441 | 0 | fd_set fds; |
442 | |
|
443 | 0 | if (!seeded) { |
444 | | /* See if anything has created the global seeded indication */ |
445 | 0 | if ((shm_id = shmget(OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID, 1, 0)) == -1) { |
446 | | /* |
447 | | * Check the kernel's version and fail if it is too recent. |
448 | | * |
449 | | * Linux kernels from 4.8 onwards do not guarantee that |
450 | | * /dev/urandom is properly seeded when /dev/random becomes |
451 | | * readable. However, such kernels support the getentropy(2) |
452 | | * system call and this should always succeed which renders |
453 | | * this alternative but essentially identical source moot. |
454 | | */ |
455 | 0 | if (uname(&un) == 0) { |
456 | 0 | kernel[0] = atoi(un.release); |
457 | 0 | p = strchr(un.release, '.'); |
458 | 0 | kernel[1] = p == NULL ? 0 : atoi(p + 1); |
459 | 0 | if (kernel[0] > kernel_version[0] |
460 | 0 | || (kernel[0] == kernel_version[0] |
461 | 0 | && kernel[1] >= kernel_version[1])) { |
462 | 0 | return 0; |
463 | 0 | } |
464 | 0 | } |
465 | | /* Open /dev/random and wait for it to be readable */ |
466 | 0 | if ((fd = open(DEVRANDOM_WAIT, O_RDONLY)) != -1) { |
467 | 0 | if (DEVRANDM_WAIT_USE_SELECT && fd < FD_SETSIZE) { |
468 | 0 | FD_ZERO(&fds); |
469 | 0 | FD_SET(fd, &fds); |
470 | 0 | while ((r = select(fd + 1, &fds, NULL, NULL, NULL)) < 0 |
471 | 0 | && errno == EINTR) |
472 | 0 | ; |
473 | 0 | } else { |
474 | 0 | while ((r = read(fd, &c, 1)) < 0 && errno == EINTR) |
475 | 0 | ; |
476 | 0 | } |
477 | 0 | close(fd); |
478 | 0 | if (r == 1) { |
479 | 0 | seeded = 1; |
480 | | /* Create the shared memory indicator */ |
481 | 0 | shm_id = shmget(OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID, 1, |
482 | 0 | IPC_CREAT | S_IRUSR | S_IRGRP | S_IROTH); |
483 | 0 | } |
484 | 0 | } |
485 | 0 | } |
486 | 0 | if (shm_id != -1) { |
487 | 0 | seeded = 1; |
488 | | /* |
489 | | * Map the shared memory to prevent its premature destruction. |
490 | | * If this call fails, it isn't a big problem. |
491 | | */ |
492 | 0 | shm_addr = shmat(shm_id, NULL, SHM_RDONLY); |
493 | 0 | if (shm_addr != (void *)-1) |
494 | 0 | OPENSSL_atexit(&cleanup_shm); |
495 | 0 | } |
496 | 0 | } |
497 | 0 | return seeded; |
498 | 0 | } |
499 | | #else /* defined __linux && DEVRANDOM_WAIT && OPENSSL_RAND_SEED_GETRANDOM */ |
500 | | static int wait_random_seeded(void) |
501 | | { |
502 | | return 1; |
503 | | } |
504 | | #endif |
505 | | |
506 | | /* |
507 | | * Verify that the file descriptor associated with the random source is |
508 | | * still valid. The rationale for doing this is the fact that it is not |
509 | | * uncommon for daemons to close all open file handles when daemonizing. |
510 | | * So the handle might have been closed or even reused for opening |
511 | | * another file. |
512 | | */ |
513 | | static int check_random_device(struct random_device *rd) |
514 | 244 | { |
515 | 244 | struct stat st; |
516 | | |
517 | 244 | return rd->fd != -1 |
518 | 0 | && fstat(rd->fd, &st) != -1 |
519 | 0 | && rd->dev == st.st_dev |
520 | 0 | && rd->ino == st.st_ino |
521 | 0 | && ((rd->mode ^ st.st_mode) & ~(S_IRWXU | S_IRWXG | S_IRWXO)) == 0 |
522 | 0 | && rd->rdev == st.st_rdev; |
523 | 244 | } |
524 | | |
525 | | /* |
526 | | * Open a random device if required and return its file descriptor or -1 on error |
527 | | */ |
528 | | static int get_random_device(size_t n) |
529 | 0 | { |
530 | 0 | struct stat st; |
531 | 0 | struct random_device *rd = &random_devices[n]; |
532 | | |
533 | | /* reuse existing file descriptor if it is (still) valid */ |
534 | 0 | if (check_random_device(rd)) |
535 | 0 | return rd->fd; |
536 | | |
537 | | /* open the random device ... */ |
538 | 0 | if ((rd->fd = open(random_device_paths[n], O_RDONLY)) == -1) |
539 | 0 | return rd->fd; |
540 | | |
541 | | /* ... and cache its relevant stat(2) data */ |
542 | 0 | if (fstat(rd->fd, &st) != -1) { |
543 | 0 | rd->dev = st.st_dev; |
544 | 0 | rd->ino = st.st_ino; |
545 | 0 | rd->mode = st.st_mode; |
546 | 0 | rd->rdev = st.st_rdev; |
547 | 0 | } else { |
548 | 0 | close(rd->fd); |
549 | 0 | rd->fd = -1; |
550 | 0 | } |
551 | |
|
552 | 0 | return rd->fd; |
553 | 0 | } |
554 | | |
555 | | /* |
556 | | * Close a random device making sure it is a random device |
557 | | */ |
558 | | static void close_random_device(size_t n) |
559 | 244 | { |
560 | 244 | struct random_device *rd = &random_devices[n]; |
561 | | |
562 | 244 | if (check_random_device(rd)) |
563 | 0 | close(rd->fd); |
564 | 244 | rd->fd = -1; |
565 | 244 | } |
566 | | |
567 | | int ossl_rand_pool_init(void) |
568 | 79 | { |
569 | 79 | size_t i; |
570 | | |
571 | 395 | for (i = 0; i < OSSL_NELEM(random_devices); i++) |
572 | 316 | random_devices[i].fd = -1; |
573 | | |
574 | 79 | return 1; |
575 | 79 | } |
576 | | |
577 | | void ossl_rand_pool_cleanup(void) |
578 | 61 | { |
579 | 61 | size_t i; |
580 | | |
581 | 305 | for (i = 0; i < OSSL_NELEM(random_devices); i++) |
582 | 244 | close_random_device(i); |
583 | 61 | } |
584 | | |
585 | | void ossl_rand_pool_keep_random_devices_open(int keep) |
586 | 0 | { |
587 | 0 | if (!keep) |
588 | 0 | ossl_rand_pool_cleanup(); |
589 | |
|
590 | 0 | keep_random_devices_open = keep; |
591 | 0 | } |
592 | | |
593 | | #else /* !defined(OPENSSL_RAND_SEED_DEVRANDOM) */ |
594 | | |
595 | | int ossl_rand_pool_init(void) |
596 | | { |
597 | | return 1; |
598 | | } |
599 | | |
600 | | void ossl_rand_pool_cleanup(void) |
601 | | { |
602 | | } |
603 | | |
604 | | void ossl_rand_pool_keep_random_devices_open(int keep) |
605 | | { |
606 | | } |
607 | | |
608 | | #endif /* defined(OPENSSL_RAND_SEED_DEVRANDOM) */ |
609 | | |
610 | | /* |
611 | | * Try the various seeding methods in turn, exit when successful. |
612 | | * |
613 | | * If more than one entropy source is available, is it |
614 | | * preferable to stop as soon as enough entropy has been collected |
615 | | * (as favored by @rsalz) or should one rather be defensive and add |
616 | | * more entropy than requested and/or from different sources? |
617 | | * |
618 | | * Currently, the user can select multiple entropy sources in the |
619 | | * configure step, yet in practice only the first available source |
620 | | * will be used. A more flexible solution has been requested, but |
621 | | * currently it is not clear how this can be achieved without |
622 | | * overengineering the problem. There are many parameters which |
623 | | * could be taken into account when selecting the order and amount |
624 | | * of input from the different entropy sources (trust, quality, |
625 | | * possibility of blocking). |
626 | | */ |
627 | | size_t ossl_pool_acquire_entropy(RAND_POOL *pool) |
628 | 1.17k | { |
629 | | #if defined(OPENSSL_RAND_SEED_NONE) |
630 | | return ossl_rand_pool_entropy_available(pool); |
631 | | #else |
632 | 1.17k | size_t entropy_available = 0; |
633 | | |
634 | 1.17k | (void)entropy_available; /* avoid compiler warning */ |
635 | | |
636 | 1.17k | #if defined(OPENSSL_RAND_SEED_GETRANDOM) |
637 | 1.17k | { |
638 | 1.17k | size_t bytes_needed; |
639 | 1.17k | unsigned char *buffer; |
640 | 1.17k | ssize_t bytes; |
641 | | /* Maximum allowed number of consecutive unsuccessful attempts */ |
642 | 1.17k | int attempts = 3; |
643 | | |
644 | 1.17k | bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/); |
645 | 2.35k | while (bytes_needed != 0 && attempts-- > 0) { |
646 | 1.17k | buffer = ossl_rand_pool_add_begin(pool, bytes_needed); |
647 | 1.17k | bytes = syscall_random(buffer, bytes_needed); |
648 | 1.17k | if (bytes > 0) { |
649 | 1.17k | ossl_rand_pool_add_end(pool, bytes, 8 * bytes); |
650 | 1.17k | bytes_needed -= bytes; |
651 | 1.17k | attempts = 3; /* reset counter after successful attempt */ |
652 | 1.17k | } else if (bytes < 0 && errno != EINTR) { |
653 | 0 | break; |
654 | 0 | } |
655 | 1.17k | } |
656 | 1.17k | } |
657 | 1.17k | entropy_available = ossl_rand_pool_entropy_available(pool); |
658 | 1.17k | if (entropy_available > 0) |
659 | 1.17k | return entropy_available; |
660 | 0 | #endif |
661 | | |
662 | | #if defined(OPENSSL_RAND_SEED_LIBRANDOM) |
663 | | { |
664 | | /* Not yet implemented. */ |
665 | | } |
666 | | #endif |
667 | | |
668 | 0 | #if defined(OPENSSL_RAND_SEED_DEVRANDOM) |
669 | 0 | if (wait_random_seeded()) { |
670 | 0 | size_t bytes_needed; |
671 | 0 | unsigned char *buffer; |
672 | 0 | size_t i; |
673 | |
|
674 | 0 | bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/); |
675 | 0 | for (i = 0; bytes_needed > 0 && i < OSSL_NELEM(random_device_paths); |
676 | 0 | i++) { |
677 | 0 | ssize_t bytes = 0; |
678 | | /* Maximum number of consecutive unsuccessful attempts */ |
679 | 0 | int attempts = 3; |
680 | 0 | const int fd = get_random_device(i); |
681 | |
|
682 | 0 | if (fd == -1) |
683 | 0 | continue; |
684 | | |
685 | 0 | while (bytes_needed != 0 && attempts-- > 0) { |
686 | 0 | buffer = ossl_rand_pool_add_begin(pool, bytes_needed); |
687 | 0 | bytes = read(fd, buffer, bytes_needed); |
688 | |
|
689 | 0 | if (bytes > 0) { |
690 | 0 | ossl_rand_pool_add_end(pool, bytes, 8 * bytes); |
691 | 0 | bytes_needed -= bytes; |
692 | 0 | attempts = 3; /* reset counter on successful attempt */ |
693 | 0 | } else if (bytes < 0 && errno != EINTR) { |
694 | 0 | break; |
695 | 0 | } |
696 | 0 | } |
697 | 0 | if (bytes < 0 || !keep_random_devices_open) |
698 | 0 | close_random_device(i); |
699 | |
|
700 | 0 | bytes_needed = ossl_rand_pool_bytes_needed(pool, 1); |
701 | 0 | } |
702 | 0 | entropy_available = ossl_rand_pool_entropy_available(pool); |
703 | 0 | if (entropy_available > 0) |
704 | 0 | return entropy_available; |
705 | 0 | } |
706 | 0 | #endif |
707 | | |
708 | | #if defined(OPENSSL_RAND_SEED_RDTSC) |
709 | | entropy_available = ossl_prov_acquire_entropy_from_tsc(pool); |
710 | | if (entropy_available > 0) |
711 | | return entropy_available; |
712 | | #endif |
713 | | |
714 | | #if defined(OPENSSL_RAND_SEED_RDCPU) |
715 | | entropy_available = ossl_prov_acquire_entropy_from_cpu(pool); |
716 | | if (entropy_available > 0) |
717 | | return entropy_available; |
718 | | #endif |
719 | | |
720 | | #if defined(OPENSSL_RAND_SEED_EGD) |
721 | | { |
722 | | static const char *paths[] = { DEVRANDOM_EGD, NULL }; |
723 | | size_t bytes_needed; |
724 | | unsigned char *buffer; |
725 | | int i; |
726 | | |
727 | | bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/); |
728 | | for (i = 0; bytes_needed > 0 && paths[i] != NULL; i++) { |
729 | | size_t bytes = 0; |
730 | | int num; |
731 | | |
732 | | buffer = ossl_rand_pool_add_begin(pool, bytes_needed); |
733 | | num = RAND_query_egd_bytes(paths[i], |
734 | | buffer, (int)bytes_needed); |
735 | | if (num == (int)bytes_needed) |
736 | | bytes = bytes_needed; |
737 | | |
738 | | ossl_rand_pool_add_end(pool, bytes, 8 * bytes); |
739 | | bytes_needed = ossl_rand_pool_bytes_needed(pool, 1); |
740 | | } |
741 | | entropy_available = ossl_rand_pool_entropy_available(pool); |
742 | | if (entropy_available > 0) |
743 | | return entropy_available; |
744 | | } |
745 | | #endif |
746 | | |
747 | 0 | return ossl_rand_pool_entropy_available(pool); |
748 | 0 | #endif |
749 | 0 | } |
750 | | #endif |
751 | | #endif |
752 | | |
753 | | #if (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS)) \ |
754 | | || defined(__DJGPP__) |
755 | | int ossl_pool_add_nonce_data(RAND_POOL *pool) |
756 | 435 | { |
757 | 435 | struct { |
758 | 435 | pid_t pid; |
759 | 435 | CRYPTO_THREAD_ID tid; |
760 | 435 | uint64_t time; |
761 | 435 | } data; |
762 | | |
763 | | /* Erase the entire structure including any padding */ |
764 | 435 | memset(&data, 0, sizeof(data)); |
765 | | |
766 | | /* |
767 | | * Add process id, thread id, and a high resolution timestamp to |
768 | | * ensure that the nonce is unique with high probability for |
769 | | * different process instances. |
770 | | */ |
771 | 435 | data.pid = getpid(); |
772 | 435 | data.tid = CRYPTO_THREAD_get_current_id(); |
773 | 435 | data.time = get_time_stamp(); |
774 | | |
775 | 435 | return ossl_rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0); |
776 | 435 | } |
777 | | |
778 | | int ossl_rand_pool_add_additional_data(RAND_POOL *pool) |
779 | 0 | { |
780 | 0 | struct { |
781 | 0 | int fork_id; |
782 | 0 | CRYPTO_THREAD_ID tid; |
783 | 0 | uint64_t time; |
784 | 0 | } data; |
785 | | |
786 | | /* Erase the entire structure including any padding */ |
787 | 0 | memset(&data, 0, sizeof(data)); |
788 | | |
789 | | /* |
790 | | * Add some noise from the thread id and a high resolution timer. |
791 | | * The fork_id adds some extra fork-safety. |
792 | | * The thread id adds a little randomness if the drbg is accessed |
793 | | * concurrently (which is the case for the <master> drbg). |
794 | | */ |
795 | 0 | data.fork_id = openssl_get_fork_id(); |
796 | 0 | data.tid = CRYPTO_THREAD_get_current_id(); |
797 | 0 | data.time = get_timer_bits(); |
798 | |
|
799 | 0 | return ossl_rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0); |
800 | 0 | } |
801 | | |
802 | | /* |
803 | | * Get the current time with the highest possible resolution |
804 | | * |
805 | | * The time stamp is added to the nonce, so it is optimized for not repeating. |
806 | | * The current time is ideal for this purpose, provided the computer's clock |
807 | | * is synchronized. |
808 | | */ |
809 | | static uint64_t get_time_stamp(void) |
810 | 435 | { |
811 | 435 | #if defined(OSSL_POSIX_TIMER_OKAY) |
812 | 435 | { |
813 | 435 | struct timespec ts; |
814 | | |
815 | 435 | if (clock_gettime(CLOCK_REALTIME, &ts) == 0) |
816 | 435 | return TWO32TO64(ts.tv_sec, ts.tv_nsec); |
817 | 435 | } |
818 | 0 | #endif |
819 | 0 | #if defined(__unix__) \ |
820 | 0 | || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) |
821 | 0 | { |
822 | 0 | struct timeval tv; |
823 | |
|
824 | 0 | if (gettimeofday(&tv, NULL) == 0) |
825 | 0 | return TWO32TO64(tv.tv_sec, tv.tv_usec); |
826 | 0 | } |
827 | 0 | #endif |
828 | 0 | return time(NULL); |
829 | 0 | } |
830 | | |
831 | | /* |
832 | | * Get an arbitrary timer value of the highest possible resolution |
833 | | * |
834 | | * The timer value is added as random noise to the additional data, |
835 | | * which is not considered a trusted entropy sourec, so any result |
836 | | * is acceptable. |
837 | | */ |
838 | | static uint64_t get_timer_bits(void) |
839 | 0 | { |
840 | 0 | uint64_t res = OPENSSL_rdtsc(); |
841 | |
|
842 | 0 | if (res != 0) |
843 | 0 | return res; |
844 | | |
845 | | #if defined(__sun) || defined(__hpux) |
846 | | return gethrtime(); |
847 | | #elif defined(_AIX) |
848 | | { |
849 | | timebasestruct_t t; |
850 | | |
851 | | read_wall_time(&t, TIMEBASE_SZ); |
852 | | return TWO32TO64(t.tb_high, t.tb_low); |
853 | | } |
854 | | #elif defined(OSSL_POSIX_TIMER_OKAY) |
855 | 0 | { |
856 | 0 | struct timespec ts; |
857 | |
|
858 | 0 | #ifdef CLOCK_BOOTTIME |
859 | 0 | #define CLOCK_TYPE CLOCK_BOOTTIME |
860 | | #elif defined(_POSIX_MONOTONIC_CLOCK) |
861 | | #define CLOCK_TYPE CLOCK_MONOTONIC |
862 | | #else |
863 | | #define CLOCK_TYPE CLOCK_REALTIME |
864 | | #endif |
865 | |
|
866 | 0 | if (clock_gettime(CLOCK_TYPE, &ts) == 0) |
867 | 0 | return TWO32TO64(ts.tv_sec, ts.tv_nsec); |
868 | 0 | } |
869 | 0 | #endif |
870 | 0 | #if defined(__unix__) \ |
871 | 0 | || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) |
872 | 0 | { |
873 | 0 | struct timeval tv; |
874 | |
|
875 | 0 | if (gettimeofday(&tv, NULL) == 0) |
876 | 0 | return TWO32TO64(tv.tv_sec, tv.tv_usec); |
877 | 0 | } |
878 | 0 | #endif |
879 | 0 | return time(NULL); |
880 | 0 | } |
881 | | #endif /* (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS)) \ |
882 | | || defined(__DJGPP__) */ |