You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ottery_entropy_urandom.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Libottery by Nick Mathewson.
  2. This software has been dedicated to the public domain under the CC0
  3. public domain dedication.
  4. To the extent possible under law, the person who associated CC0 with
  5. libottery has waived all copyright and related or neighboring rights
  6. to libottery.
  7. You should have received a copy of the CC0 legalcode along with this
  8. work in doc/cc0.txt. If not, see
  9. <http://creativecommons.org/publicdomain/zero/1.0/>.
  10. */
  11. #ifndef _WIN32
  12. /**
  13. * Read from a file into an n-byte buffer until the buffer is full or until
  14. * we reach an error. Returns the number of bytes read. If the return
  15. * value is less than n, an error occurred.
  16. */
  17. static int
  18. ottery_read_n_bytes_from_file_(int fd, uint8_t *out, size_t n)
  19. {
  20. ssize_t r;
  21. uint8_t *outp = out;
  22. while (n) {
  23. r = read(fd, outp, n);
  24. if (r <= 0 || (size_t)r > n)
  25. return outp - out;
  26. outp += r;
  27. n -= r;
  28. }
  29. return outp - out;
  30. }
  31. /** Generate random bytes using the unix-style /dev/urandom RNG, or another
  32. * such device as configured in the configuration. */
  33. static int
  34. ottery_get_entropy_urandom(const struct ottery_entropy_config *cfg,
  35. struct ottery_entropy_state *state,
  36. uint8_t *out, size_t outlen)
  37. {
  38. /* On most unixes these days, you can get strong random numbers from
  39. * /dev/urandom.
  40. *
  41. * That's assuming that /dev/urandom is seeded. For most applications,
  42. * that won't be a problem. But for stuff that starts close to system
  43. * startup, before the operating system has added any entropy to the pool,
  44. * it can be pretty bad.
  45. *
  46. * You could use /dev/random instead, if you want, but that has another
  47. * problem. It will block if the OS PRNG has received less entropy than
  48. * it has emitted. If we assume that the OS PRNG isn't cryptographically
  49. * weak, blocking in that case is simple overkill.
  50. *
  51. * It would be best if there were an alternative that blocked if the PRNG
  52. * had _never_ been seeded. But most operating systems don't have that.
  53. */
  54. int fd;
  55. ssize_t n;
  56. int result = 0;
  57. const char *urandom_fname;
  58. struct stat st;
  59. int own_fd = 0;
  60. int check_device = !cfg || !cfg->allow_nondev_urandom;
  61. #ifndef O_CLOEXEC
  62. #define O_CLOEXEC 0
  63. #endif
  64. if (cfg && cfg->urandom_fd_is_set && cfg->urandom_fd >= 0) {
  65. fd = cfg->urandom_fd;
  66. } else {
  67. if (cfg && cfg->urandom_fname)
  68. urandom_fname = cfg->urandom_fname;
  69. else
  70. urandom_fname = "/dev/urandom";
  71. fd = open(urandom_fname, O_RDONLY|O_CLOEXEC);
  72. own_fd = 1;
  73. if (fd < 0)
  74. return OTTERY_ERR_INIT_STRONG_RNG;
  75. }
  76. if (fstat(fd, &st) < 0) {
  77. result = OTTERY_ERR_INIT_STRONG_RNG;
  78. goto end;
  79. }
  80. if (check_device) {
  81. if (0 == (st.st_mode & S_IFCHR)) {
  82. result = OTTERY_ERR_INIT_STRONG_RNG;
  83. goto end;
  84. }
  85. if (state) {
  86. if (0 == state->urandom_fd_inode) {
  87. state->urandom_fd_inode = (uint64_t) st.st_ino;
  88. } else if ((uint64_t)st.st_ino != state->urandom_fd_inode) {
  89. close(fd);
  90. return OTTERY_ERR_ACCESS_STRONG_RNG;
  91. }
  92. }
  93. }
  94. n = ottery_read_n_bytes_from_file_(fd, out, outlen);
  95. if (n < 0 || (size_t)n != outlen)
  96. result = OTTERY_ERR_ACCESS_STRONG_RNG;
  97. end:
  98. if (own_fd)
  99. close(fd);
  100. return result;
  101. }
  102. #define ENTROPY_SOURCE_URANDOM \
  103. { ottery_get_entropy_urandom, SRC(RANDOMDEV)|DOM(OS)|FL(STRONG) }
  104. #endif