Path: blob/main/crypto/openssl/providers/implementations/rands/seeding/rand_vxworks.c
48531 views
/*1* Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <openssl/opensslconf.h>1011#include <openssl/rand.h>12#include "crypto/rand_pool.h"13#include "crypto/rand.h"14#include "internal/cryptlib.h"15#include "prov/seeding.h"16#include <version.h>17#include <taskLib.h>1819#if defined(OPENSSL_RAND_SEED_NONE)20/* none means none */21# undef OPENSSL_RAND_SEED_OS22#endif2324#if defined(OPENSSL_RAND_SEED_OS)25# if _WRS_VXWORKS_MAJOR >= 726# define RAND_SEED_VXRANDLIB27# else28# error "VxWorks <7 only support RAND_SEED_NONE"29# endif30#endif3132#if defined(RAND_SEED_VXRANDLIB)33# include <randomNumGen.h>34#endif3536/* Macro to convert two thirty two bit values into a sixty four bit one */37#define TWO32TO64(a, b) ((((uint64_t)(a)) << 32) + (b))3839static uint64_t get_time_stamp(void)40{41struct timespec ts;4243if (clock_gettime(CLOCK_REALTIME, &ts) == 0)44return TWO32TO64(ts.tv_sec, ts.tv_nsec);45return time(NULL);46}4748static uint64_t get_timer_bits(void)49{50uint64_t res = OPENSSL_rdtsc();51struct timespec ts;5253if (res != 0)54return res;5556if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)57return TWO32TO64(ts.tv_sec, ts.tv_nsec);58return time(NULL);59}6061/*62* empty implementation63* vxworks does not need to init/cleanup or keep open the random lib64*/65int ossl_rand_pool_init(void)66{67return 1;68}6970void ossl_rand_pool_cleanup(void)71{72}7374void ossl_rand_pool_keep_random_devices_open(int keep)75{76}7778int ossl_pool_add_nonce_data(RAND_POOL *pool)79{80struct {81pid_t pid;82CRYPTO_THREAD_ID tid;83uint64_t time;84} data;8586memset(&data, 0, sizeof(data));8788/*89* Add process id, thread id, and a high resolution timestamp to90* ensure that the nonce is unique with high probability for91* different process instances.92*/93data.pid = getpid();94data.tid = CRYPTO_THREAD_get_current_id();95data.time = get_time_stamp();9697return ossl_rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);98}99100size_t ossl_pool_acquire_entropy(RAND_POOL *pool)101{102#if defined(RAND_SEED_VXRANDLIB)103/* vxRandLib based entropy method */104size_t bytes_needed;105106bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);107if (bytes_needed > 0) {108int retryCount = 0;109STATUS result = ERROR;110unsigned char *buffer;111112buffer = ossl_rand_pool_add_begin(pool, bytes_needed);113while ((result != OK) && (retryCount < 10)) {114RANDOM_NUM_GEN_STATUS status = randStatus();115116if ((status == RANDOM_NUM_GEN_ENOUGH_ENTROPY)117|| (status == RANDOM_NUM_GEN_MAX_ENTROPY)) {118result = randBytes(buffer, bytes_needed);119if (result == OK)120ossl_rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);121/*122* no else here: randStatus said ok, if randBytes failed123* it will result in another loop or no entropy124*/125} else {126/*127* give a minimum delay here to allow OS to collect more128* entropy. taskDelay duration will depend on the system tick,129* this is by design as the sw-random lib uses interrupts130* which will at least happen during ticks131*/132taskDelay(5);133}134retryCount++;135}136}137return ossl_rand_pool_entropy_available(pool);138#else139/*140* SEED_NONE means none, without randlib we dont have entropy and141* rely on it being added externally142*/143return ossl_rand_pool_entropy_available(pool);144#endif /* defined(RAND_SEED_VXRANDLIB) */145}146147148