entropy_poll.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <wincrypt.h>
  80. int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
  81. size_t *olen )
  82. {
  83. HCRYPTPROV provider;
  84. ((void) data);
  85. *olen = 0;
  86. if( CryptAcquireContext( &provider, NULL, NULL,
  87. PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
  88. {
  89. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  90. }
  91. if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
  92. {
  93. CryptReleaseContext( provider, 0 );
  94. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  95. }
  96. CryptReleaseContext( provider, 0 );
  97. *olen = len;
  98. return( 0 );
  99. }
  100. #else /* _WIN32 && !EFIX64 && !EFI32 */
  101. /*
  102. * Test for Linux getrandom() support.
  103. * Since there is no wrapper in the libc yet, use the generic syscall wrapper
  104. * available in GNU libc and compatible libc's (eg uClibc).
  105. */
  106. #if defined(__linux__) && defined(__GLIBC__)
  107. #include <unistd.h>
  108. #include <sys/syscall.h>
  109. #if defined(SYS_getrandom)
  110. #define HAVE_GETRANDOM
  111. #include <errno.h>
  112. static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
  113. {
  114. /* MemSan cannot understand that the syscall writes to the buffer */
  115. #if defined(__has_feature)
  116. #if __has_feature(memory_sanitizer)
  117. memset( buf, 0, buflen );
  118. #endif
  119. #endif
  120. return( syscall( SYS_getrandom, buf, buflen, flags ) );
  121. }
  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. int ret;
  131. ((void) data);
  132. #if defined(HAVE_GETRANDOM)
  133. ret = getrandom_wrapper( output, len, 0 );
  134. if( ret >= 0 )
  135. {
  136. *olen = ret;
  137. return( 0 );
  138. }
  139. else if( errno != ENOSYS )
  140. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  141. /* Fall through if the system call isn't known. */
  142. #else
  143. ((void) ret);
  144. #endif /* HAVE_GETRANDOM */
  145. *olen = 0;
  146. file = fopen( "/dev/urandom", "rb" );
  147. if( file == NULL )
  148. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  149. read_len = fread( output, 1, len, file );
  150. if( read_len != len )
  151. {
  152. fclose( file );
  153. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  154. }
  155. fclose( file );
  156. *olen = len;
  157. return( 0 );
  158. }
  159. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  160. #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
  161. #if defined(MBEDTLS_TEST_NULL_ENTROPY)
  162. int mbedtls_null_entropy_poll( void *data,
  163. unsigned char *output, size_t len, size_t *olen )
  164. {
  165. ((void) data);
  166. ((void) output);
  167. *olen = 0;
  168. if( len < sizeof(unsigned char) )
  169. return( 0 );
  170. *olen = sizeof(unsigned char);
  171. return( 0 );
  172. }
  173. #endif
  174. #if defined(MBEDTLS_TIMING_C)
  175. int mbedtls_hardclock_poll( void *data,
  176. unsigned char *output, size_t len, size_t *olen )
  177. {
  178. unsigned long timer = mbedtls_timing_hardclock();
  179. ((void) data);
  180. *olen = 0;
  181. if( len < sizeof(unsigned long) )
  182. return( 0 );
  183. memcpy( output, &timer, sizeof(unsigned long) );
  184. *olen = sizeof(unsigned long);
  185. return( 0 );
  186. }
  187. #endif /* MBEDTLS_TIMING_C */
  188. #if defined(MBEDTLS_HAVEGE_C)
  189. int mbedtls_havege_poll( void *data,
  190. unsigned char *output, size_t len, size_t *olen )
  191. {
  192. mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
  193. *olen = 0;
  194. if( mbedtls_havege_random( hs, output, len ) != 0 )
  195. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  196. *olen = len;
  197. return( 0 );
  198. }
  199. #endif /* MBEDTLS_HAVEGE_C */
  200. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  201. int mbedtls_nv_seed_poll( void *data,
  202. unsigned char *output, size_t len, size_t *olen )
  203. {
  204. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  205. size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
  206. ((void) data);
  207. memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
  208. if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
  209. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  210. if( len < use_len )
  211. use_len = len;
  212. memcpy( output, buf, use_len );
  213. *olen = use_len;
  214. return( 0 );
  215. }
  216. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  217. #endif /* MBEDTLS_ENTROPY_C */