urngd.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Non-physical true random number generator based on timing jitter.
  3. *
  4. * Copyright Stephan Mueller <smueller@chronox.de>, 2014
  5. * Copyright Petr Štetiar <ynezz@true.cz>, 2019
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, and the entire permission notice in its entirety,
  12. * including the disclaimer of warranties.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The name of the author may not be used to endorse or promote
  17. * products derived from this software without specific prior
  18. * written permission.
  19. *
  20. * ALTERNATIVELY, this product may be distributed under the terms of
  21. * the GNU General Public License, in which case the provisions of the GPL are
  22. * required INSTEAD OF the above restrictions. (This clause is
  23. * necessary due to a potential bad interaction between the GPL and
  24. * the restrictions contained in a BSD-style copyright.)
  25. *
  26. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  27. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  28. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
  29. * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  32. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  33. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  36. * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
  37. * DAMAGE.
  38. */
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <unistd.h>
  42. #include <sys/ioctl.h>
  43. #include <linux/random.h>
  44. #include <libubox/uloop.h>
  45. #include "log.h"
  46. #include "jitterentropy.h"
  47. #define ENTROPYBYTES 32
  48. #define ENTROPYTHRESH 1024
  49. #define OVERSAMPLINGFACTOR 2
  50. #define DEV_RANDOM "/dev/random"
  51. #define ENTROPYAVAIL "/proc/sys/kernel/random/entropy_avail"
  52. #define ENTROPYPOOLBYTES (sizeof(struct rand_pool_info) + \
  53. (ENTROPYBYTES * OVERSAMPLINGFACTOR * sizeof(char)))
  54. #ifdef URNGD_DEBUG
  55. unsigned int debug;
  56. #endif
  57. struct urngd {
  58. struct uloop_fd rnd_fd;
  59. struct rand_data *ec;
  60. struct rand_pool_info *rpi;
  61. };
  62. static struct urngd urngd_service;
  63. static inline void memset_secure(void *s, int c, size_t n)
  64. {
  65. memset(s, c, n);
  66. __asm__ __volatile__("" : : "r" (s) : "memory");
  67. }
  68. static size_t write_entropy(struct urngd *u, char *buf, size_t len,
  69. size_t entropy_bytes)
  70. {
  71. int ret;
  72. size_t written = 0;
  73. /* value is in bits */
  74. u->rpi->entropy_count = (entropy_bytes * 8);
  75. u->rpi->buf_size = len;
  76. memcpy(u->rpi->buf, buf, len);
  77. memset(buf, 0, len);
  78. ret = ioctl(u->rnd_fd.fd, RNDADDENTROPY, u->rpi);
  79. if (0 > ret) {
  80. ERROR("error injecting entropy: %s\n", strerror(errno));
  81. } else {
  82. DEBUG(1, "injected %zub (%zub of entropy)\n", len, entropy_bytes);
  83. written = len;
  84. }
  85. u->rpi->entropy_count = 0;
  86. u->rpi->buf_size = 0;
  87. memset(u->rpi->buf, 0, len);
  88. return written;
  89. }
  90. static size_t gather_entropy(struct urngd *u)
  91. {
  92. size_t ret = 0;
  93. char buf[(ENTROPYBYTES * OVERSAMPLINGFACTOR)];
  94. if (jent_read_entropy(u->ec, buf, sizeof(buf)) < 0) {
  95. ERROR("cannot read entropy\n");
  96. return 0;
  97. }
  98. ret = write_entropy(u, buf, sizeof(buf), ENTROPYBYTES);
  99. if (sizeof(buf) != ret) {
  100. ERROR("injected %zub of entropy, less then %zub expected\n",
  101. ret, sizeof(buf));
  102. } else {
  103. ret = sizeof(buf);
  104. }
  105. memset_secure(buf, 0, sizeof(buf));
  106. DEBUG(2, DEV_RANDOM " fed with %zub of entropy\n", ret);
  107. return ret;
  108. }
  109. static void low_entropy_cb(struct uloop_fd *ufd, unsigned int events)
  110. {
  111. struct urngd *u = container_of(ufd, struct urngd, rnd_fd);
  112. DEBUG(2, DEV_RANDOM " signals low entropy\n");
  113. gather_entropy(u);
  114. }
  115. static void urngd_done(struct urngd *u)
  116. {
  117. if (u->ec) {
  118. jent_entropy_collector_free(u->ec);
  119. u->ec = NULL;
  120. }
  121. if (u->rpi) {
  122. memset(u->rpi, 0, ENTROPYPOOLBYTES);
  123. free(u->rpi);
  124. u->rpi = NULL;
  125. }
  126. if (u->rnd_fd.fd) {
  127. close(u->rnd_fd.fd);
  128. u->rnd_fd.fd = 0;
  129. }
  130. }
  131. static bool urngd_init(struct urngd *u)
  132. {
  133. int ret = jent_entropy_init();
  134. if (ret) {
  135. ERROR("jent-rng init failed, err: %d\n", ret);
  136. return false;
  137. }
  138. u->ec = jent_entropy_collector_alloc(1, 0);
  139. if (!u->ec) {
  140. ERROR("jent-rng alloc failed\n");
  141. return false;
  142. }
  143. u->rpi = malloc(ENTROPYPOOLBYTES);
  144. if (!u->rpi) {
  145. ERROR("rand pool alloc failed\n");
  146. return false;
  147. }
  148. u->rnd_fd.cb = low_entropy_cb;
  149. u->rnd_fd.fd = open(DEV_RANDOM, O_WRONLY);
  150. if (u->rnd_fd.fd < 1) {
  151. ERROR(DEV_RANDOM " open failed: %s\n", strerror(errno));
  152. return false;
  153. }
  154. uloop_fd_add(&u->rnd_fd, ULOOP_WRITE);
  155. return true;
  156. }
  157. static int usage(const char *prog)
  158. {
  159. fprintf(stderr, "Usage: %s [options]\n"
  160. "Options:\n"
  161. #ifdef URNGD_DEBUG
  162. " -d <level> Enable debug messages\n"
  163. #endif
  164. " -S Print messages to stdout\n"
  165. "\n", prog);
  166. return 1;
  167. }
  168. int main(int argc, char **argv)
  169. {
  170. int ch;
  171. int ulog_channels = ULOG_KMSG;
  172. #ifdef URNGD_DEBUG
  173. char *dbglvl = getenv("DBGLVL");
  174. if (dbglvl) {
  175. debug = atoi(dbglvl);
  176. unsetenv("DBGLVL");
  177. }
  178. #endif
  179. while ((ch = getopt(argc, argv, "d:S")) != -1) {
  180. switch (ch) {
  181. #ifdef URNGD_DEBUG
  182. case 'd':
  183. debug = atoi(optarg);
  184. break;
  185. #endif
  186. case 'S':
  187. ulog_channels = ULOG_STDIO;
  188. break;
  189. default:
  190. return usage(argv[0]);
  191. }
  192. }
  193. ulog_open(ulog_channels, LOG_DAEMON, "urngd");
  194. uloop_init();
  195. if (!urngd_init(&urngd_service)) {
  196. uloop_done();
  197. return -1;
  198. }
  199. LOG("v%s started.\n", URNGD_VERSION);
  200. gather_entropy(&urngd_service);
  201. uloop_run();
  202. uloop_done();
  203. urngd_done(&urngd_service);
  204. return 0;
  205. }