2 * Copyright 1995-2017 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>
17 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI))
19 # if (defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)) && \
20 !defined(OPENSSL_RAND_SEED_NONE)
21 # error "UEFI and VXWorks only support seeding NONE"
24 # if defined(OPENSSL_SYS_VOS)
26 # ifndef OPENSSL_RAND_SEED_OS
27 # error "Unsupported seeding method configured; must be os"
30 # if defined(OPENSSL_SYS_VOS_HPPA) && defined(OPENSSL_SYS_VOS_IA32)
31 # error "Unsupported HP-PA and IA32 at the same time."
33 # if !defined(OPENSSL_SYS_VOS_HPPA) && !defined(OPENSSL_SYS_VOS_IA32)
34 # error "Must have one of HP-PA or IA32"
38 * The following algorithm repeatedly samples the real-time clock (RTC) to
39 * generate a sequence of unpredictable data. The algorithm relies upon the
40 * uneven execution speed of the code (due to factors such as cache misses,
41 * interrupts, bus activity, and scheduling) and upon the rather large
42 * relative difference between the speed of the clock and the rate at which
43 * it can be read. If it is ported to an environment where execution speed
44 * is more constant or where the RTC ticks at a much slower rate, or the
45 * clock can be read with fewer instructions, it is likely that the results
46 * would be far more predictable. This should only be used for legacy
49 * As a precaution, we generate four times the required amount of seed
52 int RAND_poll_ex(RAND_poll_cb rand_add, void *arg)
61 # ifdef OPENSSL_SYS_VOS_HPPA
63 extern void s$sleep(long *_duration, short int *_code);
66 extern void s$sleep2(long long *_duration, short int *_code);
70 * Seed with the gid, pid, and uid, to ensure *some* variation between
71 * different processes.
74 rand_add(arg, &curr_gid, sizeof curr_gid, 0);
76 rand_add(arg, &curr_pid, sizeof curr_pid, 0);
78 rand_add(arg, &curr_uid, sizeof curr_uid, 0);
80 for (i = 0; i < (RANDOMNESS_NEEDED * 4); i++) {
82 * burn some cpu; hope for interrupts, cache collisions, bus
85 for (k = 0; k < 99; k++)
86 ts.tv_nsec = random();
88 # ifdef OPENSSL_SYS_VOS_HPPA
89 /* sleep for 1/1024 of a second (976 us). */
91 s$sleep(&duration, &code);
93 /* sleep for 1/65536 of a second (15 us). */
95 s$sleep2(&duration, &code);
98 /* Get wall clock time, take 8 bits. */
99 clock_gettime(CLOCK_REALTIME, &ts);
100 v = (unsigned char)(ts.tv_nsec & 0xFF);
101 rand_add(arg, &v, sizeof v, 1);
108 # if defined(OPENSSL_RAND_SEED_EGD) && \
109 (defined(OPENSSL_NO_EGD) || !defined(DEVRANDOM_EGD))
110 # error "Seeding uses EGD but EGD is turned off or no device given"
113 # if defined(OPENSSL_RAND_SEED_DEVRANDOM) && !defined(DEVRANDOM)
114 # error "Seeding uses urandom but DEVRANDOM is not configured"
117 # if defined(OPENSSL_RAND_SEED_OS)
118 # if defined(DEVRANDOM)
119 # define OPENSSL_RAND_SEED_DEVRANDOM
121 # error "OS seeding requires DEVRANDOM to be configured"
125 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
126 # error "librandom not (yet) supported"
130 * Try the various seeding methods in turn, exit when succesful.
132 int RAND_poll_ex(RAND_poll_cb rand_add, void *arg)
134 # ifdef OPENSSL_RAND_SEED_NONE
138 char temp[RANDOMNESS_NEEDED];
139 # define TEMPSIZE (int)sizeof(temp)
141 # ifdef OPENSSL_RAND_SEED_GETRANDOM
143 int i = getrandom(temp, TEMPSIZE, 0);
146 rand_add(arg, temp, i, i);
153 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
155 /* Not yet implemented. */
159 # ifdef OPENSSL_RAND_SEED_DEVRANDOM
161 static const char *paths[] = { DEVRANDOM, NULL };
165 for (i = 0; paths[i] != NULL; i++) {
166 if ((fp = fopen(paths[i], "rb")) == NULL)
169 if (fread(temp, 1, TEMPSIZE, fp) == TEMPSIZE) {
170 rand_add(arg, temp, TEMPSIZE, TEMPSIZE);
179 # ifdef OPENSSL_RAND_SEED_RDTSC
180 rand_read_tsc(rand_add, arg);
183 # ifdef OPENSSL_RAND_SEED_RDCPU
184 if (rand_read_cpu(rand_add, arg))
188 # ifdef OPENSSL_RAND_SEED_EGD
190 static const char *paths[] = { DEVRANDOM_EGD, NULL };
193 for (i = 0; paths[i] != NULL; i++) {
194 if (RAND_query_egd_bytes(paths[i], temp, TEMPSIZE) == TEMPSIZE) {
195 rand_add(arg, temp, TEMPSIZE, TEMPSIZE);
205 OPENSSL_cleanse(temp, TEMPSIZE);