500-random_pool_add_kernel.patch 4.1 KB

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