entropy.c 20 KB

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