2 * Copyright 1995-2016 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
14 #include "internal/cryptlib.h"
15 #include <openssl/rand.h>
18 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI))
20 # include <sys/types.h>
21 # include <sys/time.h>
22 # include <sys/times.h>
23 # include <sys/stat.h>
27 # if defined(OPENSSL_SYS_LINUX) /* should actually be available virtually
33 # define FD_SETSIZE (8*sizeof(fd_set))
36 # if defined(OPENSSL_SYS_VOS)
39 * The following algorithm repeatedly samples the real-time clock (RTC) to
40 * generate a sequence of unpredictable data. The algorithm relies upon the
41 * uneven execution speed of the code (due to factors such as cache misses,
42 * interrupts, bus activity, and scheduling) and upon the rather large
43 * relative difference between the speed of the clock and the rate at which
46 * If this code is ported to an environment where execution speed is more
47 * constant or where the RTC ticks at a much slower rate, or the clock can be
48 * read with fewer instructions, it is likely that the results would be far
51 * As a precaution, we generate 4 times the minimum required amount of seed
65 # ifdef OPENSSL_SYS_VOS_HPPA
67 extern void s$sleep(long *_duration, short int *_code);
69 # ifdef OPENSSL_SYS_VOS_IA32
71 extern void s$sleep2(long long *_duration, short int *_code);
73 # error "Unsupported Platform."
74 # endif /* OPENSSL_SYS_VOS_IA32 */
75 # endif /* OPENSSL_SYS_VOS_HPPA */
78 * Seed with the gid, pid, and uid, to ensure *some* variation between
79 * different processes.
83 RAND_add(&curr_gid, sizeof curr_gid, 1);
87 RAND_add(&curr_pid, sizeof curr_pid, 1);
91 RAND_add(&curr_uid, sizeof curr_uid, 1);
94 for (i = 0; i < (ENTROPY_NEEDED * 4); i++) {
96 * burn some cpu; hope for interrupts, cache collisions, bus
99 for (k = 0; k < 99; k++)
100 ts.tv_nsec = random();
102 # ifdef OPENSSL_SYS_VOS_HPPA
103 /* sleep for 1/1024 of a second (976 us). */
105 s$sleep(&duration, &code);
107 # ifdef OPENSSL_SYS_VOS_IA32
108 /* sleep for 1/65536 of a second (15 us). */
110 s$sleep2(&duration, &code);
111 # endif /* OPENSSL_SYS_VOS_IA32 */
112 # endif /* OPENSSL_SYS_VOS_HPPA */
114 /* get wall clock time. */
115 clock_gettime(CLOCK_REALTIME, &ts);
118 v = (unsigned char)(ts.tv_nsec % 256);
119 RAND_add(&v, sizeof v, 1);
124 # elif defined __OpenBSD__
127 u_int32_t rnd = 0, i;
128 unsigned char buf[ENTROPY_NEEDED];
130 for (i = 0; i < sizeof(buf); i++) {
136 RAND_add(buf, sizeof(buf), ENTROPY_NEEDED);
137 OPENSSL_cleanse(buf, sizeof(buf));
141 # else /* !defined(__OpenBSD__) */
145 pid_t curr_pid = getpid();
146 # if defined(DEVRANDOM) || (!defined(OPENSS_NO_EGD) && defined(DEVRANDOM_EGD))
147 unsigned char tmpbuf[ENTROPY_NEEDED];
151 static const char *randomfiles[] = { DEVRANDOM };
152 struct stat randomstats[OSSL_NELEM(randomfiles)];
156 # if !defined(OPENSSL_NO_EGD) && defined(DEVRANDOM_EGD)
157 static const char *egdsockets[] = { DEVRANDOM_EGD, NULL };
158 const char **egdsocket = NULL;
162 memset(randomstats, 0, sizeof(randomstats));
164 * Use a random entropy pool device. Linux, FreeBSD and OpenBSD have
165 * this. Use /dev/urandom if you can as /dev/random may block if it runs
166 * out of random entries.
169 for (i = 0; (i < OSSL_NELEM(randomfiles)) && (n < ENTROPY_NEEDED); i++) {
170 if ((fd = open(randomfiles[i], O_RDONLY
177 # ifdef O_NOCTTY /* If it happens to be a TTY (god forbid), do
178 * not make it our controlling tty */
182 int usec = 10 * 1000; /* spend 10ms on each file */
185 struct stat *st = &randomstats[i];
188 * Avoid using same input... Used to be O_NOFOLLOW above, but
189 * it's not universally appropriate...
191 if (fstat(fd, st) != 0) {
195 for (j = 0; j < i; j++) {
196 if (randomstats[j].st_ino == st->st_ino &&
197 randomstats[j].st_dev == st->st_dev)
208 # if defined(OPENSSL_SYS_LINUX)
213 pset.events = POLLIN;
216 if (poll(&pset, 1, usec / 1000) < 0)
219 try_read = (pset.revents & POLLIN) != 0;
229 if (FD_SETSIZE > 0 && (unsigned)fd >= FD_SETSIZE) {
231 * can't use select, so just try to read once anyway
238 if (select(fd + 1, &fset, NULL, NULL, &t) >= 0) {
240 if (FD_ISSET(fd, &fset))
248 r = read(fd, (unsigned char *)tmpbuf + n,
256 * Some Unixen will update t in select(), some won't. For
257 * those who won't, or if we didn't use select() in the first
258 * place, give up here, otherwise, we will do this once again
259 * for the remaining time.
261 if (usec == 10 * 1000)
265 (errno == EINTR || errno == EAGAIN)) && usec != 0
266 && n < ENTROPY_NEEDED);
271 # endif /* defined(DEVRANDOM) */
273 # if !defined(OPENSSL_NO_EGD) && defined(DEVRANDOM_EGD)
275 * Use an EGD socket to read entropy from an EGD or PRNGD entropy
279 for (egdsocket = egdsockets; *egdsocket && n < ENTROPY_NEEDED;
283 r = RAND_query_egd_bytes(*egdsocket, (unsigned char *)tmpbuf + n,
288 # endif /* defined(DEVRANDOM_EGD) */
290 # if defined(DEVRANDOM) || (!defined(OPENSSL_NO_EGD) && defined(DEVRANDOM_EGD))
292 RAND_add(tmpbuf, sizeof tmpbuf, (double)n);
293 OPENSSL_cleanse(tmpbuf, n);
297 /* put in some default random data, we need more than just this */
299 RAND_add(&l, sizeof(l), 0.0);
301 RAND_add(&l, sizeof(l), 0.0);
304 RAND_add(&l, sizeof(l), 0.0);
306 # if defined(DEVRANDOM) || (!defined(OPENSSL_NO_EGD) && defined(DEVRANDOM_EGD))
313 # endif /* defined(__OpenBSD__) */
314 #endif /* !(defined(OPENSSL_SYS_WINDOWS) ||
315 * defined(OPENSSL_SYS_WIN32) ||
316 * defined(OPENSSL_SYS_VMS) ||
317 * defined(OPENSSL_SYS_VXWORKS) */
319 #if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)