entropy_poll.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Platform-specific and custom entropy polling functions
  3. *
  4. * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #if !defined(MBEDTLS_CONFIG_FILE)
  24. #include "mbedtls/config.h"
  25. #else
  26. #include MBEDTLS_CONFIG_FILE
  27. #endif
  28. #if defined(MBEDTLS_ENTROPY_C)
  29. #include "mbedtls/entropy.h"
  30. #include "mbedtls/entropy_poll.h"
  31. #if defined(MBEDTLS_TIMING_C)
  32. #include <string.h>
  33. #include "mbedtls/timing.h"
  34. #endif
  35. #if defined(MBEDTLS_HAVEGE_C)
  36. #include "mbedtls/havege.h"
  37. #endif
  38. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  39. #include "mbedtls/platform.h"
  40. #endif
  41. #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
  42. #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
  43. !defined(__APPLE__) && !defined(_WIN32)
  44. #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
  45. #endif
  46. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  47. #if !defined(_WIN32_WINNT)
  48. #define _WIN32_WINNT 0x0400
  49. #endif
  50. #include <windows.h>
  51. #include <wincrypt.h>
  52. int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
  53. size_t *olen )
  54. {
  55. HCRYPTPROV provider;
  56. ((void) data);
  57. *olen = 0;
  58. if( CryptAcquireContext( &provider, NULL, NULL,
  59. PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
  60. {
  61. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  62. }
  63. if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
  64. {
  65. CryptReleaseContext( provider, 0 );
  66. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  67. }
  68. CryptReleaseContext( provider, 0 );
  69. *olen = len;
  70. return( 0 );
  71. }
  72. #else /* _WIN32 && !EFIX64 && !EFI32 */
  73. /*
  74. * Test for Linux getrandom() support.
  75. * Since there is no wrapper in the libc yet, use the generic syscall wrapper
  76. * available in GNU libc and compatible libc's (eg uClibc).
  77. */
  78. #if defined(__linux__) && defined(__GLIBC__)
  79. #include <unistd.h>
  80. #include <sys/syscall.h>
  81. #if defined(SYS_getrandom)
  82. #define HAVE_GETRANDOM
  83. static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
  84. {
  85. /* MemSan cannot understand that the syscall writes to the buffer */
  86. #if defined(__has_feature)
  87. #if __has_feature(memory_sanitizer)
  88. memset( buf, 0, buflen );
  89. #endif
  90. #endif
  91. return( syscall( SYS_getrandom, buf, buflen, flags ) );
  92. }
  93. #include <sys/utsname.h>
  94. /* Check if version is at least 3.17.0 */
  95. static int check_version_3_17_plus( void )
  96. {
  97. int minor;
  98. struct utsname un;
  99. const char *ver;
  100. /* Get version information */
  101. uname(&un);
  102. ver = un.release;
  103. /* Check major version; assume a single digit */
  104. if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' )
  105. return( -1 );
  106. if( ver[0] - '0' > 3 )
  107. return( 0 );
  108. /* Ok, so now we know major == 3, check minor.
  109. * Assume 1 or 2 digits. */
  110. if( ver[2] < '0' || ver[2] > '9' )
  111. return( -1 );
  112. minor = ver[2] - '0';
  113. if( ver[3] >= '0' && ver[3] <= '9' )
  114. minor = 10 * minor + ver[3] - '0';
  115. else if( ver [3] != '.' )
  116. return( -1 );
  117. if( minor < 17 )
  118. return( -1 );
  119. return( 0 );
  120. }
  121. static int has_getrandom = -1;
  122. #endif /* SYS_getrandom */
  123. #endif /* __linux__ */
  124. #include <stdio.h>
  125. int mbedtls_platform_entropy_poll( void *data,
  126. unsigned char *output, size_t len, size_t *olen )
  127. {
  128. FILE *file;
  129. size_t read_len;
  130. ((void) data);
  131. #if defined(HAVE_GETRANDOM)
  132. if( has_getrandom == -1 )
  133. has_getrandom = ( check_version_3_17_plus() == 0 );
  134. if( has_getrandom )
  135. {
  136. int ret;
  137. if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
  138. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  139. *olen = ret;
  140. return( 0 );
  141. }
  142. #endif /* HAVE_GETRANDOM */
  143. *olen = 0;
  144. file = fopen( "/dev/urandom", "rb" );
  145. if( file == NULL )
  146. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  147. read_len = fread( output, 1, len, file );
  148. if( read_len != len )
  149. {
  150. fclose( file );
  151. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  152. }
  153. fclose( file );
  154. *olen = len;
  155. return( 0 );
  156. }
  157. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  158. #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
  159. #if defined(MBEDTLS_TEST_NULL_ENTROPY)
  160. int mbedtls_null_entropy_poll( void *data,
  161. unsigned char *output, size_t len, size_t *olen )
  162. {
  163. ((void) data);
  164. ((void) output);
  165. *olen = 0;
  166. if( len < sizeof(unsigned char) )
  167. return( 0 );
  168. *olen = sizeof(unsigned char);
  169. return( 0 );
  170. }
  171. #endif
  172. #if defined(MBEDTLS_TIMING_C)
  173. int mbedtls_hardclock_poll( void *data,
  174. unsigned char *output, size_t len, size_t *olen )
  175. {
  176. unsigned long timer = mbedtls_timing_hardclock();
  177. ((void) data);
  178. *olen = 0;
  179. if( len < sizeof(unsigned long) )
  180. return( 0 );
  181. memcpy( output, &timer, sizeof(unsigned long) );
  182. *olen = sizeof(unsigned long);
  183. return( 0 );
  184. }
  185. #endif /* MBEDTLS_TIMING_C */
  186. #if defined(MBEDTLS_HAVEGE_C)
  187. int mbedtls_havege_poll( void *data,
  188. unsigned char *output, size_t len, size_t *olen )
  189. {
  190. mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
  191. *olen = 0;
  192. if( mbedtls_havege_random( hs, output, len ) != 0 )
  193. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  194. *olen = len;
  195. return( 0 );
  196. }
  197. #endif /* MBEDTLS_HAVEGE_C */
  198. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  199. int mbedtls_nv_seed_poll( void *data,
  200. unsigned char *output, size_t len, size_t *olen )
  201. {
  202. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  203. size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
  204. ((void) data);
  205. memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
  206. if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
  207. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  208. if( len < use_len )
  209. use_len = len;
  210. memcpy( output, buf, use_len );
  211. *olen = use_len;
  212. return( 0 );
  213. }
  214. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  215. #endif /* MBEDTLS_ENTROPY_C */