]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/rand/rand_unix.c
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_VXWORKS) || defined(OPENSSL_SYS_UEFI)) && \
18 !defined(OPENSSL_RAND_SEED_NONE)
19 # error "UEFI and VXWorks only support seeding NONE"
22 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) \
23 || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) \
24 || defined(OPENSSL_SYS_UEFI))
26 # if defined(OPENSSL_SYS_VOS)
28 # ifndef OPENSSL_RAND_SEED_OS
29 # error "Unsupported seeding method configured; must be os"
32 # if defined(OPENSSL_SYS_VOS_HPPA) && defined(OPENSSL_SYS_VOS_IA32)
33 # error "Unsupported HP-PA and IA32 at the same time."
35 # if !defined(OPENSSL_SYS_VOS_HPPA) && !defined(OPENSSL_SYS_VOS_IA32)
36 # error "Must have one of HP-PA or IA32"
40 * The following algorithm repeatedly samples the real-time clock (RTC) to
41 * generate a sequence of unpredictable data. The algorithm relies upon the
42 * uneven execution speed of the code (due to factors such as cache misses,
43 * interrupts, bus activity, and scheduling) and upon the rather large
44 * relative difference between the speed of the clock and the rate at which
45 * it can be read. If it is ported to an environment where execution speed
46 * is more constant or where the RTC ticks at a much slower rate, or the
47 * clock can be read with fewer instructions, it is likely that the results
48 * would be far more predictable. This should only be used for legacy
51 * As a precaution, we assume only 2 bits of entropy per byte.
53 size_t RAND_POOL_acquire_entropy(RAND_POOL
*pool
)
63 # ifdef OPENSSL_SYS_VOS_HPPA
65 extern void s$
sleep(long *_duration
, short int *_code
);
68 extern void s$
sleep2(long long *_duration
, short int *_code
);
72 * Seed with the gid, pid, and uid, to ensure *some* variation between
73 * different processes.
76 RAND_POOL_add(pool
, &curr_gid
, sizeof(curr_gid
), 0);
78 RAND_POOL_add(pool
, &curr_pid
, sizeof(curr_pid
), 0);
80 RAND_POOL_add(pool
, &curr_uid
, sizeof(curr_uid
), 0);
82 bytes_needed
= RAND_POOL_bytes_needed(pool
, 2 /*entropy_per_byte*/);
84 for (i
= 0; i
< bytes_needed
; i
++) {
86 * burn some cpu; hope for interrupts, cache collisions, bus
89 for (k
= 0; k
< 99; k
++)
90 ts
.tv_nsec
= random();
92 # ifdef OPENSSL_SYS_VOS_HPPA
93 /* sleep for 1/1024 of a second (976 us). */
95 s$
sleep(&duration
, &code
);
97 /* sleep for 1/65536 of a second (15 us). */
99 s$
sleep2(&duration
, &code
);
102 /* Get wall clock time, take 8 bits. */
103 clock_gettime(CLOCK_REALTIME
, &ts
);
104 v
= (unsigned char)(ts
.tv_nsec
& 0xFF);
105 RAND_POOL_add(pool
, arg
, &v
, sizeof(v
) , 2);
107 return RAND_POOL_entropy_available(pool
);
112 # if defined(OPENSSL_RAND_SEED_EGD) && \
113 (defined(OPENSSL_NO_EGD) || !defined(DEVRANDOM_EGD))
114 # error "Seeding uses EGD but EGD is turned off or no device given"
117 # if defined(OPENSSL_RAND_SEED_DEVRANDOM) && !defined(DEVRANDOM)
118 # error "Seeding uses urandom but DEVRANDOM is not configured"
121 # if defined(OPENSSL_RAND_SEED_OS)
122 # if !defined(DEVRANDOM)
123 # error "OS seeding requires DEVRANDOM to be configured"
125 # define OPENSSL_RAND_SEED_DEVRANDOM
126 # if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
127 # if __GLIBC_PREREQ(2, 25)
128 # define OPENSSL_RAND_SEED_GETRANDOM
133 # ifdef OPENSSL_RAND_SEED_GETRANDOM
134 # include <sys/random.h>
137 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
138 # error "librandom not (yet) supported"
142 * Try the various seeding methods in turn, exit when successful.
144 * TODO(DRBG): If more than one entropy source is available, is it
145 * preferable to stop as soon as enough entropy has been collected
146 * (as favored by @rsalz) or should one rather be defensive and add
147 * more entropy than requested and/or from different sources?
149 * Currently, the user can select multiple entropy sources in the
150 * configure step, yet in practice only the first available source
151 * will be used. A more flexible solution has been requested, but
152 * currently it is not clear how this can be achieved without
153 * overengineering the problem. There are many parameters which
154 * could be taken into account when selecting the order and amount
155 * of input from the different entropy sources (trust, quality,
156 * possibility of blocking).
158 size_t RAND_POOL_acquire_entropy(RAND_POOL
*pool
)
160 # ifdef OPENSSL_RAND_SEED_NONE
161 return RAND_POOL_entropy_available(pool
);
164 size_t entropy_available
= 0;
165 unsigned char *buffer
;
167 # ifdef OPENSSL_RAND_SEED_GETRANDOM
168 bytes_needed
= RAND_POOL_bytes_needed(pool
, 8 /*entropy_per_byte*/);
169 buffer
= RAND_POOL_add_begin(pool
, bytes_needed
);
170 if (buffer
!= NULL
) {
173 if (getrandom(buffer
, bytes_needed
, 0) == (int)bytes_needed
)
174 bytes
= bytes_needed
;
176 entropy_available
= RAND_POOL_add_end(pool
, bytes
, 8 * bytes
);
178 if (entropy_available
> 0)
179 return entropy_available
;
182 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
184 /* Not yet implemented. */
188 # ifdef OPENSSL_RAND_SEED_DEVRANDOM
189 bytes_needed
= RAND_POOL_bytes_needed(pool
, 8 /*entropy_per_byte*/);
190 if (bytes_needed
> 0) {
191 static const char *paths
[] = { DEVRANDOM
, NULL
};
195 for (i
= 0; paths
[i
] != NULL
; i
++) {
196 if ((fp
= fopen(paths
[i
], "rb")) == NULL
)
199 buffer
= RAND_POOL_add_begin(pool
, bytes_needed
);
200 if (buffer
!= NULL
) {
202 if (fread(buffer
, 1, bytes_needed
, fp
) == bytes_needed
)
203 bytes
= bytes_needed
;
205 entropy_available
= RAND_POOL_add_end(pool
, bytes
, 8 * bytes
);
208 if (entropy_available
> 0)
209 return entropy_available
;
211 bytes_needed
= RAND_POOL_bytes_needed(pool
, 8 /*entropy_per_byte*/);
216 # ifdef OPENSSL_RAND_SEED_RDTSC
217 entropy_available
= rand_acquire_entropy_from_tsc(pool
);
218 if (entropy_available
> 0)
219 return entropy_available
;
222 # ifdef OPENSSL_RAND_SEED_RDCPU
223 entropy_available
= rand_acquire_entropy_from_cpu(pool
);
224 if (entropy_available
> 0)
225 return entropy_available
;
228 # ifdef OPENSSL_RAND_SEED_EGD
229 bytes_needed
= RAND_POOL_bytes_needed(pool
, 8 /*entropy_per_byte*/);
230 if (bytes_needed
> 0) {
231 static const char *paths
[] = { DEVRANDOM_EGD
, NULL
};
234 for (i
= 0; paths
[i
] != NULL
; i
++) {
235 buffer
= RAND_POOL_add_begin(pool
, bytes_needed
);
236 if (buffer
!= NULL
) {
238 int num
= RAND_query_egd_bytes(paths
[i
],
239 buffer
, (int)bytes_needed
);
240 if (num
== (int)bytes_needed
)
241 bytes
= bytes_needed
;
243 entropy_available
= RAND_POOL_add_end(pool
, bytes
, 8 * bytes
);
245 if (entropy_available
> 0)
246 return entropy_available
;
251 return RAND_POOL_entropy_available(pool
);