| 1 | --- a/src/crypto/random.c |
| 2 | +++ b/src/crypto/random.c |
| 3 | @@ -25,6 +25,7 @@ |
| 4 | #include "utils/includes.h" |
| 5 | #ifdef __linux__ |
| 6 | #include <fcntl.h> |
| 7 | +#include <sys/stat.h> |
| 8 | #endif /* __linux__ */ |
| 9 | |
| 10 | #include "utils/common.h" |
| 11 | @@ -33,6 +34,8 @@ |
| 12 | #include "sha1.h" |
| 13 | #include "random.h" |
| 14 | |
| 15 | +#define RANDOM_STAMPFILE "/var/run/.random_available" |
| 16 | + |
| 17 | #define POOL_WORDS 32 |
| 18 | #define POOL_WORDS_MASK (POOL_WORDS - 1) |
| 19 | #define POOL_TAP1 26 |
| 20 | @@ -43,6 +46,8 @@ |
| 21 | #define EXTRACT_LEN 16 |
| 22 | #define MIN_READY_MARK 2 |
| 23 | |
| 24 | +#ifndef CONFIG_NO_RANDOM_POOL |
| 25 | + |
| 26 | static u32 pool[POOL_WORDS]; |
| 27 | static unsigned int input_rotate = 0; |
| 28 | static unsigned int pool_pos = 0; |
| 29 | @@ -123,7 +128,7 @@ static void random_extract(u8 *out) |
| 30 | } |
| 31 | |
| 32 | |
| 33 | -void random_add_randomness(const void *buf, size_t len) |
| 34 | +static void random_pool_add_randomness(const void *buf, size_t len) |
| 35 | { |
| 36 | struct os_time t; |
| 37 | static unsigned int count = 0; |
| 38 | @@ -213,16 +218,22 @@ int random_get_bytes(void *buf, size_t l |
| 39 | int random_pool_ready(void) |
| 40 | { |
| 41 | #ifdef __linux__ |
| 42 | + struct stat st; |
| 43 | int fd; |
| 44 | ssize_t res; |
| 45 | |
| 46 | + if (stat(RANDOM_STAMPFILE, &st) == 0) |
| 47 | + return 1; |
| 48 | + |
| 49 | /* |
| 50 | * Make sure that there is reasonable entropy available before allowing |
| 51 | * some key derivation operations to proceed. |
| 52 | */ |
| 53 | |
| 54 | - if (dummy_key_avail == sizeof(dummy_key)) |
| 55 | + if (dummy_key_avail == sizeof(dummy_key)) { |
| 56 | + random_mark_pool_ready(); |
| 57 | return 1; /* Already initialized - good to continue */ |
| 58 | + } |
| 59 | |
| 60 | /* |
| 61 | * Try to fetch some more data from the kernel high quality |
| 62 | @@ -257,6 +268,7 @@ int random_pool_ready(void) |
| 63 | if (dummy_key_avail == sizeof(dummy_key)) { |
| 64 | if (own_pool_ready < MIN_READY_MARK) |
| 65 | own_pool_ready = MIN_READY_MARK; |
| 66 | + random_mark_pool_ready(); |
| 67 | random_write_entropy(); |
| 68 | return 1; |
| 69 | } |
| 70 | @@ -269,6 +281,7 @@ int random_pool_ready(void) |
| 71 | total_collected + 10 * own_pool_ready > MIN_COLLECT_ENTROPY) { |
| 72 | wpa_printf(MSG_INFO, "random: Allow operation to proceed " |
| 73 | "based on internal entropy"); |
| 74 | + random_mark_pool_ready(); |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | @@ -284,10 +297,16 @@ int random_pool_ready(void) |
| 79 | |
| 80 | void random_mark_pool_ready(void) |
| 81 | { |
| 82 | + int fd; |
| 83 | + |
| 84 | own_pool_ready++; |
| 85 | wpa_printf(MSG_DEBUG, "random: Mark internal entropy pool to be " |
| 86 | "ready (count=%u/%u)", own_pool_ready, MIN_READY_MARK); |
| 87 | random_write_entropy(); |
| 88 | + |
| 89 | + fd = open(RANDOM_STAMPFILE, O_CREAT | O_WRONLY | O_EXCL | O_NOFOLLOW, 0600); |
| 90 | + if (fd >= 0) |
| 91 | + close(fd); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | @@ -444,3 +463,22 @@ void random_deinit(void) |
| 96 | os_free(random_entropy_file); |
| 97 | random_entropy_file = NULL; |
| 98 | } |
| 99 | + |
| 100 | +#endif /* CONFIG_NO_RANDOM_POOL */ |
| 101 | + |
| 102 | + |
| 103 | +void random_add_randomness(const void *buf, size_t len) |
| 104 | +{ |
| 105 | +#ifdef __linux__ |
| 106 | + int fd; |
| 107 | + |
| 108 | + fd = open("/dev/random", O_RDWR); |
| 109 | + if (fd >= 0) { |
| 110 | + write(fd, buf, len); |
| 111 | + close(fd); |
| 112 | + } |
| 113 | +#endif |
| 114 | +#ifndef CONFIG_NO_RANDOM_POOL |
| 115 | + random_pool_add_randomness(buf, len); |
| 116 | +#endif |
| 117 | +} |
| 118 | --- a/wpa_supplicant/Makefile |
| 119 | +++ b/wpa_supplicant/Makefile |
| 120 | @@ -1217,9 +1217,8 @@ endif |
| 121 | |
| 122 | ifdef CONFIG_NO_RANDOM_POOL |
| 123 | CFLAGS += -DCONFIG_NO_RANDOM_POOL |
| 124 | -else |
| 125 | -OBJS += ../src/crypto/random.o |
| 126 | endif |
| 127 | +OBJS += ../src/crypto/random.o |
| 128 | |
| 129 | ifdef CONFIG_CTRL_IFACE |
| 130 | ifeq ($(CONFIG_CTRL_IFACE), y) |
| 131 | --- a/wpa_supplicant/Android.mk |
| 132 | +++ b/wpa_supplicant/Android.mk |
| 133 | @@ -1161,9 +1161,8 @@ endif |
| 134 | |
| 135 | ifdef CONFIG_NO_RANDOM_POOL |
| 136 | L_CFLAGS += -DCONFIG_NO_RANDOM_POOL |
| 137 | -else |
| 138 | -OBJS += src/crypto/random.c |
| 139 | endif |
| 140 | +OBJS += src/crypto/random.c |
| 141 | |
| 142 | ifdef CONFIG_CTRL_IFACE |
| 143 | ifeq ($(CONFIG_CTRL_IFACE), y) |
| 144 | --- a/hostapd/Android.mk |
| 145 | +++ b/hostapd/Android.mk |
| 146 | @@ -748,11 +748,11 @@ endif |
| 147 | ifdef CONFIG_NO_RANDOM_POOL |
| 148 | L_CFLAGS += -DCONFIG_NO_RANDOM_POOL |
| 149 | else |
| 150 | -OBJS += src/crypto/random.c |
| 151 | -HOBJS += src/crypto/random.c |
| 152 | HOBJS += $(SHA1OBJS) |
| 153 | HOBJS += src/crypto/md5.c |
| 154 | endif |
| 155 | +OBJS += src/crypto/random.c |
| 156 | +HOBJS += src/crypto/random.c |
| 157 | |
| 158 | ifdef CONFIG_RADIUS_SERVER |
| 159 | L_CFLAGS += -DRADIUS_SERVER |
| 160 | --- a/hostapd/Makefile |
| 161 | +++ b/hostapd/Makefile |
| 162 | @@ -755,12 +755,12 @@ endif |
| 163 | ifdef CONFIG_NO_RANDOM_POOL |
| 164 | CFLAGS += -DCONFIG_NO_RANDOM_POOL |
| 165 | else |
| 166 | -OBJS += ../src/crypto/random.o |
| 167 | -HOBJS += ../src/crypto/random.o |
| 168 | HOBJS += ../src/utils/eloop.o |
| 169 | HOBJS += $(SHA1OBJS) |
| 170 | HOBJS += ../src/crypto/md5.o |
| 171 | endif |
| 172 | +OBJS += ../src/crypto/random.o |
| 173 | +HOBJS += ../src/crypto/random.o |
| 174 | |
| 175 | ifdef CONFIG_RADIUS_SERVER |
| 176 | CFLAGS += -DRADIUS_SERVER |
| 177 | |