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 "crypto/rand_pool.h"
14 #include "crypto/rand.h"
15 #include "internal/cryptlib.h"
16 #include "prov/seeding.h"
20 #if defined(OPENSSL_RAND_SEED_NONE)
22 # undef OPENSSL_RAND_SEED_OS
25 #if defined(OPENSSL_RAND_SEED_OS)
26 # if _WRS_VXWORKS_MAJOR >= 7
27 # define RAND_SEED_VXRANDLIB
29 # error "VxWorks <7 only support RAND_SEED_NONE"
33 #if defined(RAND_SEED_VXRANDLIB)
34 # include <randomNumGen.h>
37 /* Macro to convert two thirty two bit values into a sixty four bit one */
38 #define TWO32TO64(a, b) ((((uint64_t)(a)) << 32) + (b))
40 static uint64_t get_time_stamp(void)
44 if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
45 return TWO32TO64(ts.tv_sec, ts.tv_nsec);
49 static uint64_t get_timer_bits(void)
51 uint64_t res = OPENSSL_rdtsc();
57 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
58 return TWO32TO64(ts.tv_sec, ts.tv_nsec);
63 * empty implementation
64 * vxworks does not need to init/cleanup or keep open the random lib
66 int rand_pool_init(void)
71 void rand_pool_cleanup(void)
75 void rand_pool_keep_random_devices_open(int keep)
79 int rand_pool_add_additional_data(RAND_POOL *pool)
86 memset(&data, 0, sizeof(data));
89 * Add some noise from the thread id and a high resolution timer.
90 * The thread id adds a little randomness if the drbg is accessed
91 * concurrently (which is the case for the <master> drbg).
93 data.tid = CRYPTO_THREAD_get_current_id();
94 data.time = get_timer_bits();
96 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
99 int ossl_pool_add_nonce_data(RAND_POOL *pool)
103 CRYPTO_THREAD_ID tid;
107 memset(&data, 0, sizeof(data));
110 * Add process id, thread id, and a high resolution timestamp to
111 * ensure that the nonce is unique with high probability for
112 * different process instances.
115 data.tid = CRYPTO_THREAD_get_current_id();
116 data.time = get_time_stamp();
118 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
121 size_t ossl_pool_acquire_entropy(RAND_POOL *pool)
123 #if defined(RAND_SEED_VXRANDLIB)
124 /* vxRandLib based entropy method */
127 bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
128 if (bytes_needed > 0)
131 STATUS result = ERROR;
132 unsigned char *buffer;
134 buffer = rand_pool_add_begin(pool, bytes_needed);
135 while ((result != OK) && (retryCount < 10)) {
136 RANDOM_NUM_GEN_STATUS status = randStatus();
138 if ((status == RANDOM_NUM_GEN_ENOUGH_ENTROPY)
139 || (status == RANDOM_NUM_GEN_MAX_ENTROPY) ) {
140 result = randBytes(buffer, bytes_needed);
142 rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
144 * no else here: randStatus said ok, if randBytes failed
145 * it will result in another loop or no entropy
149 * give a minimum delay here to allow OS to collect more
150 * entropy. taskDelay duration will depend on the system tick,
151 * this is by design as the sw-random lib uses interrupts
152 * which will at least happen during ticks
159 return rand_pool_entropy_available(pool);
162 * SEED_NONE means none, without randlib we dont have entropy and
163 * rely on it being added externally
165 return rand_pool_entropy_available(pool);
166 #endif /* defined(RAND_SEED_VXRANDLIB) */