2 * Copyright 1995-2020 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 "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 # if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
80 # if defined(__GLIBC__)
81 # if defined(__GLIBC_PREREQ)
82 # if __GLIBC_PREREQ(2, 17)
83 # define OSSL_POSIX_TIMER_OKAY
87 # define OSSL_POSIX_TIMER_OKAY
90 #endif /* (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS))
91 || defined(__DJGPP__) */
93 #if defined(OPENSSL_RAND_SEED_NONE)
94 /* none means none. this simplifies the following logic */
95 # undef OPENSSL_RAND_SEED_OS
96 # undef OPENSSL_RAND_SEED_GETRANDOM
97 # undef OPENSSL_RAND_SEED_LIBRANDOM
98 # undef OPENSSL_RAND_SEED_DEVRANDOM
99 # undef OPENSSL_RAND_SEED_RDTSC
100 # undef OPENSSL_RAND_SEED_RDCPU
101 # undef OPENSSL_RAND_SEED_EGD
104 #if defined(OPENSSL_SYS_UEFI) && !defined(OPENSSL_RAND_SEED_NONE)
105 # error "UEFI only supports seeding NONE"
108 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) \
109 || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) \
110 || defined(OPENSSL_SYS_UEFI))
112 # if defined(OPENSSL_SYS_VOS)
114 # ifndef OPENSSL_RAND_SEED_OS
115 # error "Unsupported seeding method configured; must be os"
118 # if defined(OPENSSL_SYS_VOS_HPPA) && defined(OPENSSL_SYS_VOS_IA32)
119 # error "Unsupported HP-PA and IA32 at the same time."
121 # if !defined(OPENSSL_SYS_VOS_HPPA) && !defined(OPENSSL_SYS_VOS_IA32)
122 # error "Must have one of HP-PA or IA32"
126 * The following algorithm repeatedly samples the real-time clock (RTC) to
127 * generate a sequence of unpredictable data. The algorithm relies upon the
128 * uneven execution speed of the code (due to factors such as cache misses,
129 * interrupts, bus activity, and scheduling) and upon the rather large
130 * relative difference between the speed of the clock and the rate at which
131 * it can be read. If it is ported to an environment where execution speed
132 * is more constant or where the RTC ticks at a much slower rate, or the
133 * clock can be read with fewer instructions, it is likely that the results
134 * would be far more predictable. This should only be used for legacy
137 * As a precaution, we assume only 2 bits of entropy per byte.
139 size_t ossl_pool_acquire_entropy(RAND_POOL *pool)
146 # ifdef OPENSSL_SYS_VOS_HPPA
148 extern void s$sleep(long *_duration, short int *_code);
151 extern void s$sleep2(long long *_duration, short int *_code);
154 bytes_needed = rand_pool_bytes_needed(pool, 4 /*entropy_factor*/);
156 for (i = 0; i < bytes_needed; i++) {
158 * burn some cpu; hope for interrupts, cache collisions, bus
161 for (k = 0; k < 99; k++)
162 ts.tv_nsec = random();
164 # ifdef OPENSSL_SYS_VOS_HPPA
165 /* sleep for 1/1024 of a second (976 us). */
167 s$sleep(&duration, &code);
169 /* sleep for 1/65536 of a second (15 us). */
171 s$sleep2(&duration, &code);
174 /* Get wall clock time, take 8 bits. */
175 clock_gettime(CLOCK_REALTIME, &ts);
176 v = (unsigned char)(ts.tv_nsec & 0xFF);
177 rand_pool_add(pool, arg, &v, sizeof(v) , 2);
179 return rand_pool_entropy_available(pool);
182 void rand_pool_cleanup(void)
186 void rand_pool_keep_random_devices_open(int keep)
192 # if defined(OPENSSL_RAND_SEED_EGD) && \
193 (defined(OPENSSL_NO_EGD) || !defined(DEVRANDOM_EGD))
194 # error "Seeding uses EGD but EGD is turned off or no device given"
197 # if defined(OPENSSL_RAND_SEED_DEVRANDOM) && !defined(DEVRANDOM)
198 # error "Seeding uses urandom but DEVRANDOM is not configured"
201 # if defined(OPENSSL_RAND_SEED_OS)
202 # if !defined(DEVRANDOM)
203 # error "OS seeding requires DEVRANDOM to be configured"
205 # define OPENSSL_RAND_SEED_GETRANDOM
206 # define OPENSSL_RAND_SEED_DEVRANDOM
209 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
210 # error "librandom not (yet) supported"
213 # if (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND)
215 * sysctl_random(): Use sysctl() to read a random number from the kernel
216 * Returns the number of bytes returned in buf on success, -1 on failure.
218 static ssize_t sysctl_random(char *buf, size_t buflen)
225 * Note: sign conversion between size_t and ssize_t is safe even
226 * without a range check, see comment in syscall_random()
230 * On FreeBSD old implementations returned longs, newer versions support
231 * variable sizes up to 256 byte. The code below would not work properly
232 * when the sysctl returns long and we want to request something not a
233 * multiple of longs, which should never be the case.
235 #if defined(__FreeBSD__)
236 if (!ossl_assert(buflen % sizeof(long) == 0)) {
243 * On NetBSD before 4.0 KERN_ARND was an alias for KERN_URND, and only
244 * filled in an int, leaving the rest uninitialized. Since NetBSD 4.0
245 * it returns a variable number of bytes with the current version supporting
247 * Just return an error on older NetBSD versions.
249 #if defined(__NetBSD__) && __NetBSD_Version__ < 400000000
258 len = buflen > 256 ? 256 : buflen;
259 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1)
260 return done > 0 ? done : -1;
264 } while (buflen > 0);
270 # if defined(OPENSSL_RAND_SEED_GETRANDOM)
272 # if defined(__linux) && !defined(__NR_getrandom)
273 # if defined(__arm__)
274 # define __NR_getrandom (__NR_SYSCALL_BASE+384)
275 # elif defined(__i386__)
276 # define __NR_getrandom 355
277 # elif defined(__x86_64__)
278 # if defined(__ILP32__)
279 # define __NR_getrandom (__X32_SYSCALL_BIT + 318)
281 # define __NR_getrandom 318
283 # elif defined(__xtensa__)
284 # define __NR_getrandom 338
285 # elif defined(__s390__) || defined(__s390x__)
286 # define __NR_getrandom 349
287 # elif defined(__bfin__)
288 # define __NR_getrandom 389
289 # elif defined(__powerpc__)
290 # define __NR_getrandom 359
291 # elif defined(__mips__) || defined(__mips64)
292 # if _MIPS_SIM == _MIPS_SIM_ABI32
293 # define __NR_getrandom (__NR_Linux + 353)
294 # elif _MIPS_SIM == _MIPS_SIM_ABI64
295 # define __NR_getrandom (__NR_Linux + 313)
296 # elif _MIPS_SIM == _MIPS_SIM_NABI32
297 # define __NR_getrandom (__NR_Linux + 317)
299 # elif defined(__hppa__)
300 # define __NR_getrandom (__NR_Linux + 339)
301 # elif defined(__sparc__)
302 # define __NR_getrandom 347
303 # elif defined(__ia64__)
304 # define __NR_getrandom 1339
305 # elif defined(__alpha__)
306 # define __NR_getrandom 511
307 # elif defined(__sh__)
308 # if defined(__SH5__)
309 # define __NR_getrandom 373
311 # define __NR_getrandom 384
313 # elif defined(__avr32__)
314 # define __NR_getrandom 317
315 # elif defined(__microblaze__)
316 # define __NR_getrandom 385
317 # elif defined(__m68k__)
318 # define __NR_getrandom 352
319 # elif defined(__cris__)
320 # define __NR_getrandom 356
321 # elif defined(__aarch64__)
322 # define __NR_getrandom 278
324 # define __NR_getrandom 278
329 * syscall_random(): Try to get random data using a system call
330 * returns the number of bytes returned in buf, or < 0 on error.
332 static ssize_t syscall_random(void *buf, size_t buflen)
335 * Note: 'buflen' equals the size of the buffer which is used by the
336 * get_entropy() callback of the RAND_DRBG. It is roughly bounded by
338 * 2 * RAND_POOL_FACTOR * (RAND_DRBG_STRENGTH / 8) = 2^14
340 * which is way below the OSSL_SSIZE_MAX limit. Therefore sign conversion
341 * between size_t and ssize_t is safe even without a range check.
345 * Do runtime detection to find getentropy().
347 * Known OSs that should support this:
348 * - Darwin since 16 (OSX 10.12, IOS 10.0).
349 * - Solaris since 11.3
350 * - OpenBSD since 5.6
351 * - Linux since 3.17 with glibc 2.25
352 * - FreeBSD since 12.0 (1200061)
354 * Note: Sometimes getentropy() can be provided but not implemented
355 * internally. So we need to check errno for ENOSYS
357 # if !defined(__DragonFly__) && !defined(__NetBSD__)
358 # if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__) && !defined(__hpux)
359 extern int getentropy(void *buffer, size_t length) __attribute__((weak));
361 if (getentropy != NULL) {
362 if (getentropy(buf, buflen) == 0)
363 return (ssize_t)buflen;
370 int (*f)(void *buffer, size_t length);
374 * We could cache the result of the lookup, but we normally don't
375 * call this function often.
378 p_getentropy.p = DSO_global_lookup("getentropy");
380 if (p_getentropy.p != NULL)
381 return p_getentropy.f(buf, buflen) == 0 ? (ssize_t)buflen : -1;
383 # endif /* !__DragonFly__ */
385 /* Linux supports this since version 3.17 */
386 # if defined(__linux) && defined(__NR_getrandom)
387 return syscall(__NR_getrandom, buf, buflen, 0);
388 # elif (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND)
389 return sysctl_random(buf, buflen);
390 # elif (defined(__DragonFly__) && __DragonFly_version >= 500700) \
391 || (defined(__NetBSD__) && __NetBSD_Version >= 1000000000)
392 return getrandom(buf, buflen, 0);
398 # endif /* defined(OPENSSL_RAND_SEED_GETRANDOM) */
400 # if defined(OPENSSL_RAND_SEED_DEVRANDOM)
401 static const char *random_device_paths[] = { DEVRANDOM };
402 static struct random_device {
408 } random_devices[OSSL_NELEM(random_device_paths)];
409 static int keep_random_devices_open = 1;
411 # if defined(__linux) && defined(DEVRANDOM_WAIT) \
412 && defined(OPENSSL_RAND_SEED_GETRANDOM)
413 static void *shm_addr;
415 static void cleanup_shm(void)
421 * Ensure that the system randomness source has been adequately seeded.
422 * This is done by having the first start of libcrypto, wait until the device
423 * /dev/random becomes able to supply a byte of entropy. Subsequent starts
424 * of the library and later reseedings do not need to do this.
426 static int wait_random_seeded(void)
428 static int seeded = OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID < 0;
429 static const int kernel_version[] = { DEVRANDOM_SAFE_KERNEL };
437 /* See if anything has created the global seeded indication */
438 if ((shm_id = shmget(OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID, 1, 0)) == -1) {
440 * Check the kernel's version and fail if it is too recent.
442 * Linux kernels from 4.8 onwards do not guarantee that
443 * /dev/urandom is properly seeded when /dev/random becomes
444 * readable. However, such kernels support the getentropy(2)
445 * system call and this should always succeed which renders
446 * this alternative but essentially identical source moot.
448 if (uname(&un) == 0) {
449 kernel[0] = atoi(un.release);
450 p = strchr(un.release, '.');
451 kernel[1] = p == NULL ? 0 : atoi(p + 1);
452 if (kernel[0] > kernel_version[0]
453 || (kernel[0] == kernel_version[0]
454 && kernel[1] >= kernel_version[1])) {
458 /* Open /dev/random and wait for it to be readable */
459 if ((fd = open(DEVRANDOM_WAIT, O_RDONLY)) != -1) {
460 if (DEVRANDM_WAIT_USE_SELECT && fd < FD_SETSIZE) {
463 while ((r = select(fd + 1, &fds, NULL, NULL, NULL)) < 0
466 while ((r = read(fd, &c, 1)) < 0 && errno == EINTR);
471 /* Create the shared memory indicator */
472 shm_id = shmget(OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID, 1,
473 IPC_CREAT | S_IRUSR | S_IRGRP | S_IROTH);
480 * Map the shared memory to prevent its premature destruction.
481 * If this call fails, it isn't a big problem.
483 shm_addr = shmat(shm_id, NULL, SHM_RDONLY);
484 if (shm_addr != (void *)-1)
485 OPENSSL_atexit(&cleanup_shm);
490 # else /* defined __linux && DEVRANDOM_WAIT && OPENSSL_RAND_SEED_GETRANDOM */
491 static int wait_random_seeded(void)
498 * Verify that the file descriptor associated with the random source is
499 * still valid. The rationale for doing this is the fact that it is not
500 * uncommon for daemons to close all open file handles when daemonizing.
501 * So the handle might have been closed or even reused for opening
504 static int check_random_device(struct random_device * rd)
509 && fstat(rd->fd, &st) != -1
510 && rd->dev == st.st_dev
511 && rd->ino == st.st_ino
512 && ((rd->mode ^ st.st_mode) & ~(S_IRWXU | S_IRWXG | S_IRWXO)) == 0
513 && rd->rdev == st.st_rdev;
517 * Open a random device if required and return its file descriptor or -1 on error
519 static int get_random_device(size_t n)
522 struct random_device * rd = &random_devices[n];
524 /* reuse existing file descriptor if it is (still) valid */
525 if (check_random_device(rd))
528 /* open the random device ... */
529 if ((rd->fd = open(random_device_paths[n], O_RDONLY)) == -1)
532 /* ... and cache its relevant stat(2) data */
533 if (fstat(rd->fd, &st) != -1) {
536 rd->mode = st.st_mode;
537 rd->rdev = st.st_rdev;
547 * Close a random device making sure it is a random device
549 static void close_random_device(size_t n)
551 struct random_device * rd = &random_devices[n];
553 if (check_random_device(rd))
558 int rand_pool_init(void)
562 for (i = 0; i < OSSL_NELEM(random_devices); i++)
563 random_devices[i].fd = -1;
568 void rand_pool_cleanup(void)
572 for (i = 0; i < OSSL_NELEM(random_devices); i++)
573 close_random_device(i);
576 void rand_pool_keep_random_devices_open(int keep)
581 keep_random_devices_open = keep;
584 # else /* !defined(OPENSSL_RAND_SEED_DEVRANDOM) */
586 int rand_pool_init(void)
591 void rand_pool_cleanup(void)
595 void rand_pool_keep_random_devices_open(int keep)
599 # endif /* defined(OPENSSL_RAND_SEED_DEVRANDOM) */
602 * Try the various seeding methods in turn, exit when successful.
604 * TODO(DRBG): If more than one entropy source is available, is it
605 * preferable to stop as soon as enough entropy has been collected
606 * (as favored by @rsalz) or should one rather be defensive and add
607 * more entropy than requested and/or from different sources?
609 * Currently, the user can select multiple entropy sources in the
610 * configure step, yet in practice only the first available source
611 * will be used. A more flexible solution has been requested, but
612 * currently it is not clear how this can be achieved without
613 * overengineering the problem. There are many parameters which
614 * could be taken into account when selecting the order and amount
615 * of input from the different entropy sources (trust, quality,
616 * possibility of blocking).
618 size_t ossl_pool_acquire_entropy(RAND_POOL *pool)
620 # if defined(OPENSSL_RAND_SEED_NONE)
621 return rand_pool_entropy_available(pool);
623 size_t entropy_available = 0;
625 (void)entropy_available; /* avoid compiler warning */
627 # if defined(OPENSSL_RAND_SEED_GETRANDOM)
630 unsigned char *buffer;
632 /* Maximum allowed number of consecutive unsuccessful attempts */
635 bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
636 while (bytes_needed != 0 && attempts-- > 0) {
637 buffer = rand_pool_add_begin(pool, bytes_needed);
638 bytes = syscall_random(buffer, bytes_needed);
640 rand_pool_add_end(pool, bytes, 8 * bytes);
641 bytes_needed -= bytes;
642 attempts = 3; /* reset counter after successful attempt */
643 } else if (bytes < 0 && errno != EINTR) {
648 entropy_available = rand_pool_entropy_available(pool);
649 if (entropy_available > 0)
650 return entropy_available;
653 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
655 /* Not yet implemented. */
659 # if defined(OPENSSL_RAND_SEED_DEVRANDOM)
660 if (wait_random_seeded()) {
662 unsigned char *buffer;
665 bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
666 for (i = 0; bytes_needed > 0 && i < OSSL_NELEM(random_device_paths);
669 /* Maximum number of consecutive unsuccessful attempts */
671 const int fd = get_random_device(i);
676 while (bytes_needed != 0 && attempts-- > 0) {
677 buffer = rand_pool_add_begin(pool, bytes_needed);
678 bytes = read(fd, buffer, bytes_needed);
681 rand_pool_add_end(pool, bytes, 8 * bytes);
682 bytes_needed -= bytes;
683 attempts = 3; /* reset counter on successful attempt */
684 } else if (bytes < 0 && errno != EINTR) {
688 if (bytes < 0 || !keep_random_devices_open)
689 close_random_device(i);
691 bytes_needed = rand_pool_bytes_needed(pool, 1);
693 entropy_available = rand_pool_entropy_available(pool);
694 if (entropy_available > 0)
695 return entropy_available;
699 # if defined(OPENSSL_RAND_SEED_RDTSC)
700 entropy_available = prov_acquire_entropy_from_tsc(pool);
701 if (entropy_available > 0)
702 return entropy_available;
705 # if defined(OPENSSL_RAND_SEED_RDCPU)
706 entropy_available = prov_acquire_entropy_from_cpu(pool);
707 if (entropy_available > 0)
708 return entropy_available;
711 # if defined(OPENSSL_RAND_SEED_EGD)
713 static const char *paths[] = { DEVRANDOM_EGD, NULL };
715 unsigned char *buffer;
718 bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
719 for (i = 0; bytes_needed > 0 && paths[i] != NULL; i++) {
723 buffer = rand_pool_add_begin(pool, bytes_needed);
724 num = RAND_query_egd_bytes(paths[i],
725 buffer, (int)bytes_needed);
726 if (num == (int)bytes_needed)
727 bytes = bytes_needed;
729 rand_pool_add_end(pool, bytes, 8 * bytes);
730 bytes_needed = rand_pool_bytes_needed(pool, 1);
732 entropy_available = rand_pool_entropy_available(pool);
733 if (entropy_available > 0)
734 return entropy_available;
738 return rand_pool_entropy_available(pool);
744 #if (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS)) \
745 || defined(__DJGPP__)
746 int ossl_pool_add_nonce_data(RAND_POOL *pool)
750 CRYPTO_THREAD_ID tid;
754 /* Erase the entire structure including any padding */
755 memset(&data, 0, sizeof(data));
758 * Add process id, thread id, and a high resolution timestamp to
759 * ensure that the nonce is unique with high probability for
760 * different process instances.
763 data.tid = CRYPTO_THREAD_get_current_id();
764 data.time = get_time_stamp();
766 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
769 int rand_pool_add_additional_data(RAND_POOL *pool)
773 CRYPTO_THREAD_ID tid;
777 /* Erase the entire structure including any padding */
778 memset(&data, 0, sizeof(data));
781 * Add some noise from the thread id and a high resolution timer.
782 * The fork_id adds some extra fork-safety.
783 * The thread id adds a little randomness if the drbg is accessed
784 * concurrently (which is the case for the <master> drbg).
786 data.fork_id = openssl_get_fork_id();
787 data.tid = CRYPTO_THREAD_get_current_id();
788 data.time = get_timer_bits();
790 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
795 * Get the current time with the highest possible resolution
797 * The time stamp is added to the nonce, so it is optimized for not repeating.
798 * The current time is ideal for this purpose, provided the computer's clock
801 static uint64_t get_time_stamp(void)
803 # if defined(OSSL_POSIX_TIMER_OKAY)
807 if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
808 return TWO32TO64(ts.tv_sec, ts.tv_nsec);
811 # if defined(__unix__) \
812 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
816 if (gettimeofday(&tv, NULL) == 0)
817 return TWO32TO64(tv.tv_sec, tv.tv_usec);
824 * Get an arbitrary timer value of the highest possible resolution
826 * The timer value is added as random noise to the additional data,
827 * which is not considered a trusted entropy sourec, so any result
830 static uint64_t get_timer_bits(void)
832 uint64_t res = OPENSSL_rdtsc();
837 # if defined(__sun) || defined(__hpux)
843 read_wall_time(&t, TIMEBASE_SZ);
844 return TWO32TO64(t.tb_high, t.tb_low);
846 # elif defined(OSSL_POSIX_TIMER_OKAY)
850 # ifdef CLOCK_BOOTTIME
851 # define CLOCK_TYPE CLOCK_BOOTTIME
852 # elif defined(_POSIX_MONOTONIC_CLOCK)
853 # define CLOCK_TYPE CLOCK_MONOTONIC
855 # define CLOCK_TYPE CLOCK_REALTIME
858 if (clock_gettime(CLOCK_TYPE, &ts) == 0)
859 return TWO32TO64(ts.tv_sec, ts.tv_nsec);
862 # if defined(__unix__) \
863 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
867 if (gettimeofday(&tv, NULL) == 0)
868 return TWO32TO64(tv.tv_sec, tv.tv_usec);
873 #endif /* (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS))
874 || defined(__DJGPP__) */