entropy_poll.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 <bcrypt.h>
  55. #if defined(_MSC_VER) && _MSC_VER <= 1600
  56. /* Visual Studio 2010 and earlier issue a warning when both <stdint.h> and
  57. * <intsafe.h> are included, as they redefine a number of <TYPE>_MAX constants.
  58. * These constants are guaranteed to be the same, though, so we suppress the
  59. * warning when including intsafe.h.
  60. */
  61. #pragma warning( push )
  62. #pragma warning( disable : 4005 )
  63. #endif
  64. #include <intsafe.h>
  65. #if defined(_MSC_VER) && _MSC_VER <= 1600
  66. #pragma warning( pop )
  67. #endif
  68. int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
  69. size_t *olen )
  70. {
  71. ULONG len_as_ulong = 0;
  72. ((void) data);
  73. *olen = 0;
  74. /*
  75. * BCryptGenRandom takes ULONG for size, which is smaller than size_t on
  76. * 64-bit Windows platforms. Ensure len's value can be safely converted into
  77. * a ULONG.
  78. */
  79. if ( FAILED( SizeTToULong( len, &len_as_ulong ) ) )
  80. {
  81. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  82. }
  83. if ( !BCRYPT_SUCCESS( BCryptGenRandom( NULL, output, len_as_ulong, BCRYPT_USE_SYSTEM_PREFERRED_RNG ) ) )
  84. {
  85. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  86. }
  87. *olen = len;
  88. return( 0 );
  89. }
  90. #else /* _WIN32 && !EFIX64 && !EFI32 */
  91. /*
  92. * Test for Linux getrandom() support.
  93. * Since there is no wrapper in the libc yet, use the generic syscall wrapper
  94. * available in GNU libc and compatible libc's (eg uClibc).
  95. */
  96. #if defined(__linux__) && defined(__GLIBC__)
  97. #include <unistd.h>
  98. #include <sys/syscall.h>
  99. #if defined(SYS_getrandom)
  100. #define HAVE_GETRANDOM
  101. #include <errno.h>
  102. static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
  103. {
  104. /* MemSan cannot understand that the syscall writes to the buffer */
  105. #if defined(__has_feature)
  106. #if __has_feature(memory_sanitizer)
  107. memset( buf, 0, buflen );
  108. #endif
  109. #endif
  110. return( syscall( SYS_getrandom, buf, buflen, flags ) );
  111. }
  112. #endif /* SYS_getrandom */
  113. #endif /* __linux__ */
  114. #include <stdio.h>
  115. int mbedtls_platform_entropy_poll( void *data,
  116. unsigned char *output, size_t len, size_t *olen )
  117. {
  118. FILE *file;
  119. size_t read_len;
  120. int ret;
  121. ((void) data);
  122. #if defined(HAVE_GETRANDOM)
  123. ret = getrandom_wrapper( output, len, 0 );
  124. if( ret >= 0 )
  125. {
  126. *olen = ret;
  127. return( 0 );
  128. }
  129. else if( errno != ENOSYS )
  130. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  131. /* Fall through if the system call isn't known. */
  132. #else
  133. ((void) ret);
  134. #endif /* HAVE_GETRANDOM */
  135. *olen = 0;
  136. file = fopen( "/dev/urandom", "rb" );
  137. if( file == NULL )
  138. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  139. read_len = fread( output, 1, len, file );
  140. if( read_len != len )
  141. {
  142. fclose( file );
  143. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  144. }
  145. fclose( file );
  146. *olen = len;
  147. return( 0 );
  148. }
  149. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  150. #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
  151. #if defined(MBEDTLS_TEST_NULL_ENTROPY)
  152. int mbedtls_null_entropy_poll( void *data,
  153. unsigned char *output, size_t len, size_t *olen )
  154. {
  155. ((void) data);
  156. ((void) output);
  157. *olen = 0;
  158. if( len < sizeof(unsigned char) )
  159. return( 0 );
  160. *olen = sizeof(unsigned char);
  161. return( 0 );
  162. }
  163. #endif
  164. #if defined(MBEDTLS_TIMING_C)
  165. int mbedtls_hardclock_poll( void *data,
  166. unsigned char *output, size_t len, size_t *olen )
  167. {
  168. unsigned long timer = mbedtls_timing_hardclock();
  169. ((void) data);
  170. *olen = 0;
  171. if( len < sizeof(unsigned long) )
  172. return( 0 );
  173. memcpy( output, &timer, sizeof(unsigned long) );
  174. *olen = sizeof(unsigned long);
  175. return( 0 );
  176. }
  177. #endif /* MBEDTLS_TIMING_C */
  178. #if defined(MBEDTLS_HAVEGE_C)
  179. int mbedtls_havege_poll( void *data,
  180. unsigned char *output, size_t len, size_t *olen )
  181. {
  182. mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
  183. *olen = 0;
  184. if( mbedtls_havege_random( hs, output, len ) != 0 )
  185. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  186. *olen = len;
  187. return( 0 );
  188. }
  189. #endif /* MBEDTLS_HAVEGE_C */
  190. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  191. int mbedtls_nv_seed_poll( void *data,
  192. unsigned char *output, size_t len, size_t *olen )
  193. {
  194. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  195. size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
  196. ((void) data);
  197. memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
  198. if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
  199. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  200. if( len < use_len )
  201. use_len = len;
  202. memcpy( output, buf, use_len );
  203. *olen = use_len;
  204. return( 0 );
  205. }
  206. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  207. #endif /* MBEDTLS_ENTROPY_C */