1 /*
2 * Random number generator
3 * Copyright (c) 2010-2011, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 *
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 #ifdef CONFIG_GETRANDOM
29 #include <sys/random.h>
30 #endif /* CONFIG_GETRANDOM */
31 #endif /* __linux__ */
32
33 #include "utils/common.h"
34 #include "utils/eloop.h"
35 #include "crypto/crypto.h"
36 #include "sha1.h"
37 #include "random.h"
38
39 #define POOL_WORDS 32
40 #define POOL_WORDS_MASK (POOL_WORDS - 1)
41 #define POOL_TAP1 26
42 #define POOL_TAP2 20
43 #define POOL_TAP3 14
44 #define POOL_TAP4 7
45 #define POOL_TAP5 1
46 #define EXTRACT_LEN 16
47 #define MIN_READY_MARK 2
48
49 static u32 pool[POOL_WORDS];
50 static unsigned int input_rotate = 0;
51 static unsigned int pool_pos = 0;
52 static u8 stub_key[20];
53 #ifdef __linux__
54 static size_t stub_key_avail = 0;
55 static int random_fd = -1;
56 #endif /* __linux__ */
57 static unsigned int own_pool_ready = 0;
58 #define RANDOM_ENTROPY_SIZE 20
59 static char *random_entropy_file = NULL;
60
61 #define MIN_COLLECT_ENTROPY 1000
62 static unsigned int entropy = 0;
63 static unsigned int total_collected = 0;
64
65 #ifdef CONFIG_FILE
66 static void random_write_entropy(void);
67 #endif
68
__ROL32(u32 x,u32 y)69 static u32 __ROL32(u32 x, u32 y)
70 {
71 if (y == 0)
72 return x;
73
74 return (x << (y & 31)) | (x >> (32 - (y & 31)));
75 }
76
77
random_mix_pool(const void * buf,size_t len)78 static void random_mix_pool(const void *buf, size_t len)
79 {
80 static const u32 twist[8] = {
81 0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158,
82 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278
83 };
84 const u8 *pos = buf;
85 u32 w;
86
87 wpa_hexdump_key(MSG_EXCESSIVE, "random_mix_pool", buf, len);
88
89 while (len--) {
90 w = __ROL32(*pos++, input_rotate & 31);
91 input_rotate += pool_pos ? 7 : 14;
92 pool_pos = (pool_pos - 1) & POOL_WORDS_MASK;
93 w ^= pool[pool_pos];
94 w ^= pool[(pool_pos + POOL_TAP1) & POOL_WORDS_MASK];
95 w ^= pool[(pool_pos + POOL_TAP2) & POOL_WORDS_MASK];
96 w ^= pool[(pool_pos + POOL_TAP3) & POOL_WORDS_MASK];
97 w ^= pool[(pool_pos + POOL_TAP4) & POOL_WORDS_MASK];
98 w ^= pool[(pool_pos + POOL_TAP5) & POOL_WORDS_MASK];
99 pool[pool_pos] = (w >> 3) ^ twist[w & 7];
100 }
101 }
102
103
random_extract(u8 * out)104 static void random_extract(u8 *out)
105 {
106 unsigned int i;
107 u8 hash[SHA1_MAC_LEN];
108 u32 *hash_ptr;
109 u32 buf[POOL_WORDS / 2];
110
111 /* First, add hash back to pool to make backtracking more difficult. */
112 hmac_sha1(stub_key, sizeof(stub_key), (const u8 *) pool,
113 sizeof(pool), hash);
114 random_mix_pool(hash, sizeof(hash));
115 /* Hash half the pool to extra data */
116 for (i = 0; i < POOL_WORDS / 2; i++)
117 buf[i] = pool[(pool_pos - i) & POOL_WORDS_MASK];
118 hmac_sha1(stub_key, sizeof(stub_key), (const u8 *) buf,
119 sizeof(buf), hash);
120 /*
121 * Fold the hash to further reduce any potential output pattern.
122 * Though, compromise this to reduce CPU use for the most common output
123 * length (32) and return 16 bytes from instead of only half.
124 */
125 hash_ptr = (u32 *) hash;
126 hash_ptr[0] ^= hash_ptr[4];
127 os_memcpy(out, hash, EXTRACT_LEN);
128 }
129
130
random_add_randomness(const void * buf,size_t len)131 void random_add_randomness(const void *buf, size_t len)
132 {
133 struct os_time t;
134 static unsigned int count = 0;
135
136 count++;
137 if (entropy > MIN_COLLECT_ENTROPY && (count & 0x3ff) != 0) {
138 /*
139 * No need to add more entropy at this point, so save CPU and
140 * skip the update.
141 */
142 return;
143 }
144 wpa_printf(MSG_EXCESSIVE, "Add randomness: count=%u entropy=%u",
145 count, entropy);
146
147 os_get_time(&t);
148 wpa_hexdump_key(MSG_EXCESSIVE, "random pool",
149 (const u8 *) pool, sizeof(pool));
150 random_mix_pool(&t, sizeof(t));
151 random_mix_pool(buf, len);
152 wpa_hexdump_key(MSG_EXCESSIVE, "random pool",
153 (const u8 *) pool, sizeof(pool));
154 entropy++;
155 total_collected++;
156 }
157
158
random_get_bytes(void * buf,size_t len)159 int random_get_bytes(void *buf, size_t len)
160 {
161 int ret;
162 u8 *bytes = buf;
163 size_t left;
164
165 wpa_printf(MSG_MSGDUMP, "Get randomness: len=%u entropy=%u",
166 (unsigned int) len, entropy);
167
168 /* Start with assumed strong randomness from OS */
169 ret = os_get_random(buf, len);
170 wpa_hexdump_key(MSG_EXCESSIVE, "random from os_get_random",
171 buf, len);
172
173 /* Mix in additional entropy extracted from the internal pool */
174 left = len;
175 while (left) {
176 size_t siz, i;
177 u8 tmp[EXTRACT_LEN];
178 random_extract(tmp);
179 wpa_hexdump_key(MSG_EXCESSIVE, "random from internal pool",
180 tmp, sizeof(tmp));
181 siz = left > EXTRACT_LEN ? EXTRACT_LEN : left;
182 for (i = 0; i < siz; i++)
183 *bytes++ ^= tmp[i];
184 left -= siz;
185 }
186
187 #ifdef CONFIG_FIPS
188 /* Mix in additional entropy from the crypto module */
189 bytes = buf;
190 left = len;
191 while (left) {
192 size_t siz, i;
193 u8 tmp[EXTRACT_LEN];
194 if (crypto_get_random(tmp, sizeof(tmp)) < 0) {
195 wpa_printf(MSG_ERROR, "random: No entropy available "
196 "for generating strong random bytes");
197 return -1;
198 }
199 wpa_hexdump_key(MSG_EXCESSIVE, "random from crypto module",
200 tmp, sizeof(tmp));
201 siz = left > EXTRACT_LEN ? EXTRACT_LEN : left;
202 for (i = 0; i < siz; i++)
203 *bytes++ ^= tmp[i];
204 left -= siz;
205 }
206 #endif /* CONFIG_FIPS */
207
208 wpa_hexdump_key(MSG_EXCESSIVE, "mixed random", buf, len);
209
210 if (entropy < len)
211 entropy = 0;
212 else
213 entropy -= len;
214
215 return ret;
216 }
217
218
random_pool_ready(void)219 int random_pool_ready(void)
220 {
221 #ifdef __linux__
222 int fd;
223 ssize_t res;
224
225 /*
226 * Make sure that there is reasonable entropy available before allowing
227 * some key derivation operations to proceed.
228 */
229
230 if (stub_key_avail == sizeof(stub_key))
231 return 1; /* Already initialized - good to continue */
232
233 /*
234 * Try to fetch some more data from the kernel high quality RNG.
235 * There may not be enough data available at this point,
236 * so use non-blocking read to avoid blocking the application
237 * completely.
238 */
239
240 #ifdef CONFIG_GETRANDOM
241 res = getrandom(stub_key + stub_key_avail,
242 sizeof(stub_key) - stub_key_avail, GRND_NONBLOCK);
243 if (res < 0) {
244 if (errno == ENOSYS) {
245 wpa_printf(MSG_DEBUG,
246 "random: getrandom() not supported, falling back to /dev/random");
247 } else {
248 wpa_printf(MSG_INFO,
249 "random: no data from getrandom(): %s",
250 strerror(errno));
251 res = 0;
252 }
253 }
254 #else /* CONFIG_GETRANDOM */
255 res = -1;
256 #endif /* CONFIG_GETRANDOM */
257 #ifdef CONFIG_FILE
258 if (res < 0) {
259 fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
260 if (fd < 0) {
261 wpa_printf(MSG_ERROR,
262 "random: Cannot open /dev/random: %s",
263 strerror(errno));
264 return -1;
265 }
266
267 res = read(fd, stub_key + stub_key_avail,
268 sizeof(stub_key) - stub_key_avail);
269 if (res < 0) {
270 wpa_printf(MSG_ERROR,
271 "random: Cannot read from /dev/random: %s",
272 strerror(errno));
273 res = 0;
274 }
275 close(fd);
276 }
277
278 wpa_printf(MSG_DEBUG, "random: Got %u/%u random bytes", (unsigned) res,
279 (unsigned) (sizeof(stub_key) - stub_key_avail));
280 stub_key_avail += res;
281
282 if (stub_key_avail == sizeof(stub_key)) {
283 if (own_pool_ready < MIN_READY_MARK)
284 own_pool_ready = MIN_READY_MARK;
285 random_write_entropy();
286 return 1;
287 }
288 #endif
289 wpa_printf(MSG_INFO, "random: Only %u/%u bytes of strong "
290 "random data available",
291 (unsigned) stub_key_avail, (unsigned) sizeof(stub_key));
292
293 if (own_pool_ready >= MIN_READY_MARK ||
294 total_collected + 10 * own_pool_ready > MIN_COLLECT_ENTROPY) {
295 wpa_printf(MSG_INFO, "random: Allow operation to proceed "
296 "based on internal entropy");
297 return 1;
298 }
299
300 wpa_printf(MSG_INFO, "random: Not enough entropy pool available for "
301 "secure operations");
302 return 0;
303 #else /* __linux__ */
304 /* TODO: could do similar checks on non-Linux platforms */
305 return 1;
306 #endif /* __linux__ */
307 }
308
309
random_mark_pool_ready(void)310 void random_mark_pool_ready(void)
311 {
312 own_pool_ready++;
313 wpa_printf(MSG_DEBUG, "random: Mark internal entropy pool to be "
314 "ready (count=%u/%u)", own_pool_ready, MIN_READY_MARK);
315 random_write_entropy();
316 }
317
318
319 #ifdef __linux__
320
random_close_fd(void)321 static void random_close_fd(void)
322 {
323 if (random_fd >= 0) {
324 eloop_unregister_read_sock(random_fd);
325 close(random_fd);
326 random_fd = -1;
327 }
328 }
329
330
random_read_fd(int sock,void * eloop_ctx,void * sock_ctx)331 static void random_read_fd(int sock, void *eloop_ctx, void *sock_ctx)
332 {
333 ssize_t res;
334
335 if (stub_key_avail == sizeof(stub_key)) {
336 random_close_fd();
337 return;
338 }
339
340 res = read(sock, stub_key + stub_key_avail,
341 sizeof(stub_key) - stub_key_avail);
342 if (res < 0) {
343 wpa_printf(MSG_ERROR, "random: Cannot read from /dev/random: "
344 "%s", strerror(errno));
345 return;
346 }
347
348 wpa_printf(MSG_DEBUG, "random: Got %u/%u bytes from /dev/random",
349 (unsigned) res,
350 (unsigned) (sizeof(stub_key) - stub_key_avail));
351 stub_key_avail += res;
352
353 if (stub_key_avail == sizeof(stub_key)) {
354 random_close_fd();
355 if (own_pool_ready < MIN_READY_MARK)
356 own_pool_ready = MIN_READY_MARK;
357 random_write_entropy();
358 }
359 }
360
361 #endif /* __linux__ */
362
363
random_read_entropy(void)364 static void random_read_entropy(void)
365 {
366 char *buf;
367 size_t len;
368
369 if (!random_entropy_file)
370 return;
371
372 buf = os_readfile(random_entropy_file, &len);
373 if (buf == NULL)
374 return; /* entropy file not yet available */
375
376 if (len != 1 + RANDOM_ENTROPY_SIZE) {
377 wpa_printf(MSG_DEBUG, "random: Invalid entropy file %s",
378 random_entropy_file);
379 os_free(buf);
380 return;
381 }
382
383 own_pool_ready = (u8) buf[0];
384 random_add_randomness(buf + 1, RANDOM_ENTROPY_SIZE);
385 os_free(buf);
386 wpa_printf(MSG_DEBUG, "random: Added entropy from %s "
387 "(own_pool_ready=%u)",
388 random_entropy_file, own_pool_ready);
389 }
390
391 #ifdef CONFIG_FILE
random_write_entropy(void)392 static void random_write_entropy(void)
393 {
394 char buf[RANDOM_ENTROPY_SIZE];
395 FILE *f;
396 u8 opr;
397 int fail = 0;
398
399 if (!random_entropy_file)
400 return;
401
402 if (random_get_bytes(buf, RANDOM_ENTROPY_SIZE) < 0)
403 return;
404
405 f = fopen(random_entropy_file, "wb");
406 if (f == NULL) {
407 wpa_printf(MSG_ERROR, "random: Could not open entropy file %s "
408 "for writing", random_entropy_file);
409 return;
410 }
411
412 opr = own_pool_ready > 0xff ? 0xff : own_pool_ready;
413 if (fwrite(&opr, 1, 1, f) != 1 ||
414 fwrite(buf, RANDOM_ENTROPY_SIZE, 1, f) != 1)
415 fail = 1;
416 fclose(f);
417 if (fail) {
418 wpa_printf(MSG_ERROR, "random: Could not write entropy data "
419 "to %s", random_entropy_file);
420 return;
421 }
422
423 wpa_printf(MSG_DEBUG, "random: Updated entropy file %s "
424 "(own_pool_ready=%u)",
425 random_entropy_file, own_pool_ready);
426 }
427 #endif
428
random_init(const char * entropy_file)429 void random_init(const char *entropy_file)
430 {
431 os_free(random_entropy_file);
432 if (entropy_file)
433 random_entropy_file = os_strdup(entropy_file);
434 else
435 random_entropy_file = NULL;
436 random_read_entropy();
437
438 #ifdef __linux__
439 if (random_fd >= 0)
440 return;
441
442 #ifdef CONFIG_GETRANDOM
443 {
444 u8 stub;
445
446 if (getrandom(&stub, 0, GRND_NONBLOCK) == 0 ||
447 errno != ENOSYS) {
448 wpa_printf(MSG_DEBUG,
449 "random: getrandom() support available");
450 return;
451 }
452 }
453 #endif /* CONFIG_GETRANDOM */
454
455 random_fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
456 if (random_fd < 0) {
457 wpa_printf(MSG_ERROR, "random: Cannot open /dev/random: %s",
458 strerror(errno));
459 return;
460 }
461 wpa_printf(MSG_DEBUG, "random: Trying to read entropy from "
462 "/dev/random");
463
464 eloop_register_read_sock(random_fd, random_read_fd, NULL, NULL);
465 #endif /* __linux__ */
466
467 random_write_entropy();
468 }
469
470
random_deinit(void)471 void random_deinit(void)
472 {
473 #ifdef __linux__
474 random_close_fd();
475 #endif /* __linux__ */
476 random_write_entropy();
477 os_free(random_entropy_file);
478 random_entropy_file = NULL;
479 }
480