entropy_poll.c 6.1 KB

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