entropy.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. */
  4. #if !defined(MBEDTLS_CONFIG_FILE)
  5. #include "mbedtls/config.h"
  6. #else
  7. #include MBEDTLS_CONFIG_FILE
  8. #endif
  9. #if defined(MBEDTLS_ENTROPY_C)
  10. #if defined(MBEDTLS_TEST_NULL_ENTROPY)
  11. #warning "**** WARNING! MBEDTLS_TEST_NULL_ENTROPY defined! "
  12. #warning "**** THIS BUILD HAS NO DEFINED ENTROPY SOURCES "
  13. #warning "**** THIS BUILD IS *NOT* SUITABLE FOR PRODUCTION USE "
  14. #endif
  15. #include "mbedtls/entropy.h"
  16. #include "mbedtls/entropy_poll.h"
  17. #include <string.h>
  18. #if defined(MBEDTLS_FS_IO)
  19. #include <stdio.h>
  20. #endif
  21. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  22. #include "mbedtls/platform.h"
  23. #endif
  24. #if defined(MBEDTLS_SELF_TEST)
  25. #if defined(MBEDTLS_PLATFORM_C)
  26. #include "mbedtls/platform.h"
  27. #else
  28. #include <stdio.h>
  29. #include "mbedtls/debug.h"
  30. #define mbedtls_printf tls_info
  31. #endif /* MBEDTLS_PLATFORM_C */
  32. #endif /* MBEDTLS_SELF_TEST */
  33. #if defined(MBEDTLS_HAVEGE_C)
  34. #include "mbedtls/havege.h"
  35. #endif
  36. /* Implementation that should never be optimized out by the compiler */
  37. static void mbedtls_zeroize( void *v, size_t n ) {
  38. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  39. }
  40. #define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
  41. void mbedtls_entropy_init( mbedtls_entropy_context *ctx )
  42. {
  43. memset( ctx, 0, sizeof(mbedtls_entropy_context) );
  44. #if defined(MBEDTLS_THREADING_C)
  45. mbedtls_mutex_init( &ctx->mutex );
  46. #endif
  47. #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
  48. mbedtls_sha512_starts( &ctx->accumulator, 0 );
  49. #else
  50. mbedtls_sha256_starts( &ctx->accumulator, 0 );
  51. #endif
  52. #if defined(MBEDTLS_HAVEGE_C)
  53. mbedtls_havege_init( &ctx->havege_data );
  54. #endif
  55. #if defined(MBEDTLS_TEST_NULL_ENTROPY)
  56. mbedtls_entropy_add_source( ctx, mbedtls_null_entropy_poll, NULL,
  57. 1, MBEDTLS_ENTROPY_SOURCE_STRONG );
  58. #endif
  59. #if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
  60. #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
  61. mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL,
  62. MBEDTLS_ENTROPY_MIN_PLATFORM,
  63. MBEDTLS_ENTROPY_SOURCE_STRONG );
  64. #endif
  65. #if defined(MBEDTLS_TIMING_C)
  66. mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL,
  67. MBEDTLS_ENTROPY_MIN_HARDCLOCK,
  68. MBEDTLS_ENTROPY_SOURCE_WEAK );
  69. #endif
  70. #if defined(MBEDTLS_HAVEGE_C)
  71. mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data,
  72. MBEDTLS_ENTROPY_MIN_HAVEGE,
  73. MBEDTLS_ENTROPY_SOURCE_STRONG );
  74. #endif
  75. #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
  76. mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL,
  77. MBEDTLS_ENTROPY_MIN_HARDWARE,
  78. MBEDTLS_ENTROPY_SOURCE_STRONG );
  79. #endif
  80. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  81. mbedtls_entropy_add_source( ctx, mbedtls_nv_seed_poll, NULL,
  82. MBEDTLS_ENTROPY_BLOCK_SIZE,
  83. MBEDTLS_ENTROPY_SOURCE_STRONG );
  84. #endif
  85. #endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
  86. }
  87. void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
  88. {
  89. #if defined(MBEDTLS_HAVEGE_C)
  90. mbedtls_havege_free( &ctx->havege_data );
  91. #endif
  92. #if defined(MBEDTLS_THREADING_C)
  93. mbedtls_mutex_free( &ctx->mutex );
  94. #endif
  95. mbedtls_zeroize( ctx, sizeof( mbedtls_entropy_context ) );
  96. }
  97. int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
  98. mbedtls_entropy_f_source_ptr f_source, void *p_source,
  99. size_t threshold, int strong )
  100. {
  101. int index, ret = 0;
  102. #if defined(MBEDTLS_THREADING_C)
  103. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  104. return( ret );
  105. #endif
  106. index = ctx->source_count;
  107. if( index >= MBEDTLS_ENTROPY_MAX_SOURCES )
  108. {
  109. ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
  110. goto exit;
  111. }
  112. ctx->source[index].f_source = f_source;
  113. ctx->source[index].p_source = p_source;
  114. ctx->source[index].threshold = threshold;
  115. ctx->source[index].strong = strong;
  116. ctx->source_count++;
  117. exit:
  118. #if defined(MBEDTLS_THREADING_C)
  119. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  120. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  121. #endif
  122. return( ret );
  123. }
  124. /*
  125. * Entropy accumulator update
  126. */
  127. static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
  128. const unsigned char *data, size_t len )
  129. {
  130. unsigned char header[2];
  131. unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
  132. size_t use_len = len;
  133. const unsigned char *p = data;
  134. if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
  135. {
  136. #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
  137. mbedtls_sha512( data, len, tmp, 0 );
  138. #else
  139. mbedtls_sha256( data, len, tmp, 0 );
  140. #endif
  141. p = tmp;
  142. use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
  143. }
  144. header[0] = source_id;
  145. header[1] = use_len & 0xFF;
  146. #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
  147. mbedtls_sha512_update( &ctx->accumulator, header, 2 );
  148. mbedtls_sha512_update( &ctx->accumulator, p, use_len );
  149. #else
  150. mbedtls_sha256_update( &ctx->accumulator, header, 2 );
  151. mbedtls_sha256_update( &ctx->accumulator, p, use_len );
  152. #endif
  153. return( 0 );
  154. }
  155. int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
  156. const unsigned char *data, size_t len )
  157. {
  158. int ret;
  159. #if defined(MBEDTLS_THREADING_C)
  160. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  161. return( ret );
  162. #endif
  163. ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len );
  164. #if defined(MBEDTLS_THREADING_C)
  165. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  166. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  167. #endif
  168. return( ret );
  169. }
  170. /*
  171. * Run through the different sources to add entropy to our accumulator
  172. */
  173. static int entropy_gather_internal( mbedtls_entropy_context *ctx )
  174. {
  175. int ret, i, have_one_strong = 0;
  176. unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
  177. size_t olen;
  178. if( ctx->source_count == 0 )
  179. return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
  180. /*
  181. * Run through our entropy sources
  182. */
  183. for( i = 0; i < ctx->source_count; i++ )
  184. {
  185. if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
  186. have_one_strong = 1;
  187. olen = 0;
  188. if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
  189. buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
  190. {
  191. return( ret );
  192. }
  193. /*
  194. * Add if we actually gathered something
  195. */
  196. if( olen > 0 )
  197. {
  198. entropy_update( ctx, (unsigned char) i, buf, olen );
  199. ctx->source[i].size += olen;
  200. }
  201. }
  202. if( have_one_strong == 0 )
  203. return( MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE );
  204. return( 0 );
  205. }
  206. /*
  207. * Thread-safe wrapper for entropy_gather_internal()
  208. */
  209. int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
  210. {
  211. int ret;
  212. #if defined(MBEDTLS_THREADING_C)
  213. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  214. return( ret );
  215. #endif
  216. ret = entropy_gather_internal( ctx );
  217. #if defined(MBEDTLS_THREADING_C)
  218. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  219. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  220. #endif
  221. return( ret );
  222. }
  223. int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
  224. {
  225. int ret, count = 0, i, done;
  226. mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
  227. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  228. if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
  229. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  230. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  231. /* Update the NV entropy seed before generating any entropy for outside
  232. * use.
  233. */
  234. if( ctx->initial_entropy_run == 0 )
  235. {
  236. ctx->initial_entropy_run = 1;
  237. if( ( ret = mbedtls_entropy_update_nv_seed( ctx ) ) != 0 )
  238. return( ret );
  239. }
  240. #endif
  241. #if defined(MBEDTLS_THREADING_C)
  242. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  243. return( ret );
  244. #endif
  245. /*
  246. * Always gather extra entropy before a call
  247. */
  248. do
  249. {
  250. if( count++ > ENTROPY_MAX_LOOP )
  251. {
  252. ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  253. goto exit;
  254. }
  255. if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
  256. goto exit;
  257. done = 1;
  258. for( i = 0; i < ctx->source_count; i++ )
  259. if( ctx->source[i].size < ctx->source[i].threshold )
  260. done = 0;
  261. }
  262. while( ! done );
  263. memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
  264. #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
  265. mbedtls_sha512_finish( &ctx->accumulator, buf );
  266. /*
  267. * Reset accumulator and counters and recycle existing entropy
  268. */
  269. memset( &ctx->accumulator, 0, sizeof( mbedtls_sha512_context ) );
  270. mbedtls_sha512_starts( &ctx->accumulator, 0 );
  271. mbedtls_sha512_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
  272. /*
  273. * Perform second SHA-512 on entropy
  274. */
  275. mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
  276. #else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
  277. mbedtls_sha256_finish( &ctx->accumulator, buf );
  278. /*
  279. * Reset accumulator and counters and recycle existing entropy
  280. */
  281. memset( &ctx->accumulator, 0, sizeof( mbedtls_sha256_context ) );
  282. mbedtls_sha256_starts( &ctx->accumulator, 0 );
  283. mbedtls_sha256_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
  284. /*
  285. * Perform second SHA-256 on entropy
  286. */
  287. mbedtls_sha256( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
  288. #endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
  289. for( i = 0; i < ctx->source_count; i++ )
  290. ctx->source[i].size = 0;
  291. memcpy( output, buf, len );
  292. ret = 0;
  293. exit:
  294. #if defined(MBEDTLS_THREADING_C)
  295. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  296. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  297. #endif
  298. return( ret );
  299. }
  300. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  301. int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx )
  302. {
  303. int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
  304. unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
  305. /* Read new seed and write it to NV */
  306. if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
  307. return( ret );
  308. if( mbedtls_nv_seed_write( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
  309. return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
  310. /* Manually update the remaining stream with a separator value to diverge */
  311. memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
  312. mbedtls_entropy_update_manual( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
  313. return( 0 );
  314. }
  315. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  316. #if defined(MBEDTLS_FS_IO)
  317. int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
  318. {
  319. int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
  320. FILE *f;
  321. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  322. if( ( f = fopen( path, "wb" ) ) == NULL )
  323. return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
  324. if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
  325. goto exit;
  326. if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
  327. {
  328. ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
  329. goto exit;
  330. }
  331. ret = 0;
  332. exit:
  333. fclose( f );
  334. return( ret );
  335. }
  336. int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
  337. {
  338. FILE *f;
  339. size_t n;
  340. unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
  341. if( ( f = fopen( path, "rb" ) ) == NULL )
  342. return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
  343. fseek( f, 0, SEEK_END );
  344. n = (size_t) ftell( f );
  345. fseek( f, 0, SEEK_SET );
  346. if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
  347. n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
  348. if( fread( buf, 1, n, f ) != n )
  349. {
  350. fclose( f );
  351. return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
  352. }
  353. fclose( f );
  354. mbedtls_entropy_update_manual( ctx, buf, n );
  355. return( mbedtls_entropy_write_seed_file( ctx, path ) );
  356. }
  357. #endif /* MBEDTLS_FS_IO */
  358. #if defined(MBEDTLS_SELF_TEST)
  359. #if !defined(MBEDTLS_TEST_NULL_ENTROPY)
  360. /*
  361. * Dummy source function
  362. */
  363. static int entropy_dummy_source( void *data, unsigned char *output,
  364. size_t len, size_t *olen )
  365. {
  366. ((void) data);
  367. memset( output, 0x2a, len );
  368. *olen = len;
  369. return( 0 );
  370. }
  371. #endif /* !MBEDTLS_TEST_NULL_ENTROPY */
  372. #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
  373. static int mbedtls_entropy_source_self_test_gather( unsigned char *buf, size_t buf_len )
  374. {
  375. int ret = 0;
  376. size_t entropy_len = 0;
  377. size_t olen = 0;
  378. size_t attempts = buf_len;
  379. while( attempts > 0 && entropy_len < buf_len )
  380. {
  381. if( ( ret = mbedtls_hardware_poll( NULL, buf + entropy_len,
  382. buf_len - entropy_len, &olen ) ) != 0 )
  383. return( ret );
  384. entropy_len += olen;
  385. attempts--;
  386. }
  387. if( entropy_len < buf_len )
  388. {
  389. ret = 1;
  390. }
  391. return( ret );
  392. }
  393. static int mbedtls_entropy_source_self_test_check_bits( const unsigned char *buf,
  394. size_t buf_len )
  395. {
  396. unsigned char set= 0xFF;
  397. unsigned char unset = 0x00;
  398. size_t i;
  399. for( i = 0; i < buf_len; i++ )
  400. {
  401. set &= buf[i];
  402. unset |= buf[i];
  403. }
  404. return( set == 0xFF || unset == 0x00 );
  405. }
  406. /*
  407. * A test to ensure hat the entropy sources are functioning correctly
  408. * and there is no obvious failure. The test performs the following checks:
  409. * - The entropy source is not providing only 0s (all bits unset) or 1s (all
  410. * bits set).
  411. * - The entropy source is not providing values in a pattern. Because the
  412. * hardware could be providing data in an arbitrary length, this check polls
  413. * the hardware entropy source twice and compares the result to ensure they
  414. * are not equal.
  415. * - The error code returned by the entropy source is not an error.
  416. */
  417. int mbedtls_entropy_source_self_test( int verbose )
  418. {
  419. int ret = 0;
  420. unsigned char buf0[2 * sizeof( unsigned long long int )];
  421. unsigned char buf1[2 * sizeof( unsigned long long int )];
  422. if( verbose != 0 )
  423. mbedtls_printf( " ENTROPY_BIAS test: " );
  424. memset( buf0, 0x00, sizeof( buf0 ) );
  425. memset( buf1, 0x00, sizeof( buf1 ) );
  426. if( ( ret = mbedtls_entropy_source_self_test_gather( buf0, sizeof( buf0 ) ) ) != 0 )
  427. goto cleanup;
  428. if( ( ret = mbedtls_entropy_source_self_test_gather( buf1, sizeof( buf1 ) ) ) != 0 )
  429. goto cleanup;
  430. /* Make sure that the returned values are not all 0 or 1 */
  431. if( ( ret = mbedtls_entropy_source_self_test_check_bits( buf0, sizeof( buf0 ) ) ) != 0 )
  432. goto cleanup;
  433. if( ( ret = mbedtls_entropy_source_self_test_check_bits( buf1, sizeof( buf1 ) ) ) != 0 )
  434. goto cleanup;
  435. /* Make sure that the entropy source is not returning values in a
  436. * pattern */
  437. ret = memcmp( buf0, buf1, sizeof( buf0 ) ) == 0;
  438. cleanup:
  439. if( verbose != 0 )
  440. {
  441. if( ret != 0 )
  442. mbedtls_printf( "failed\n" );
  443. else
  444. mbedtls_printf( "passed\n" );
  445. mbedtls_printf( "\n" );
  446. }
  447. return( ret != 0 );
  448. }
  449. #endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
  450. /*
  451. * The actual entropy quality is hard to test, but we can at least
  452. * test that the functions don't cause errors and write the correct
  453. * amount of data to buffers.
  454. */
  455. int mbedtls_entropy_self_test( int verbose )
  456. {
  457. int ret = 1;
  458. #if !defined(MBEDTLS_TEST_NULL_ENTROPY)
  459. mbedtls_entropy_context ctx;
  460. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
  461. unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
  462. size_t i, j;
  463. #endif /* !MBEDTLS_TEST_NULL_ENTROPY */
  464. if( verbose != 0 )
  465. mbedtls_printf( " ENTROPY test: " );
  466. #if !defined(MBEDTLS_TEST_NULL_ENTROPY)
  467. mbedtls_entropy_init( &ctx );
  468. /* First do a gather to make sure we have default sources */
  469. if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
  470. goto cleanup;
  471. ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16,
  472. MBEDTLS_ENTROPY_SOURCE_WEAK );
  473. if( ret != 0 )
  474. goto cleanup;
  475. if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
  476. goto cleanup;
  477. /*
  478. * To test that mbedtls_entropy_func writes correct number of bytes:
  479. * - use the whole buffer and rely on ASan to detect overruns
  480. * - collect entropy 8 times and OR the result in an accumulator:
  481. * any byte should then be 0 with probably 2^(-64), so requiring
  482. * each of the 32 or 64 bytes to be non-zero has a false failure rate
  483. * of at most 2^(-58) which is acceptable.
  484. */
  485. for( i = 0; i < 8; i++ )
  486. {
  487. if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
  488. goto cleanup;
  489. for( j = 0; j < sizeof( buf ); j++ )
  490. acc[j] |= buf[j];
  491. }
  492. for( j = 0; j < sizeof( buf ); j++ )
  493. {
  494. if( acc[j] == 0 )
  495. {
  496. ret = 1;
  497. goto cleanup;
  498. }
  499. }
  500. #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
  501. if( ( ret = mbedtls_entropy_source_self_test( 0 ) ) != 0 )
  502. goto cleanup;
  503. #endif
  504. cleanup:
  505. mbedtls_entropy_free( &ctx );
  506. #endif /* !MBEDTLS_TEST_NULL_ENTROPY */
  507. if( verbose != 0 )
  508. {
  509. if( ret != 0 )
  510. mbedtls_printf( "failed\n" );
  511. else
  512. mbedtls_printf( "passed\n" );
  513. mbedtls_printf( "\n" );
  514. }
  515. return( ret != 0 );
  516. }
  517. #endif /* MBEDTLS_SELF_TEST */
  518. #endif /* MBEDTLS_ENTROPY_C */