entropy.c 21 KB

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