]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/rand/rand_unix.c
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"
17 #ifdef OPENSSL_SYS_UNIX
18 # include <sys/types.h>
20 # include <sys/time.h>
22 static uint64_t get_time_stamp(void);
23 static uint64_t get_timer_bits(void);
25 /* Macro to convert two thirty two bit values into a sixty four bit one */
26 # define TWO32TO64(a, b) ((((uint64_t)(a)) << 32) + (b))
29 * Check for the existence and support of POSIX timers. The standard
30 * says that the _POSIX_TIMERS macro will have a positive value if they
33 * However, we want an additional constraint: that the timer support does
34 * not require an extra library dependency. Early versions of glibc
35 * require -lrt to be specified on the link line to access the timers,
36 * so this needs to be checked for.
38 * It is worse because some libraries define __GLIBC__ but don't
39 * support the version testing macro (e.g. uClibc). This means
40 * an extra check is needed.
42 * The final condition is:
43 * "have posix timers and either not glibc or glibc without -lrt"
45 * The nested #if sequences are required to avoid using a parameterised
46 * macro that might be undefined.
48 # undef OSSL_POSIX_TIMER_OKAY
49 # if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
50 # if defined(__GLIBC__)
51 # if defined(__GLIBC_PREREQ)
52 # if __GLIBC_PREREQ(2, 17)
53 # define OSSL_POSIX_TIMER_OKAY
57 # define OSSL_POSIX_TIMER_OKAY
62 #if (defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)) && \
63 !defined(OPENSSL_RAND_SEED_NONE)
64 # error "UEFI and VXWorks only support seeding NONE"
67 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) \
68 || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) \
69 || defined(OPENSSL_SYS_UEFI))
71 # if defined(OPENSSL_SYS_VOS)
73 # ifndef OPENSSL_RAND_SEED_OS
74 # error "Unsupported seeding method configured; must be os"
77 # if defined(OPENSSL_SYS_VOS_HPPA) && defined(OPENSSL_SYS_VOS_IA32)
78 # error "Unsupported HP-PA and IA32 at the same time."
80 # if !defined(OPENSSL_SYS_VOS_HPPA) && !defined(OPENSSL_SYS_VOS_IA32)
81 # error "Must have one of HP-PA or IA32"
85 * The following algorithm repeatedly samples the real-time clock (RTC) to
86 * generate a sequence of unpredictable data. The algorithm relies upon the
87 * uneven execution speed of the code (due to factors such as cache misses,
88 * interrupts, bus activity, and scheduling) and upon the rather large
89 * relative difference between the speed of the clock and the rate at which
90 * it can be read. If it is ported to an environment where execution speed
91 * is more constant or where the RTC ticks at a much slower rate, or the
92 * clock can be read with fewer instructions, it is likely that the results
93 * would be far more predictable. This should only be used for legacy
96 * As a precaution, we assume only 2 bits of entropy per byte.
98 size_t rand_pool_acquire_entropy(RAND_POOL
*pool
)
105 # ifdef OPENSSL_SYS_VOS_HPPA
107 extern void s$
sleep(long *_duration
, short int *_code
);
110 extern void s$
sleep2(long long *_duration
, short int *_code
);
113 bytes_needed
= rand_pool_bytes_needed(pool
, 2 /*entropy_per_byte*/);
115 for (i
= 0; i
< bytes_needed
; i
++) {
117 * burn some cpu; hope for interrupts, cache collisions, bus
120 for (k
= 0; k
< 99; k
++)
121 ts
.tv_nsec
= random();
123 # ifdef OPENSSL_SYS_VOS_HPPA
124 /* sleep for 1/1024 of a second (976 us). */
126 s$
sleep(&duration
, &code
);
128 /* sleep for 1/65536 of a second (15 us). */
130 s$
sleep2(&duration
, &code
);
133 /* Get wall clock time, take 8 bits. */
134 clock_gettime(CLOCK_REALTIME
, &ts
);
135 v
= (unsigned char)(ts
.tv_nsec
& 0xFF);
136 rand_pool_add(pool
, arg
, &v
, sizeof(v
) , 2);
138 return rand_pool_entropy_available(pool
);
143 # if defined(OPENSSL_RAND_SEED_EGD) && \
144 (defined(OPENSSL_NO_EGD) || !defined(DEVRANDOM_EGD))
145 # error "Seeding uses EGD but EGD is turned off or no device given"
148 # if defined(OPENSSL_RAND_SEED_DEVRANDOM) && !defined(DEVRANDOM)
149 # error "Seeding uses urandom but DEVRANDOM is not configured"
152 # if defined(OPENSSL_RAND_SEED_OS)
153 # if !defined(DEVRANDOM)
154 # error "OS seeding requires DEVRANDOM to be configured"
156 # define OPENSSL_RAND_SEED_DEVRANDOM
157 # if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
158 # if __GLIBC_PREREQ(2, 25)
159 # define OPENSSL_RAND_SEED_GETRANDOM
164 # ifdef OPENSSL_RAND_SEED_GETRANDOM
165 # include <sys/random.h>
168 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
169 # error "librandom not (yet) supported"
173 * Try the various seeding methods in turn, exit when successful.
175 * TODO(DRBG): If more than one entropy source is available, is it
176 * preferable to stop as soon as enough entropy has been collected
177 * (as favored by @rsalz) or should one rather be defensive and add
178 * more entropy than requested and/or from different sources?
180 * Currently, the user can select multiple entropy sources in the
181 * configure step, yet in practice only the first available source
182 * will be used. A more flexible solution has been requested, but
183 * currently it is not clear how this can be achieved without
184 * overengineering the problem. There are many parameters which
185 * could be taken into account when selecting the order and amount
186 * of input from the different entropy sources (trust, quality,
187 * possibility of blocking).
189 size_t rand_pool_acquire_entropy(RAND_POOL
*pool
)
191 # ifdef OPENSSL_RAND_SEED_NONE
192 return rand_pool_entropy_available(pool
);
195 size_t entropy_available
= 0;
196 unsigned char *buffer
;
198 # ifdef OPENSSL_RAND_SEED_GETRANDOM
199 bytes_needed
= rand_pool_bytes_needed(pool
, 8 /*entropy_per_byte*/);
200 buffer
= rand_pool_add_begin(pool
, bytes_needed
);
201 if (buffer
!= NULL
) {
204 if (getrandom(buffer
, bytes_needed
, 0) == (int)bytes_needed
)
205 bytes
= bytes_needed
;
207 rand_pool_add_end(pool
, bytes
, 8 * bytes
);
208 entropy_available
= rand_pool_entropy_available(pool
);
210 if (entropy_available
> 0)
211 return entropy_available
;
214 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
216 /* Not yet implemented. */
220 # ifdef OPENSSL_RAND_SEED_DEVRANDOM
221 bytes_needed
= rand_pool_bytes_needed(pool
, 8 /*entropy_per_byte*/);
222 if (bytes_needed
> 0) {
223 static const char *paths
[] = { DEVRANDOM
, NULL
};
227 for (i
= 0; paths
[i
] != NULL
; i
++) {
228 if ((fp
= fopen(paths
[i
], "rb")) == NULL
)
231 buffer
= rand_pool_add_begin(pool
, bytes_needed
);
232 if (buffer
!= NULL
) {
234 if (fread(buffer
, 1, bytes_needed
, fp
) == bytes_needed
)
235 bytes
= bytes_needed
;
237 rand_pool_add_end(pool
, bytes
, 8 * bytes
);
238 entropy_available
= rand_pool_entropy_available(pool
);
241 if (entropy_available
> 0)
242 return entropy_available
;
244 bytes_needed
= rand_pool_bytes_needed(pool
, 8 /*entropy_per_byte*/);
249 # ifdef OPENSSL_RAND_SEED_RDTSC
250 entropy_available
= rand_acquire_entropy_from_tsc(pool
);
251 if (entropy_available
> 0)
252 return entropy_available
;
255 # ifdef OPENSSL_RAND_SEED_RDCPU
256 entropy_available
= rand_acquire_entropy_from_cpu(pool
);
257 if (entropy_available
> 0)
258 return entropy_available
;
261 # ifdef OPENSSL_RAND_SEED_EGD
262 bytes_needed
= rand_pool_bytes_needed(pool
, 8 /*entropy_per_byte*/);
263 if (bytes_needed
> 0) {
264 static const char *paths
[] = { DEVRANDOM_EGD
, NULL
};
267 for (i
= 0; paths
[i
] != NULL
; i
++) {
268 buffer
= rand_pool_add_begin(pool
, bytes_needed
);
269 if (buffer
!= NULL
) {
271 int num
= RAND_query_egd_bytes(paths
[i
],
272 buffer
, (int)bytes_needed
);
273 if (num
== (int)bytes_needed
)
274 bytes
= bytes_needed
;
276 rand_pool_add_end(pool
, bytes
, 8 * bytes
);
277 entropy_available
= rand_pool_entropy_available(pool
);
279 if (entropy_available
> 0)
280 return entropy_available
;
285 return rand_pool_entropy_available(pool
);
291 #ifdef OPENSSL_SYS_UNIX
292 int rand_pool_add_nonce_data(RAND_POOL
*pool
)
296 CRYPTO_THREAD_ID tid
;
301 * Add process id, thread id, and a high resolution timestamp to
302 * ensure that the nonce is unique whith high probability for
303 * different process instances.
306 data
.tid
= CRYPTO_THREAD_get_current_id();
307 data
.time
= get_time_stamp();
309 return rand_pool_add(pool
, (unsigned char *)&data
, sizeof(data
), 0);
312 int rand_pool_add_additional_data(RAND_POOL
*pool
)
315 CRYPTO_THREAD_ID tid
;
320 * Add some noise from the thread id and a high resolution timer.
321 * The thread id adds a little randomness if the drbg is accessed
322 * concurrently (which is the case for the <master> drbg).
324 data
.tid
= CRYPTO_THREAD_get_current_id();
325 data
.time
= get_timer_bits();
327 return rand_pool_add(pool
, (unsigned char *)&data
, sizeof(data
), 0);
333 * Get the current time with the highest possible resolution
335 * The time stamp is added to the nonce, so it is optimized for not repeating.
336 * The current time is ideal for this purpose, provided the computer's clock
339 static uint64_t get_time_stamp(void)
341 # if defined(OSSL_POSIX_TIMER_OKAY)
345 if (clock_gettime(CLOCK_REALTIME
, &ts
) == 0)
346 return TWO32TO64(ts
.tv_sec
, ts
.tv_nsec
);
349 # if defined(__unix__) \
350 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
354 if (gettimeofday(&tv
, NULL
) == 0)
355 return TWO32TO64(tv
.tv_sec
, tv
.tv_usec
);
362 * Get an arbitrary timer value of the highest possible resolution
364 * The timer value is added as random noise to the additional data,
365 * which is not considered a trusted entropy sourec, so any result
368 static uint64_t get_timer_bits(void)
370 uint64_t res
= OPENSSL_rdtsc();
375 # if defined(__sun) || defined(__hpux)
381 read_wall_time(&t
, TIMEBASE_SZ
);
382 return TWO32TO64(t
.tb_high
, t
.tb_low
);
384 # elif defined(OSSL_POSIX_TIMER_OKAY)
388 # ifdef CLOCK_BOOTTIME
389 # define CLOCK_TYPE CLOCK_BOOTTIME
390 # elif defined(_POSIX_MONOTONIC_CLOCK)
391 # define CLOCK_TYPE CLOCK_MONOTONIC
393 # define CLOCK_TYPE CLOCK_REALTIME
396 if (clock_gettime(CLOCK_TYPE
, &ts
) == 0)
397 return TWO32TO64(ts
.tv_sec
, ts
.tv_nsec
);
400 # if defined(__unix__) \
401 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
405 if (gettimeofday(&tv
, NULL
) == 0)
406 return TWO32TO64(tv
.tv_sec
, tv
.tv_usec
);