blob: 9774a695d69970e395cad2b1611d4854cdba7a87 [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;
57static int random_entropy_file_read = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070058
59#define MIN_COLLECT_ENTROPY 1000
60static unsigned int entropy = 0;
61static unsigned int total_collected = 0;
62
63
Jouni Malinen75ecf522011-06-27 15:19:46 -070064static void random_write_entropy(void);
65
66
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070067static u32 __ROL32(u32 x, u32 y)
68{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070069 if (y == 0)
70 return x;
71
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072 return (x << (y & 31)) | (x >> (32 - (y & 31)));
73}
74
75
76static void random_mix_pool(const void *buf, size_t len)
77{
78 static const u32 twist[8] = {
79 0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158,
80 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278
81 };
82 const u8 *pos = buf;
83 u32 w;
84
85 wpa_hexdump_key(MSG_EXCESSIVE, "random_mix_pool", buf, len);
86
87 while (len--) {
88 w = __ROL32(*pos++, input_rotate & 31);
89 input_rotate += pool_pos ? 7 : 14;
90 pool_pos = (pool_pos - 1) & POOL_WORDS_MASK;
91 w ^= pool[pool_pos];
92 w ^= pool[(pool_pos + POOL_TAP1) & POOL_WORDS_MASK];
93 w ^= pool[(pool_pos + POOL_TAP2) & POOL_WORDS_MASK];
94 w ^= pool[(pool_pos + POOL_TAP3) & POOL_WORDS_MASK];
95 w ^= pool[(pool_pos + POOL_TAP4) & POOL_WORDS_MASK];
96 w ^= pool[(pool_pos + POOL_TAP5) & POOL_WORDS_MASK];
97 pool[pool_pos] = (w >> 3) ^ twist[w & 7];
98 }
99}
100
101
102static void random_extract(u8 *out)
103{
104 unsigned int i;
105 u8 hash[SHA1_MAC_LEN];
106 u32 *hash_ptr;
107 u32 buf[POOL_WORDS / 2];
108
109 /* First, add hash back to pool to make backtracking more difficult. */
110 hmac_sha1(dummy_key, sizeof(dummy_key), (const u8 *) pool,
111 sizeof(pool), hash);
112 random_mix_pool(hash, sizeof(hash));
113 /* Hash half the pool to extra data */
114 for (i = 0; i < POOL_WORDS / 2; i++)
115 buf[i] = pool[(pool_pos - i) & POOL_WORDS_MASK];
116 hmac_sha1(dummy_key, sizeof(dummy_key), (const u8 *) buf,
117 sizeof(buf), hash);
118 /*
119 * Fold the hash to further reduce any potential output pattern.
120 * Though, compromise this to reduce CPU use for the most common output
121 * length (32) and return 16 bytes from instead of only half.
122 */
123 hash_ptr = (u32 *) hash;
124 hash_ptr[0] ^= hash_ptr[4];
125 os_memcpy(out, hash, EXTRACT_LEN);
126}
127
128
129void random_add_randomness(const void *buf, size_t len)
130{
131 struct os_time t;
132 static unsigned int count = 0;
133
134 count++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700135 if (entropy > MIN_COLLECT_ENTROPY && (count & 0x3ff) != 0) {
136 /*
137 * No need to add more entropy at this point, so save CPU and
138 * skip the update.
139 */
140 return;
141 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700142 wpa_printf(MSG_EXCESSIVE, "Add randomness: count=%u entropy=%u",
143 count, entropy);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700144
145 os_get_time(&t);
146 wpa_hexdump_key(MSG_EXCESSIVE, "random pool",
147 (const u8 *) pool, sizeof(pool));
148 random_mix_pool(&t, sizeof(t));
149 random_mix_pool(buf, len);
150 wpa_hexdump_key(MSG_EXCESSIVE, "random pool",
151 (const u8 *) pool, sizeof(pool));
152 entropy++;
153 total_collected++;
154}
155
156
157int random_get_bytes(void *buf, size_t len)
158{
159 int ret;
160 u8 *bytes = buf;
161 size_t left;
162
163 wpa_printf(MSG_MSGDUMP, "Get randomness: len=%u entropy=%u",
164 (unsigned int) len, entropy);
165
Rich Canningsac1636c2018-10-09 13:56:37 -0700166#ifdef CONFIG_USE_OPENSSL_RNG
167 /* Start with assumed strong randomness from OpenSSL */
168 ret = crypto_get_random(buf, len);
169 wpa_hexdump_key(MSG_EXCESSIVE, "random from crypto_get_random",
170 buf, len);
171#else /* CONFIG_USE_OPENSSL_RNG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172 /* Start with assumed strong randomness from OS */
173 ret = os_get_random(buf, len);
174 wpa_hexdump_key(MSG_EXCESSIVE, "random from os_get_random",
175 buf, len);
Rich Canningsac1636c2018-10-09 13:56:37 -0700176#endif /* CONFIG_USE_OPENSSL_RNG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700177
178 /* Mix in additional entropy extracted from the internal pool */
179 left = len;
180 while (left) {
181 size_t siz, i;
182 u8 tmp[EXTRACT_LEN];
183 random_extract(tmp);
184 wpa_hexdump_key(MSG_EXCESSIVE, "random from internal pool",
185 tmp, sizeof(tmp));
186 siz = left > EXTRACT_LEN ? EXTRACT_LEN : left;
187 for (i = 0; i < siz; i++)
188 *bytes++ ^= tmp[i];
189 left -= siz;
190 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700191
192#ifdef CONFIG_FIPS
193 /* Mix in additional entropy from the crypto module */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800194 bytes = buf;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700195 left = len;
196 while (left) {
197 size_t siz, i;
198 u8 tmp[EXTRACT_LEN];
199 if (crypto_get_random(tmp, sizeof(tmp)) < 0) {
200 wpa_printf(MSG_ERROR, "random: No entropy available "
201 "for generating strong random bytes");
202 return -1;
203 }
204 wpa_hexdump_key(MSG_EXCESSIVE, "random from crypto module",
205 tmp, sizeof(tmp));
206 siz = left > EXTRACT_LEN ? EXTRACT_LEN : left;
207 for (i = 0; i < siz; i++)
208 *bytes++ ^= tmp[i];
209 left -= siz;
210 }
211#endif /* CONFIG_FIPS */
212
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213 wpa_hexdump_key(MSG_EXCESSIVE, "mixed random", buf, len);
214
215 if (entropy < len)
216 entropy = 0;
217 else
218 entropy -= len;
219
220 return ret;
221}
222
223
224int random_pool_ready(void)
225{
226#ifdef __linux__
227 int fd;
228 ssize_t res;
229
230 /*
231 * Make sure that there is reasonable entropy available before allowing
232 * some key derivation operations to proceed.
233 */
234
235 if (dummy_key_avail == sizeof(dummy_key))
236 return 1; /* Already initialized - good to continue */
237
238 /*
239 * Try to fetch some more data from the kernel high quality
240 * /dev/random. There may not be enough data available at this point,
241 * so use non-blocking read to avoid blocking the application
242 * completely.
243 */
244 fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
245 if (fd < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700246 wpa_printf(MSG_ERROR, "random: Cannot open /dev/random: %s",
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800247 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248 return -1;
249 }
250
251 res = read(fd, dummy_key + dummy_key_avail,
252 sizeof(dummy_key) - dummy_key_avail);
253 if (res < 0) {
254 wpa_printf(MSG_ERROR, "random: Cannot read from /dev/random: "
255 "%s", strerror(errno));
256 res = 0;
257 }
258 wpa_printf(MSG_DEBUG, "random: Got %u/%u bytes from "
259 "/dev/random", (unsigned) res,
260 (unsigned) (sizeof(dummy_key) - dummy_key_avail));
261 dummy_key_avail += res;
262 close(fd);
263
Jouni Malinen75ecf522011-06-27 15:19:46 -0700264 if (dummy_key_avail == sizeof(dummy_key)) {
265 if (own_pool_ready < MIN_READY_MARK)
266 own_pool_ready = MIN_READY_MARK;
267 random_write_entropy();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700268 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700269 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270
271 wpa_printf(MSG_INFO, "random: Only %u/%u bytes of strong "
272 "random data available from /dev/random",
273 (unsigned) dummy_key_avail, (unsigned) sizeof(dummy_key));
274
275 if (own_pool_ready >= MIN_READY_MARK ||
276 total_collected + 10 * own_pool_ready > MIN_COLLECT_ENTROPY) {
277 wpa_printf(MSG_INFO, "random: Allow operation to proceed "
278 "based on internal entropy");
279 return 1;
280 }
281
282 wpa_printf(MSG_INFO, "random: Not enough entropy pool available for "
283 "secure operations");
284 return 0;
285#else /* __linux__ */
286 /* TODO: could do similar checks on non-Linux platforms */
287 return 1;
288#endif /* __linux__ */
289}
290
291
292void random_mark_pool_ready(void)
293{
294 own_pool_ready++;
295 wpa_printf(MSG_DEBUG, "random: Mark internal entropy pool to be "
296 "ready (count=%u/%u)", own_pool_ready, MIN_READY_MARK);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700297 random_write_entropy();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700298}
299
300
301#ifdef __linux__
302
303static void random_close_fd(void)
304{
305 if (random_fd >= 0) {
306 eloop_unregister_read_sock(random_fd);
307 close(random_fd);
308 random_fd = -1;
309 }
310}
311
312
313static void random_read_fd(int sock, void *eloop_ctx, void *sock_ctx)
314{
315 ssize_t res;
316
317 if (dummy_key_avail == sizeof(dummy_key)) {
318 random_close_fd();
319 return;
320 }
321
322 res = read(sock, dummy_key + dummy_key_avail,
323 sizeof(dummy_key) - dummy_key_avail);
324 if (res < 0) {
325 wpa_printf(MSG_ERROR, "random: Cannot read from /dev/random: "
326 "%s", strerror(errno));
327 return;
328 }
329
330 wpa_printf(MSG_DEBUG, "random: Got %u/%u bytes from /dev/random",
331 (unsigned) res,
332 (unsigned) (sizeof(dummy_key) - dummy_key_avail));
333 dummy_key_avail += res;
334
Jouni Malinen75ecf522011-06-27 15:19:46 -0700335 if (dummy_key_avail == sizeof(dummy_key)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336 random_close_fd();
Jouni Malinen75ecf522011-06-27 15:19:46 -0700337 if (own_pool_ready < MIN_READY_MARK)
338 own_pool_ready = MIN_READY_MARK;
339 random_write_entropy();
340 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341}
342
343#endif /* __linux__ */
344
345
Jouni Malinen75ecf522011-06-27 15:19:46 -0700346static void random_read_entropy(void)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347{
Jouni Malinen75ecf522011-06-27 15:19:46 -0700348 char *buf;
349 size_t len;
350
351 if (!random_entropy_file)
352 return;
353
354 buf = os_readfile(random_entropy_file, &len);
355 if (buf == NULL)
356 return; /* entropy file not yet available */
357
358 if (len != 1 + RANDOM_ENTROPY_SIZE) {
359 wpa_printf(MSG_DEBUG, "random: Invalid entropy file %s",
360 random_entropy_file);
361 os_free(buf);
362 return;
363 }
364
365 own_pool_ready = (u8) buf[0];
366 random_add_randomness(buf + 1, RANDOM_ENTROPY_SIZE);
367 random_entropy_file_read = 1;
368 os_free(buf);
369 wpa_printf(MSG_DEBUG, "random: Added entropy from %s "
370 "(own_pool_ready=%u)",
371 random_entropy_file, own_pool_ready);
372}
373
374
375static void random_write_entropy(void)
376{
377 char buf[RANDOM_ENTROPY_SIZE];
378 FILE *f;
379 u8 opr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800380 int fail = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700381
382 if (!random_entropy_file)
383 return;
384
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800385 if (random_get_bytes(buf, RANDOM_ENTROPY_SIZE) < 0)
386 return;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700387
388 f = fopen(random_entropy_file, "wb");
389 if (f == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800390 wpa_printf(MSG_ERROR, "random: Could not open entropy file %s "
391 "for writing", random_entropy_file);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700392 return;
393 }
394
395 opr = own_pool_ready > 0xff ? 0xff : own_pool_ready;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800396 if (fwrite(&opr, 1, 1, f) != 1 ||
397 fwrite(buf, RANDOM_ENTROPY_SIZE, 1, f) != 1)
398 fail = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700399 fclose(f);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800400 if (fail) {
401 wpa_printf(MSG_ERROR, "random: Could not write entropy data "
402 "to %s", random_entropy_file);
403 return;
404 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700405
406 wpa_printf(MSG_DEBUG, "random: Updated entropy file %s "
407 "(own_pool_ready=%u)",
408 random_entropy_file, own_pool_ready);
409}
410
411
412void random_init(const char *entropy_file)
413{
414 os_free(random_entropy_file);
415 if (entropy_file)
416 random_entropy_file = os_strdup(entropy_file);
417 else
418 random_entropy_file = NULL;
419 random_read_entropy();
420
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700421#ifdef __linux__
422 if (random_fd >= 0)
423 return;
424
425 random_fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
426 if (random_fd < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427 wpa_printf(MSG_ERROR, "random: Cannot open /dev/random: %s",
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800428 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429 return;
430 }
431 wpa_printf(MSG_DEBUG, "random: Trying to read entropy from "
432 "/dev/random");
433
434 eloop_register_read_sock(random_fd, random_read_fd, NULL, NULL);
435#endif /* __linux__ */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700436
437 random_write_entropy();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700438}
439
440
441void random_deinit(void)
442{
443#ifdef __linux__
444 random_close_fd();
445#endif /* __linux__ */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700446 random_write_entropy();
447 os_free(random_entropy_file);
448 random_entropy_file = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700449}