entropy_poll.c 6.4 KB

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