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"
20 #include "internal/dso.h"
21 #include "prov/seeding.h"
24 # include <sys/syscall.h>
25 # ifdef DEVRANDOM_WAIT
27 # include <sys/utsname.h>
30 #if (defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(OPENSSL_SYS_UEFI)
31 # include <sys/types.h>
32 # include <sys/sysctl.h>
33 # include <sys/param.h>
35 #if defined(__OpenBSD__)
36 # include <sys/param.h>
38 #if defined(__DragonFly__)
39 # include <sys/param.h>
40 # include <sys/random.h>
43 #if (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS)) \
45 # include <sys/types.h>
46 # include <sys/stat.h>
49 # include <sys/time.h>
51 static uint64_t get_time_stamp(void);
52 static uint64_t get_timer_bits(void);
54 /* Macro to convert two thirty two bit values into a sixty four bit one */
55 # define TWO32TO64(a, b) ((((uint64_t)(a)) << 32) + (b))
58 * Check for the existence and support of POSIX timers. The standard
59 * says that the _POSIX_TIMERS macro will have a positive value if they
62 * However, we want an additional constraint: that the timer support does
63 * not require an extra library dependency. Early versions of glibc
64 * require -lrt to be specified on the link line to access the timers,
65 * so this needs to be checked for.
67 * It is worse because some libraries define __GLIBC__ but don't
68 * support the version testing macro (e.g. uClibc). This means
69 * an extra check is needed.
71 * The final condition is:
72 * "have posix timers and either not glibc or glibc without -lrt"
74 * The nested #if sequences are required to avoid using a parameterised
75 * macro that might be undefined.
77 # undef OSSL_POSIX_TIMER_OKAY
78 /* On some systems, _POSIX_TIMERS is defined but empty.
79 * Subtracting by 0 when comparing avoids an error in this case. */
80 # if defined(_POSIX_TIMERS) && _POSIX_TIMERS -0 > 0
81 # if defined(__GLIBC__)
82 # if defined(__GLIBC_PREREQ)
83 # if __GLIBC_PREREQ(2, 17)
84 # define OSSL_POSIX_TIMER_OKAY
88 # define OSSL_POSIX_TIMER_OKAY
91 #endif /* (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS))
92 || defined(__DJGPP__) */
94 #if defined(OPENSSL_RAND_SEED_NONE)
95 /* none means none. this simplifies the following logic */
96 # undef OPENSSL_RAND_SEED_OS
97 # undef OPENSSL_RAND_SEED_GETRANDOM
98 # undef OPENSSL_RAND_SEED_LIBRANDOM
99 # undef OPENSSL_RAND_SEED_DEVRANDOM
100 # undef OPENSSL_RAND_SEED_RDTSC
101 # undef OPENSSL_RAND_SEED_RDCPU
102 # undef OPENSSL_RAND_SEED_EGD
105 #if defined(OPENSSL_SYS_UEFI) && !defined(OPENSSL_RAND_SEED_NONE)
106 # error "UEFI only supports seeding NONE"
109 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) \
110 || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) \
111 || defined(OPENSSL_SYS_UEFI))
113 # if defined(OPENSSL_SYS_VOS)
115 # ifndef OPENSSL_RAND_SEED_OS
116 # error "Unsupported seeding method configured; must be os"
119 # if defined(OPENSSL_SYS_VOS_HPPA) && defined(OPENSSL_SYS_VOS_IA32)
120 # error "Unsupported HP-PA and IA32 at the same time."
122 # if !defined(OPENSSL_SYS_VOS_HPPA) && !defined(OPENSSL_SYS_VOS_IA32)
123 # error "Must have one of HP-PA or IA32"
127 * The following algorithm repeatedly samples the real-time clock (RTC) to
128 * generate a sequence of unpredictable data. The algorithm relies upon the
129 * uneven execution speed of the code (due to factors such as cache misses,
130 * interrupts, bus activity, and scheduling) and upon the rather large
131 * relative difference between the speed of the clock and the rate at which
132 * it can be read. If it is ported to an environment where execution speed
133 * is more constant or where the RTC ticks at a much slower rate, or the
134 * clock can be read with fewer instructions, it is likely that the results
135 * would be far more predictable. This should only be used for legacy
138 * As a precaution, we assume only 2 bits of entropy per byte.
140 size_t ossl_pool_acquire_entropy(RAND_POOL *pool)
147 # ifdef OPENSSL_SYS_VOS_HPPA
149 extern void s$sleep(long *_duration, short int *_code);
152 extern void s$sleep2(long long *_duration, short int *_code);
155 bytes_needed = ossl_rand_pool_bytes_needed(pool, 4 /*entropy_factor*/);
157 for (i = 0; i < bytes_needed; i++) {
159 * burn some cpu; hope for interrupts, cache collisions, bus
162 for (k = 0; k < 99; k++)
163 ts.tv_nsec = random();
165 # ifdef OPENSSL_SYS_VOS_HPPA
166 /* sleep for 1/1024 of a second (976 us). */
168 s$sleep(&duration, &code);
170 /* sleep for 1/65536 of a second (15 us). */
172 s$sleep2(&duration, &code);
175 /* Get wall clock time, take 8 bits. */
176 clock_gettime(CLOCK_REALTIME, &ts);
177 v = (unsigned char)(ts.tv_nsec & 0xFF);
178 ossl_rand_pool_add(pool, arg, &v, sizeof(v), 2);
180 return ossl_rand_pool_entropy_available(pool);
183 void ossl_rand_pool_cleanup(void)
187 void ossl_rand_pool_keep_random_devices_open(int keep)
193 # if defined(OPENSSL_RAND_SEED_EGD) && \
194 (defined(OPENSSL_NO_EGD) || !defined(DEVRANDOM_EGD))
195 # error "Seeding uses EGD but EGD is turned off or no device given"
198 # if defined(OPENSSL_RAND_SEED_DEVRANDOM) && !defined(DEVRANDOM)
199 # error "Seeding uses urandom but DEVRANDOM is not configured"
202 # if defined(OPENSSL_RAND_SEED_OS)
203 # if !defined(DEVRANDOM)
204 # error "OS seeding requires DEVRANDOM to be configured"
206 # define OPENSSL_RAND_SEED_GETRANDOM
207 # define OPENSSL_RAND_SEED_DEVRANDOM
210 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
211 # error "librandom not (yet) supported"
214 # if (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND)
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.
219 static ssize_t sysctl_random(char *buf, size_t buflen)
226 * Note: sign conversion between size_t and ssize_t is safe even
227 * without a range check, see comment in syscall_random()
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.
236 #if defined(__FreeBSD__)
237 if (!ossl_assert(buflen % sizeof(long) == 0)) {
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
248 * Just return an error on older NetBSD versions.
250 #if defined(__NetBSD__) && __NetBSD_Version__ < 400000000
259 len = buflen > 256 ? 256 : buflen;
260 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1)
261 return done > 0 ? done : -1;
265 } while (buflen > 0);
271 # if defined(OPENSSL_RAND_SEED_GETRANDOM)
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)
282 # define __NR_getrandom 318
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)
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
312 # define __NR_getrandom 384
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
325 # define __NR_getrandom 278
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.
333 static ssize_t syscall_random(void *buf, size_t buflen)
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
339 * 2 * RAND_POOL_FACTOR * (RAND_DRBG_STRENGTH / 8) = 2^14
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.
346 * Do runtime detection to find getentropy().
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)
355 * Note: Sometimes getentropy() can be provided but not implemented
356 * internally. So we need to check errno for ENOSYS
358 # if !defined(__DragonFly__) && !defined(__NetBSD__)
359 # if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__) && !defined(__hpux)
360 extern int getentropy(void *buffer, size_t length) __attribute__((weak));
362 if (getentropy != NULL) {
363 if (getentropy(buf, buflen) == 0)
364 return (ssize_t)buflen;
368 # elif defined(OPENSSL_APPLE_CRYPTO_RANDOM)
370 if (CCRandomGenerateBytes(buf, buflen) == kCCSuccess)
371 return (ssize_t)buflen;
377 int (*f)(void *buffer, size_t length);
381 * We could cache the result of the lookup, but we normally don't
382 * call this function often.
385 p_getentropy.p = DSO_global_lookup("getentropy");
387 if (p_getentropy.p != NULL)
388 return p_getentropy.f(buf, buflen) == 0 ? (ssize_t)buflen : -1;
390 # endif /* !__DragonFly__ */
392 /* Linux supports this since version 3.17 */
393 # if defined(__linux) && defined(__NR_getrandom)
394 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);
405 # endif /* defined(OPENSSL_RAND_SEED_GETRANDOM) */
407 # if defined(OPENSSL_RAND_SEED_DEVRANDOM)
408 static const char *random_device_paths[] = { DEVRANDOM };
409 static struct random_device {
415 } random_devices[OSSL_NELEM(random_device_paths)];
416 static int keep_random_devices_open = 1;
418 # if defined(__linux) && defined(DEVRANDOM_WAIT) \
419 && defined(OPENSSL_RAND_SEED_GETRANDOM)
420 static void *shm_addr;
422 static void cleanup_shm(void)
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.
433 static int wait_random_seeded(void)
435 static int seeded = OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID < 0;
436 static const int kernel_version[] = { DEVRANDOM_SAFE_KERNEL };
444 /* See if anything has created the global seeded indication */
445 if ((shm_id = shmget(OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID, 1, 0)) == -1) {
447 * Check the kernel's version and fail if it is too recent.
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.
455 if (uname(&un) == 0) {
456 kernel[0] = atoi(un.release);
457 p = strchr(un.release, '.');
458 kernel[1] = p == NULL ? 0 : atoi(p + 1);
459 if (kernel[0] > kernel_version[0]
460 || (kernel[0] == kernel_version[0]
461 && kernel[1] >= kernel_version[1])) {
465 /* Open /dev/random and wait for it to be readable */
466 if ((fd = open(DEVRANDOM_WAIT, O_RDONLY)) != -1) {
467 if (DEVRANDM_WAIT_USE_SELECT && fd < FD_SETSIZE) {
470 while ((r = select(fd + 1, &fds, NULL, NULL, NULL)) < 0
473 while ((r = read(fd, &c, 1)) < 0 && errno == EINTR);
478 /* Create the shared memory indicator */
479 shm_id = shmget(OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID, 1,
480 IPC_CREAT | S_IRUSR | S_IRGRP | S_IROTH);
487 * Map the shared memory to prevent its premature destruction.
488 * If this call fails, it isn't a big problem.
490 shm_addr = shmat(shm_id, NULL, SHM_RDONLY);
491 if (shm_addr != (void *)-1)
492 OPENSSL_atexit(&cleanup_shm);
497 # else /* defined __linux && DEVRANDOM_WAIT && OPENSSL_RAND_SEED_GETRANDOM */
498 static int wait_random_seeded(void)
505 * Verify that the file descriptor associated with the random source is
506 * still valid. The rationale for doing this is the fact that it is not
507 * uncommon for daemons to close all open file handles when daemonizing.
508 * So the handle might have been closed or even reused for opening
511 static int check_random_device(struct random_device * rd)
516 && fstat(rd->fd, &st) != -1
517 && rd->dev == st.st_dev
518 && rd->ino == st.st_ino
519 && ((rd->mode ^ st.st_mode) & ~(S_IRWXU | S_IRWXG | S_IRWXO)) == 0
520 && rd->rdev == st.st_rdev;
524 * Open a random device if required and return its file descriptor or -1 on error
526 static int get_random_device(size_t n)
529 struct random_device * rd = &random_devices[n];
531 /* reuse existing file descriptor if it is (still) valid */
532 if (check_random_device(rd))
535 /* open the random device ... */
536 if ((rd->fd = open(random_device_paths[n], O_RDONLY)) == -1)
539 /* ... and cache its relevant stat(2) data */
540 if (fstat(rd->fd, &st) != -1) {
543 rd->mode = st.st_mode;
544 rd->rdev = st.st_rdev;
554 * Close a random device making sure it is a random device
556 static void close_random_device(size_t n)
558 struct random_device * rd = &random_devices[n];
560 if (check_random_device(rd))
565 int ossl_rand_pool_init(void)
569 for (i = 0; i < OSSL_NELEM(random_devices); i++)
570 random_devices[i].fd = -1;
575 void ossl_rand_pool_cleanup(void)
579 for (i = 0; i < OSSL_NELEM(random_devices); i++)
580 close_random_device(i);
583 void ossl_rand_pool_keep_random_devices_open(int keep)
586 ossl_rand_pool_cleanup();
588 keep_random_devices_open = keep;
591 # else /* !defined(OPENSSL_RAND_SEED_DEVRANDOM) */
593 int ossl_rand_pool_init(void)
598 void ossl_rand_pool_cleanup(void)
602 void ossl_rand_pool_keep_random_devices_open(int keep)
606 # endif /* defined(OPENSSL_RAND_SEED_DEVRANDOM) */
609 * Try the various seeding methods in turn, exit when successful.
611 * If more than one entropy source is available, is it
612 * preferable to stop as soon as enough entropy has been collected
613 * (as favored by @rsalz) or should one rather be defensive and add
614 * more entropy than requested and/or from different sources?
616 * Currently, the user can select multiple entropy sources in the
617 * configure step, yet in practice only the first available source
618 * will be used. A more flexible solution has been requested, but
619 * currently it is not clear how this can be achieved without
620 * overengineering the problem. There are many parameters which
621 * could be taken into account when selecting the order and amount
622 * of input from the different entropy sources (trust, quality,
623 * possibility of blocking).
625 size_t ossl_pool_acquire_entropy(RAND_POOL *pool)
627 # if defined(OPENSSL_RAND_SEED_NONE)
628 return ossl_rand_pool_entropy_available(pool);
630 size_t entropy_available = 0;
632 (void)entropy_available; /* avoid compiler warning */
634 # if defined(OPENSSL_RAND_SEED_GETRANDOM)
637 unsigned char *buffer;
639 /* Maximum allowed number of consecutive unsuccessful attempts */
642 bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
643 while (bytes_needed != 0 && attempts-- > 0) {
644 buffer = ossl_rand_pool_add_begin(pool, bytes_needed);
645 bytes = syscall_random(buffer, bytes_needed);
647 ossl_rand_pool_add_end(pool, bytes, 8 * bytes);
648 bytes_needed -= bytes;
649 attempts = 3; /* reset counter after successful attempt */
650 } else if (bytes < 0 && errno != EINTR) {
655 entropy_available = ossl_rand_pool_entropy_available(pool);
656 if (entropy_available > 0)
657 return entropy_available;
660 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
662 /* Not yet implemented. */
666 # if defined(OPENSSL_RAND_SEED_DEVRANDOM)
667 if (wait_random_seeded()) {
669 unsigned char *buffer;
672 bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
673 for (i = 0; bytes_needed > 0 && i < OSSL_NELEM(random_device_paths);
676 /* Maximum number of consecutive unsuccessful attempts */
678 const int fd = get_random_device(i);
683 while (bytes_needed != 0 && attempts-- > 0) {
684 buffer = ossl_rand_pool_add_begin(pool, bytes_needed);
685 bytes = read(fd, buffer, bytes_needed);
688 ossl_rand_pool_add_end(pool, bytes, 8 * bytes);
689 bytes_needed -= bytes;
690 attempts = 3; /* reset counter on successful attempt */
691 } else if (bytes < 0 && errno != EINTR) {
695 if (bytes < 0 || !keep_random_devices_open)
696 close_random_device(i);
698 bytes_needed = ossl_rand_pool_bytes_needed(pool, 1);
700 entropy_available = ossl_rand_pool_entropy_available(pool);
701 if (entropy_available > 0)
702 return entropy_available;
706 # if defined(OPENSSL_RAND_SEED_RDTSC)
707 entropy_available = ossl_prov_acquire_entropy_from_tsc(pool);
708 if (entropy_available > 0)
709 return entropy_available;
712 # if defined(OPENSSL_RAND_SEED_RDCPU)
713 entropy_available = ossl_prov_acquire_entropy_from_cpu(pool);
714 if (entropy_available > 0)
715 return entropy_available;
718 # if defined(OPENSSL_RAND_SEED_EGD)
720 static const char *paths[] = { DEVRANDOM_EGD, NULL };
722 unsigned char *buffer;
725 bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
726 for (i = 0; bytes_needed > 0 && paths[i] != NULL; i++) {
730 buffer = ossl_rand_pool_add_begin(pool, bytes_needed);
731 num = RAND_query_egd_bytes(paths[i],
732 buffer, (int)bytes_needed);
733 if (num == (int)bytes_needed)
734 bytes = bytes_needed;
736 ossl_rand_pool_add_end(pool, bytes, 8 * bytes);
737 bytes_needed = ossl_rand_pool_bytes_needed(pool, 1);
739 entropy_available = ossl_rand_pool_entropy_available(pool);
740 if (entropy_available > 0)
741 return entropy_available;
745 return ossl_rand_pool_entropy_available(pool);
751 #if (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS)) \
752 || defined(__DJGPP__)
753 int ossl_pool_add_nonce_data(RAND_POOL *pool)
757 CRYPTO_THREAD_ID tid;
761 /* Erase the entire structure including any padding */
762 memset(&data, 0, sizeof(data));
765 * Add process id, thread id, and a high resolution timestamp to
766 * ensure that the nonce is unique with high probability for
767 * different process instances.
770 data.tid = CRYPTO_THREAD_get_current_id();
771 data.time = get_time_stamp();
773 return ossl_rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
776 int ossl_rand_pool_add_additional_data(RAND_POOL *pool)
780 CRYPTO_THREAD_ID tid;
784 /* Erase the entire structure including any padding */
785 memset(&data, 0, sizeof(data));
788 * Add some noise from the thread id and a high resolution timer.
789 * The fork_id adds some extra fork-safety.
790 * The thread id adds a little randomness if the drbg is accessed
791 * concurrently (which is the case for the <master> drbg).
793 data.fork_id = openssl_get_fork_id();
794 data.tid = CRYPTO_THREAD_get_current_id();
795 data.time = get_timer_bits();
797 return ossl_rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
802 * Get the current time with the highest possible resolution
804 * The time stamp is added to the nonce, so it is optimized for not repeating.
805 * The current time is ideal for this purpose, provided the computer's clock
808 static uint64_t get_time_stamp(void)
810 # if defined(OSSL_POSIX_TIMER_OKAY)
814 if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
815 return TWO32TO64(ts.tv_sec, ts.tv_nsec);
818 # if defined(__unix__) \
819 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
823 if (gettimeofday(&tv, NULL) == 0)
824 return TWO32TO64(tv.tv_sec, tv.tv_usec);
831 * Get an arbitrary timer value of the highest possible resolution
833 * The timer value is added as random noise to the additional data,
834 * which is not considered a trusted entropy sourec, so any result
837 static uint64_t get_timer_bits(void)
839 uint64_t res = OPENSSL_rdtsc();
844 # if defined(__sun) || defined(__hpux)
850 read_wall_time(&t, TIMEBASE_SZ);
851 return TWO32TO64(t.tb_high, t.tb_low);
853 # elif defined(OSSL_POSIX_TIMER_OKAY)
857 # ifdef CLOCK_BOOTTIME
858 # define CLOCK_TYPE CLOCK_BOOTTIME
859 # elif defined(_POSIX_MONOTONIC_CLOCK)
860 # define CLOCK_TYPE CLOCK_MONOTONIC
862 # define CLOCK_TYPE CLOCK_REALTIME
865 if (clock_gettime(CLOCK_TYPE, &ts) == 0)
866 return TWO32TO64(ts.tv_sec, ts.tv_nsec);
869 # if defined(__unix__) \
870 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
874 if (gettimeofday(&tv, NULL) == 0)
875 return TWO32TO64(tv.tv_sec, tv.tv_usec);
880 #endif /* (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS))
881 || defined(__DJGPP__) */