500-random_pool_add_kernel.patch 3.9 KB

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