2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
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
13 #include "internal/e_os.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"
21 #include "internal/dso.h"
22 #include "prov/seeding.h"
25 # include <sys/syscall.h>
26 # ifdef DEVRANDOM_WAIT
28 # include <sys/utsname.h>
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>
36 #if defined(__OpenBSD__)
37 # include <sys/param.h>
39 #if defined(__DragonFly__)
40 # include <sys/param.h>
41 # include <sys/random.h>
44 #if (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS)) \
46 # include <sys/types.h>
47 # include <sys/stat.h>
50 # include <sys/time.h>
52 static uint64_t get_time_stamp(void);
53 static uint64_t get_timer_bits(void);
55 /* Macro to convert two thirty two bit values into a sixty four bit one */
56 # define TWO32TO64(a, b) ((((uint64_t)(a)) << 32) + (b))
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
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.
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.
72 * The final condition is:
73 * "have posix timers and either not glibc or glibc without -lrt"
75 * The nested #if sequences are required to avoid using a parameterised
76 * macro that might be undefined.
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
89 # define OSSL_POSIX_TIMER_OKAY
92 #endif /* (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS))
93 || defined(__DJGPP__) */
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
106 #if defined(OPENSSL_SYS_UEFI) && !defined(OPENSSL_RAND_SEED_NONE)
107 # error "UEFI only supports seeding NONE"
110 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) \
111 || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) \
112 || defined(OPENSSL_SYS_UEFI))
114 # if defined(OPENSSL_SYS_VOS)
116 # ifndef OPENSSL_RAND_SEED_OS
117 # error "Unsupported seeding method configured; must be os"
120 # if defined(OPENSSL_SYS_VOS_HPPA) && defined(OPENSSL_SYS_VOS_IA32)
121 # error "Unsupported HP-PA and IA32 at the same time."
123 # if !defined(OPENSSL_SYS_VOS_HPPA) && !defined(OPENSSL_SYS_VOS_IA32)
124 # error "Must have one of HP-PA or IA32"
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
139 * As a precaution, we assume only 2 bits of entropy per byte.
141 size_t ossl_pool_acquire_entropy(RAND_POOL *pool)
148 # ifdef OPENSSL_SYS_VOS_HPPA
150 extern void s$sleep(long *_duration, short int *_code);
153 extern void s$sleep2(long long *_duration, short int *_code);
156 bytes_needed = ossl_rand_pool_bytes_needed(pool, 4 /*entropy_factor*/);
158 for (i = 0; i < bytes_needed; i++) {
160 * burn some cpu; hope for interrupts, cache collisions, bus
163 for (k = 0; k < 99; k++)
164 ts.tv_nsec = random();
166 # ifdef OPENSSL_SYS_VOS_HPPA
167 /* sleep for 1/1024 of a second (976 us). */
169 s$sleep(&duration, &code);
171 /* sleep for 1/65536 of a second (15 us). */
173 s$sleep2(&duration, &code);
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);
181 return ossl_rand_pool_entropy_available(pool);
184 void ossl_rand_pool_cleanup(void)
188 void ossl_rand_pool_keep_random_devices_open(int keep)
194 # if defined(OPENSSL_RAND_SEED_EGD) && \
195 (defined(OPENSSL_NO_EGD) || !defined(DEVRANDOM_EGD))
196 # error "Seeding uses EGD but EGD is turned off or no device given"
199 # if defined(OPENSSL_RAND_SEED_DEVRANDOM) && !defined(DEVRANDOM)
200 # error "Seeding uses urandom but DEVRANDOM is not configured"
203 # if defined(OPENSSL_RAND_SEED_OS)
204 # if !defined(DEVRANDOM)
205 # error "OS seeding requires DEVRANDOM to be configured"
207 # define OPENSSL_RAND_SEED_GETRANDOM
208 # define OPENSSL_RAND_SEED_DEVRANDOM
211 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
212 # error "librandom not (yet) supported"
215 # if (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND)
217 * sysctl_random(): Use sysctl() to read a random number from the kernel
218 * Returns the number of bytes returned in buf on success, -1 on failure.
220 static ssize_t sysctl_random(char *buf, size_t buflen)
227 * Note: sign conversion between size_t and ssize_t is safe even
228 * without a range check, see comment in syscall_random()
232 * On FreeBSD old implementations returned longs, newer versions support
233 * variable sizes up to 256 byte. The code below would not work properly
234 * when the sysctl returns long and we want to request something not a
235 * multiple of longs, which should never be the case.
237 #if defined(__FreeBSD__)
238 if (!ossl_assert(buflen % sizeof(long) == 0)) {
245 * On NetBSD before 4.0 KERN_ARND was an alias for KERN_URND, and only
246 * filled in an int, leaving the rest uninitialized. Since NetBSD 4.0
247 * it returns a variable number of bytes with the current version supporting
249 * Just return an error on older NetBSD versions.
251 #if defined(__NetBSD__) && __NetBSD_Version__ < 400000000
260 len = buflen > 256 ? 256 : buflen;
261 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1)
262 return done > 0 ? done : -1;
266 } while (buflen > 0);
272 # if defined(OPENSSL_RAND_SEED_GETRANDOM)
274 # if defined(__linux) && !defined(__NR_getrandom)
275 # if defined(__arm__)
276 # define __NR_getrandom (__NR_SYSCALL_BASE+384)
277 # elif defined(__i386__)
278 # define __NR_getrandom 355
279 # elif defined(__x86_64__)
280 # if defined(__ILP32__)
281 # define __NR_getrandom (__X32_SYSCALL_BIT + 318)
283 # define __NR_getrandom 318
285 # elif defined(__xtensa__)
286 # define __NR_getrandom 338
287 # elif defined(__s390__) || defined(__s390x__)
288 # define __NR_getrandom 349
289 # elif defined(__bfin__)
290 # define __NR_getrandom 389
291 # elif defined(__powerpc__)
292 # define __NR_getrandom 359
293 # elif defined(__mips__) || defined(__mips64)
294 # if _MIPS_SIM == _MIPS_SIM_ABI32
295 # define __NR_getrandom (__NR_Linux + 353)
296 # elif _MIPS_SIM == _MIPS_SIM_ABI64
297 # define __NR_getrandom (__NR_Linux + 313)
298 # elif _MIPS_SIM == _MIPS_SIM_NABI32
299 # define __NR_getrandom (__NR_Linux + 317)
301 # elif defined(__hppa__)
302 # define __NR_getrandom (__NR_Linux + 339)
303 # elif defined(__sparc__)
304 # define __NR_getrandom 347
305 # elif defined(__ia64__)
306 # define __NR_getrandom 1339
307 # elif defined(__alpha__)
308 # define __NR_getrandom 511
309 # elif defined(__sh__)
310 # if defined(__SH5__)
311 # define __NR_getrandom 373
313 # define __NR_getrandom 384
315 # elif defined(__avr32__)
316 # define __NR_getrandom 317
317 # elif defined(__microblaze__)
318 # define __NR_getrandom 385
319 # elif defined(__m68k__)
320 # define __NR_getrandom 352
321 # elif defined(__cris__)
322 # define __NR_getrandom 356
323 # elif defined(__aarch64__)
324 # define __NR_getrandom 278
326 # define __NR_getrandom 278
331 * syscall_random(): Try to get random data using a system call
332 * returns the number of bytes returned in buf, or < 0 on error.
334 static ssize_t syscall_random(void *buf, size_t buflen)
337 * Note: 'buflen' equals the size of the buffer which is used by the
338 * get_entropy() callback of the RAND_DRBG. It is roughly bounded by
340 * 2 * RAND_POOL_FACTOR * (RAND_DRBG_STRENGTH / 8) = 2^14
342 * which is way below the OSSL_SSIZE_MAX limit. Therefore sign conversion
343 * between size_t and ssize_t is safe even without a range check.
347 * Do runtime detection to find getentropy().
349 * Known OSs that should support this:
350 * - Darwin since 16 (OSX 10.12, IOS 10.0).
351 * - Solaris since 11.3
352 * - OpenBSD since 5.6
353 * - Linux since 3.17 with glibc 2.25
354 * - FreeBSD since 12.0 (1200061)
356 * Note: Sometimes getentropy() can be provided but not implemented
357 * internally. So we need to check errno for ENOSYS
359 # if !defined(__DragonFly__) && !defined(__NetBSD__)
360 # if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__) && !defined(__hpux)
361 extern int getentropy(void *buffer, size_t length) __attribute__((weak));
363 if (getentropy != NULL) {
364 if (getentropy(buf, buflen) == 0)
365 return (ssize_t)buflen;
369 # elif defined(OPENSSL_APPLE_CRYPTO_RANDOM)
371 if (CCRandomGenerateBytes(buf, buflen) == kCCSuccess)
372 return (ssize_t)buflen;
378 int (*f)(void *buffer, size_t length);
382 * We could cache the result of the lookup, but we normally don't
383 * call this function often.
386 p_getentropy.p = DSO_global_lookup("getentropy");
388 if (p_getentropy.p != NULL)
389 return p_getentropy.f(buf, buflen) == 0 ? (ssize_t)buflen : -1;
391 # endif /* !__DragonFly__ */
393 /* Linux supports this since version 3.17 */
394 # if defined(__linux) && defined(__NR_getrandom)
395 return syscall(__NR_getrandom, buf, buflen, 0);
396 # elif (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND)
397 return sysctl_random(buf, buflen);
398 # elif (defined(__DragonFly__) && __DragonFly_version >= 500700) \
399 || (defined(__NetBSD__) && __NetBSD_Version >= 1000000000)
400 return getrandom(buf, buflen, 0);
406 # endif /* defined(OPENSSL_RAND_SEED_GETRANDOM) */
408 # if defined(OPENSSL_RAND_SEED_DEVRANDOM)
409 static const char *random_device_paths[] = { DEVRANDOM };
410 static struct random_device {
416 } random_devices[OSSL_NELEM(random_device_paths)];
417 static int keep_random_devices_open = 1;
419 # if defined(__linux) && defined(DEVRANDOM_WAIT) \
420 && defined(OPENSSL_RAND_SEED_GETRANDOM)
421 static void *shm_addr;
423 static void cleanup_shm(void)
429 * Ensure that the system randomness source has been adequately seeded.
430 * This is done by having the first start of libcrypto, wait until the device
431 * /dev/random becomes able to supply a byte of entropy. Subsequent starts
432 * of the library and later reseedings do not need to do this.
434 static int wait_random_seeded(void)
436 static int seeded = OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID < 0;
437 static const int kernel_version[] = { DEVRANDOM_SAFE_KERNEL };
445 /* See if anything has created the global seeded indication */
446 if ((shm_id = shmget(OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID, 1, 0)) == -1) {
448 * Check the kernel's version and fail if it is too recent.
450 * Linux kernels from 4.8 onwards do not guarantee that
451 * /dev/urandom is properly seeded when /dev/random becomes
452 * readable. However, such kernels support the getentropy(2)
453 * system call and this should always succeed which renders
454 * this alternative but essentially identical source moot.
456 if (uname(&un) == 0) {
457 kernel[0] = atoi(un.release);
458 p = strchr(un.release, '.');
459 kernel[1] = p == NULL ? 0 : atoi(p + 1);
460 if (kernel[0] > kernel_version[0]
461 || (kernel[0] == kernel_version[0]
462 && kernel[1] >= kernel_version[1])) {
466 /* Open /dev/random and wait for it to be readable */
467 if ((fd = open(DEVRANDOM_WAIT, O_RDONLY)) != -1) {
468 if (DEVRANDM_WAIT_USE_SELECT && fd < FD_SETSIZE) {
471 while ((r = select(fd + 1, &fds, NULL, NULL, NULL)) < 0
474 while ((r = read(fd, &c, 1)) < 0 && errno == EINTR);
479 /* Create the shared memory indicator */
480 shm_id = shmget(OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID, 1,
481 IPC_CREAT | S_IRUSR | S_IRGRP | S_IROTH);
488 * Map the shared memory to prevent its premature destruction.
489 * If this call fails, it isn't a big problem.
491 shm_addr = shmat(shm_id, NULL, SHM_RDONLY);
492 if (shm_addr != (void *)-1)
493 OPENSSL_atexit(&cleanup_shm);
498 # else /* defined __linux && DEVRANDOM_WAIT && OPENSSL_RAND_SEED_GETRANDOM */
499 static int wait_random_seeded(void)
506 * Verify that the file descriptor associated with the random source is
507 * still valid. The rationale for doing this is the fact that it is not
508 * uncommon for daemons to close all open file handles when daemonizing.
509 * So the handle might have been closed or even reused for opening
512 static int check_random_device(struct random_device * rd)
517 && fstat(rd->fd, &st) != -1
518 && rd->dev == st.st_dev
519 && rd->ino == st.st_ino
520 && ((rd->mode ^ st.st_mode) & ~(S_IRWXU | S_IRWXG | S_IRWXO)) == 0
521 && rd->rdev == st.st_rdev;
525 * Open a random device if required and return its file descriptor or -1 on error
527 static int get_random_device(size_t n)
530 struct random_device * rd = &random_devices[n];
532 /* reuse existing file descriptor if it is (still) valid */
533 if (check_random_device(rd))
536 /* open the random device ... */
537 if ((rd->fd = open(random_device_paths[n], O_RDONLY)) == -1)
540 /* ... and cache its relevant stat(2) data */
541 if (fstat(rd->fd, &st) != -1) {
544 rd->mode = st.st_mode;
545 rd->rdev = st.st_rdev;
555 * Close a random device making sure it is a random device
557 static void close_random_device(size_t n)
559 struct random_device * rd = &random_devices[n];
561 if (check_random_device(rd))
566 int ossl_rand_pool_init(void)
570 for (i = 0; i < OSSL_NELEM(random_devices); i++)
571 random_devices[i].fd = -1;
576 void ossl_rand_pool_cleanup(void)
580 for (i = 0; i < OSSL_NELEM(random_devices); i++)
581 close_random_device(i);
584 void ossl_rand_pool_keep_random_devices_open(int keep)
587 ossl_rand_pool_cleanup();
589 keep_random_devices_open = keep;
592 # else /* !defined(OPENSSL_RAND_SEED_DEVRANDOM) */
594 int ossl_rand_pool_init(void)
599 void ossl_rand_pool_cleanup(void)
603 void ossl_rand_pool_keep_random_devices_open(int keep)
607 # endif /* defined(OPENSSL_RAND_SEED_DEVRANDOM) */
610 * Try the various seeding methods in turn, exit when successful.
612 * If more than one entropy source is available, is it
613 * preferable to stop as soon as enough entropy has been collected
614 * (as favored by @rsalz) or should one rather be defensive and add
615 * more entropy than requested and/or from different sources?
617 * Currently, the user can select multiple entropy sources in the
618 * configure step, yet in practice only the first available source
619 * will be used. A more flexible solution has been requested, but
620 * currently it is not clear how this can be achieved without
621 * overengineering the problem. There are many parameters which
622 * could be taken into account when selecting the order and amount
623 * of input from the different entropy sources (trust, quality,
624 * possibility of blocking).
626 size_t ossl_pool_acquire_entropy(RAND_POOL *pool)
628 # if defined(OPENSSL_RAND_SEED_NONE)
629 return ossl_rand_pool_entropy_available(pool);
631 size_t entropy_available = 0;
633 (void)entropy_available; /* avoid compiler warning */
635 # if defined(OPENSSL_RAND_SEED_GETRANDOM)
638 unsigned char *buffer;
640 /* Maximum allowed number of consecutive unsuccessful attempts */
643 bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
644 while (bytes_needed != 0 && attempts-- > 0) {
645 buffer = ossl_rand_pool_add_begin(pool, bytes_needed);
646 bytes = syscall_random(buffer, bytes_needed);
648 ossl_rand_pool_add_end(pool, bytes, 8 * bytes);
649 bytes_needed -= bytes;
650 attempts = 3; /* reset counter after successful attempt */
651 } else if (bytes < 0 && errno != EINTR) {
656 entropy_available = ossl_rand_pool_entropy_available(pool);
657 if (entropy_available > 0)
658 return entropy_available;
661 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
663 /* Not yet implemented. */
667 # if defined(OPENSSL_RAND_SEED_DEVRANDOM)
668 if (wait_random_seeded()) {
670 unsigned char *buffer;
673 bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
674 for (i = 0; bytes_needed > 0 && i < OSSL_NELEM(random_device_paths);
677 /* Maximum number of consecutive unsuccessful attempts */
679 const int fd = get_random_device(i);
684 while (bytes_needed != 0 && attempts-- > 0) {
685 buffer = ossl_rand_pool_add_begin(pool, bytes_needed);
686 bytes = read(fd, buffer, bytes_needed);
689 ossl_rand_pool_add_end(pool, bytes, 8 * bytes);
690 bytes_needed -= bytes;
691 attempts = 3; /* reset counter on successful attempt */
692 } else if (bytes < 0 && errno != EINTR) {
696 if (bytes < 0 || !keep_random_devices_open)
697 close_random_device(i);
699 bytes_needed = ossl_rand_pool_bytes_needed(pool, 1);
701 entropy_available = ossl_rand_pool_entropy_available(pool);
702 if (entropy_available > 0)
703 return entropy_available;
707 # if defined(OPENSSL_RAND_SEED_RDTSC)
708 entropy_available = ossl_prov_acquire_entropy_from_tsc(pool);
709 if (entropy_available > 0)
710 return entropy_available;
713 # if defined(OPENSSL_RAND_SEED_RDCPU)
714 entropy_available = ossl_prov_acquire_entropy_from_cpu(pool);
715 if (entropy_available > 0)
716 return entropy_available;
719 # if defined(OPENSSL_RAND_SEED_EGD)
721 static const char *paths[] = { DEVRANDOM_EGD, NULL };
723 unsigned char *buffer;
726 bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
727 for (i = 0; bytes_needed > 0 && paths[i] != NULL; i++) {
731 buffer = ossl_rand_pool_add_begin(pool, bytes_needed);
732 num = RAND_query_egd_bytes(paths[i],
733 buffer, (int)bytes_needed);
734 if (num == (int)bytes_needed)
735 bytes = bytes_needed;
737 ossl_rand_pool_add_end(pool, bytes, 8 * bytes);
738 bytes_needed = ossl_rand_pool_bytes_needed(pool, 1);
740 entropy_available = ossl_rand_pool_entropy_available(pool);
741 if (entropy_available > 0)
742 return entropy_available;
746 return ossl_rand_pool_entropy_available(pool);
752 #if (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS)) \
753 || defined(__DJGPP__)
754 int ossl_pool_add_nonce_data(RAND_POOL *pool)
758 CRYPTO_THREAD_ID tid;
762 /* Erase the entire structure including any padding */
763 memset(&data, 0, sizeof(data));
766 * Add process id, thread id, and a high resolution timestamp to
767 * ensure that the nonce is unique with high probability for
768 * different process instances.
771 data.tid = CRYPTO_THREAD_get_current_id();
772 data.time = get_time_stamp();
774 return ossl_rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
777 int ossl_rand_pool_add_additional_data(RAND_POOL *pool)
781 CRYPTO_THREAD_ID tid;
785 /* Erase the entire structure including any padding */
786 memset(&data, 0, sizeof(data));
789 * Add some noise from the thread id and a high resolution timer.
790 * The fork_id adds some extra fork-safety.
791 * The thread id adds a little randomness if the drbg is accessed
792 * concurrently (which is the case for the <master> drbg).
794 data.fork_id = openssl_get_fork_id();
795 data.tid = CRYPTO_THREAD_get_current_id();
796 data.time = get_timer_bits();
798 return ossl_rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
803 * Get the current time with the highest possible resolution
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
809 static uint64_t get_time_stamp(void)
811 # if defined(OSSL_POSIX_TIMER_OKAY)
815 if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
816 return TWO32TO64(ts.tv_sec, ts.tv_nsec);
819 # if defined(__unix__) \
820 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
824 if (gettimeofday(&tv, NULL) == 0)
825 return TWO32TO64(tv.tv_sec, tv.tv_usec);
832 * Get an arbitrary timer value of the highest possible resolution
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
838 static uint64_t get_timer_bits(void)
840 uint64_t res = OPENSSL_rdtsc();
845 # if defined(__sun) || defined(__hpux)
851 read_wall_time(&t, TIMEBASE_SZ);
852 return TWO32TO64(t.tb_high, t.tb_low);
854 # elif defined(OSSL_POSIX_TIMER_OKAY)
858 # ifdef CLOCK_BOOTTIME
859 # define CLOCK_TYPE CLOCK_BOOTTIME
860 # elif defined(_POSIX_MONOTONIC_CLOCK)
861 # define CLOCK_TYPE CLOCK_MONOTONIC
863 # define CLOCK_TYPE CLOCK_REALTIME
866 if (clock_gettime(CLOCK_TYPE, &ts) == 0)
867 return TWO32TO64(ts.tv_sec, ts.tv_nsec);
870 # if defined(__unix__) \
871 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
875 if (gettimeofday(&tv, NULL) == 0)
876 return TWO32TO64(tv.tv_sec, tv.tv_usec);
881 #endif /* (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS))
882 || defined(__DJGPP__) */