entropy_poll.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Platform-specific and custom entropy polling functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. #if defined(__linux__) || defined(__midipix__)
  8. /* Ensure that syscall() is available even when compiling with -std=c99 */
  9. #if !defined(_GNU_SOURCE)
  10. #define _GNU_SOURCE
  11. #endif
  12. #endif
  13. #include "common.h"
  14. #include <string.h>
  15. #if defined(MBEDTLS_ENTROPY_C)
  16. #include "mbedtls/entropy.h"
  17. #include "entropy_poll.h"
  18. #include "mbedtls/error.h"
  19. #if defined(MBEDTLS_TIMING_C)
  20. #include "mbedtls/timing.h"
  21. #endif
  22. #include "mbedtls/platform.h"
  23. #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
  24. #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
  25. !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
  26. !defined(__HAIKU__) && !defined(__midipix__) && !defined(__MVS__)
  27. #error \
  28. "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in mbedtls_config.h"
  29. #endif
  30. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  31. #include <windows.h>
  32. #include <bcrypt.h>
  33. #include <intsafe.h>
  34. #if defined(_MSC_VER)
  35. #pragma comment(lib, "bcrypt.lib")
  36. #endif
  37. int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len,
  38. size_t *olen)
  39. {
  40. ((void) data);
  41. *olen = 0;
  42. /*
  43. * BCryptGenRandom takes ULONG for size, which is smaller than size_t on
  44. * 64-bit Windows platforms. Extract entropy in chunks of len (dependent
  45. * on ULONG_MAX) size.
  46. */
  47. while (len != 0) {
  48. unsigned long ulong_bytes =
  49. (len > ULONG_MAX) ? ULONG_MAX : (unsigned long) len;
  50. if (!BCRYPT_SUCCESS(BCryptGenRandom(NULL, output, ulong_bytes,
  51. BCRYPT_USE_SYSTEM_PREFERRED_RNG))) {
  52. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  53. }
  54. *olen += ulong_bytes;
  55. len -= ulong_bytes;
  56. }
  57. return 0;
  58. }
  59. #else /* _WIN32 && !EFIX64 && !EFI32 */
  60. /*
  61. * Test for Linux getrandom() support.
  62. * Since there is no wrapper in the libc yet, use the generic syscall wrapper
  63. * available in GNU libc and compatible libc's (eg uClibc).
  64. */
  65. #if ((defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__))
  66. #include <unistd.h>
  67. #include <sys/syscall.h>
  68. #if defined(SYS_getrandom)
  69. #define HAVE_GETRANDOM
  70. #include <errno.h>
  71. static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
  72. {
  73. /* MemSan cannot understand that the syscall writes to the buffer */
  74. #if defined(__has_feature)
  75. #if __has_feature(memory_sanitizer)
  76. memset(buf, 0, buflen);
  77. #endif
  78. #endif
  79. return (int) syscall(SYS_getrandom, buf, buflen, flags);
  80. }
  81. #endif /* SYS_getrandom */
  82. #endif /* __linux__ || __midipix__ */
  83. #if defined(__FreeBSD__) || defined(__DragonFly__)
  84. #include <sys/param.h>
  85. #if (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || \
  86. (defined(__DragonFly__) && __DragonFly_version >= 500700)
  87. #include <errno.h>
  88. #include <sys/random.h>
  89. #define HAVE_GETRANDOM
  90. static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
  91. {
  92. return (int) getrandom(buf, buflen, flags);
  93. }
  94. #endif /* (__FreeBSD__ && __FreeBSD_version >= 1200000) ||
  95. (__DragonFly__ && __DragonFly_version >= 500700) */
  96. #endif /* __FreeBSD__ || __DragonFly__ */
  97. /*
  98. * Some BSD systems provide KERN_ARND.
  99. * This is equivalent to reading from /dev/urandom, only it doesn't require an
  100. * open file descriptor, and provides up to 256 bytes per call (basically the
  101. * same as getentropy(), but with a longer history).
  102. *
  103. * Documentation: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7
  104. */
  105. #if (defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(HAVE_GETRANDOM)
  106. #include <sys/param.h>
  107. #include <sys/sysctl.h>
  108. #if defined(KERN_ARND)
  109. #define HAVE_SYSCTL_ARND
  110. static int sysctl_arnd_wrapper(unsigned char *buf, size_t buflen)
  111. {
  112. int name[2];
  113. size_t len;
  114. name[0] = CTL_KERN;
  115. name[1] = KERN_ARND;
  116. while (buflen > 0) {
  117. len = buflen > 256 ? 256 : buflen;
  118. if (sysctl(name, 2, buf, &len, NULL, 0) == -1) {
  119. return -1;
  120. }
  121. buflen -= len;
  122. buf += len;
  123. }
  124. return 0;
  125. }
  126. #endif /* KERN_ARND */
  127. #endif /* __FreeBSD__ || __NetBSD__ */
  128. #include <stdio.h>
  129. int mbedtls_platform_entropy_poll(void *data,
  130. unsigned char *output, size_t len, size_t *olen)
  131. {
  132. FILE *file;
  133. size_t read_len;
  134. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  135. ((void) data);
  136. #if defined(HAVE_GETRANDOM)
  137. ret = getrandom_wrapper(output, len, 0);
  138. if (ret >= 0) {
  139. *olen = (size_t) ret;
  140. return 0;
  141. } else if (errno != ENOSYS) {
  142. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  143. }
  144. /* Fall through if the system call isn't known. */
  145. #else
  146. ((void) ret);
  147. #endif /* HAVE_GETRANDOM */
  148. #if defined(HAVE_SYSCTL_ARND)
  149. ((void) file);
  150. ((void) read_len);
  151. if (sysctl_arnd_wrapper(output, len) == -1) {
  152. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  153. }
  154. *olen = len;
  155. return 0;
  156. #else
  157. *olen = 0;
  158. file = fopen("/dev/urandom", "rb");
  159. if (file == NULL) {
  160. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  161. }
  162. /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
  163. mbedtls_setbuf(file, NULL);
  164. read_len = fread(output, 1, len, file);
  165. if (read_len != len) {
  166. fclose(file);
  167. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  168. }
  169. fclose(file);
  170. *olen = len;
  171. return 0;
  172. #endif /* HAVE_SYSCTL_ARND */
  173. }
  174. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  175. #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
  176. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  177. int mbedtls_nv_seed_poll(void *data,
  178. unsigned char *output, size_t len, size_t *olen)
  179. {
  180. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  181. size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
  182. ((void) data);
  183. memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
  184. if (mbedtls_nv_seed_read(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) < 0) {
  185. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  186. }
  187. if (len < use_len) {
  188. use_len = len;
  189. }
  190. memcpy(output, buf, use_len);
  191. *olen = use_len;
  192. return 0;
  193. }
  194. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  195. #endif /* MBEDTLS_ENTROPY_C */