entropy_poll.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  36. #include "mbedtls/platform.h"
  37. #endif
  38. #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
  39. #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
  40. !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
  41. !defined(__HAIKU__) && !defined(__midipix__)
  42. #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
  43. #endif
  44. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  45. #if !defined(_WIN32_WINNT)
  46. #define _WIN32_WINNT 0x0400
  47. #endif
  48. #include <windows.h>
  49. #include <bcrypt.h>
  50. #if defined(_MSC_VER) && _MSC_VER <= 1600
  51. /* Visual Studio 2010 and earlier issue a warning when both <stdint.h> and
  52. * <intsafe.h> are included, as they redefine a number of <TYPE>_MAX constants.
  53. * These constants are guaranteed to be the same, though, so we suppress the
  54. * warning when including intsafe.h.
  55. */
  56. #pragma warning( push )
  57. #pragma warning( disable : 4005 )
  58. #endif
  59. #include <intsafe.h>
  60. #if defined(_MSC_VER) && _MSC_VER <= 1600
  61. #pragma warning( pop )
  62. #endif
  63. int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
  64. size_t *olen )
  65. {
  66. ULONG len_as_ulong = 0;
  67. ((void) data);
  68. *olen = 0;
  69. /*
  70. * BCryptGenRandom takes ULONG for size, which is smaller than size_t on
  71. * 64-bit Windows platforms. Ensure len's value can be safely converted into
  72. * a ULONG.
  73. */
  74. if ( FAILED( SizeTToULong( len, &len_as_ulong ) ) )
  75. {
  76. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  77. }
  78. if ( !BCRYPT_SUCCESS( BCryptGenRandom( NULL, output, len_as_ulong, BCRYPT_USE_SYSTEM_PREFERRED_RNG ) ) )
  79. {
  80. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  81. }
  82. *olen = len;
  83. return( 0 );
  84. }
  85. #else /* _WIN32 && !EFIX64 && !EFI32 */
  86. /*
  87. * Test for Linux getrandom() support.
  88. * Since there is no wrapper in the libc yet, use the generic syscall wrapper
  89. * available in GNU libc and compatible libc's (eg uClibc).
  90. */
  91. #if ((defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__))
  92. #include <unistd.h>
  93. #include <sys/syscall.h>
  94. #if defined(SYS_getrandom)
  95. #define HAVE_GETRANDOM
  96. #include <errno.h>
  97. static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
  98. {
  99. /* MemSan cannot understand that the syscall writes to the buffer */
  100. #if defined(__has_feature)
  101. #if __has_feature(memory_sanitizer)
  102. memset( buf, 0, buflen );
  103. #endif
  104. #endif
  105. return( syscall( SYS_getrandom, buf, buflen, flags ) );
  106. }
  107. #endif /* SYS_getrandom */
  108. #endif /* __linux__ || __midipix__ */
  109. #if defined(__FreeBSD__) || defined(__DragonFly__)
  110. #include <sys/param.h>
  111. #if (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || \
  112. (defined(__DragonFly__) && __DragonFly_version >= 500700)
  113. #include <errno.h>
  114. #include <sys/random.h>
  115. #define HAVE_GETRANDOM
  116. static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
  117. {
  118. return getrandom( buf, buflen, flags );
  119. }
  120. #endif /* (__FreeBSD__ && __FreeBSD_version >= 1200000) ||
  121. (__DragonFly__ && __DragonFly_version >= 500700) */
  122. #endif /* __FreeBSD__ || __DragonFly__ */
  123. /*
  124. * Some BSD systems provide KERN_ARND.
  125. * This is equivalent to reading from /dev/urandom, only it doesn't require an
  126. * open file descriptor, and provides up to 256 bytes per call (basically the
  127. * same as getentropy(), but with a longer history).
  128. *
  129. * Documentation: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7
  130. */
  131. #if (defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(HAVE_GETRANDOM)
  132. #include <sys/param.h>
  133. #include <sys/sysctl.h>
  134. #if defined(KERN_ARND)
  135. #define HAVE_SYSCTL_ARND
  136. static int sysctl_arnd_wrapper( unsigned char *buf, size_t buflen )
  137. {
  138. int name[2];
  139. size_t len;
  140. name[0] = CTL_KERN;
  141. name[1] = KERN_ARND;
  142. while( buflen > 0 )
  143. {
  144. len = buflen > 256 ? 256 : buflen;
  145. if( sysctl(name, 2, buf, &len, NULL, 0) == -1 )
  146. return( -1 );
  147. buflen -= len;
  148. buf += len;
  149. }
  150. return( 0 );
  151. }
  152. #endif /* KERN_ARND */
  153. #endif /* __FreeBSD__ || __NetBSD__ */
  154. #include <stdio.h>
  155. int mbedtls_platform_entropy_poll( void *data,
  156. unsigned char *output, size_t len, size_t *olen )
  157. {
  158. FILE *file;
  159. size_t read_len;
  160. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  161. ((void) data);
  162. #if defined(HAVE_GETRANDOM)
  163. ret = getrandom_wrapper( output, len, 0 );
  164. if( ret >= 0 )
  165. {
  166. *olen = ret;
  167. return( 0 );
  168. }
  169. else if( errno != ENOSYS )
  170. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  171. /* Fall through if the system call isn't known. */
  172. #else
  173. ((void) ret);
  174. #endif /* HAVE_GETRANDOM */
  175. #if defined(HAVE_SYSCTL_ARND)
  176. ((void) file);
  177. ((void) read_len);
  178. if( sysctl_arnd_wrapper( output, len ) == -1 )
  179. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  180. *olen = len;
  181. return( 0 );
  182. #else
  183. *olen = 0;
  184. file = fopen( "/dev/urandom", "rb" );
  185. if( file == NULL )
  186. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  187. read_len = fread( output, 1, len, file );
  188. if( read_len != len )
  189. {
  190. fclose( file );
  191. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  192. }
  193. fclose( file );
  194. *olen = len;
  195. return( 0 );
  196. #endif /* HAVE_SYSCTL_ARND */
  197. }
  198. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  199. #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
  200. #if defined(MBEDTLS_TEST_NULL_ENTROPY)
  201. int mbedtls_null_entropy_poll( void *data,
  202. unsigned char *output, size_t len, size_t *olen )
  203. {
  204. ((void) data);
  205. ((void) output);
  206. *olen = 0;
  207. if( len < sizeof(unsigned char) )
  208. return( 0 );
  209. output[0] = 0;
  210. *olen = sizeof(unsigned char);
  211. return( 0 );
  212. }
  213. #endif
  214. #if defined(MBEDTLS_TIMING_C)
  215. int mbedtls_hardclock_poll( void *data,
  216. unsigned char *output, size_t len, size_t *olen )
  217. {
  218. unsigned long timer = mbedtls_timing_hardclock();
  219. ((void) data);
  220. *olen = 0;
  221. if( len < sizeof(unsigned long) )
  222. return( 0 );
  223. memcpy( output, &timer, sizeof(unsigned long) );
  224. *olen = sizeof(unsigned long);
  225. return( 0 );
  226. }
  227. #endif /* MBEDTLS_TIMING_C */
  228. #if defined(MBEDTLS_HAVEGE_C)
  229. int mbedtls_havege_poll( void *data,
  230. unsigned char *output, size_t len, size_t *olen )
  231. {
  232. mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
  233. *olen = 0;
  234. if( mbedtls_havege_random( hs, output, len ) != 0 )
  235. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  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. if( len < use_len )
  251. use_len = len;
  252. memcpy( output, buf, use_len );
  253. *olen = use_len;
  254. return( 0 );
  255. }
  256. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  257. #endif /* MBEDTLS_ENTROPY_C */