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