]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
0d664759 | 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
0c61e299 | 3 | * |
b1322259 RS |
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 | |
0c61e299 | 8 | */ |
b1322259 | 9 | |
da0616cd | 10 | #include "e_os.h" |
07016a8a | 11 | #include <stdio.h> |
b39fc560 | 12 | #include "internal/cryptlib.h" |
0c61e299 RL |
13 | #include <openssl/rand.h> |
14 | #include "rand_lcl.h" | |
6decf943 | 15 | #include "internal/rand_int.h" |
8389ec4b | 16 | #include <stdio.h> |
5bc6bcf8 DMSP |
17 | #ifdef OPENSSL_SYS_UNIX |
18 | # include <sys/types.h> | |
19 | # include <unistd.h> | |
20 | # include <sys/time.h> | |
21 | ||
22 | static uint64_t get_time_stamp(void); | |
23 | static uint64_t get_timer_bits(void); | |
24 | ||
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)) | |
27 | ||
28 | /* | |
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 | |
31 | * are available. | |
32 | * | |
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. | |
37 | * | |
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. | |
41 | * | |
42 | * The final condition is: | |
43 | * "have posix timers and either not glibc or glibc without -lrt" | |
44 | * | |
45 | * The nested #if sequences are required to avoid using a parameterised | |
46 | * macro that might be undefined. | |
47 | */ | |
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 | |
54 | # endif | |
55 | # endif | |
56 | # else | |
57 | # define OSSL_POSIX_TIMER_OKAY | |
58 | # endif | |
59 | # endif | |
60 | #endif | |
0c61e299 | 61 | |
c16de9d8 | 62 | #if (defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)) && \ |
8389ec4b | 63 | !defined(OPENSSL_RAND_SEED_NONE) |
c16de9d8 DMSP |
64 | # error "UEFI and VXWorks only support seeding NONE" |
65 | #endif | |
66 | ||
67 | #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) \ | |
68 | || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) \ | |
69 | || defined(OPENSSL_SYS_UEFI)) | |
0f113f3e MC |
70 | |
71 | # if defined(OPENSSL_SYS_VOS) | |
72 | ||
8389ec4b RS |
73 | # ifndef OPENSSL_RAND_SEED_OS |
74 | # error "Unsupported seeding method configured; must be os" | |
75 | # endif | |
76 | ||
77 | # if defined(OPENSSL_SYS_VOS_HPPA) && defined(OPENSSL_SYS_VOS_IA32) | |
78 | # error "Unsupported HP-PA and IA32 at the same time." | |
79 | # endif | |
80 | # if !defined(OPENSSL_SYS_VOS_HPPA) && !defined(OPENSSL_SYS_VOS_IA32) | |
81 | # error "Must have one of HP-PA or IA32" | |
82 | # endif | |
83 | ||
0f113f3e MC |
84 | /* |
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 | |
75e2c877 RS |
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 | |
94 | * platforms. | |
0f113f3e | 95 | * |
c16de9d8 | 96 | * As a precaution, we assume only 2 bits of entropy per byte. |
0f113f3e | 97 | */ |
6decf943 | 98 | size_t rand_pool_acquire_entropy(RAND_POOL *pool) |
cc7399e7 | 99 | { |
0f113f3e | 100 | short int code; |
0f113f3e | 101 | int i, k; |
c16de9d8 | 102 | size_t bytes_needed; |
0f113f3e MC |
103 | struct timespec ts; |
104 | unsigned char v; | |
0f113f3e MC |
105 | # ifdef OPENSSL_SYS_VOS_HPPA |
106 | long duration; | |
107 | extern void s$sleep(long *_duration, short int *_code); | |
108 | # else | |
0f113f3e MC |
109 | long long duration; |
110 | extern void s$sleep2(long long *_duration, short int *_code); | |
8389ec4b | 111 | # endif |
0f113f3e | 112 | |
6decf943 | 113 | bytes_needed = rand_pool_bytes_needed(pool, 2 /*entropy_per_byte*/); |
c16de9d8 DMSP |
114 | |
115 | for (i = 0; i < bytes_needed; i++) { | |
0f113f3e MC |
116 | /* |
117 | * burn some cpu; hope for interrupts, cache collisions, bus | |
118 | * interference, etc. | |
119 | */ | |
120 | for (k = 0; k < 99; k++) | |
121 | ts.tv_nsec = random(); | |
122 | ||
123 | # ifdef OPENSSL_SYS_VOS_HPPA | |
124 | /* sleep for 1/1024 of a second (976 us). */ | |
125 | duration = 1; | |
126 | s$sleep(&duration, &code); | |
127 | # else | |
0f113f3e MC |
128 | /* sleep for 1/65536 of a second (15 us). */ |
129 | duration = 1; | |
130 | s$sleep2(&duration, &code); | |
8389ec4b | 131 | # endif |
0f113f3e | 132 | |
8389ec4b | 133 | /* Get wall clock time, take 8 bits. */ |
0f113f3e | 134 | clock_gettime(CLOCK_REALTIME, &ts); |
8389ec4b | 135 | v = (unsigned char)(ts.tv_nsec & 0xFF); |
6decf943 | 136 | rand_pool_add(pool, arg, &v, sizeof(v) , 2); |
0f113f3e | 137 | } |
6decf943 | 138 | return rand_pool_entropy_available(pool); |
cc7399e7 | 139 | } |
8389ec4b | 140 | |
810ef917 | 141 | # else |
8389ec4b RS |
142 | |
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" | |
0f113f3e MC |
146 | # endif |
147 | ||
8389ec4b RS |
148 | # if defined(OPENSSL_RAND_SEED_DEVRANDOM) && !defined(DEVRANDOM) |
149 | # error "Seeding uses urandom but DEVRANDOM is not configured" | |
150 | # endif | |
0f113f3e | 151 | |
8389ec4b | 152 | # if defined(OPENSSL_RAND_SEED_OS) |
72960279 | 153 | # if !defined(DEVRANDOM) |
8389ec4b | 154 | # error "OS seeding requires DEVRANDOM to be configured" |
0f113f3e | 155 | # endif |
72960279 KR |
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 | |
160 | # endif | |
161 | # endif | |
162 | # endif | |
163 | ||
164 | # ifdef OPENSSL_RAND_SEED_GETRANDOM | |
165 | # include <sys/random.h> | |
8389ec4b | 166 | # endif |
0f113f3e | 167 | |
8389ec4b RS |
168 | # if defined(OPENSSL_RAND_SEED_LIBRANDOM) |
169 | # error "librandom not (yet) supported" | |
170 | # endif | |
0f113f3e | 171 | |
75e2c877 | 172 | /* |
c16de9d8 DMSP |
173 | * Try the various seeding methods in turn, exit when successful. |
174 | * | |
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? | |
179 | * | |
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). | |
75e2c877 | 188 | */ |
6decf943 | 189 | size_t rand_pool_acquire_entropy(RAND_POOL *pool) |
8389ec4b RS |
190 | { |
191 | # ifdef OPENSSL_RAND_SEED_NONE | |
6decf943 | 192 | return rand_pool_entropy_available(pool); |
8389ec4b | 193 | # else |
c16de9d8 DMSP |
194 | size_t bytes_needed; |
195 | size_t entropy_available = 0; | |
196 | unsigned char *buffer; | |
0f113f3e | 197 | |
75e2c877 | 198 | # ifdef OPENSSL_RAND_SEED_GETRANDOM |
6decf943 DMSP |
199 | bytes_needed = rand_pool_bytes_needed(pool, 8 /*entropy_per_byte*/); |
200 | buffer = rand_pool_add_begin(pool, bytes_needed); | |
c16de9d8 DMSP |
201 | if (buffer != NULL) { |
202 | size_t bytes = 0; | |
0f113f3e | 203 | |
c16de9d8 DMSP |
204 | if (getrandom(buffer, bytes_needed, 0) == (int)bytes_needed) |
205 | bytes = bytes_needed; | |
206 | ||
8e2bec9b RL |
207 | rand_pool_add_end(pool, bytes, 8 * bytes); |
208 | entropy_available = rand_pool_entropy_available(pool); | |
75e2c877 | 209 | } |
c16de9d8 DMSP |
210 | if (entropy_available > 0) |
211 | return entropy_available; | |
0f113f3e MC |
212 | # endif |
213 | ||
75e2c877 | 214 | # if defined(OPENSSL_RAND_SEED_LIBRANDOM) |
8389ec4b | 215 | { |
75e2c877 | 216 | /* Not yet implemented. */ |
0f113f3e | 217 | } |
8389ec4b | 218 | # endif |
0f113f3e | 219 | |
8389ec4b | 220 | # ifdef OPENSSL_RAND_SEED_DEVRANDOM |
6decf943 | 221 | bytes_needed = rand_pool_bytes_needed(pool, 8 /*entropy_per_byte*/); |
c16de9d8 | 222 | if (bytes_needed > 0) { |
8389ec4b RS |
223 | static const char *paths[] = { DEVRANDOM, NULL }; |
224 | FILE *fp; | |
225 | int i; | |
0f113f3e | 226 | |
8389ec4b RS |
227 | for (i = 0; paths[i] != NULL; i++) { |
228 | if ((fp = fopen(paths[i], "rb")) == NULL) | |
229 | continue; | |
230 | setbuf(fp, NULL); | |
6decf943 | 231 | buffer = rand_pool_add_begin(pool, bytes_needed); |
c16de9d8 DMSP |
232 | if (buffer != NULL) { |
233 | size_t bytes = 0; | |
234 | if (fread(buffer, 1, bytes_needed, fp) == bytes_needed) | |
235 | bytes = bytes_needed; | |
236 | ||
8e2bec9b RL |
237 | rand_pool_add_end(pool, bytes, 8 * bytes); |
238 | entropy_available = rand_pool_entropy_available(pool); | |
8389ec4b | 239 | } |
75e2c877 | 240 | fclose(fp); |
c16de9d8 DMSP |
241 | if (entropy_available > 0) |
242 | return entropy_available; | |
243 | ||
6decf943 | 244 | bytes_needed = rand_pool_bytes_needed(pool, 8 /*entropy_per_byte*/); |
8389ec4b | 245 | } |
0f113f3e | 246 | } |
8389ec4b | 247 | # endif |
0f113f3e | 248 | |
75e2c877 | 249 | # ifdef OPENSSL_RAND_SEED_RDTSC |
c16de9d8 DMSP |
250 | entropy_available = rand_acquire_entropy_from_tsc(pool); |
251 | if (entropy_available > 0) | |
252 | return entropy_available; | |
75e2c877 RS |
253 | # endif |
254 | ||
255 | # ifdef OPENSSL_RAND_SEED_RDCPU | |
c16de9d8 DMSP |
256 | entropy_available = rand_acquire_entropy_from_cpu(pool); |
257 | if (entropy_available > 0) | |
258 | return entropy_available; | |
75e2c877 RS |
259 | # endif |
260 | ||
261 | # ifdef OPENSSL_RAND_SEED_EGD | |
6decf943 | 262 | bytes_needed = rand_pool_bytes_needed(pool, 8 /*entropy_per_byte*/); |
c16de9d8 | 263 | if (bytes_needed > 0) { |
75e2c877 RS |
264 | static const char *paths[] = { DEVRANDOM_EGD, NULL }; |
265 | int i; | |
0f113f3e | 266 | |
75e2c877 | 267 | for (i = 0; paths[i] != NULL; i++) { |
6decf943 | 268 | buffer = rand_pool_add_begin(pool, bytes_needed); |
c16de9d8 DMSP |
269 | if (buffer != NULL) { |
270 | size_t bytes = 0; | |
271 | int num = RAND_query_egd_bytes(paths[i], | |
272 | buffer, (int)bytes_needed); | |
273 | if (num == (int)bytes_needed) | |
274 | bytes = bytes_needed; | |
275 | ||
8e2bec9b RL |
276 | rand_pool_add_end(pool, bytes, 8 * bytes); |
277 | entropy_available = rand_pool_entropy_available(pool); | |
75e2c877 | 278 | } |
c16de9d8 DMSP |
279 | if (entropy_available > 0) |
280 | return entropy_available; | |
8389ec4b RS |
281 | } |
282 | } | |
283 | # endif | |
0f113f3e | 284 | |
6decf943 | 285 | return rand_pool_entropy_available(pool); |
0f113f3e | 286 | # endif |
0c61e299 | 287 | } |
8389ec4b | 288 | # endif |
5bc6bcf8 DMSP |
289 | #endif |
290 | ||
291 | #ifdef OPENSSL_SYS_UNIX | |
292 | int rand_pool_add_nonce_data(RAND_POOL *pool) | |
293 | { | |
294 | struct { | |
295 | pid_t pid; | |
296 | CRYPTO_THREAD_ID tid; | |
297 | uint64_t time; | |
298 | } data = { 0 }; | |
299 | ||
300 | /* | |
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. | |
304 | */ | |
305 | data.pid = getpid(); | |
306 | data.tid = CRYPTO_THREAD_get_current_id(); | |
307 | data.time = get_time_stamp(); | |
308 | ||
309 | return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0); | |
310 | } | |
311 | ||
312 | int rand_pool_add_additional_data(RAND_POOL *pool) | |
313 | { | |
314 | struct { | |
315 | CRYPTO_THREAD_ID tid; | |
316 | uint64_t time; | |
317 | } data = { 0 }; | |
318 | ||
319 | /* | |
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). | |
323 | */ | |
324 | data.tid = CRYPTO_THREAD_get_current_id(); | |
325 | data.time = get_timer_bits(); | |
326 | ||
327 | return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0); | |
328 | } | |
329 | ||
330 | ||
331 | ||
332 | /* | |
333 | * Get the current time with the highest possible resolution | |
334 | * | |
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 | |
337 | * is synchronized. | |
338 | */ | |
339 | static uint64_t get_time_stamp(void) | |
340 | { | |
341 | # if defined(OSSL_POSIX_TIMER_OKAY) | |
342 | { | |
343 | struct timespec ts; | |
344 | ||
345 | if (clock_gettime(CLOCK_REALTIME, &ts) == 0) | |
346 | return TWO32TO64(ts.tv_sec, ts.tv_nsec); | |
347 | } | |
348 | # endif | |
349 | # if defined(__unix__) \ | |
350 | || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) | |
351 | { | |
352 | struct timeval tv; | |
353 | ||
354 | if (gettimeofday(&tv, NULL) == 0) | |
355 | return TWO32TO64(tv.tv_sec, tv.tv_usec); | |
356 | } | |
357 | # endif | |
358 | return time(NULL); | |
359 | } | |
360 | ||
361 | /* | |
362 | * Get an arbitrary timer value of the highest possible resolution | |
363 | * | |
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 | |
366 | * is acceptable. | |
367 | */ | |
368 | static uint64_t get_timer_bits(void) | |
369 | { | |
370 | uint64_t res = OPENSSL_rdtsc(); | |
371 | ||
372 | if (res != 0) | |
373 | return res; | |
374 | ||
375 | # if defined(__sun) || defined(__hpux) | |
376 | return gethrtime(); | |
377 | # elif defined(_AIX) | |
378 | { | |
379 | timebasestruct_t t; | |
380 | ||
381 | read_wall_time(&t, TIMEBASE_SZ); | |
382 | return TWO32TO64(t.tb_high, t.tb_low); | |
383 | } | |
384 | # elif defined(OSSL_POSIX_TIMER_OKAY) | |
385 | { | |
386 | struct timespec ts; | |
387 | ||
388 | # ifdef CLOCK_BOOTTIME | |
389 | # define CLOCK_TYPE CLOCK_BOOTTIME | |
390 | # elif defined(_POSIX_MONOTONIC_CLOCK) | |
391 | # define CLOCK_TYPE CLOCK_MONOTONIC | |
392 | # else | |
393 | # define CLOCK_TYPE CLOCK_REALTIME | |
394 | # endif | |
395 | ||
396 | if (clock_gettime(CLOCK_TYPE, &ts) == 0) | |
397 | return TWO32TO64(ts.tv_sec, ts.tv_nsec); | |
398 | } | |
399 | # endif | |
400 | # if defined(__unix__) \ | |
401 | || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) | |
402 | { | |
403 | struct timeval tv; | |
0c61e299 | 404 | |
5bc6bcf8 DMSP |
405 | if (gettimeofday(&tv, NULL) == 0) |
406 | return TWO32TO64(tv.tv_sec, tv.tv_usec); | |
407 | } | |
408 | # endif | |
409 | return time(NULL); | |
410 | } | |
57d8ff79 | 411 | #endif |