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