entropy_poll.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Platform-specific and custom entropy polling functions
  3. *
  4. * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
  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. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_ENTROPY_C)
  27. #include "mbedtls/entropy.h"
  28. #include "mbedtls/entropy_poll.h"
  29. #if defined(MBEDTLS_TIMING_C)
  30. #include <string.h>
  31. #include "mbedtls/timing.h"
  32. #endif
  33. #if defined(MBEDTLS_HAVEGE_C)
  34. #include "mbedtls/havege.h"
  35. #endif
  36. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  37. #include "mbedtls/platform.h"
  38. #endif
  39. #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
  40. #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
  41. !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__)
  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__)
  92. #include <unistd.h>
  93. #include <sys/syscall.h>
  94. #if defined(SYS_getrandom)
  95. #define HAVE_GETRANDOM
  96. static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
  97. {
  98. /* MemSan cannot understand that the syscall writes to the buffer */
  99. #if defined(__has_feature)
  100. #if __has_feature(memory_sanitizer)
  101. memset( buf, 0, buflen );
  102. #endif
  103. #endif
  104. return( syscall( SYS_getrandom, buf, buflen, flags ) );
  105. }
  106. #include <sys/utsname.h>
  107. /* Check if version is at least 3.17.0 */
  108. static int check_version_3_17_plus( void )
  109. {
  110. int minor;
  111. struct utsname un;
  112. const char *ver;
  113. /* Get version information */
  114. uname(&un);
  115. ver = un.release;
  116. /* Check major version; assume a single digit */
  117. if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' )
  118. return( -1 );
  119. if( ver[0] - '0' > 3 )
  120. return( 0 );
  121. /* Ok, so now we know major == 3, check minor.
  122. * Assume 1 or 2 digits. */
  123. if( ver[2] < '0' || ver[2] > '9' )
  124. return( -1 );
  125. minor = ver[2] - '0';
  126. if( ver[3] >= '0' && ver[3] <= '9' )
  127. minor = 10 * minor + ver[3] - '0';
  128. else if( ver [3] != '.' )
  129. return( -1 );
  130. if( minor < 17 )
  131. return( -1 );
  132. return( 0 );
  133. }
  134. static int has_getrandom = -1;
  135. #endif /* SYS_getrandom */
  136. #endif /* __linux__ */
  137. #include <stdio.h>
  138. int mbedtls_platform_entropy_poll( void *data,
  139. unsigned char *output, size_t len, size_t *olen )
  140. {
  141. FILE *file;
  142. size_t read_len;
  143. ((void) data);
  144. #if defined(HAVE_GETRANDOM)
  145. if( has_getrandom == -1 )
  146. has_getrandom = ( check_version_3_17_plus() == 0 );
  147. if( has_getrandom )
  148. {
  149. int ret;
  150. if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
  151. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  152. *olen = ret;
  153. return( 0 );
  154. }
  155. #endif /* HAVE_GETRANDOM */
  156. *olen = 0;
  157. file = fopen( "/dev/urandom", "rb" );
  158. if( file == NULL )
  159. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  160. read_len = fread( output, 1, len, file );
  161. if( read_len != len )
  162. {
  163. fclose( file );
  164. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  165. }
  166. fclose( file );
  167. *olen = len;
  168. return( 0 );
  169. }
  170. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  171. #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
  172. #if defined(MBEDTLS_TEST_NULL_ENTROPY)
  173. int mbedtls_null_entropy_poll( void *data,
  174. unsigned char *output, size_t len, size_t *olen )
  175. {
  176. ((void) data);
  177. ((void) output);
  178. *olen = 0;
  179. if( len < sizeof(unsigned char) )
  180. return( 0 );
  181. *olen = sizeof(unsigned char);
  182. return( 0 );
  183. }
  184. #endif
  185. #if defined(MBEDTLS_TIMING_C)
  186. int mbedtls_hardclock_poll( void *data,
  187. unsigned char *output, size_t len, size_t *olen )
  188. {
  189. unsigned long timer = mbedtls_timing_hardclock();
  190. ((void) data);
  191. *olen = 0;
  192. if( len < sizeof(unsigned long) )
  193. return( 0 );
  194. memcpy( output, &timer, sizeof(unsigned long) );
  195. *olen = sizeof(unsigned long);
  196. return( 0 );
  197. }
  198. #endif /* MBEDTLS_TIMING_C */
  199. #if defined(MBEDTLS_HAVEGE_C)
  200. int mbedtls_havege_poll( void *data,
  201. unsigned char *output, size_t len, size_t *olen )
  202. {
  203. mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
  204. *olen = 0;
  205. if( mbedtls_havege_random( hs, output, len ) != 0 )
  206. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  207. *olen = len;
  208. return( 0 );
  209. }
  210. #endif /* MBEDTLS_HAVEGE_C */
  211. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  212. int mbedtls_nv_seed_poll( void *data,
  213. unsigned char *output, size_t len, size_t *olen )
  214. {
  215. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  216. size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
  217. ((void) data);
  218. memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
  219. if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
  220. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  221. if( len < use_len )
  222. use_len = len;
  223. memcpy( output, buf, use_len );
  224. *olen = use_len;
  225. return( 0 );
  226. }
  227. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  228. #endif /* MBEDTLS_ENTROPY_C */