2 * Copyright 1995-2018 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
15 #include "internal/cryptlib.h"
16 #include <openssl/rand.h>
17 #include <openssl/crypto.h>
18 #include "rand_local.h"
19 #include "crypto/rand.h"
21 #include "internal/dso.h"
24 * Defines related to seed sources
28 * set this to a comma-separated list of 'random' device files to try out. By
29 * default, we will try to read at least one of these files
31 # define DEVRANDOM "/dev/urandom", "/dev/random", "/dev/hwrng", "/dev/srandom"
32 # if defined(__linux) && !defined(__ANDROID__)
33 # ifndef DEVRANDOM_WAIT
34 # define DEVRANDOM_WAIT "/dev/random"
37 * Linux kernels 4.8 and later changes how their random device works and there
38 * is no reliable way to tell that /dev/urandom has been seeded -- getentropy(2)
39 * should be used instead.
41 # ifndef DEVRANDOM_SAFE_KERNEL
42 # define DEVRANDOM_SAFE_KERNEL 4, 8
45 * Some operating systems do not permit select(2) on their random devices,
46 * defining this to zero will force the use of read(2) to extract one byte
49 # ifndef DEVRANDM_WAIT_USE_SELECT
50 # define DEVRANDM_WAIT_USE_SELECT 1
53 * Define the shared memory identifier used to indicate if the operating
54 * system has properly seeded the DEVRANDOM source.
56 # ifndef OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID
57 # define OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID 114
63 #if !defined(OPENSSL_NO_EGD) && !defined(DEVRANDOM_EGD)
65 * set this to a comma-separated list of 'egd' sockets to try out. These
66 * sockets will be tried in the order listed in case accessing the device
67 * files listed in DEVRANDOM did not return enough randomness.
69 # define DEVRANDOM_EGD "/var/run/egd-pool", "/dev/egd-pool", "/etc/egd-pool", "/etc/entropy"
73 # include <sys/syscall.h>
74 # ifdef DEVRANDOM_WAIT
76 # include <sys/utsname.h>
79 #if defined(__FreeBSD__) && !defined(OPENSSL_SYS_UEFI)
80 # include <sys/types.h>
81 # include <sys/sysctl.h>
82 # include <sys/param.h>
84 #if defined(__OpenBSD__) || defined(__NetBSD__)
85 # include <sys/param.h>
88 #if (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS)) \
90 # include <sys/types.h>
91 # include <sys/stat.h>
94 # include <sys/time.h>
96 static uint64_t get_time_stamp(void);
97 static uint64_t get_timer_bits(void);
99 /* Macro to convert two thirty two bit values into a sixty four bit one */
100 # define TWO32TO64(a, b) ((((uint64_t)(a)) << 32) + (b))
103 * Check for the existence and support of POSIX timers. The standard
104 * says that the _POSIX_TIMERS macro will have a positive value if they
107 * However, we want an additional constraint: that the timer support does
108 * not require an extra library dependency. Early versions of glibc
109 * require -lrt to be specified on the link line to access the timers,
110 * so this needs to be checked for.
112 * It is worse because some libraries define __GLIBC__ but don't
113 * support the version testing macro (e.g. uClibc). This means
114 * an extra check is needed.
116 * The final condition is:
117 * "have posix timers and either not glibc or glibc without -lrt"
119 * The nested #if sequences are required to avoid using a parameterised
120 * macro that might be undefined.
122 # undef OSSL_POSIX_TIMER_OKAY
123 # if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
124 # if defined(__GLIBC__)
125 # if defined(__GLIBC_PREREQ)
126 # if __GLIBC_PREREQ(2, 17)
127 # define OSSL_POSIX_TIMER_OKAY
131 # define OSSL_POSIX_TIMER_OKAY
134 #endif /* (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS))
135 || defined(__DJGPP__) */
137 #if defined(OPENSSL_RAND_SEED_NONE)
138 /* none means none. this simplifies the following logic */
139 # undef OPENSSL_RAND_SEED_OS
140 # undef OPENSSL_RAND_SEED_GETRANDOM
141 # undef OPENSSL_RAND_SEED_LIBRANDOM
142 # undef OPENSSL_RAND_SEED_DEVRANDOM
143 # undef OPENSSL_RAND_SEED_RDTSC
144 # undef OPENSSL_RAND_SEED_RDCPU
145 # undef OPENSSL_RAND_SEED_EGD
148 #if defined(OPENSSL_SYS_UEFI) && !defined(OPENSSL_RAND_SEED_NONE)
149 # error "UEFI only supports seeding NONE"
152 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) \
153 || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) \
154 || defined(OPENSSL_SYS_UEFI))
156 # if defined(OPENSSL_SYS_VOS)
158 # ifndef OPENSSL_RAND_SEED_OS
159 # error "Unsupported seeding method configured; must be os"
162 # if defined(OPENSSL_SYS_VOS_HPPA) && defined(OPENSSL_SYS_VOS_IA32)
163 # error "Unsupported HP-PA and IA32 at the same time."
165 # if !defined(OPENSSL_SYS_VOS_HPPA) && !defined(OPENSSL_SYS_VOS_IA32)
166 # error "Must have one of HP-PA or IA32"
170 * The following algorithm repeatedly samples the real-time clock (RTC) to
171 * generate a sequence of unpredictable data. The algorithm relies upon the
172 * uneven execution speed of the code (due to factors such as cache misses,
173 * interrupts, bus activity, and scheduling) and upon the rather large
174 * relative difference between the speed of the clock and the rate at which
175 * it can be read. If it is ported to an environment where execution speed
176 * is more constant or where the RTC ticks at a much slower rate, or the
177 * clock can be read with fewer instructions, it is likely that the results
178 * would be far more predictable. This should only be used for legacy
181 * As a precaution, we assume only 2 bits of entropy per byte.
183 size_t rand_pool_acquire_entropy(RAND_POOL *pool)
190 # ifdef OPENSSL_SYS_VOS_HPPA
192 extern void s$sleep(long *_duration, short int *_code);
195 extern void s$sleep2(long long *_duration, short int *_code);
198 bytes_needed = rand_pool_bytes_needed(pool, 4 /*entropy_factor*/);
200 for (i = 0; i < bytes_needed; i++) {
202 * burn some cpu; hope for interrupts, cache collisions, bus
205 for (k = 0; k < 99; k++)
206 ts.tv_nsec = random();
208 # ifdef OPENSSL_SYS_VOS_HPPA
209 /* sleep for 1/1024 of a second (976 us). */
211 s$sleep(&duration, &code);
213 /* sleep for 1/65536 of a second (15 us). */
215 s$sleep2(&duration, &code);
218 /* Get wall clock time, take 8 bits. */
219 clock_gettime(CLOCK_REALTIME, &ts);
220 v = (unsigned char)(ts.tv_nsec & 0xFF);
221 rand_pool_add(pool, arg, &v, sizeof(v) , 2);
223 return rand_pool_entropy_available(pool);
226 void rand_pool_cleanup(void)
230 void rand_pool_keep_random_devices_open(int keep)
236 # if defined(OPENSSL_RAND_SEED_EGD) && \
237 (defined(OPENSSL_NO_EGD) || !defined(DEVRANDOM_EGD))
238 # error "Seeding uses EGD but EGD is turned off or no device given"
241 # if defined(OPENSSL_RAND_SEED_DEVRANDOM) && !defined(DEVRANDOM)
242 # error "Seeding uses urandom but DEVRANDOM is not configured"
245 # if defined(OPENSSL_RAND_SEED_OS)
246 # if !defined(DEVRANDOM)
247 # error "OS seeding requires DEVRANDOM to be configured"
249 # define OPENSSL_RAND_SEED_GETRANDOM
250 # define OPENSSL_RAND_SEED_DEVRANDOM
253 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
254 # error "librandom not (yet) supported"
257 # if (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND)
259 * sysctl_random(): Use sysctl() to read a random number from the kernel
260 * Returns the number of bytes returned in buf on success, -1 on failure.
262 static ssize_t sysctl_random(char *buf, size_t buflen)
269 * Note: sign conversion between size_t and ssize_t is safe even
270 * without a range check, see comment in syscall_random()
274 * On FreeBSD old implementations returned longs, newer versions support
275 * variable sizes up to 256 byte. The code below would not work properly
276 * when the sysctl returns long and we want to request something not a
277 * multiple of longs, which should never be the case.
279 if (!ossl_assert(buflen % sizeof(long) == 0)) {
285 * On NetBSD before 4.0 KERN_ARND was an alias for KERN_URND, and only
286 * filled in an int, leaving the rest uninitialized. Since NetBSD 4.0
287 * it returns a variable number of bytes with the current version supporting
289 * Just return an error on older NetBSD versions.
291 #if defined(__NetBSD__) && __NetBSD_Version__ < 400000000
301 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1)
302 return done > 0 ? done : -1;
306 } while (buflen > 0);
312 # if defined(OPENSSL_RAND_SEED_GETRANDOM)
314 # if defined(__linux) && !defined(__NR_getrandom)
315 # if defined(__arm__)
316 # define __NR_getrandom (__NR_SYSCALL_BASE+384)
317 # elif defined(__i386__)
318 # define __NR_getrandom 355
319 # elif defined(__x86_64__)
320 # if defined(__ILP32__)
321 # define __NR_getrandom (__X32_SYSCALL_BIT + 318)
323 # define __NR_getrandom 318
325 # elif defined(__xtensa__)
326 # define __NR_getrandom 338
327 # elif defined(__s390__) || defined(__s390x__)
328 # define __NR_getrandom 349
329 # elif defined(__bfin__)
330 # define __NR_getrandom 389
331 # elif defined(__powerpc__)
332 # define __NR_getrandom 359
333 # elif defined(__mips__) || defined(__mips64)
334 # if _MIPS_SIM == _MIPS_SIM_ABI32
335 # define __NR_getrandom (__NR_Linux + 353)
336 # elif _MIPS_SIM == _MIPS_SIM_ABI64
337 # define __NR_getrandom (__NR_Linux + 313)
338 # elif _MIPS_SIM == _MIPS_SIM_NABI32
339 # define __NR_getrandom (__NR_Linux + 317)
341 # elif defined(__hppa__)
342 # define __NR_getrandom (__NR_Linux + 339)
343 # elif defined(__sparc__)
344 # define __NR_getrandom 347
345 # elif defined(__ia64__)
346 # define __NR_getrandom 1339
347 # elif defined(__alpha__)
348 # define __NR_getrandom 511
349 # elif defined(__sh__)
350 # if defined(__SH5__)
351 # define __NR_getrandom 373
353 # define __NR_getrandom 384
355 # elif defined(__avr32__)
356 # define __NR_getrandom 317
357 # elif defined(__microblaze__)
358 # define __NR_getrandom 385
359 # elif defined(__m68k__)
360 # define __NR_getrandom 352
361 # elif defined(__cris__)
362 # define __NR_getrandom 356
363 # elif defined(__aarch64__)
364 # define __NR_getrandom 278
366 # define __NR_getrandom 278
371 * syscall_random(): Try to get random data using a system call
372 * returns the number of bytes returned in buf, or < 0 on error.
374 static ssize_t syscall_random(void *buf, size_t buflen)
377 * Note: 'buflen' equals the size of the buffer which is used by the
378 * get_entropy() callback of the RAND_DRBG. It is roughly bounded by
380 * 2 * RAND_POOL_FACTOR * (RAND_DRBG_STRENGTH / 8) = 2^14
382 * which is way below the OSSL_SSIZE_MAX limit. Therefore sign conversion
383 * between size_t and ssize_t is safe even without a range check.
387 * Do runtime detection to find getentropy().
389 * Known OSs that should support this:
390 * - Darwin since 16 (OSX 10.12, IOS 10.0).
391 * - Solaris since 11.3
392 * - OpenBSD since 5.6
393 * - Linux since 3.17 with glibc 2.25
394 * - FreeBSD since 12.0 (1200061)
396 # if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__) && !defined(__hpux)
397 extern int getentropy(void *buffer, size_t length) __attribute__((weak));
399 if (getentropy != NULL)
400 return getentropy(buf, buflen) == 0 ? (ssize_t)buflen : -1;
401 # elif !defined(FIPS_MODE)
404 int (*f)(void *buffer, size_t length);
408 * We could cache the result of the lookup, but we normally don't
409 * call this function often.
412 p_getentropy.p = DSO_global_lookup("getentropy");
414 if (p_getentropy.p != NULL)
415 return p_getentropy.f(buf, buflen) == 0 ? (ssize_t)buflen : -1;
418 /* Linux supports this since version 3.17 */
419 # if defined(__linux) && defined(__NR_getrandom)
420 return syscall(__NR_getrandom, buf, buflen, 0);
421 # elif (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND)
422 return sysctl_random(buf, buflen);
428 # endif /* defined(OPENSSL_RAND_SEED_GETRANDOM) */
430 # if defined(OPENSSL_RAND_SEED_DEVRANDOM)
431 static const char *random_device_paths[] = { DEVRANDOM };
432 static struct random_device {
438 } random_devices[OSSL_NELEM(random_device_paths)];
439 static int keep_random_devices_open = 1;
441 # if defined(__linux) && defined(DEVRANDOM_WAIT)
442 static void *shm_addr;
444 # if !defined(FIPS_MODE)
445 static void cleanup_shm(void)
452 * Ensure that the system randomness source has been adequately seeded.
453 * This is done by having the first start of libcrypto, wait until the device
454 * /dev/random becomes able to supply a byte of entropy. Subsequent starts
455 * of the library and later reseedings do not need to do this.
457 static int wait_random_seeded(void)
459 static int seeded = OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID < 0;
460 static const int kernel_version[] = { DEVRANDOM_SAFE_KERNEL };
468 /* See if anything has created the global seeded indication */
469 if ((shm_id = shmget(OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID, 1, 0)) == -1) {
471 * Check the kernel's version and fail if it is too recent.
473 * Linux kernels from 4.8 onwards do not guarantee that
474 * /dev/urandom is properly seeded when /dev/random becomes
475 * readable. However, such kernels support the getentropy(2)
476 * system call and this should always succeed which renders
477 * this alternative but essentially identical source moot.
479 if (uname(&un) == 0) {
480 kernel[0] = atoi(un.release);
481 p = strchr(un.release, '.');
482 kernel[1] = p == NULL ? 0 : atoi(p + 1);
483 if (kernel[0] > kernel_version[0]
484 || (kernel[0] == kernel_version[0]
485 && kernel[1] >= kernel_version[1])) {
489 /* Open /dev/random and wait for it to be readable */
490 if ((fd = open(DEVRANDOM_WAIT, O_RDONLY)) != -1) {
491 if (DEVRANDM_WAIT_USE_SELECT && fd < FD_SETSIZE) {
494 while ((r = select(fd + 1, &fds, NULL, NULL, NULL)) < 0
497 while ((r = read(fd, &c, 1)) < 0 && errno == EINTR);
502 /* Create the shared memory indicator */
503 shm_id = shmget(OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID, 1,
504 IPC_CREAT | S_IRUSR | S_IRGRP | S_IROTH);
511 * Map the shared memory to prevent its premature destruction.
512 * If this call fails, it isn't a big problem.
514 shm_addr = shmat(shm_id, NULL, SHM_RDONLY);
516 /* TODO 3.0: The FIPS provider doesn't have OPENSSL_atexit */
517 if (shm_addr != (void *)-1)
518 OPENSSL_atexit(&cleanup_shm);
524 # else /* defined __linux */
525 static int wait_random_seeded(void)
532 * Verify that the file descriptor associated with the random source is
533 * still valid. The rationale for doing this is the fact that it is not
534 * uncommon for daemons to close all open file handles when daemonizing.
535 * So the handle might have been closed or even reused for opening
538 static int check_random_device(struct random_device * rd)
543 && fstat(rd->fd, &st) != -1
544 && rd->dev == st.st_dev
545 && rd->ino == st.st_ino
546 && ((rd->mode ^ st.st_mode) & ~(S_IRWXU | S_IRWXG | S_IRWXO)) == 0
547 && rd->rdev == st.st_rdev;
551 * Open a random device if required and return its file descriptor or -1 on error
553 static int get_random_device(size_t n)
556 struct random_device * rd = &random_devices[n];
558 /* reuse existing file descriptor if it is (still) valid */
559 if (check_random_device(rd))
562 /* open the random device ... */
563 if ((rd->fd = open(random_device_paths[n], O_RDONLY)) == -1)
566 /* ... and cache its relevant stat(2) data */
567 if (fstat(rd->fd, &st) != -1) {
570 rd->mode = st.st_mode;
571 rd->rdev = st.st_rdev;
581 * Close a random device making sure it is a random device
583 static void close_random_device(size_t n)
585 struct random_device * rd = &random_devices[n];
587 if (check_random_device(rd))
592 int rand_pool_init(void)
596 for (i = 0; i < OSSL_NELEM(random_devices); i++)
597 random_devices[i].fd = -1;
602 void rand_pool_cleanup(void)
606 for (i = 0; i < OSSL_NELEM(random_devices); i++)
607 close_random_device(i);
610 void rand_pool_keep_random_devices_open(int keep)
615 keep_random_devices_open = keep;
618 # else /* !defined(OPENSSL_RAND_SEED_DEVRANDOM) */
620 int rand_pool_init(void)
625 void rand_pool_cleanup(void)
629 void rand_pool_keep_random_devices_open(int keep)
633 # endif /* defined(OPENSSL_RAND_SEED_DEVRANDOM) */
636 * Try the various seeding methods in turn, exit when successful.
638 * TODO(DRBG): If more than one entropy source is available, is it
639 * preferable to stop as soon as enough entropy has been collected
640 * (as favored by @rsalz) or should one rather be defensive and add
641 * more entropy than requested and/or from different sources?
643 * Currently, the user can select multiple entropy sources in the
644 * configure step, yet in practice only the first available source
645 * will be used. A more flexible solution has been requested, but
646 * currently it is not clear how this can be achieved without
647 * overengineering the problem. There are many parameters which
648 * could be taken into account when selecting the order and amount
649 * of input from the different entropy sources (trust, quality,
650 * possibility of blocking).
652 size_t rand_pool_acquire_entropy(RAND_POOL *pool)
654 # if defined(OPENSSL_RAND_SEED_NONE)
655 return rand_pool_entropy_available(pool);
657 size_t entropy_available;
659 # if defined(OPENSSL_RAND_SEED_GETRANDOM)
662 unsigned char *buffer;
664 /* Maximum allowed number of consecutive unsuccessful attempts */
667 bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
668 while (bytes_needed != 0 && attempts-- > 0) {
669 buffer = rand_pool_add_begin(pool, bytes_needed);
670 bytes = syscall_random(buffer, bytes_needed);
672 rand_pool_add_end(pool, bytes, 8 * bytes);
673 bytes_needed -= bytes;
674 attempts = 3; /* reset counter after successful attempt */
675 } else if (bytes < 0 && errno != EINTR) {
680 entropy_available = rand_pool_entropy_available(pool);
681 if (entropy_available > 0)
682 return entropy_available;
685 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
687 /* Not yet implemented. */
691 # if defined(OPENSSL_RAND_SEED_DEVRANDOM)
692 if (wait_random_seeded()) {
694 unsigned char *buffer;
697 bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
698 for (i = 0; bytes_needed > 0 && i < OSSL_NELEM(random_device_paths);
701 /* Maximum number of consecutive unsuccessful attempts */
703 const int fd = get_random_device(i);
708 while (bytes_needed != 0 && attempts-- > 0) {
709 buffer = rand_pool_add_begin(pool, bytes_needed);
710 bytes = read(fd, buffer, bytes_needed);
713 rand_pool_add_end(pool, bytes, 8 * bytes);
714 bytes_needed -= bytes;
715 attempts = 3; /* reset counter on successful attempt */
716 } else if (bytes < 0 && errno != EINTR) {
720 if (bytes < 0 || !keep_random_devices_open)
721 close_random_device(i);
723 bytes_needed = rand_pool_bytes_needed(pool, 1);
725 entropy_available = rand_pool_entropy_available(pool);
726 if (entropy_available > 0)
727 return entropy_available;
731 # if defined(OPENSSL_RAND_SEED_RDTSC)
732 entropy_available = rand_acquire_entropy_from_tsc(pool);
733 if (entropy_available > 0)
734 return entropy_available;
737 # if defined(OPENSSL_RAND_SEED_RDCPU)
738 entropy_available = rand_acquire_entropy_from_cpu(pool);
739 if (entropy_available > 0)
740 return entropy_available;
743 # if defined(OPENSSL_RAND_SEED_EGD)
745 static const char *paths[] = { DEVRANDOM_EGD, NULL };
747 unsigned char *buffer;
750 bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
751 for (i = 0; bytes_needed > 0 && paths[i] != NULL; i++) {
755 buffer = rand_pool_add_begin(pool, bytes_needed);
756 num = RAND_query_egd_bytes(paths[i],
757 buffer, (int)bytes_needed);
758 if (num == (int)bytes_needed)
759 bytes = bytes_needed;
761 rand_pool_add_end(pool, bytes, 8 * bytes);
762 bytes_needed = rand_pool_bytes_needed(pool, 1);
764 entropy_available = rand_pool_entropy_available(pool);
765 if (entropy_available > 0)
766 return entropy_available;
770 return rand_pool_entropy_available(pool);
776 #if (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS)) \
777 || defined(__DJGPP__)
778 int rand_pool_add_nonce_data(RAND_POOL *pool)
782 CRYPTO_THREAD_ID tid;
786 /* Erase the entire structure including any padding */
787 memset(&data, 0, sizeof(data));
790 * Add process id, thread id, and a high resolution timestamp to
791 * ensure that the nonce is unique with high probability for
792 * different process instances.
795 data.tid = CRYPTO_THREAD_get_current_id();
796 data.time = get_time_stamp();
798 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
801 int rand_pool_add_additional_data(RAND_POOL *pool)
805 CRYPTO_THREAD_ID tid;
809 /* Erase the entire structure including any padding */
810 memset(&data, 0, sizeof(data));
813 * Add some noise from the thread id and a high resolution timer.
814 * The fork_id adds some extra fork-safety.
815 * The thread id adds a little randomness if the drbg is accessed
816 * concurrently (which is the case for the <master> drbg).
818 data.fork_id = openssl_get_fork_id();
819 data.tid = CRYPTO_THREAD_get_current_id();
820 data.time = get_timer_bits();
822 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
827 * Get the current time with the highest possible resolution
829 * The time stamp is added to the nonce, so it is optimized for not repeating.
830 * The current time is ideal for this purpose, provided the computer's clock
833 static uint64_t get_time_stamp(void)
835 # if defined(OSSL_POSIX_TIMER_OKAY)
839 if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
840 return TWO32TO64(ts.tv_sec, ts.tv_nsec);
843 # if defined(__unix__) \
844 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
848 if (gettimeofday(&tv, NULL) == 0)
849 return TWO32TO64(tv.tv_sec, tv.tv_usec);
856 * Get an arbitrary timer value of the highest possible resolution
858 * The timer value is added as random noise to the additional data,
859 * which is not considered a trusted entropy sourec, so any result
862 static uint64_t get_timer_bits(void)
864 uint64_t res = OPENSSL_rdtsc();
869 # if defined(__sun) || defined(__hpux)
875 read_wall_time(&t, TIMEBASE_SZ);
876 return TWO32TO64(t.tb_high, t.tb_low);
878 # elif defined(OSSL_POSIX_TIMER_OKAY)
882 # ifdef CLOCK_BOOTTIME
883 # define CLOCK_TYPE CLOCK_BOOTTIME
884 # elif defined(_POSIX_MONOTONIC_CLOCK)
885 # define CLOCK_TYPE CLOCK_MONOTONIC
887 # define CLOCK_TYPE CLOCK_REALTIME
890 if (clock_gettime(CLOCK_TYPE, &ts) == 0)
891 return TWO32TO64(ts.tv_sec, ts.tv_nsec);
894 # if defined(__unix__) \
895 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
899 if (gettimeofday(&tv, NULL) == 0)
900 return TWO32TO64(tv.tv_sec, tv.tv_usec);
905 #endif /* (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS))
906 || defined(__DJGPP__) */