2 * Copyright 2019-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
10 #include <openssl/opensslconf.h>
12 #include <openssl/rand.h>
13 #include "rand_local.h"
14 #include "crypto/rand.h"
15 #include "internal/cryptlib.h"
19 #if defined(OPENSSL_RAND_SEED_NONE)
21 # undef OPENSSL_RAND_SEED_OS
24 #if defined(OPENSSL_RAND_SEED_OS)
25 # if _WRS_VXWORKS_MAJOR >= 7
26 # define RAND_SEED_VXRANDLIB
28 # error "VxWorks <7 only support RAND_SEED_NONE"
32 #if defined(RAND_SEED_VXRANDLIB)
33 # include <randomNumGen.h>
36 /* Macro to convert two thirty two bit values into a sixty four bit one */
37 #define TWO32TO64(a, b) ((((uint64_t)(a)) << 32) + (b))
39 static uint64_t get_time_stamp(void)
43 if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
44 return TWO32TO64(ts.tv_sec, ts.tv_nsec);
48 static uint64_t get_timer_bits(void)
50 uint64_t res = OPENSSL_rdtsc();
56 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
57 return TWO32TO64(ts.tv_sec, ts.tv_nsec);
62 * empty implementation
63 * vxworks does not need to init/cleanup or keep open the random lib
65 int rand_pool_init(void)
70 void rand_pool_cleanup(void)
74 void rand_pool_keep_random_devices_open(int keep)
78 int rand_pool_add_additional_data(RAND_POOL *pool)
85 memset(&data, 0, sizeof(data));
88 * Add some noise from the thread id and a high resolution timer.
89 * The thread id adds a little randomness if the drbg is accessed
90 * concurrently (which is the case for the <master> drbg).
92 data.tid = CRYPTO_THREAD_get_current_id();
93 data.time = get_timer_bits();
95 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
98 int rand_pool_add_nonce_data(RAND_POOL *pool)
102 CRYPTO_THREAD_ID tid;
106 memset(&data, 0, sizeof(data));
109 * Add process id, thread id, and a high resolution timestamp to
110 * ensure that the nonce is unique with high probability for
111 * different process instances.
114 data.tid = CRYPTO_THREAD_get_current_id();
115 data.time = get_time_stamp();
117 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
120 size_t rand_pool_acquire_entropy(RAND_POOL *pool)
122 #if defined(RAND_SEED_VXRANDLIB)
123 /* vxRandLib based entropy method */
126 bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
127 if (bytes_needed > 0)
130 STATUS result = ERROR;
131 unsigned char *buffer;
133 buffer = rand_pool_add_begin(pool, bytes_needed);
134 while ((result != OK) && (retryCount < 10)) {
135 RANDOM_NUM_GEN_STATUS status = randStatus();
137 if ((status == RANDOM_NUM_GEN_ENOUGH_ENTROPY)
138 || (status == RANDOM_NUM_GEN_MAX_ENTROPY) ) {
139 result = randBytes(buffer, bytes_needed);
141 rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
143 * no else here: randStatus said ok, if randBytes failed
144 * it will result in another loop or no entropy
148 * give a minimum delay here to allow OS to collect more
149 * entropy. taskDelay duration will depend on the system tick,
150 * this is by design as the sw-random lib uses interrupts
151 * which will at least happen during ticks
158 return rand_pool_entropy_available(pool);
161 * SEED_NONE means none, without randlib we dont have entropy and
162 * rely on it being added externally
164 return rand_pool_entropy_available(pool);
165 #endif /* defined(RAND_SEED_VXRANDLIB) */