2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
10 #include <openssl/opensslconf.h>
12 #ifndef OPENSSL_SYS_VXWORKS
13 NON_EMPTY_TRANSLATION_UNIT
15 # include <openssl/rand.h>
16 # include "rand_lcl.h"
17 # include "internal/rand_int.h"
18 # include "internal/cryptlib.h"
22 # if defined(OPENSSL_RAND_SEED_NONE)
24 # undef OPENSSL_RAND_SEED_OS
27 # if defined(OPENSSL_RAND_SEED_OS)
28 # if _WRS_VXWORKS_MAJOR >= 7
29 # define RAND_SEED_VXRANDLIB
31 # error "VxWorks <7 only support RAND_SEED_NONE"
35 # if defined(RAND_SEED_VXRANDLIB)
36 # include <randomNumGen.h>
39 /* Macro to convert two thirty two bit values into a sixty four bit one */
40 # define TWO32TO64(a, b) ((((uint64_t)(a)) << 32) + (b))
42 static uint64_t get_time_stamp(void)
46 if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
47 return TWO32TO64(ts.tv_sec, ts.tv_nsec);
51 static uint64_t get_timer_bits(void)
53 uint64_t res = OPENSSL_rdtsc();
59 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
60 return TWO32TO64(ts.tv_sec, ts.tv_nsec);
65 * empty implementation
66 * vxworks does not need to init/cleanup or keep open the random lib
68 int rand_pool_init(void)
73 void rand_pool_cleanup(void)
77 void rand_pool_keep_random_devices_open(int keep)
81 int rand_pool_add_additional_data(RAND_POOL *pool)
88 memset(&data, 0, sizeof(data));
91 * Add some noise from the thread id and a high resolution timer.
92 * The thread id adds a little randomness if the drbg is accessed
93 * concurrently (which is the case for the <master> drbg).
95 data.tid = CRYPTO_THREAD_get_current_id();
96 data.time = get_timer_bits();
98 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
101 int rand_pool_add_nonce_data(RAND_POOL *pool)
105 CRYPTO_THREAD_ID tid;
109 memset(&data, 0, sizeof(data));
112 * Add process id, thread id, and a high resolution timestamp to
113 * ensure that the nonce is unique with high probability for
114 * different process instances.
117 data.tid = CRYPTO_THREAD_get_current_id();
118 data.time = get_time_stamp();
120 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
123 size_t rand_pool_acquire_entropy(RAND_POOL *pool)
125 # if defined(RAND_SEED_VXRANDLIB)
126 /* vxRandLib based entropy method */
129 bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
130 if (bytes_needed > 0)
133 STATUS result = ERROR;
134 unsigned char *buffer;
136 buffer = rand_pool_add_begin(pool, bytes_needed);
137 while ((result != OK) && (retryCount < 10)) {
138 RANDOM_NUM_GEN_STATUS status = randStatus();
140 if ((status == RANDOM_NUM_GEN_ENOUGH_ENTROPY)
141 || (status == RANDOM_NUM_GEN_MAX_ENTROPY) ) {
142 result = randBytes(buffer, bytes_needed);
144 rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
146 * no else here: randStatus said ok, if randBytes failed
147 * it will result in another loop or no entropy
151 * give a minimum delay here to allow OS to collect more
152 * entropy. taskDelay duration will depend on the system tick,
153 * this is by design as the sw-random lib uses interrupts
154 * which will at least happen during ticks
161 return rand_pool_entropy_available(pool);
164 * SEED_NONE means none, without randlib we dont have entropy and
165 * rely on it being added externally
167 return rand_pool_entropy_available(pool);
168 # endif /* defined(RAND_SEED_VXRANDLIB) */
171 #endif /* OPENSSL_SYS_VXWORKS */