blob: 6f4cf8177ed76fa936ff2dcd38f1282cd5cb8ee8 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Random number generator
3 * Copyright (c) 2010-2011, Jouni Malinen <j@w1.fi>
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 *
8 * This random number generator is used to provide additional entropy to the
9 * one provided by the operating system (os_get_random()) for session key
10 * generation. The os_get_random() output is expected to be secure and the
11 * implementation here is expected to provide only limited protection against
12 * cases where os_get_random() cannot provide strong randomness. This
13 * implementation shall not be assumed to be secure as the sole source of
14 * randomness. The random_get_bytes() function mixes in randomness from
15 * os_get_random() and as such, calls to os_get_random() can be replaced with
16 * calls to random_get_bytes() without reducing security.
17 *
18 * The design here follows partially the design used in the Linux
19 * drivers/char/random.c, but the implementation here is simpler and not as
20 * strong. This is a compromise to reduce duplicated CPU effort and to avoid
21 * extra code/memory size. As pointed out above, os_get_random() needs to be
22 * guaranteed to be secure for any of the security assumptions to hold.
23 */
24
25#include "utils/includes.h"
26#ifdef __linux__
27#include <fcntl.h>
28#endif /* __linux__ */
29
30#include "utils/common.h"
31#include "utils/eloop.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070032#include "crypto/crypto.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "sha1.h"
34#include "random.h"
35
36#define POOL_WORDS 32
37#define POOL_WORDS_MASK (POOL_WORDS - 1)
38#define POOL_TAP1 26
39#define POOL_TAP2 20
40#define POOL_TAP3 14
41#define POOL_TAP4 7
42#define POOL_TAP5 1
43#define EXTRACT_LEN 16
44#define MIN_READY_MARK 2
45
46static u32 pool[POOL_WORDS];
47static unsigned int input_rotate = 0;
48static unsigned int pool_pos = 0;
49static u8 dummy_key[20];
50#ifdef __linux__
51static size_t dummy_key_avail = 0;
52static int random_fd = -1;
53#endif /* __linux__ */
54static unsigned int own_pool_ready = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -070055#define RANDOM_ENTROPY_SIZE 20
56static char *random_entropy_file = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057
58#define MIN_COLLECT_ENTROPY 1000
59static unsigned int entropy = 0;
60static unsigned int total_collected = 0;
61
62
Jouni Malinen75ecf522011-06-27 15:19:46 -070063static void random_write_entropy(void);
64
65
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070066static u32 __ROL32(u32 x, u32 y)
67{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070068 if (y == 0)
69 return x;
70
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070071 return (x << (y & 31)) | (x >> (32 - (y & 31)));
72}
73
74
75static void random_mix_pool(const void *buf, size_t len)
76{
77 static const u32 twist[8] = {
78 0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158,
79 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278
80 };
81 const u8 *pos = buf;
82 u32 w;
83
84 wpa_hexdump_key(MSG_EXCESSIVE, "random_mix_pool", buf, len);
85
86 while (len--) {
87 w = __ROL32(*pos++, input_rotate & 31);
88 input_rotate += pool_pos ? 7 : 14;
89 pool_pos = (pool_pos - 1) & POOL_WORDS_MASK;
90 w ^= pool[pool_pos];
91 w ^= pool[(pool_pos + POOL_TAP1) & POOL_WORDS_MASK];
92 w ^= pool[(pool_pos + POOL_TAP2) & POOL_WORDS_MASK];
93 w ^= pool[(pool_pos + POOL_TAP3) & POOL_WORDS_MASK];
94 w ^= pool[(pool_pos + POOL_TAP4) & POOL_WORDS_MASK];
95 w ^= pool[(pool_pos + POOL_TAP5) & POOL_WORDS_MASK];
96 pool[pool_pos] = (w >> 3) ^ twist[w & 7];
97 }
98}
99
100
101static void random_extract(u8 *out)
102{
103 unsigned int i;
104 u8 hash[SHA1_MAC_LEN];
105 u32 *hash_ptr;
106 u32 buf[POOL_WORDS / 2];
107
108 /* First, add hash back to pool to make backtracking more difficult. */
109 hmac_sha1(dummy_key, sizeof(dummy_key), (const u8 *) pool,
110 sizeof(pool), hash);
111 random_mix_pool(hash, sizeof(hash));
112 /* Hash half the pool to extra data */
113 for (i = 0; i < POOL_WORDS / 2; i++)
114 buf[i] = pool[(pool_pos - i) & POOL_WORDS_MASK];
115 hmac_sha1(dummy_key, sizeof(dummy_key), (const u8 *) buf,
116 sizeof(buf), hash);
117 /*
118 * Fold the hash to further reduce any potential output pattern.
119 * Though, compromise this to reduce CPU use for the most common output
120 * length (32) and return 16 bytes from instead of only half.
121 */
122 hash_ptr = (u32 *) hash;
123 hash_ptr[0] ^= hash_ptr[4];
124 os_memcpy(out, hash, EXTRACT_LEN);
125}
126
127
128void random_add_randomness(const void *buf, size_t len)
129{
130 struct os_time t;
131 static unsigned int count = 0;
132
133 count++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700134 if (entropy > MIN_COLLECT_ENTROPY && (count & 0x3ff) != 0) {
135 /*
136 * No need to add more entropy at this point, so save CPU and
137 * skip the update.
138 */
139 return;
140 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700141 wpa_printf(MSG_EXCESSIVE, "Add randomness: count=%u entropy=%u",
142 count, entropy);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700143
144 os_get_time(&t);
145 wpa_hexdump_key(MSG_EXCESSIVE, "random pool",
146 (const u8 *) pool, sizeof(pool));
147 random_mix_pool(&t, sizeof(t));
148 random_mix_pool(buf, len);
149 wpa_hexdump_key(MSG_EXCESSIVE, "random pool",
150 (const u8 *) pool, sizeof(pool));
151 entropy++;
152 total_collected++;
153}
154
155
156int random_get_bytes(void *buf, size_t len)
157{
158 int ret;
159 u8 *bytes = buf;
160 size_t left;
161
162 wpa_printf(MSG_MSGDUMP, "Get randomness: len=%u entropy=%u",
163 (unsigned int) len, entropy);
164
Rich Canningsf8d3d112018-10-09 13:56:37 -0700165#ifdef CONFIG_USE_OPENSSL_RNG
166 /* Start with assumed strong randomness from OpenSSL */
167 ret = crypto_get_random(buf, len);
168 wpa_hexdump_key(MSG_EXCESSIVE, "random from crypto_get_random",
169 buf, len);
170#else /* CONFIG_USE_OPENSSL_RNG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700171 /* Start with assumed strong randomness from OS */
172 ret = os_get_random(buf, len);
173 wpa_hexdump_key(MSG_EXCESSIVE, "random from os_get_random",
174 buf, len);
Rich Canningsf8d3d112018-10-09 13:56:37 -0700175#endif /* CONFIG_USE_OPENSSL_RNG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700176
177 /* Mix in additional entropy extracted from the internal pool */
178 left = len;
179 while (left) {
180 size_t siz, i;
181 u8 tmp[EXTRACT_LEN];
182 random_extract(tmp);
183 wpa_hexdump_key(MSG_EXCESSIVE, "random from internal pool",
184 tmp, sizeof(tmp));
185 siz = left > EXTRACT_LEN ? EXTRACT_LEN : left;
186 for (i = 0; i < siz; i++)
187 *bytes++ ^= tmp[i];
188 left -= siz;
189 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700190
191#ifdef CONFIG_FIPS
192 /* Mix in additional entropy from the crypto module */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800193 bytes = buf;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700194 left = len;
195 while (left) {
196 size_t siz, i;
197 u8 tmp[EXTRACT_LEN];
198 if (crypto_get_random(tmp, sizeof(tmp)) < 0) {
199 wpa_printf(MSG_ERROR, "random: No entropy available "
200 "for generating strong random bytes");
201 return -1;
202 }
203 wpa_hexdump_key(MSG_EXCESSIVE, "random from crypto module",
204 tmp, sizeof(tmp));
205 siz = left > EXTRACT_LEN ? EXTRACT_LEN : left;
206 for (i = 0; i < siz; i++)
207 *bytes++ ^= tmp[i];
208 left -= siz;
209 }
210#endif /* CONFIG_FIPS */
211
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700212 wpa_hexdump_key(MSG_EXCESSIVE, "mixed random", buf, len);
213
214 if (entropy < len)
215 entropy = 0;
216 else
217 entropy -= len;
218
219 return ret;
220}
221
222
223int random_pool_ready(void)
224{
225#ifdef __linux__
226 int fd;
227 ssize_t res;
228
229 /*
230 * Make sure that there is reasonable entropy available before allowing
231 * some key derivation operations to proceed.
232 */
233
234 if (dummy_key_avail == sizeof(dummy_key))
235 return 1; /* Already initialized - good to continue */
236
237 /*
238 * Try to fetch some more data from the kernel high quality
239 * /dev/random. There may not be enough data available at this point,
240 * so use non-blocking read to avoid blocking the application
241 * completely.
242 */
243 fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
244 if (fd < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245 wpa_printf(MSG_ERROR, "random: Cannot open /dev/random: %s",
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800246 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700247 return -1;
248 }
249
250 res = read(fd, dummy_key + dummy_key_avail,
251 sizeof(dummy_key) - dummy_key_avail);
252 if (res < 0) {
253 wpa_printf(MSG_ERROR, "random: Cannot read from /dev/random: "
254 "%s", strerror(errno));
255 res = 0;
256 }
257 wpa_printf(MSG_DEBUG, "random: Got %u/%u bytes from "
258 "/dev/random", (unsigned) res,
259 (unsigned) (sizeof(dummy_key) - dummy_key_avail));
260 dummy_key_avail += res;
261 close(fd);
262
Jouni Malinen75ecf522011-06-27 15:19:46 -0700263 if (dummy_key_avail == sizeof(dummy_key)) {
264 if (own_pool_ready < MIN_READY_MARK)
265 own_pool_ready = MIN_READY_MARK;
266 random_write_entropy();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700267 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700268 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700269
270 wpa_printf(MSG_INFO, "random: Only %u/%u bytes of strong "
271 "random data available from /dev/random",
272 (unsigned) dummy_key_avail, (unsigned) sizeof(dummy_key));
273
274 if (own_pool_ready >= MIN_READY_MARK ||
275 total_collected + 10 * own_pool_ready > MIN_COLLECT_ENTROPY) {
276 wpa_printf(MSG_INFO, "random: Allow operation to proceed "
277 "based on internal entropy");
278 return 1;
279 }
280
281 wpa_printf(MSG_INFO, "random: Not enough entropy pool available for "
282 "secure operations");
283 return 0;
284#else /* __linux__ */
285 /* TODO: could do similar checks on non-Linux platforms */
286 return 1;
287#endif /* __linux__ */
288}
289
290
291void random_mark_pool_ready(void)
292{
293 own_pool_ready++;
294 wpa_printf(MSG_DEBUG, "random: Mark internal entropy pool to be "
295 "ready (count=%u/%u)", own_pool_ready, MIN_READY_MARK);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700296 random_write_entropy();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700297}
298
299
300#ifdef __linux__
301
302static void random_close_fd(void)
303{
304 if (random_fd >= 0) {
305 eloop_unregister_read_sock(random_fd);
306 close(random_fd);
307 random_fd = -1;
308 }
309}
310
311
312static void random_read_fd(int sock, void *eloop_ctx, void *sock_ctx)
313{
314 ssize_t res;
315
316 if (dummy_key_avail == sizeof(dummy_key)) {
317 random_close_fd();
318 return;
319 }
320
321 res = read(sock, dummy_key + dummy_key_avail,
322 sizeof(dummy_key) - dummy_key_avail);
323 if (res < 0) {
324 wpa_printf(MSG_ERROR, "random: Cannot read from /dev/random: "
325 "%s", strerror(errno));
326 return;
327 }
328
329 wpa_printf(MSG_DEBUG, "random: Got %u/%u bytes from /dev/random",
330 (unsigned) res,
331 (unsigned) (sizeof(dummy_key) - dummy_key_avail));
332 dummy_key_avail += res;
333
Jouni Malinen75ecf522011-06-27 15:19:46 -0700334 if (dummy_key_avail == sizeof(dummy_key)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 random_close_fd();
Jouni Malinen75ecf522011-06-27 15:19:46 -0700336 if (own_pool_ready < MIN_READY_MARK)
337 own_pool_ready = MIN_READY_MARK;
338 random_write_entropy();
339 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340}
341
342#endif /* __linux__ */
343
344
Jouni Malinen75ecf522011-06-27 15:19:46 -0700345static void random_read_entropy(void)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700346{
Jouni Malinen75ecf522011-06-27 15:19:46 -0700347 char *buf;
348 size_t len;
349
350 if (!random_entropy_file)
351 return;
352
353 buf = os_readfile(random_entropy_file, &len);
354 if (buf == NULL)
355 return; /* entropy file not yet available */
356
357 if (len != 1 + RANDOM_ENTROPY_SIZE) {
358 wpa_printf(MSG_DEBUG, "random: Invalid entropy file %s",
359 random_entropy_file);
360 os_free(buf);
361 return;
362 }
363
364 own_pool_ready = (u8) buf[0];
365 random_add_randomness(buf + 1, RANDOM_ENTROPY_SIZE);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700366 os_free(buf);
367 wpa_printf(MSG_DEBUG, "random: Added entropy from %s "
368 "(own_pool_ready=%u)",
369 random_entropy_file, own_pool_ready);
370}
371
372
373static void random_write_entropy(void)
374{
375 char buf[RANDOM_ENTROPY_SIZE];
376 FILE *f;
377 u8 opr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800378 int fail = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700379
380 if (!random_entropy_file)
381 return;
382
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800383 if (random_get_bytes(buf, RANDOM_ENTROPY_SIZE) < 0)
384 return;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700385
386 f = fopen(random_entropy_file, "wb");
387 if (f == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800388 wpa_printf(MSG_ERROR, "random: Could not open entropy file %s "
389 "for writing", random_entropy_file);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700390 return;
391 }
392
393 opr = own_pool_ready > 0xff ? 0xff : own_pool_ready;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800394 if (fwrite(&opr, 1, 1, f) != 1 ||
395 fwrite(buf, RANDOM_ENTROPY_SIZE, 1, f) != 1)
396 fail = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700397 fclose(f);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800398 if (fail) {
399 wpa_printf(MSG_ERROR, "random: Could not write entropy data "
400 "to %s", random_entropy_file);
401 return;
402 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700403
404 wpa_printf(MSG_DEBUG, "random: Updated entropy file %s "
405 "(own_pool_ready=%u)",
406 random_entropy_file, own_pool_ready);
407}
408
409
410void random_init(const char *entropy_file)
411{
412 os_free(random_entropy_file);
413 if (entropy_file)
414 random_entropy_file = os_strdup(entropy_file);
415 else
416 random_entropy_file = NULL;
417 random_read_entropy();
418
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419#ifdef __linux__
420 if (random_fd >= 0)
421 return;
422
423 random_fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
424 if (random_fd < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700425 wpa_printf(MSG_ERROR, "random: Cannot open /dev/random: %s",
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800426 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427 return;
428 }
429 wpa_printf(MSG_DEBUG, "random: Trying to read entropy from "
430 "/dev/random");
431
432 eloop_register_read_sock(random_fd, random_read_fd, NULL, NULL);
433#endif /* __linux__ */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700434
435 random_write_entropy();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700436}
437
438
439void random_deinit(void)
440{
441#ifdef __linux__
442 random_close_fd();
443#endif /* __linux__ */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700444 random_write_entropy();
445 os_free(random_entropy_file);
446 random_entropy_file = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447}