2 * Random number generator
3 * Copyright (c) 2010-2011, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
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.
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.
25 #include "utils/includes.h"
28 #endif /* __linux__ */
30 #include "utils/common.h"
31 #include "utils/eloop.h"
36 #define POOL_WORDS_MASK (POOL_WORDS - 1)
42 #define EXTRACT_LEN 16
43 #define MIN_READY_MARK 2
45 static u32 pool[POOL_WORDS];
46 static unsigned int input_rotate = 0;
47 static unsigned int pool_pos = 0;
48 static u8 dummy_key[20];
50 static size_t dummy_key_avail = 0;
51 static int random_fd = -1;
52 #endif /* __linux__ */
53 static unsigned int own_pool_ready = 0;
54 #define RANDOM_ENTROPY_SIZE 20
55 static char *random_entropy_file = NULL;
56 static int random_entropy_file_read = 0;
58 #define MIN_COLLECT_ENTROPY 1000
59 static unsigned int entropy = 0;
60 static unsigned int total_collected = 0;
63 static void random_write_entropy(void);
66 static u32 __ROL32(u32 x, u32 y)
68 return (x << (y & 31)) | (x >> (32 - (y & 31)));
72 static void random_mix_pool(const void *buf, size_t len)
74 static const u32 twist[8] = {
75 0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158,
76 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278
81 wpa_hexdump_key(MSG_EXCESSIVE, "random_mix_pool", buf, len);
84 w = __ROL32(*pos++, input_rotate & 31);
85 input_rotate += pool_pos ? 7 : 14;
86 pool_pos = (pool_pos - 1) & POOL_WORDS_MASK;
88 w ^= pool[(pool_pos + POOL_TAP1) & POOL_WORDS_MASK];
89 w ^= pool[(pool_pos + POOL_TAP2) & POOL_WORDS_MASK];
90 w ^= pool[(pool_pos + POOL_TAP3) & POOL_WORDS_MASK];
91 w ^= pool[(pool_pos + POOL_TAP4) & POOL_WORDS_MASK];
92 w ^= pool[(pool_pos + POOL_TAP5) & POOL_WORDS_MASK];
93 pool[pool_pos] = (w >> 3) ^ twist[w & 7];
98 static void random_extract(u8 *out)
101 u8 hash[SHA1_MAC_LEN];
103 u32 buf[POOL_WORDS / 2];
105 /* First, add hash back to pool to make backtracking more difficult. */
106 hmac_sha1(dummy_key, sizeof(dummy_key), (const u8 *) pool,
108 random_mix_pool(hash, sizeof(hash));
109 /* Hash half the pool to extra data */
110 for (i = 0; i < POOL_WORDS / 2; i++)
111 buf[i] = pool[(pool_pos - i) & POOL_WORDS_MASK];
112 hmac_sha1(dummy_key, sizeof(dummy_key), (const u8 *) buf,
115 * Fold the hash to further reduce any potential output pattern.
116 * Though, compromise this to reduce CPU use for the most common output
117 * length (32) and return 16 bytes from instead of only half.
119 hash_ptr = (u32 *) hash;
120 hash_ptr[0] ^= hash_ptr[4];
121 os_memcpy(out, hash, EXTRACT_LEN);
125 void random_add_randomness(const void *buf, size_t len)
128 static unsigned int count = 0;
131 if (entropy > MIN_COLLECT_ENTROPY && (count & 0x3ff) != 0) {
133 * No need to add more entropy at this point, so save CPU and
138 wpa_printf(MSG_EXCESSIVE, "Add randomness: count=%u entropy=%u",
142 wpa_hexdump_key(MSG_EXCESSIVE, "random pool",
143 (const u8 *) pool, sizeof(pool));
144 random_mix_pool(&t, sizeof(t));
145 random_mix_pool(buf, len);
146 wpa_hexdump_key(MSG_EXCESSIVE, "random pool",
147 (const u8 *) pool, sizeof(pool));
153 int random_get_bytes(void *buf, size_t len)
159 wpa_printf(MSG_MSGDUMP, "Get randomness: len=%u entropy=%u",
160 (unsigned int) len, entropy);
162 /* Start with assumed strong randomness from OS */
163 ret = os_get_random(buf, len);
164 wpa_hexdump_key(MSG_EXCESSIVE, "random from os_get_random",
167 /* Mix in additional entropy extracted from the internal pool */
173 wpa_hexdump_key(MSG_EXCESSIVE, "random from internal pool",
175 siz = left > EXTRACT_LEN ? EXTRACT_LEN : left;
176 for (i = 0; i < siz; i++)
180 wpa_hexdump_key(MSG_EXCESSIVE, "mixed random", buf, len);
191 int random_pool_ready(void)
198 * Make sure that there is reasonable entropy available before allowing
199 * some key derivation operations to proceed.
202 if (dummy_key_avail == sizeof(dummy_key))
203 return 1; /* Already initialized - good to continue */
206 * Try to fetch some more data from the kernel high quality
207 * /dev/random. There may not be enough data available at this point,
208 * so use non-blocking read to avoid blocking the application
211 fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
213 #ifndef CONFIG_NO_STDOUT_DEBUG
215 perror("open(/dev/random)");
216 wpa_printf(MSG_ERROR, "random: Cannot open /dev/random: %s",
218 #endif /* CONFIG_NO_STDOUT_DEBUG */
222 res = read(fd, dummy_key + dummy_key_avail,
223 sizeof(dummy_key) - dummy_key_avail);
225 wpa_printf(MSG_ERROR, "random: Cannot read from /dev/random: "
226 "%s", strerror(errno));
229 wpa_printf(MSG_DEBUG, "random: Got %u/%u bytes from "
230 "/dev/random", (unsigned) res,
231 (unsigned) (sizeof(dummy_key) - dummy_key_avail));
232 dummy_key_avail += res;
235 if (dummy_key_avail == sizeof(dummy_key)) {
236 if (own_pool_ready < MIN_READY_MARK)
237 own_pool_ready = MIN_READY_MARK;
238 random_write_entropy();
242 wpa_printf(MSG_INFO, "random: Only %u/%u bytes of strong "
243 "random data available from /dev/random",
244 (unsigned) dummy_key_avail, (unsigned) sizeof(dummy_key));
246 if (own_pool_ready >= MIN_READY_MARK ||
247 total_collected + 10 * own_pool_ready > MIN_COLLECT_ENTROPY) {
248 wpa_printf(MSG_INFO, "random: Allow operation to proceed "
249 "based on internal entropy");
253 wpa_printf(MSG_INFO, "random: Not enough entropy pool available for "
254 "secure operations");
256 #else /* __linux__ */
257 /* TODO: could do similar checks on non-Linux platforms */
259 #endif /* __linux__ */
263 void random_mark_pool_ready(void)
266 wpa_printf(MSG_DEBUG, "random: Mark internal entropy pool to be "
267 "ready (count=%u/%u)", own_pool_ready, MIN_READY_MARK);
268 random_write_entropy();
274 static void random_close_fd(void)
276 if (random_fd >= 0) {
277 eloop_unregister_read_sock(random_fd);
284 static void random_read_fd(int sock, void *eloop_ctx, void *sock_ctx)
288 if (dummy_key_avail == sizeof(dummy_key)) {
293 res = read(sock, dummy_key + dummy_key_avail,
294 sizeof(dummy_key) - dummy_key_avail);
296 wpa_printf(MSG_ERROR, "random: Cannot read from /dev/random: "
297 "%s", strerror(errno));
301 wpa_printf(MSG_DEBUG, "random: Got %u/%u bytes from /dev/random",
303 (unsigned) (sizeof(dummy_key) - dummy_key_avail));
304 dummy_key_avail += res;
306 if (dummy_key_avail == sizeof(dummy_key)) {
308 if (own_pool_ready < MIN_READY_MARK)
309 own_pool_ready = MIN_READY_MARK;
310 random_write_entropy();
314 #endif /* __linux__ */
317 static void random_read_entropy(void)
322 if (!random_entropy_file)
325 buf = os_readfile(random_entropy_file, &len);
327 return; /* entropy file not yet available */
329 if (len != 1 + RANDOM_ENTROPY_SIZE) {
330 wpa_printf(MSG_DEBUG, "random: Invalid entropy file %s",
331 random_entropy_file);
336 own_pool_ready = (u8) buf[0];
337 random_add_randomness(buf + 1, RANDOM_ENTROPY_SIZE);
338 random_entropy_file_read = 1;
340 wpa_printf(MSG_DEBUG, "random: Added entropy from %s "
341 "(own_pool_ready=%u)",
342 random_entropy_file, own_pool_ready);
346 static void random_write_entropy(void)
348 char buf[RANDOM_ENTROPY_SIZE];
353 if (!random_entropy_file)
356 if (random_get_bytes(buf, RANDOM_ENTROPY_SIZE) < 0)
359 f = fopen(random_entropy_file, "wb");
361 wpa_printf(MSG_ERROR, "random: Could not open entropy file %s "
362 "for writing", random_entropy_file);
366 opr = own_pool_ready > 0xff ? 0xff : own_pool_ready;
367 if (fwrite(&opr, 1, 1, f) != 1 ||
368 fwrite(buf, RANDOM_ENTROPY_SIZE, 1, f) != 1)
372 wpa_printf(MSG_ERROR, "random: Could not write entropy data "
373 "to %s", random_entropy_file);
377 wpa_printf(MSG_DEBUG, "random: Updated entropy file %s "
378 "(own_pool_ready=%u)",
379 random_entropy_file, own_pool_ready);
383 void random_init(const char *entropy_file)
385 os_free(random_entropy_file);
387 random_entropy_file = os_strdup(entropy_file);
389 random_entropy_file = NULL;
390 random_read_entropy();
396 random_fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
398 #ifndef CONFIG_NO_STDOUT_DEBUG
400 perror("open(/dev/random)");
401 wpa_printf(MSG_ERROR, "random: Cannot open /dev/random: %s",
403 #endif /* CONFIG_NO_STDOUT_DEBUG */
406 wpa_printf(MSG_DEBUG, "random: Trying to read entropy from "
409 eloop_register_read_sock(random_fd, random_read_fd, NULL, NULL);
410 #endif /* __linux__ */
412 random_write_entropy();
416 void random_deinit(void)
420 #endif /* __linux__ */
421 random_write_entropy();
422 os_free(random_entropy_file);
423 random_entropy_file = NULL;