entropy.c 18 KB

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