#include <openssl/opensslconf.h>
#include <openssl/rand.h>
#include "crypto/rand_pool.h"
#include "crypto/rand.h"
#include "internal/cryptlib.h"
#include "prov/seeding.h"
#include <version.h>
#include <taskLib.h>
#if defined(OPENSSL_RAND_SEED_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
#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);
}
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);
}
int ossl_rand_pool_init(void)
{
return 1;
}
void ossl_rand_pool_cleanup(void)
{
}
void ossl_rand_pool_keep_random_devices_open(int keep)
{
}
int ossl_pool_add_nonce_data(RAND_POOL *pool)
{
struct {
pid_t pid;
CRYPTO_THREAD_ID tid;
uint64_t time;
} data;
memset(&data, 0, sizeof(data));
data.pid = getpid();
data.tid = CRYPTO_THREAD_get_current_id();
data.time = get_time_stamp();
return ossl_rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
}
size_t ossl_pool_acquire_entropy(RAND_POOL *pool)
{
#if defined(RAND_SEED_VXRANDLIB)
size_t bytes_needed;
bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 );
if (bytes_needed > 0) {
int retryCount = 0;
STATUS result = ERROR;
unsigned char *buffer;
buffer = ossl_rand_pool_add_begin(pool, bytes_needed);
while ((result != OK) && (retryCount < 10)) {
RANDOM_NUM_GEN_STATUS status = randStatus();
if ((status == RANDOM_NUM_GEN_ENOUGH_ENTROPY)
|| (status == RANDOM_NUM_GEN_MAX_ENTROPY)) {
result = randBytes(buffer, bytes_needed);
if (result == OK)
ossl_rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
} else {
taskDelay(5);
}
retryCount++;
}
}
return ossl_rand_pool_entropy_available(pool);
#else
return ossl_rand_pool_entropy_available(pool);
#endif
}