entropy_poll.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Platform-specific and custom entropy polling functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if defined(__linux__) && !defined(_GNU_SOURCE)
  47. /* Ensure that syscall() is available even when compiling with -std=c99 */
  48. #define _GNU_SOURCE
  49. #endif
  50. #if !defined(MBEDTLS_CONFIG_FILE)
  51. #include "mbedtls/config.h"
  52. #else
  53. #include MBEDTLS_CONFIG_FILE
  54. #endif
  55. #include <string.h>
  56. #if defined(MBEDTLS_ENTROPY_C)
  57. #include "mbedtls/entropy.h"
  58. #include "mbedtls/entropy_poll.h"
  59. #if defined(MBEDTLS_TIMING_C)
  60. #include "mbedtls/timing.h"
  61. #endif
  62. #if defined(MBEDTLS_HAVEGE_C)
  63. #include "mbedtls/havege.h"
  64. #endif
  65. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  66. #include "mbedtls/platform.h"
  67. #endif
  68. #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
  69. #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
  70. !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
  71. !defined(__HAIKU__)
  72. #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
  73. #endif
  74. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  75. #if !defined(_WIN32_WINNT)
  76. #define _WIN32_WINNT 0x0400
  77. #endif
  78. #include <windows.h>
  79. #include <bcrypt.h>
  80. #if defined(_MSC_VER) && _MSC_VER <= 1600
  81. /* Visual Studio 2010 and earlier issue a warning when both <stdint.h> and
  82. * <intsafe.h> are included, as they redefine a number of <TYPE>_MAX constants.
  83. * These constants are guaranteed to be the same, though, so we suppress the
  84. * warning when including intsafe.h.
  85. */
  86. #pragma warning( push )
  87. #pragma warning( disable : 4005 )
  88. #endif
  89. #include <intsafe.h>
  90. #if defined(_MSC_VER) && _MSC_VER <= 1600
  91. #pragma warning( pop )
  92. #endif
  93. int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
  94. size_t *olen )
  95. {
  96. ULONG len_as_ulong = 0;
  97. ((void) data);
  98. *olen = 0;
  99. /*
  100. * BCryptGenRandom takes ULONG for size, which is smaller than size_t on
  101. * 64-bit Windows platforms. Ensure len's value can be safely converted into
  102. * a ULONG.
  103. */
  104. if ( FAILED( SizeTToULong( len, &len_as_ulong ) ) )
  105. {
  106. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  107. }
  108. if ( !BCRYPT_SUCCESS( BCryptGenRandom( NULL, output, len_as_ulong, BCRYPT_USE_SYSTEM_PREFERRED_RNG ) ) )
  109. {
  110. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  111. }
  112. *olen = len;
  113. return( 0 );
  114. }
  115. #else /* _WIN32 && !EFIX64 && !EFI32 */
  116. /*
  117. * Test for Linux getrandom() support.
  118. * Since there is no wrapper in the libc yet, use the generic syscall wrapper
  119. * available in GNU libc and compatible libc's (eg uClibc).
  120. */
  121. #if defined(__linux__) && defined(__GLIBC__)
  122. #include <unistd.h>
  123. #include <sys/syscall.h>
  124. #if defined(SYS_getrandom)
  125. #define HAVE_GETRANDOM
  126. #include <errno.h>
  127. static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
  128. {
  129. /* MemSan cannot understand that the syscall writes to the buffer */
  130. #if defined(__has_feature)
  131. #if __has_feature(memory_sanitizer)
  132. memset( buf, 0, buflen );
  133. #endif
  134. #endif
  135. return( syscall( SYS_getrandom, buf, buflen, flags ) );
  136. }
  137. #endif /* SYS_getrandom */
  138. #endif /* __linux__ */
  139. #include <stdio.h>
  140. int mbedtls_platform_entropy_poll( void *data,
  141. unsigned char *output, size_t len, size_t *olen )
  142. {
  143. FILE *file;
  144. size_t read_len;
  145. int ret;
  146. ((void) data);
  147. #if defined(HAVE_GETRANDOM)
  148. ret = getrandom_wrapper( output, len, 0 );
  149. if( ret >= 0 )
  150. {
  151. *olen = ret;
  152. return( 0 );
  153. }
  154. else if( errno != ENOSYS )
  155. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  156. /* Fall through if the system call isn't known. */
  157. #else
  158. ((void) ret);
  159. #endif /* HAVE_GETRANDOM */
  160. *olen = 0;
  161. file = fopen( "/dev/urandom", "rb" );
  162. if( file == NULL )
  163. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  164. read_len = fread( output, 1, len, file );
  165. if( read_len != len )
  166. {
  167. fclose( file );
  168. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  169. }
  170. fclose( file );
  171. *olen = len;
  172. return( 0 );
  173. }
  174. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  175. #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
  176. #if defined(MBEDTLS_TEST_NULL_ENTROPY)
  177. int mbedtls_null_entropy_poll( void *data,
  178. unsigned char *output, size_t len, size_t *olen )
  179. {
  180. ((void) data);
  181. ((void) output);
  182. *olen = 0;
  183. if( len < sizeof(unsigned char) )
  184. return( 0 );
  185. *olen = sizeof(unsigned char);
  186. return( 0 );
  187. }
  188. #endif
  189. #if defined(MBEDTLS_TIMING_C)
  190. int mbedtls_hardclock_poll( void *data,
  191. unsigned char *output, size_t len, size_t *olen )
  192. {
  193. unsigned long timer = mbedtls_timing_hardclock();
  194. ((void) data);
  195. *olen = 0;
  196. if( len < sizeof(unsigned long) )
  197. return( 0 );
  198. memcpy( output, &timer, sizeof(unsigned long) );
  199. *olen = sizeof(unsigned long);
  200. return( 0 );
  201. }
  202. #endif /* MBEDTLS_TIMING_C */
  203. #if defined(MBEDTLS_HAVEGE_C)
  204. int mbedtls_havege_poll( void *data,
  205. unsigned char *output, size_t len, size_t *olen )
  206. {
  207. mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
  208. *olen = 0;
  209. if( mbedtls_havege_random( hs, output, len ) != 0 )
  210. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  211. *olen = len;
  212. return( 0 );
  213. }
  214. #endif /* MBEDTLS_HAVEGE_C */
  215. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  216. int mbedtls_nv_seed_poll( void *data,
  217. unsigned char *output, size_t len, size_t *olen )
  218. {
  219. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  220. size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
  221. ((void) data);
  222. memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
  223. if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
  224. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  225. if( len < use_len )
  226. use_len = len;
  227. memcpy( output, buf, use_len );
  228. *olen = use_len;
  229. return( 0 );
  230. }
  231. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  232. #endif /* MBEDTLS_ENTROPY_C */