entropy_poll.c.orig 6.7 KB

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