entropy.c 19 KB

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