2 * Copyright 1995-2018 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
12 #include "internal/cryptlib.h"
13 #include <openssl/rand.h>
15 #include "internal/rand_int.h"
18 #if (defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)) && \
19 !defined(OPENSSL_RAND_SEED_NONE)
20 # error "UEFI and VXWorks only support seeding NONE"
23 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) \
24 || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) \
25 || defined(OPENSSL_SYS_UEFI))
27 # if defined(OPENSSL_SYS_VOS)
29 # ifndef OPENSSL_RAND_SEED_OS
30 # error "Unsupported seeding method configured; must be os"
33 # if defined(OPENSSL_SYS_VOS_HPPA) && defined(OPENSSL_SYS_VOS_IA32)
34 # error "Unsupported HP-PA and IA32 at the same time."
36 # if !defined(OPENSSL_SYS_VOS_HPPA) && !defined(OPENSSL_SYS_VOS_IA32)
37 # error "Must have one of HP-PA or IA32"
41 * The following algorithm repeatedly samples the real-time clock (RTC) to
42 * generate a sequence of unpredictable data. The algorithm relies upon the
43 * uneven execution speed of the code (due to factors such as cache misses,
44 * interrupts, bus activity, and scheduling) and upon the rather large
45 * relative difference between the speed of the clock and the rate at which
46 * it can be read. If it is ported to an environment where execution speed
47 * is more constant or where the RTC ticks at a much slower rate, or the
48 * clock can be read with fewer instructions, it is likely that the results
49 * would be far more predictable. This should only be used for legacy
52 * As a precaution, we assume only 2 bits of entropy per byte.
54 size_t rand_pool_acquire_entropy(RAND_POOL *pool)
64 # ifdef OPENSSL_SYS_VOS_HPPA
66 extern void s$sleep(long *_duration, short int *_code);
69 extern void s$sleep2(long long *_duration, short int *_code);
73 * Seed with the gid, pid, and uid, to ensure *some* variation between
74 * different processes.
77 rand_pool_add(pool, &curr_gid, sizeof(curr_gid), 0);
79 rand_pool_add(pool, &curr_pid, sizeof(curr_pid), 0);
81 rand_pool_add(pool, &curr_uid, sizeof(curr_uid), 0);
83 bytes_needed = rand_pool_bytes_needed(pool, 2 /*entropy_per_byte*/);
85 for (i = 0; i < bytes_needed; i++) {
87 * burn some cpu; hope for interrupts, cache collisions, bus
90 for (k = 0; k < 99; k++)
91 ts.tv_nsec = random();
93 # ifdef OPENSSL_SYS_VOS_HPPA
94 /* sleep for 1/1024 of a second (976 us). */
96 s$sleep(&duration, &code);
98 /* sleep for 1/65536 of a second (15 us). */
100 s$sleep2(&duration, &code);
103 /* Get wall clock time, take 8 bits. */
104 clock_gettime(CLOCK_REALTIME, &ts);
105 v = (unsigned char)(ts.tv_nsec & 0xFF);
106 rand_pool_add(pool, arg, &v, sizeof(v) , 2);
108 return rand_pool_entropy_available(pool);
113 # if defined(OPENSSL_RAND_SEED_EGD) && \
114 (defined(OPENSSL_NO_EGD) || !defined(DEVRANDOM_EGD))
115 # error "Seeding uses EGD but EGD is turned off or no device given"
118 # if defined(OPENSSL_RAND_SEED_DEVRANDOM) && !defined(DEVRANDOM)
119 # error "Seeding uses urandom but DEVRANDOM is not configured"
122 # if defined(OPENSSL_RAND_SEED_OS)
123 # if !defined(DEVRANDOM)
124 # error "OS seeding requires DEVRANDOM to be configured"
126 # define OPENSSL_RAND_SEED_DEVRANDOM
127 # if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
128 # if __GLIBC_PREREQ(2, 25)
129 # define OPENSSL_RAND_SEED_GETRANDOM
134 # ifdef OPENSSL_RAND_SEED_GETRANDOM
135 # include <sys/random.h>
138 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
139 # error "librandom not (yet) supported"
143 * Try the various seeding methods in turn, exit when successful.
145 * TODO(DRBG): If more than one entropy source is available, is it
146 * preferable to stop as soon as enough entropy has been collected
147 * (as favored by @rsalz) or should one rather be defensive and add
148 * more entropy than requested and/or from different sources?
150 * Currently, the user can select multiple entropy sources in the
151 * configure step, yet in practice only the first available source
152 * will be used. A more flexible solution has been requested, but
153 * currently it is not clear how this can be achieved without
154 * overengineering the problem. There are many parameters which
155 * could be taken into account when selecting the order and amount
156 * of input from the different entropy sources (trust, quality,
157 * possibility of blocking).
159 size_t rand_pool_acquire_entropy(RAND_POOL *pool)
161 # ifdef OPENSSL_RAND_SEED_NONE
162 return rand_pool_entropy_available(pool);
165 size_t entropy_available = 0;
166 unsigned char *buffer;
168 # ifdef OPENSSL_RAND_SEED_GETRANDOM
169 bytes_needed = rand_pool_bytes_needed(pool, 8 /*entropy_per_byte*/);
170 buffer = rand_pool_add_begin(pool, bytes_needed);
171 if (buffer != NULL) {
174 if (getrandom(buffer, bytes_needed, 0) == (int)bytes_needed)
175 bytes = bytes_needed;
177 rand_pool_add_end(pool, bytes, 8 * bytes);
178 entropy_available = rand_pool_entropy_available(pool);
180 if (entropy_available > 0)
181 return entropy_available;
184 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
186 /* Not yet implemented. */
190 # ifdef OPENSSL_RAND_SEED_DEVRANDOM
191 bytes_needed = rand_pool_bytes_needed(pool, 8 /*entropy_per_byte*/);
192 if (bytes_needed > 0) {
193 static const char *paths[] = { DEVRANDOM, NULL };
197 for (i = 0; paths[i] != NULL; i++) {
198 if ((fp = fopen(paths[i], "rb")) == NULL)
201 buffer = rand_pool_add_begin(pool, bytes_needed);
202 if (buffer != NULL) {
204 if (fread(buffer, 1, bytes_needed, fp) == bytes_needed)
205 bytes = bytes_needed;
207 rand_pool_add_end(pool, bytes, 8 * bytes);
208 entropy_available = rand_pool_entropy_available(pool);
211 if (entropy_available > 0)
212 return entropy_available;
214 bytes_needed = rand_pool_bytes_needed(pool, 8 /*entropy_per_byte*/);
219 # ifdef OPENSSL_RAND_SEED_RDTSC
220 entropy_available = rand_acquire_entropy_from_tsc(pool);
221 if (entropy_available > 0)
222 return entropy_available;
225 # ifdef OPENSSL_RAND_SEED_RDCPU
226 entropy_available = rand_acquire_entropy_from_cpu(pool);
227 if (entropy_available > 0)
228 return entropy_available;
231 # ifdef OPENSSL_RAND_SEED_EGD
232 bytes_needed = rand_pool_bytes_needed(pool, 8 /*entropy_per_byte*/);
233 if (bytes_needed > 0) {
234 static const char *paths[] = { DEVRANDOM_EGD, NULL };
237 for (i = 0; paths[i] != NULL; i++) {
238 buffer = rand_pool_add_begin(pool, bytes_needed);
239 if (buffer != NULL) {
241 int num = RAND_query_egd_bytes(paths[i],
242 buffer, (int)bytes_needed);
243 if (num == (int)bytes_needed)
244 bytes = bytes_needed;
246 rand_pool_add_end(pool, bytes, 8 * bytes);
247 entropy_available = rand_pool_entropy_available(pool);
249 if (entropy_available > 0)
250 return entropy_available;
255 return rand_pool_entropy_available(pool);