getrandom.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* Obtain a series of random bytes.
  2. Copyright 2020-2021 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Paul Eggert. */
  14. #include <config.h>
  15. #include <sys/random.h>
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <stdbool.h>
  19. #include <unistd.h>
  20. #if defined _WIN32 && ! defined __CYGWIN__
  21. # define WIN32_LEAN_AND_MEAN
  22. # include <windows.h>
  23. # if HAVE_BCRYPT_H
  24. # include <bcrypt.h>
  25. # else
  26. # define NTSTATUS LONG
  27. typedef void * BCRYPT_ALG_HANDLE;
  28. # define BCRYPT_USE_SYSTEM_PREFERRED_RNG 0x00000002
  29. # if HAVE_LIB_BCRYPT
  30. extern NTSTATUS WINAPI BCryptGenRandom (BCRYPT_ALG_HANDLE, UCHAR *, ULONG, ULONG);
  31. # endif
  32. # endif
  33. # if !HAVE_LIB_BCRYPT
  34. # include <wincrypt.h>
  35. # ifndef CRYPT_VERIFY_CONTEXT
  36. # define CRYPT_VERIFY_CONTEXT 0xF0000000
  37. # endif
  38. # endif
  39. #endif
  40. #include "minmax.h"
  41. #if defined _WIN32 && ! defined __CYGWIN__
  42. /* Don't assume that UNICODE is not defined. */
  43. # undef LoadLibrary
  44. # define LoadLibrary LoadLibraryA
  45. # undef CryptAcquireContext
  46. # define CryptAcquireContext CryptAcquireContextA
  47. # if !HAVE_LIB_BCRYPT
  48. /* Avoid warnings from gcc -Wcast-function-type. */
  49. # define GetProcAddress \
  50. (void *) GetProcAddress
  51. /* BCryptGenRandom with the BCRYPT_USE_SYSTEM_PREFERRED_RNG flag works only
  52. starting with Windows 7. */
  53. typedef NTSTATUS (WINAPI * BCryptGenRandomFuncType) (BCRYPT_ALG_HANDLE, UCHAR *, ULONG, ULONG);
  54. static BCryptGenRandomFuncType BCryptGenRandomFunc = NULL;
  55. static BOOL initialized = FALSE;
  56. static void
  57. initialize (void)
  58. {
  59. HMODULE bcrypt = LoadLibrary ("bcrypt.dll");
  60. if (bcrypt != NULL)
  61. {
  62. BCryptGenRandomFunc =
  63. (BCryptGenRandomFuncType) GetProcAddress (bcrypt, "BCryptGenRandom");
  64. }
  65. initialized = TRUE;
  66. }
  67. # else
  68. # define BCryptGenRandomFunc BCryptGenRandom
  69. # endif
  70. #else
  71. /* These devices exist on all platforms except native Windows. */
  72. /* Name of a device through which the kernel returns high quality random
  73. numbers, from an entropy pool. When the pool is empty, the call blocks
  74. until entropy sources have added enough bits of entropy. */
  75. # ifndef NAME_OF_RANDOM_DEVICE
  76. # define NAME_OF_RANDOM_DEVICE "/dev/random"
  77. # endif
  78. /* Name of a device through which the kernel returns random or pseudo-random
  79. numbers. It uses an entropy pool, but, in order to avoid blocking, adds
  80. bits generated by a pseudo-random number generator, as needed. */
  81. # ifndef NAME_OF_NONCE_DEVICE
  82. # define NAME_OF_NONCE_DEVICE "/dev/urandom"
  83. # endif
  84. #endif
  85. /* Set BUFFER (of size LENGTH) to random bytes under the control of FLAGS.
  86. Return the number of bytes written (> 0).
  87. Upon error, return -1 and set errno. */
  88. ssize_t
  89. getrandom (void *buffer, size_t length, unsigned int flags)
  90. #undef getrandom
  91. {
  92. #if defined _WIN32 && ! defined __CYGWIN__
  93. /* BCryptGenRandom, defined in <bcrypt.h>
  94. <https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom>
  95. with the BCRYPT_USE_SYSTEM_PREFERRED_RNG flag
  96. works in Windows 7 and newer. */
  97. static int bcrypt_not_working /* = 0 */;
  98. if (!bcrypt_not_working)
  99. {
  100. # if !HAVE_LIB_BCRYPT
  101. if (!initialized)
  102. initialize ();
  103. # endif
  104. if (BCryptGenRandomFunc != NULL
  105. && BCryptGenRandomFunc (NULL, buffer, length,
  106. BCRYPT_USE_SYSTEM_PREFERRED_RNG)
  107. == 0 /*STATUS_SUCCESS*/)
  108. return length;
  109. bcrypt_not_working = 1;
  110. }
  111. # if !HAVE_LIB_BCRYPT
  112. /* CryptGenRandom, defined in <wincrypt.h>
  113. <https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptgenrandom>
  114. works in older releases as well, but is now deprecated.
  115. CryptAcquireContext, defined in <wincrypt.h>
  116. <https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptacquirecontexta> */
  117. {
  118. static int crypt_initialized /* = 0 */;
  119. static HCRYPTPROV provider;
  120. if (!crypt_initialized)
  121. {
  122. if (CryptAcquireContext (&provider, NULL, NULL, PROV_RSA_FULL,
  123. CRYPT_VERIFY_CONTEXT))
  124. crypt_initialized = 1;
  125. else
  126. crypt_initialized = -1;
  127. }
  128. if (crypt_initialized >= 0)
  129. {
  130. if (!CryptGenRandom (provider, length, buffer))
  131. {
  132. errno = EIO;
  133. return -1;
  134. }
  135. return length;
  136. }
  137. }
  138. # endif
  139. errno = ENOSYS;
  140. return -1;
  141. #elif HAVE_GETRANDOM
  142. return getrandom (buffer, length, flags);
  143. #else
  144. static int randfd[2] = { -1, -1 };
  145. bool devrandom = (flags & GRND_RANDOM) != 0;
  146. int fd = randfd[devrandom];
  147. if (fd < 0)
  148. {
  149. static char const randdevice[][MAX (sizeof NAME_OF_NONCE_DEVICE,
  150. sizeof NAME_OF_RANDOM_DEVICE)]
  151. = { NAME_OF_NONCE_DEVICE, NAME_OF_RANDOM_DEVICE };
  152. int oflags = (O_RDONLY + O_CLOEXEC
  153. + (flags & GRND_NONBLOCK ? O_NONBLOCK : 0));
  154. fd = open (randdevice[devrandom], oflags);
  155. if (fd < 0)
  156. return fd;
  157. randfd[devrandom] = fd;
  158. }
  159. return read (fd, buffer, length);
  160. #endif
  161. }