/* * Get an arbitrary timer value of the highest possible resolution * * The timer value is added as random noise to the additional data, * which is not considered a trusted entropy sourec, so any result * is acceptable. */ static uint64_t get_timer_bits(void) { uint64_t res = OPENSSL_rdtsc(); if (res != 0) return res; # if defined(__sun) || defined(__hpux) return gethrtime(); # elif defined(_AIX) { timebasestruct_t t; read_wall_time(&t, TIMEBASE_SZ); return TWO32TO64(t.tb_high, t.tb_low); } # elif defined(OSSL_POSIX_TIMER_OKAY) { struct timespec ts; # ifdef CLOCK_BOOTTIME # define CLOCK_TYPE CLOCK_BOOTTIME # elif defined(_POSIX_MONOTONIC_CLOCK) # define CLOCK_TYPE CLOCK_MONOTONIC # else # define CLOCK_TYPE CLOCK_REALTIME # endif if (clock_gettime(CLOCK_TYPE, &ts) == 0) return TWO32TO64(ts.tv_sec, ts.tv_nsec); } # endif # if defined(__unix__) \ || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) { struct timeval tv; if (gettimeofday(&tv, NULL) == 0) return TWO32TO64(tv.tv_sec, tv.tv_usec); } # endif return time(NULL); }
/* * Get the current time with the highest possible resolution * * The time stamp is added to the nonce, so it is optimized for not repeating. * The current time is ideal for this purpose, provided the computer's clock * is synchronized. */ static uint64_t get_time_stamp(void) { # if defined(OSSL_POSIX_TIMER_OKAY) { struct timespec ts; if (clock_gettime(CLOCK_REALTIME, &ts) == 0) return TWO32TO64(ts.tv_sec, ts.tv_nsec); } # endif # if defined(__unix__) \ || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) { struct timeval tv; if (gettimeofday(&tv, NULL) == 0) return TWO32TO64(tv.tv_sec, tv.tv_usec); } # endif return time(NULL); }
static uint64_t get_timer_bits(void) { uint64_t res = OPENSSL_rdtsc(); struct timespec ts; if (res != 0) return res; if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) return TWO32TO64(ts.tv_sec, ts.tv_nsec); return time(NULL); }
NON_EMPTY_TRANSLATION_UNIT #else # include <openssl/rand.h> # include "rand_lcl.h" # include "internal/rand_int.h" # include "internal/cryptlib.h" # include <version.h> # include <taskLib.h> # if defined(OPENSSL_RAND_SEED_NONE) /* none means none */ # undef OPENSSL_RAND_SEED_OS # endif # if defined(OPENSSL_RAND_SEED_OS) # if _WRS_VXWORKS_MAJOR >= 7 # define RAND_SEED_VXRANDLIB # else # error "VxWorks <7 only support RAND_SEED_NONE" # endif # endif # if defined(RAND_SEED_VXRANDLIB) # include <randomNumGen.h> # endif /* Macro to convert two thirty two bit values into a sixty four bit one */ # define TWO32TO64(a, b) ((((uint64_t)(a)) << 32) + (b)) static uint64_t get_time_stamp(void) { struct timespec ts; if (clock_gettime(CLOCK_REALTIME, &ts) == 0) return TWO32TO64(ts.tv_sec, ts.tv_nsec); return time(NULL); }
/* * Find a suitable source of time. Start with the highest resolution source * and work down to the slower ones. This is added as additional data and * isn't counted as randomness, so any result is acceptable. * * Returns 0 when we weren't able to find any time source */ static uint64_t get_timer_bits(void) { uint64_t res = OPENSSL_rdtsc(); if (res != 0) return res; #if defined(_WIN32) { LARGE_INTEGER t; FILETIME ft; if (QueryPerformanceCounter(&t) != 0) return t.QuadPart; GetSystemTimeAsFileTime(&ft); return TWO32TO64(ft.dwHighDateTime, ft.dwLowDateTime); } #elif defined(__sun) || defined(__hpux) return gethrtime(); #elif defined(_AIX) { timebasestruct_t t; read_wall_time(&t, TIMEBASE_SZ); return TWO32TO64(t.tb_high, t.tb_low); } #else # if defined(OSSL_POSIX_TIMER_OKAY) { struct timespec ts; clockid_t cid; # ifdef CLOCK_BOOTTIME cid = CLOCK_BOOTTIME; # elif defined(_POSIX_MONOTONIC_CLOCK) cid = CLOCK_MONOTONIC; # else cid = CLOCK_REALTIME; # endif if (clock_gettime(cid, &ts) == 0) return TWO32TO64(ts.tv_sec, ts.tv_nsec); } # endif # if defined(__unix__) \ || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) { struct timeval tv; if (gettimeofday(&tv, NULL) == 0) return TWO32TO64(tv.tv_sec, tv.tv_usec); } # endif { time_t t = time(NULL); if (t == (time_t)-1) return 0; return t; } #endif }