entropy_poll.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 <wincrypt.h>
  50. int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
  51. size_t *olen )
  52. {
  53. HCRYPTPROV provider;
  54. ((void) data);
  55. *olen = 0;
  56. if( CryptAcquireContext( &provider, NULL, NULL,
  57. PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
  58. {
  59. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  60. }
  61. if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
  62. {
  63. CryptReleaseContext( provider, 0 );
  64. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  65. }
  66. CryptReleaseContext( provider, 0 );
  67. *olen = len;
  68. return( 0 );
  69. }
  70. #else /* _WIN32 && !EFIX64 && !EFI32 */
  71. /*
  72. * Test for Linux getrandom() support.
  73. * Since there is no wrapper in the libc yet, use the generic syscall wrapper
  74. * available in GNU libc and compatible libc's (eg uClibc).
  75. */
  76. #if defined(__linux__) && defined(__GLIBC__)
  77. #include <unistd.h>
  78. #include <sys/syscall.h>
  79. #if defined(SYS_getrandom)
  80. #define HAVE_GETRANDOM
  81. static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
  82. {
  83. /* MemSan cannot understand that the syscall writes to the buffer */
  84. #if defined(__has_feature)
  85. #if __has_feature(memory_sanitizer)
  86. memset( buf, 0, buflen );
  87. #endif
  88. #endif
  89. return( syscall( SYS_getrandom, buf, buflen, flags ) );
  90. }
  91. #include <sys/utsname.h>
  92. /* Check if version is at least 3.17.0 */
  93. static int check_version_3_17_plus( void )
  94. {
  95. int minor;
  96. struct utsname un;
  97. const char *ver;
  98. /* Get version information */
  99. uname(&un);
  100. ver = un.release;
  101. /* Check major version; assume a single digit */
  102. if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' )
  103. return( -1 );
  104. if( ver[0] - '0' > 3 )
  105. return( 0 );
  106. /* Ok, so now we know major == 3, check minor.
  107. * Assume 1 or 2 digits. */
  108. if( ver[2] < '0' || ver[2] > '9' )
  109. return( -1 );
  110. minor = ver[2] - '0';
  111. if( ver[3] >= '0' && ver[3] <= '9' )
  112. minor = 10 * minor + ver[3] - '0';
  113. else if( ver [3] != '.' )
  114. return( -1 );
  115. if( minor < 17 )
  116. return( -1 );
  117. return( 0 );
  118. }
  119. static int has_getrandom = -1;
  120. #endif /* SYS_getrandom */
  121. #endif /* __linux__ */
  122. #ifdef PRINTF_STDLIB
  123. #include <stdio.h>
  124. #endif
  125. #ifdef PRINTF_CUSTOM
  126. #include "tinystdio.h"
  127. #endif
  128. int mbedtls_platform_entropy_poll( void *data,
  129. unsigned char *output, size_t len, size_t *olen )
  130. {
  131. #if 0
  132. FILE *file;
  133. size_t read_len;
  134. ((void) data);
  135. #if defined(HAVE_GETRANDOM)
  136. if( has_getrandom == -1 )
  137. has_getrandom = ( check_version_3_17_plus() == 0 );
  138. if( has_getrandom )
  139. {
  140. int ret;
  141. if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
  142. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  143. *olen = ret;
  144. return( 0 );
  145. }
  146. #endif /* HAVE_GETRANDOM */
  147. *olen = 0;
  148. file = fopen( "/dev/urandom", "rb" );
  149. if( file == NULL )
  150. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  151. read_len = fread( output, 1, len, file );
  152. if( read_len != len )
  153. {
  154. fclose( file );
  155. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  156. }
  157. fclose( file );
  158. *olen = len;
  159. #endif
  160. return( 0 );
  161. }
  162. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  163. #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
  164. #if defined(MBEDTLS_TEST_NULL_ENTROPY)
  165. int mbedtls_null_entropy_poll( void *data,
  166. unsigned char *output, size_t len, size_t *olen )
  167. {
  168. ((void) data);
  169. ((void) output);
  170. *olen = 0;
  171. if( len < sizeof(unsigned char) )
  172. return( 0 );
  173. *olen = sizeof(unsigned char);
  174. return( 0 );
  175. }
  176. #endif
  177. #if defined(MBEDTLS_TIMING_C)
  178. int mbedtls_hardclock_poll( void *data,
  179. unsigned char *output, size_t len, size_t *olen )
  180. {
  181. unsigned long timer = mbedtls_timing_hardclock();
  182. ((void) data);
  183. *olen = 0;
  184. if( len < sizeof(unsigned long) )
  185. return( 0 );
  186. memcpy( output, &timer, sizeof(unsigned long) );
  187. *olen = sizeof(unsigned long);
  188. return( 0 );
  189. }
  190. #endif /* MBEDTLS_TIMING_C */
  191. #if defined(MBEDTLS_HAVEGE_C)
  192. int mbedtls_havege_poll( void *data,
  193. unsigned char *output, size_t len, size_t *olen )
  194. {
  195. mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
  196. *olen = 0;
  197. if( mbedtls_havege_random( hs, output, len ) != 0 )
  198. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  199. *olen = len;
  200. return( 0 );
  201. }
  202. #endif /* MBEDTLS_HAVEGE_C */
  203. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  204. int mbedtls_nv_seed_poll( void *data,
  205. unsigned char *output, size_t len, size_t *olen )
  206. {
  207. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  208. size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
  209. ((void) data);
  210. memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
  211. if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
  212. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  213. if( len < use_len )
  214. use_len = len;
  215. memcpy( output, buf, use_len );
  216. *olen = use_len;
  217. return( 0 );
  218. }
  219. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  220. #endif /* MBEDTLS_ENTROPY_C */