entropy.c 18 KB

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