entropy_poll.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Platform-specific and custom entropy polling functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  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. #if defined(__linux__) && !defined(_GNU_SOURCE)
  20. /* Ensure that syscall() is available even when compiling with -std=c99 */
  21. #define _GNU_SOURCE
  22. #endif
  23. #include "common.h"
  24. #include <string.h>
  25. #if defined(MBEDTLS_ENTROPY_C)
  26. #include "mbedtls/entropy.h"
  27. #include "mbedtls/entropy_poll.h"
  28. #include "mbedtls/error.h"
  29. #if defined(MBEDTLS_TIMING_C)
  30. #include "mbedtls/timing.h"
  31. #endif
  32. #if defined(MBEDTLS_HAVEGE_C)
  33. #include "mbedtls/havege.h"
  34. #endif
  35. #include "mbedtls/platform.h"
  36. #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
  37. #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
  38. !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
  39. !defined(__HAIKU__) && !defined(__midipix__)
  40. #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
  41. #endif
  42. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  43. #if !defined(_WIN32_WINNT)
  44. #define _WIN32_WINNT 0x0400
  45. #endif
  46. #include <windows.h>
  47. #include <wincrypt.h>
  48. int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
  49. size_t *olen )
  50. {
  51. HCRYPTPROV provider;
  52. ((void) data);
  53. *olen = 0;
  54. if( CryptAcquireContext( &provider, NULL, NULL,
  55. PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
  56. {
  57. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  58. }
  59. if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
  60. {
  61. CryptReleaseContext( provider, 0 );
  62. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  63. }
  64. CryptReleaseContext( provider, 0 );
  65. *olen = len;
  66. return( 0 );
  67. }
  68. #else /* _WIN32 && !EFIX64 && !EFI32 */
  69. /*
  70. * Test for Linux getrandom() support.
  71. * Since there is no wrapper in the libc yet, use the generic syscall wrapper
  72. * available in GNU libc and compatible libc's (eg uClibc).
  73. */
  74. #if ((defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__))
  75. #include <unistd.h>
  76. #include <sys/syscall.h>
  77. #if defined(SYS_getrandom)
  78. #define HAVE_GETRANDOM
  79. #include <errno.h>
  80. static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
  81. {
  82. /* MemSan cannot understand that the syscall writes to the buffer */
  83. #if defined(__has_feature)
  84. #if __has_feature(memory_sanitizer)
  85. memset( buf, 0, buflen );
  86. #endif
  87. #endif
  88. return( syscall( SYS_getrandom, buf, buflen, flags ) );
  89. }
  90. #endif /* SYS_getrandom */
  91. #endif /* __linux__ || __midipix__ */
  92. #if defined(__FreeBSD__) || defined(__DragonFly__)
  93. #include <sys/param.h>
  94. #if (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || \
  95. (defined(__DragonFly__) && __DragonFly_version >= 500700)
  96. #include <errno.h>
  97. #include <sys/random.h>
  98. #define HAVE_GETRANDOM
  99. static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
  100. {
  101. return getrandom( buf, buflen, flags );
  102. }
  103. #endif /* (__FreeBSD__ && __FreeBSD_version >= 1200000) ||
  104. (__DragonFly__ && __DragonFly_version >= 500700) */
  105. #endif /* __FreeBSD__ || __DragonFly__ */
  106. /*
  107. * Some BSD systems provide KERN_ARND.
  108. * This is equivalent to reading from /dev/urandom, only it doesn't require an
  109. * open file descriptor, and provides up to 256 bytes per call (basically the
  110. * same as getentropy(), but with a longer history).
  111. *
  112. * Documentation: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7
  113. */
  114. #if (defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(HAVE_GETRANDOM)
  115. #include <sys/param.h>
  116. #include <sys/sysctl.h>
  117. #if defined(KERN_ARND)
  118. #define HAVE_SYSCTL_ARND
  119. static int sysctl_arnd_wrapper( unsigned char *buf, size_t buflen )
  120. {
  121. int name[2];
  122. size_t len;
  123. name[0] = CTL_KERN;
  124. name[1] = KERN_ARND;
  125. while( buflen > 0 )
  126. {
  127. len = buflen > 256 ? 256 : buflen;
  128. if( sysctl(name, 2, buf, &len, NULL, 0) == -1 )
  129. return( -1 );
  130. buflen -= len;
  131. buf += len;
  132. }
  133. return( 0 );
  134. }
  135. #endif /* KERN_ARND */
  136. #endif /* __FreeBSD__ || __NetBSD__ */
  137. #include <stdio.h>
  138. int mbedtls_platform_entropy_poll( void *data,
  139. unsigned char *output, size_t len, size_t *olen )
  140. {
  141. FILE *file;
  142. size_t read_len;
  143. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  144. ((void) data);
  145. #if defined(HAVE_GETRANDOM)
  146. ret = getrandom_wrapper( output, len, 0 );
  147. if( ret >= 0 )
  148. {
  149. *olen = ret;
  150. return( 0 );
  151. }
  152. else if( errno != ENOSYS )
  153. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  154. /* Fall through if the system call isn't known. */
  155. #else
  156. ((void) ret);
  157. #endif /* HAVE_GETRANDOM */
  158. #if defined(HAVE_SYSCTL_ARND)
  159. ((void) file);
  160. ((void) read_len);
  161. if( sysctl_arnd_wrapper( output, len ) == -1 )
  162. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  163. *olen = len;
  164. return( 0 );
  165. #else
  166. *olen = 0;
  167. file = fopen( "/dev/urandom", "rb" );
  168. if( file == NULL )
  169. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  170. read_len = fread( output, 1, len, file );
  171. if( read_len != len )
  172. {
  173. fclose( file );
  174. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  175. }
  176. fclose( file );
  177. *olen = len;
  178. return( 0 );
  179. #endif /* HAVE_SYSCTL_ARND */
  180. }
  181. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  182. #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
  183. #if defined(MBEDTLS_TEST_NULL_ENTROPY)
  184. int mbedtls_null_entropy_poll( void *data,
  185. unsigned char *output, size_t len, size_t *olen )
  186. {
  187. ((void) data);
  188. ((void) output);
  189. *olen = 0;
  190. if( len < sizeof(unsigned char) )
  191. return( 0 );
  192. output[0] = 0;
  193. *olen = sizeof(unsigned char);
  194. return( 0 );
  195. }
  196. #endif
  197. #if defined(MBEDTLS_TIMING_C)
  198. int mbedtls_hardclock_poll( void *data,
  199. unsigned char *output, size_t len, size_t *olen )
  200. {
  201. unsigned long timer = mbedtls_timing_hardclock();
  202. ((void) data);
  203. *olen = 0;
  204. if( len < sizeof(unsigned long) )
  205. return( 0 );
  206. memcpy( output, &timer, sizeof(unsigned long) );
  207. *olen = sizeof(unsigned long);
  208. return( 0 );
  209. }
  210. #endif /* MBEDTLS_TIMING_C */
  211. #if defined(MBEDTLS_HAVEGE_C)
  212. int mbedtls_havege_poll( void *data,
  213. unsigned char *output, size_t len, size_t *olen )
  214. {
  215. mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
  216. *olen = 0;
  217. if( mbedtls_havege_random( hs, output, len ) != 0 )
  218. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  219. *olen = len;
  220. return( 0 );
  221. }
  222. #endif /* MBEDTLS_HAVEGE_C */
  223. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  224. int mbedtls_nv_seed_poll( void *data,
  225. unsigned char *output, size_t len, size_t *olen )
  226. {
  227. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  228. size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
  229. ((void) data);
  230. memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
  231. if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
  232. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  233. if( len < use_len )
  234. use_len = len;
  235. memcpy( output, buf, use_len );
  236. *olen = use_len;
  237. return( 0 );
  238. }
  239. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  240. #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
  241. int luat_crypto_trng(char* buff, size_t len);
  242. int mbedtls_hardware_poll( void *data,
  243. unsigned char *output, size_t len, size_t *olen ){
  244. if (data != NULL)
  245. data = NULL;
  246. *olen = 0;
  247. int rnd = luat_crypto_trng((char *)output, len);
  248. if (rnd != 0)
  249. {
  250. return -1;
  251. }
  252. *olen = len;
  253. return( 0 );
  254. }
  255. #endif
  256. #endif /* MBEDTLS_ENTROPY_C */