entropy_poll.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Platform-specific and custom entropy polling functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #if defined(__linux__) && !defined(_GNU_SOURCE)
  20. /* Ensure that syscall() is available even when compiling with -std=c99 */
  21. #define _GNU_SOURCE
  22. #endif
  23. #include "common.h"
  24. #include <string.h>
  25. #if defined(MBEDTLS_ENTROPY_C)
  26. #include "mbedtls/entropy.h"
  27. #include "mbedtls/entropy_poll.h"
  28. #include "mbedtls/error.h"
  29. #if defined(MBEDTLS_TIMING_C)
  30. #include "mbedtls/timing.h"
  31. #endif
  32. #if defined(MBEDTLS_HAVEGE_C)
  33. #include "mbedtls/havege.h"
  34. #endif
  35. #include "mbedtls/platform.h"
  36. #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
  37. #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
  38. !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
  39. !defined(__HAIKU__) && !defined(__midipix__)
  40. #error \
  41. "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
  42. #endif
  43. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  44. #if !defined(_WIN32_WINNT)
  45. #define _WIN32_WINNT 0x0400
  46. #endif
  47. #include <windows.h>
  48. #include <bcrypt.h>
  49. #if defined(_MSC_VER) && _MSC_VER <= 1600
  50. /* Visual Studio 2010 and earlier issue a warning when both <stdint.h> and
  51. * <intsafe.h> are included, as they redefine a number of <TYPE>_MAX constants.
  52. * These constants are guaranteed to be the same, though, so we suppress the
  53. * warning when including intsafe.h.
  54. */
  55. #pragma warning( push )
  56. #pragma warning( disable : 4005 )
  57. #endif
  58. #include <intsafe.h>
  59. #if defined(_MSC_VER) && _MSC_VER <= 1600
  60. #pragma warning( pop )
  61. #endif
  62. int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len,
  63. size_t *olen)
  64. {
  65. ULONG len_as_ulong = 0;
  66. ((void) data);
  67. *olen = 0;
  68. /*
  69. * BCryptGenRandom takes ULONG for size, which is smaller than size_t on
  70. * 64-bit Windows platforms. Ensure len's value can be safely converted into
  71. * a ULONG.
  72. */
  73. if (FAILED(SizeTToULong(len, &len_as_ulong))) {
  74. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  75. }
  76. if (!BCRYPT_SUCCESS(BCryptGenRandom(NULL, output, len_as_ulong, BCRYPT_USE_SYSTEM_PREFERRED_RNG))) {
  77. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  78. }
  79. *olen = len;
  80. return 0;
  81. }
  82. #else /* _WIN32 && !EFIX64 && !EFI32 */
  83. /*
  84. * Test for Linux getrandom() support.
  85. * Since there is no wrapper in the libc yet, use the generic syscall wrapper
  86. * available in GNU libc and compatible libc's (eg uClibc).
  87. */
  88. #if ((defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__))
  89. #include <unistd.h>
  90. #include <sys/syscall.h>
  91. #if defined(SYS_getrandom)
  92. #define HAVE_GETRANDOM
  93. #include <errno.h>
  94. static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
  95. {
  96. /* MemSan cannot understand that the syscall writes to the buffer */
  97. #if defined(__has_feature)
  98. #if __has_feature(memory_sanitizer)
  99. memset(buf, 0, buflen);
  100. #endif
  101. #endif
  102. return syscall(SYS_getrandom, buf, buflen, flags);
  103. }
  104. #endif /* SYS_getrandom */
  105. #endif /* __linux__ || __midipix__ */
  106. #if defined(__FreeBSD__) || defined(__DragonFly__)
  107. #include <sys/param.h>
  108. #if (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || \
  109. (defined(__DragonFly__) && __DragonFly_version >= 500700)
  110. #include <errno.h>
  111. #include <sys/random.h>
  112. #define HAVE_GETRANDOM
  113. static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
  114. {
  115. return getrandom(buf, buflen, flags);
  116. }
  117. #endif /* (__FreeBSD__ && __FreeBSD_version >= 1200000) ||
  118. (__DragonFly__ && __DragonFly_version >= 500700) */
  119. #endif /* __FreeBSD__ || __DragonFly__ */
  120. /*
  121. * Some BSD systems provide KERN_ARND.
  122. * This is equivalent to reading from /dev/urandom, only it doesn't require an
  123. * open file descriptor, and provides up to 256 bytes per call (basically the
  124. * same as getentropy(), but with a longer history).
  125. *
  126. * Documentation: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7
  127. */
  128. #if (defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(HAVE_GETRANDOM)
  129. #include <sys/param.h>
  130. #include <sys/sysctl.h>
  131. #if defined(KERN_ARND)
  132. #define HAVE_SYSCTL_ARND
  133. static int sysctl_arnd_wrapper(unsigned char *buf, size_t buflen)
  134. {
  135. int name[2];
  136. size_t len;
  137. name[0] = CTL_KERN;
  138. name[1] = KERN_ARND;
  139. while (buflen > 0) {
  140. len = buflen > 256 ? 256 : buflen;
  141. if (sysctl(name, 2, buf, &len, NULL, 0) == -1) {
  142. return -1;
  143. }
  144. buflen -= len;
  145. buf += len;
  146. }
  147. return 0;
  148. }
  149. #endif /* KERN_ARND */
  150. #endif /* __FreeBSD__ || __NetBSD__ */
  151. #include <stdio.h>
  152. int mbedtls_platform_entropy_poll(void *data,
  153. unsigned char *output, size_t len, size_t *olen)
  154. {
  155. FILE *file;
  156. size_t read_len;
  157. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  158. ((void) data);
  159. #if defined(HAVE_GETRANDOM)
  160. ret = getrandom_wrapper(output, len, 0);
  161. if (ret >= 0) {
  162. *olen = ret;
  163. return 0;
  164. } else if (errno != ENOSYS) {
  165. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  166. }
  167. /* Fall through if the system call isn't known. */
  168. #else
  169. ((void) ret);
  170. #endif /* HAVE_GETRANDOM */
  171. #if defined(HAVE_SYSCTL_ARND)
  172. ((void) file);
  173. ((void) read_len);
  174. if (sysctl_arnd_wrapper(output, len) == -1) {
  175. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  176. }
  177. *olen = len;
  178. return 0;
  179. #else
  180. *olen = 0;
  181. file = fopen("/dev/urandom", "rb");
  182. if (file == NULL) {
  183. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  184. }
  185. read_len = fread(output, 1, len, file);
  186. if (read_len != len) {
  187. fclose(file);
  188. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  189. }
  190. fclose(file);
  191. *olen = len;
  192. return 0;
  193. #endif /* HAVE_SYSCTL_ARND */
  194. }
  195. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  196. #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
  197. #if defined(MBEDTLS_TEST_NULL_ENTROPY)
  198. int mbedtls_null_entropy_poll(void *data,
  199. unsigned char *output, size_t len, size_t *olen)
  200. {
  201. ((void) data);
  202. ((void) output);
  203. *olen = 0;
  204. if (len < sizeof(unsigned char)) {
  205. return 0;
  206. }
  207. output[0] = 0;
  208. *olen = sizeof(unsigned char);
  209. return 0;
  210. }
  211. #endif
  212. #if defined(MBEDTLS_TIMING_C)
  213. int mbedtls_hardclock_poll(void *data,
  214. unsigned char *output, size_t len, size_t *olen)
  215. {
  216. unsigned long timer = mbedtls_timing_hardclock();
  217. ((void) data);
  218. *olen = 0;
  219. if (len < sizeof(unsigned long)) {
  220. return 0;
  221. }
  222. memcpy(output, &timer, sizeof(unsigned long));
  223. *olen = sizeof(unsigned long);
  224. return 0;
  225. }
  226. #endif /* MBEDTLS_TIMING_C */
  227. #if defined(MBEDTLS_HAVEGE_C)
  228. int mbedtls_havege_poll(void *data,
  229. unsigned char *output, size_t len, size_t *olen)
  230. {
  231. mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
  232. *olen = 0;
  233. if (mbedtls_havege_random(hs, output, len) != 0) {
  234. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  235. }
  236. *olen = len;
  237. return 0;
  238. }
  239. #endif /* MBEDTLS_HAVEGE_C */
  240. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  241. int mbedtls_nv_seed_poll(void *data,
  242. unsigned char *output, size_t len, size_t *olen)
  243. {
  244. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  245. size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
  246. ((void) data);
  247. memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
  248. if (mbedtls_nv_seed_read(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) < 0) {
  249. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  250. }
  251. if (len < use_len) {
  252. use_len = len;
  253. }
  254. memcpy(output, buf, use_len);
  255. *olen = use_len;
  256. return 0;
  257. }
  258. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  259. #endif /* MBEDTLS_ENTROPY_C */