entropy.c 21 KB

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