entropy_poll.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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)
  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 _MSC_VER <= 1600
  51. /* Visual Studio 2010 and earlier issue a warning when both <stdint.h> and <intsafe.h> are included, as they
  52. * redefine a number of <TYPE>_MAX constants. These constants are guaranteed to be the same, though, so
  53. * we suppress the warning when including intsafe.h.
  54. */
  55. #pragma warning( push )
  56. #pragma warning( disable : 4005 )
  57. #endif
  58. #include <intsafe.h>
  59. #if _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 64-bit platforms.
  70. * Ensure len's value can be safely converted into a ULONG.
  71. */
  72. if ( FAILED( SizeTToULong( len, &len_as_ulong ) ) )
  73. {
  74. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  75. }
  76. if ( !BCRYPT_SUCCESS( BCryptGenRandom( NULL, output, len_as_ulong, BCRYPT_USE_SYSTEM_PREFERRED_RNG ) ) )
  77. {
  78. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  79. }
  80. *olen = len;
  81. return( 0 );
  82. }
  83. #else /* _WIN32 && !EFIX64 && !EFI32 */
  84. /*
  85. * Test for Linux getrandom() support.
  86. * Since there is no wrapper in the libc yet, use the generic syscall wrapper
  87. * available in GNU libc and compatible libc's (eg uClibc).
  88. */
  89. #if defined(__linux__) && defined(__GLIBC__)
  90. #include <unistd.h>
  91. #include <sys/syscall.h>
  92. #if defined(SYS_getrandom)
  93. #define HAVE_GETRANDOM
  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. #include <sys/utsname.h>
  105. /* Check if version is at least 3.17.0 */
  106. static int check_version_3_17_plus( void )
  107. {
  108. int minor;
  109. struct utsname un;
  110. const char *ver;
  111. /* Get version information */
  112. uname(&un);
  113. ver = un.release;
  114. /* Check major version; assume a single digit */
  115. if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' )
  116. return( -1 );
  117. if( ver[0] - '0' > 3 )
  118. return( 0 );
  119. /* Ok, so now we know major == 3, check minor.
  120. * Assume 1 or 2 digits. */
  121. if( ver[2] < '0' || ver[2] > '9' )
  122. return( -1 );
  123. minor = ver[2] - '0';
  124. if( ver[3] >= '0' && ver[3] <= '9' )
  125. minor = 10 * minor + ver[3] - '0';
  126. else if( ver [3] != '.' )
  127. return( -1 );
  128. if( minor < 17 )
  129. return( -1 );
  130. return( 0 );
  131. }
  132. static int has_getrandom = -1;
  133. #endif /* SYS_getrandom */
  134. #endif /* __linux__ */
  135. #include <stdio.h>
  136. int mbedtls_platform_entropy_poll( void *data,
  137. unsigned char *output, size_t len, size_t *olen )
  138. {
  139. FILE *file;
  140. size_t read_len;
  141. ((void) data);
  142. #if defined(HAVE_GETRANDOM)
  143. if( has_getrandom == -1 )
  144. has_getrandom = ( check_version_3_17_plus() == 0 );
  145. if( has_getrandom )
  146. {
  147. int ret;
  148. if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
  149. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  150. *olen = ret;
  151. return( 0 );
  152. }
  153. #endif /* HAVE_GETRANDOM */
  154. *olen = 0;
  155. file = fopen( "/dev/urandom", "rb" );
  156. if( file == NULL )
  157. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  158. read_len = fread( output, 1, len, file );
  159. if( read_len != len )
  160. {
  161. fclose( file );
  162. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  163. }
  164. fclose( file );
  165. *olen = len;
  166. return( 0 );
  167. }
  168. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  169. #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
  170. #if defined(MBEDTLS_TEST_NULL_ENTROPY)
  171. int mbedtls_null_entropy_poll( void *data,
  172. unsigned char *output, size_t len, size_t *olen )
  173. {
  174. ((void) data);
  175. ((void) output);
  176. *olen = 0;
  177. if( len < sizeof(unsigned char) )
  178. return( 0 );
  179. *olen = sizeof(unsigned char);
  180. return( 0 );
  181. }
  182. #endif
  183. #if defined(MBEDTLS_TIMING_C)
  184. int mbedtls_hardclock_poll( void *data,
  185. unsigned char *output, size_t len, size_t *olen )
  186. {
  187. unsigned long timer = mbedtls_timing_hardclock();
  188. ((void) data);
  189. *olen = 0;
  190. if( len < sizeof(unsigned long) )
  191. return( 0 );
  192. memcpy( output, &timer, sizeof(unsigned long) );
  193. *olen = sizeof(unsigned long);
  194. return( 0 );
  195. }
  196. #endif /* MBEDTLS_TIMING_C */
  197. #if defined(MBEDTLS_HAVEGE_C)
  198. int mbedtls_havege_poll( void *data,
  199. unsigned char *output, size_t len, size_t *olen )
  200. {
  201. mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
  202. *olen = 0;
  203. if( mbedtls_havege_random( hs, output, len ) != 0 )
  204. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  205. *olen = len;
  206. return( 0 );
  207. }
  208. #endif /* MBEDTLS_HAVEGE_C */
  209. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  210. int mbedtls_nv_seed_poll( void *data,
  211. unsigned char *output, size_t len, size_t *olen )
  212. {
  213. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  214. size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
  215. ((void) data);
  216. memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
  217. if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
  218. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  219. if( len < use_len )
  220. use_len = len;
  221. memcpy( output, buf, use_len );
  222. *olen = use_len;
  223. return( 0 );
  224. }
  225. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  226. #endif /* MBEDTLS_ENTROPY_C */