]>
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 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
15 #include "internal/cryptlib.h"
16 #include <openssl/rand.h>
18 #include "internal/rand_int.h"
20 #include "internal/dso.h"
22 # include <asm/unistd.h>
24 #if defined(__FreeBSD__) && !defined(OPENSSL_SYS_UEFI)
25 # include <sys/types.h>
26 # include <sys/sysctl.h>
27 # include <sys/param.h>
29 #if defined(__OpenBSD__) || defined(__NetBSD__)
30 # include <sys/param.h>
33 #if (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS)) \
35 # include <sys/types.h>
36 # include <sys/stat.h>
39 # include <sys/time.h>
41 static uint64_t get_time_stamp(void);
42 static uint64_t get_timer_bits(void);
44 /* Macro to convert two thirty two bit values into a sixty four bit one */
45 # define TWO32TO64(a, b) ((((uint64_t)(a)) << 32) + (b))
48 * Check for the existence and support of POSIX timers. The standard
49 * says that the _POSIX_TIMERS macro will have a positive value if they
52 * However, we want an additional constraint: that the timer support does
53 * not require an extra library dependency. Early versions of glibc
54 * require -lrt to be specified on the link line to access the timers,
55 * so this needs to be checked for.
57 * It is worse because some libraries define __GLIBC__ but don't
58 * support the version testing macro (e.g. uClibc). This means
59 * an extra check is needed.
61 * The final condition is:
62 * "have posix timers and either not glibc or glibc without -lrt"
64 * The nested #if sequences are required to avoid using a parameterised
65 * macro that might be undefined.
67 # undef OSSL_POSIX_TIMER_OKAY
68 # if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
69 # if defined(__GLIBC__)
70 # if defined(__GLIBC_PREREQ)
71 # if __GLIBC_PREREQ(2, 17)
72 # define OSSL_POSIX_TIMER_OKAY
76 # define OSSL_POSIX_TIMER_OKAY
79 #endif /* defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__) */
81 #if defined(OPENSSL_RAND_SEED_NONE)
82 /* none means none. this simplifies the following logic */
83 # undef OPENSSL_RAND_SEED_OS
84 # undef OPENSSL_RAND_SEED_GETRANDOM
85 # undef OPENSSL_RAND_SEED_LIBRANDOM
86 # undef OPENSSL_RAND_SEED_DEVRANDOM
87 # undef OPENSSL_RAND_SEED_RDTSC
88 # undef OPENSSL_RAND_SEED_RDCPU
89 # undef OPENSSL_RAND_SEED_EGD
92 #if defined(OPENSSL_SYS_UEFI) && !defined(OPENSSL_RAND_SEED_NONE)
93 # error "UEFI only supports seeding NONE"
96 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) \
97 || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) \
98 || defined(OPENSSL_SYS_UEFI))
100 # if defined(OPENSSL_SYS_VOS)
102 # ifndef OPENSSL_RAND_SEED_OS
103 # error "Unsupported seeding method configured; must be os"
106 # if defined(OPENSSL_SYS_VOS_HPPA) && defined(OPENSSL_SYS_VOS_IA32)
107 # error "Unsupported HP-PA and IA32 at the same time."
109 # if !defined(OPENSSL_SYS_VOS_HPPA) && !defined(OPENSSL_SYS_VOS_IA32)
110 # error "Must have one of HP-PA or IA32"
114 * The following algorithm repeatedly samples the real-time clock (RTC) to
115 * generate a sequence of unpredictable data. The algorithm relies upon the
116 * uneven execution speed of the code (due to factors such as cache misses,
117 * interrupts, bus activity, and scheduling) and upon the rather large
118 * relative difference between the speed of the clock and the rate at which
119 * it can be read. If it is ported to an environment where execution speed
120 * is more constant or where the RTC ticks at a much slower rate, or the
121 * clock can be read with fewer instructions, it is likely that the results
122 * would be far more predictable. This should only be used for legacy
125 * As a precaution, we assume only 2 bits of entropy per byte.
127 size_t rand_pool_acquire_entropy(RAND_POOL
*pool
)
134 # ifdef OPENSSL_SYS_VOS_HPPA
136 extern void s$
sleep(long *_duration
, short int *_code
);
139 extern void s$
sleep2(long long *_duration
, short int *_code
);
142 bytes_needed
= rand_pool_bytes_needed(pool
, 4 /*entropy_factor*/);
144 for (i
= 0; i
< bytes_needed
; i
++) {
146 * burn some cpu; hope for interrupts, cache collisions, bus
149 for (k
= 0; k
< 99; k
++)
150 ts
.tv_nsec
= random();
152 # ifdef OPENSSL_SYS_VOS_HPPA
153 /* sleep for 1/1024 of a second (976 us). */
155 s$
sleep(&duration
, &code
);
157 /* sleep for 1/65536 of a second (15 us). */
159 s$
sleep2(&duration
, &code
);
162 /* Get wall clock time, take 8 bits. */
163 clock_gettime(CLOCK_REALTIME
, &ts
);
164 v
= (unsigned char)(ts
.tv_nsec
& 0xFF);
165 rand_pool_add(pool
, arg
, &v
, sizeof(v
) , 2);
167 return rand_pool_entropy_available(pool
);
170 void rand_pool_cleanup(void)
174 void rand_pool_keep_random_devices_open(int keep
)
180 # if defined(OPENSSL_RAND_SEED_EGD) && \
181 (defined(OPENSSL_NO_EGD) || !defined(DEVRANDOM_EGD))
182 # error "Seeding uses EGD but EGD is turned off or no device given"
185 # if defined(OPENSSL_RAND_SEED_DEVRANDOM) && !defined(DEVRANDOM)
186 # error "Seeding uses urandom but DEVRANDOM is not configured"
189 # if defined(OPENSSL_RAND_SEED_OS)
190 # if !defined(DEVRANDOM)
191 # error "OS seeding requires DEVRANDOM to be configured"
193 # define OPENSSL_RAND_SEED_GETRANDOM
194 # define OPENSSL_RAND_SEED_DEVRANDOM
197 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
198 # error "librandom not (yet) supported"
201 # if (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND)
203 * sysctl_random(): Use sysctl() to read a random number from the kernel
204 * Returns the number of bytes returned in buf on success, -1 on failure.
206 static ssize_t
sysctl_random(char *buf
, size_t buflen
)
213 * Note: sign conversion between size_t and ssize_t is safe even
214 * without a range check, see comment in syscall_random()
218 * On FreeBSD old implementations returned longs, newer versions support
219 * variable sizes up to 256 byte. The code below would not work properly
220 * when the sysctl returns long and we want to request something not a
221 * multiple of longs, which should never be the case.
223 if (!ossl_assert(buflen
% sizeof(long) == 0)) {
229 * On NetBSD before 4.0 KERN_ARND was an alias for KERN_URND, and only
230 * filled in an int, leaving the rest uninitialized. Since NetBSD 4.0
231 * it returns a variable number of bytes with the current version supporting
233 * Just return an error on older NetBSD versions.
235 #if defined(__NetBSD__) && __NetBSD_Version__ < 400000000
245 if (sysctl(mib
, 2, buf
, &len
, NULL
, 0) == -1)
246 return done
> 0 ? done
: -1;
250 } while (buflen
> 0);
256 # if defined(OPENSSL_RAND_SEED_GETRANDOM)
258 * syscall_random(): Try to get random data using a system call
259 * returns the number of bytes returned in buf, or < 0 on error.
261 static ssize_t
syscall_random(void *buf
, size_t buflen
)
264 * Note: 'buflen' equals the size of the buffer which is used by the
265 * get_entropy() callback of the RAND_DRBG. It is roughly bounded by
267 * 2 * RAND_POOL_FACTOR * (RAND_DRBG_STRENGTH / 8) = 2^14
269 * which is way below the OSSL_SSIZE_MAX limit. Therefore sign conversion
270 * between size_t and ssize_t is safe even without a range check.
274 * Do runtime detection to find getentropy().
276 * Known OSs that should support this:
277 * - Darwin since 16 (OSX 10.12, IOS 10.0).
278 * - Solaris since 11.3
279 * - OpenBSD since 5.6
280 * - Linux since 3.17 with glibc 2.25
281 * - FreeBSD since 12.0 (1200061)
283 # if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__) && !defined(__hpux)
284 extern int getentropy(void *buffer
, size_t length
) __attribute__((weak
));
286 if (getentropy
!= NULL
)
287 return getentropy(buf
, buflen
) == 0 ? (ssize_t
)buflen
: -1;
288 # elif !defined(FIPS_MODE)
291 int (*f
)(void *buffer
, size_t length
);
295 * We could cache the result of the lookup, but we normally don't
296 * call this function often.
299 p_getentropy
.p
= DSO_global_lookup("getentropy");
301 if (p_getentropy
.p
!= NULL
)
302 return p_getentropy
.f(buf
, buflen
) == 0 ? (ssize_t
)buflen
: -1;
305 /* Linux supports this since version 3.17 */
306 # if defined(__linux) && defined(__NR_getrandom)
307 return syscall(__NR_getrandom
, buf
, buflen
, 0);
308 # elif (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND)
309 return sysctl_random(buf
, buflen
);
315 # endif /* defined(OPENSSL_RAND_SEED_GETRANDOM) */
317 # if defined(OPENSSL_RAND_SEED_DEVRANDOM)
318 static const char *random_device_paths
[] = { DEVRANDOM
};
319 static struct random_device
{
325 } random_devices
[OSSL_NELEM(random_device_paths
)];
326 static int keep_random_devices_open
= 1;
329 * Verify that the file descriptor associated with the random source is
330 * still valid. The rationale for doing this is the fact that it is not
331 * uncommon for daemons to close all open file handles when daemonizing.
332 * So the handle might have been closed or even reused for opening
335 static int check_random_device(struct random_device
* rd
)
340 && fstat(rd
->fd
, &st
) != -1
341 && rd
->dev
== st
.st_dev
342 && rd
->ino
== st
.st_ino
343 && ((rd
->mode
^ st
.st_mode
) & ~(S_IRWXU
| S_IRWXG
| S_IRWXO
)) == 0
344 && rd
->rdev
== st
.st_rdev
;
348 * Open a random device if required and return its file descriptor or -1 on error
350 static int get_random_device(size_t n
)
353 struct random_device
* rd
= &random_devices
[n
];
355 /* reuse existing file descriptor if it is (still) valid */
356 if (check_random_device(rd
))
359 /* open the random device ... */
360 if ((rd
->fd
= open(random_device_paths
[n
], O_RDONLY
)) == -1)
363 /* ... and cache its relevant stat(2) data */
364 if (fstat(rd
->fd
, &st
) != -1) {
367 rd
->mode
= st
.st_mode
;
368 rd
->rdev
= st
.st_rdev
;
378 * Close a random device making sure it is a random device
380 static void close_random_device(size_t n
)
382 struct random_device
* rd
= &random_devices
[n
];
384 if (check_random_device(rd
))
389 int rand_pool_init(void)
393 for (i
= 0; i
< OSSL_NELEM(random_devices
); i
++)
394 random_devices
[i
].fd
= -1;
399 void rand_pool_cleanup(void)
403 for (i
= 0; i
< OSSL_NELEM(random_devices
); i
++)
404 close_random_device(i
);
407 void rand_pool_keep_random_devices_open(int keep
)
412 keep_random_devices_open
= keep
;
415 # else /* !defined(OPENSSL_RAND_SEED_DEVRANDOM) */
417 int rand_pool_init(void)
422 void rand_pool_cleanup(void)
426 void rand_pool_keep_random_devices_open(int keep
)
430 # endif /* defined(OPENSSL_RAND_SEED_DEVRANDOM) */
433 * Try the various seeding methods in turn, exit when successful.
435 * TODO(DRBG): If more than one entropy source is available, is it
436 * preferable to stop as soon as enough entropy has been collected
437 * (as favored by @rsalz) or should one rather be defensive and add
438 * more entropy than requested and/or from different sources?
440 * Currently, the user can select multiple entropy sources in the
441 * configure step, yet in practice only the first available source
442 * will be used. A more flexible solution has been requested, but
443 * currently it is not clear how this can be achieved without
444 * overengineering the problem. There are many parameters which
445 * could be taken into account when selecting the order and amount
446 * of input from the different entropy sources (trust, quality,
447 * possibility of blocking).
449 size_t rand_pool_acquire_entropy(RAND_POOL
*pool
)
451 # if defined(OPENSSL_RAND_SEED_NONE)
452 return rand_pool_entropy_available(pool
);
455 size_t entropy_available
= 0;
456 unsigned char *buffer
;
458 # if defined(OPENSSL_RAND_SEED_GETRANDOM)
461 /* Maximum allowed number of consecutive unsuccessful attempts */
464 bytes_needed
= rand_pool_bytes_needed(pool
, 1 /*entropy_factor*/);
465 while (bytes_needed
!= 0 && attempts
-- > 0) {
466 buffer
= rand_pool_add_begin(pool
, bytes_needed
);
467 bytes
= syscall_random(buffer
, bytes_needed
);
469 rand_pool_add_end(pool
, bytes
, 8 * bytes
);
470 bytes_needed
-= bytes
;
471 attempts
= 3; /* reset counter after successful attempt */
472 } else if (bytes
< 0 && errno
!= EINTR
) {
477 entropy_available
= rand_pool_entropy_available(pool
);
478 if (entropy_available
> 0)
479 return entropy_available
;
482 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
484 /* Not yet implemented. */
488 # if defined(OPENSSL_RAND_SEED_DEVRANDOM)
489 bytes_needed
= rand_pool_bytes_needed(pool
, 1 /*entropy_factor*/);
493 for (i
= 0; bytes_needed
> 0 && i
< OSSL_NELEM(random_device_paths
); i
++) {
495 /* Maximum allowed number of consecutive unsuccessful attempts */
497 const int fd
= get_random_device(i
);
502 while (bytes_needed
!= 0 && attempts
-- > 0) {
503 buffer
= rand_pool_add_begin(pool
, bytes_needed
);
504 bytes
= read(fd
, buffer
, bytes_needed
);
507 rand_pool_add_end(pool
, bytes
, 8 * bytes
);
508 bytes_needed
-= bytes
;
509 attempts
= 3; /* reset counter after successful attempt */
510 } else if (bytes
< 0 && errno
!= EINTR
) {
514 if (bytes
< 0 || !keep_random_devices_open
)
515 close_random_device(i
);
517 bytes_needed
= rand_pool_bytes_needed(pool
, 1 /*entropy_factor*/);
519 entropy_available
= rand_pool_entropy_available(pool
);
520 if (entropy_available
> 0)
521 return entropy_available
;
525 # if defined(OPENSSL_RAND_SEED_RDTSC)
526 entropy_available
= rand_acquire_entropy_from_tsc(pool
);
527 if (entropy_available
> 0)
528 return entropy_available
;
531 # if defined(OPENSSL_RAND_SEED_RDCPU)
532 entropy_available
= rand_acquire_entropy_from_cpu(pool
);
533 if (entropy_available
> 0)
534 return entropy_available
;
537 # if defined(OPENSSL_RAND_SEED_EGD)
538 bytes_needed
= rand_pool_bytes_needed(pool
, 1 /*entropy_factor*/);
539 if (bytes_needed
> 0) {
540 static const char *paths
[] = { DEVRANDOM_EGD
, NULL
};
543 for (i
= 0; paths
[i
] != NULL
; i
++) {
544 buffer
= rand_pool_add_begin(pool
, bytes_needed
);
545 if (buffer
!= NULL
) {
547 int num
= RAND_query_egd_bytes(paths
[i
],
548 buffer
, (int)bytes_needed
);
549 if (num
== (int)bytes_needed
)
550 bytes
= bytes_needed
;
552 rand_pool_add_end(pool
, bytes
, 8 * bytes
);
553 entropy_available
= rand_pool_entropy_available(pool
);
555 if (entropy_available
> 0)
556 return entropy_available
;
561 return rand_pool_entropy_available(pool
);
567 #if (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS)) \
568 || defined(__DJGPP__)
569 int rand_pool_add_nonce_data(RAND_POOL
*pool
)
573 CRYPTO_THREAD_ID tid
;
577 /* Erase the entire structure including any padding */
578 memset(&data
, 0, sizeof(data
));
581 * Add process id, thread id, and a high resolution timestamp to
582 * ensure that the nonce is unique with high probability for
583 * different process instances.
586 data
.tid
= CRYPTO_THREAD_get_current_id();
587 data
.time
= get_time_stamp();
589 return rand_pool_add(pool
, (unsigned char *)&data
, sizeof(data
), 0);
592 int rand_pool_add_additional_data(RAND_POOL
*pool
)
595 CRYPTO_THREAD_ID tid
;
599 /* Erase the entire structure including any padding */
600 memset(&data
, 0, sizeof(data
));
603 * Add some noise from the thread id and a high resolution timer.
604 * The thread id adds a little randomness if the drbg is accessed
605 * concurrently (which is the case for the <master> drbg).
607 data
.tid
= CRYPTO_THREAD_get_current_id();
608 data
.time
= get_timer_bits();
610 return rand_pool_add(pool
, (unsigned char *)&data
, sizeof(data
), 0);
615 * Get the current time with the highest possible resolution
617 * The time stamp is added to the nonce, so it is optimized for not repeating.
618 * The current time is ideal for this purpose, provided the computer's clock
621 static uint64_t get_time_stamp(void)
623 # if defined(OSSL_POSIX_TIMER_OKAY)
627 if (clock_gettime(CLOCK_REALTIME
, &ts
) == 0)
628 return TWO32TO64(ts
.tv_sec
, ts
.tv_nsec
);
631 # if defined(__unix__) \
632 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
636 if (gettimeofday(&tv
, NULL
) == 0)
637 return TWO32TO64(tv
.tv_sec
, tv
.tv_usec
);
644 * Get an arbitrary timer value of the highest possible resolution
646 * The timer value is added as random noise to the additional data,
647 * which is not considered a trusted entropy sourec, so any result
650 static uint64_t get_timer_bits(void)
652 uint64_t res
= OPENSSL_rdtsc();
657 # if defined(__sun) || defined(__hpux)
663 read_wall_time(&t
, TIMEBASE_SZ
);
664 return TWO32TO64(t
.tb_high
, t
.tb_low
);
666 # elif defined(OSSL_POSIX_TIMER_OKAY)
670 # ifdef CLOCK_BOOTTIME
671 # define CLOCK_TYPE CLOCK_BOOTTIME
672 # elif defined(_POSIX_MONOTONIC_CLOCK)
673 # define CLOCK_TYPE CLOCK_MONOTONIC
675 # define CLOCK_TYPE CLOCK_REALTIME
678 if (clock_gettime(CLOCK_TYPE
, &ts
) == 0)
679 return TWO32TO64(ts
.tv_sec
, ts
.tv_nsec
);
682 # if defined(__unix__) \
683 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
687 if (gettimeofday(&tv
, NULL
) == 0)
688 return TWO32TO64(tv
.tv_sec
, tv
.tv_usec
);
693 #endif /* defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__) */