entropy.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*
  2. * Entropy accumulator implementation
  3. *
  4. * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_ENTROPY_C)
  27. #if defined(MBEDTLS_TEST_NULL_ENTROPY)
  28. #warning "**** WARNING! MBEDTLS_TEST_NULL_ENTROPY defined! "
  29. #warning "**** THIS BUILD HAS NO DEFINED ENTROPY SOURCES "
  30. #warning "**** THIS BUILD IS *NOT* SUITABLE FOR PRODUCTION USE "
  31. #endif
  32. #include "mbedtls/entropy.h"
  33. #include "mbedtls/entropy_poll.h"
  34. #include <string.h>
  35. #if defined(MBEDTLS_FS_IO)
  36. #include <stdio.h>
  37. #endif
  38. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  39. #include "mbedtls/platform.h"
  40. #endif
  41. #if defined(MBEDTLS_SELF_TEST)
  42. #if defined(MBEDTLS_PLATFORM_C)
  43. #include "mbedtls/platform.h"
  44. #else
  45. #include <stdio.h>
  46. #define mbedtls_printf printf
  47. #endif /* MBEDTLS_PLATFORM_C */
  48. #endif /* MBEDTLS_SELF_TEST */
  49. #if defined(MBEDTLS_HAVEGE_C)
  50. #include "mbedtls/havege.h"
  51. #endif
  52. /* Implementation that should never be optimized out by the compiler */
  53. static void mbedtls_zeroize( void *v, size_t n ) {
  54. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  55. }
  56. #define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
  57. void mbedtls_entropy_init( mbedtls_entropy_context *ctx )
  58. {
  59. ctx->source_count = 0;
  60. memset( ctx->source, 0, sizeof( ctx->source ) );
  61. #if defined(MBEDTLS_THREADING_C)
  62. mbedtls_mutex_init( &ctx->mutex );
  63. #endif
  64. ctx->accumulator_started = 0;
  65. #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
  66. mbedtls_sha512_init( &ctx->accumulator );
  67. #else
  68. mbedtls_sha256_init( &ctx->accumulator );
  69. #endif
  70. #if defined(MBEDTLS_HAVEGE_C)
  71. mbedtls_havege_init( &ctx->havege_data );
  72. #endif
  73. /* Reminder: Update ENTROPY_HAVE_STRONG in the test files
  74. * when adding more strong entropy sources here. */
  75. #if defined(MBEDTLS_TEST_NULL_ENTROPY)
  76. mbedtls_entropy_add_source( ctx, mbedtls_null_entropy_poll, NULL,
  77. 1, MBEDTLS_ENTROPY_SOURCE_STRONG );
  78. #endif
  79. #if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
  80. #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
  81. mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL,
  82. MBEDTLS_ENTROPY_MIN_PLATFORM,
  83. MBEDTLS_ENTROPY_SOURCE_STRONG );
  84. #endif
  85. #if defined(MBEDTLS_TIMING_C)
  86. mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL,
  87. MBEDTLS_ENTROPY_MIN_HARDCLOCK,
  88. MBEDTLS_ENTROPY_SOURCE_WEAK );
  89. #endif
  90. #if defined(MBEDTLS_HAVEGE_C)
  91. mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data,
  92. MBEDTLS_ENTROPY_MIN_HAVEGE,
  93. MBEDTLS_ENTROPY_SOURCE_STRONG );
  94. #endif
  95. #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
  96. mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL,
  97. MBEDTLS_ENTROPY_MIN_HARDWARE,
  98. MBEDTLS_ENTROPY_SOURCE_STRONG );
  99. #endif
  100. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  101. mbedtls_entropy_add_source( ctx, mbedtls_nv_seed_poll, NULL,
  102. MBEDTLS_ENTROPY_BLOCK_SIZE,
  103. MBEDTLS_ENTROPY_SOURCE_STRONG );
  104. ctx->initial_entropy_run = 0;
  105. #endif
  106. #endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
  107. }
  108. void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
  109. {
  110. #if defined(MBEDTLS_HAVEGE_C)
  111. mbedtls_havege_free( &ctx->havege_data );
  112. #endif
  113. #if defined(MBEDTLS_THREADING_C)
  114. mbedtls_mutex_free( &ctx->mutex );
  115. #endif
  116. #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
  117. mbedtls_sha512_free( &ctx->accumulator );
  118. #else
  119. mbedtls_sha256_free( &ctx->accumulator );
  120. #endif
  121. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  122. ctx->initial_entropy_run = 0;
  123. #endif
  124. ctx->source_count = 0;
  125. mbedtls_zeroize( ctx->source, sizeof( ctx->source ) );
  126. ctx->accumulator_started = 0;
  127. }
  128. int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
  129. mbedtls_entropy_f_source_ptr f_source, void *p_source,
  130. size_t threshold, int strong )
  131. {
  132. int idx, ret = 0;
  133. #if defined(MBEDTLS_THREADING_C)
  134. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  135. return( ret );
  136. #endif
  137. idx = ctx->source_count;
  138. if( idx >= MBEDTLS_ENTROPY_MAX_SOURCES )
  139. {
  140. ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
  141. goto exit;
  142. }
  143. ctx->source[idx].f_source = f_source;
  144. ctx->source[idx].p_source = p_source;
  145. ctx->source[idx].threshold = threshold;
  146. ctx->source[idx].strong = strong;
  147. ctx->source_count++;
  148. exit:
  149. #if defined(MBEDTLS_THREADING_C)
  150. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  151. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  152. #endif
  153. return( ret );
  154. }
  155. /*
  156. * Entropy accumulator update
  157. */
  158. static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
  159. const unsigned char *data, size_t len )
  160. {
  161. unsigned char header[2];
  162. unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
  163. size_t use_len = len;
  164. const unsigned char *p = data;
  165. int ret = 0;
  166. if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
  167. {
  168. #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
  169. if( ( ret = mbedtls_sha512_ret( data, len, tmp, 0 ) ) != 0 )
  170. goto cleanup;
  171. #else
  172. if( ( ret = mbedtls_sha256_ret( data, len, tmp, 0 ) ) != 0 )
  173. goto cleanup;
  174. #endif
  175. p = tmp;
  176. use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
  177. }
  178. header[0] = source_id;
  179. header[1] = use_len & 0xFF;
  180. /*
  181. * Start the accumulator if this has not already happened. Note that
  182. * it is sufficient to start the accumulator here only because all calls to
  183. * gather entropy eventually execute this code.
  184. */
  185. #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
  186. if( ctx->accumulator_started == 0 &&
  187. ( ret = mbedtls_sha512_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
  188. goto cleanup;
  189. else
  190. ctx->accumulator_started = 1;
  191. if( ( ret = mbedtls_sha512_update_ret( &ctx->accumulator, header, 2 ) ) != 0 )
  192. goto cleanup;
  193. ret = mbedtls_sha512_update_ret( &ctx->accumulator, p, use_len );
  194. #else
  195. if( ctx->accumulator_started == 0 &&
  196. ( ret = mbedtls_sha256_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
  197. goto cleanup;
  198. else
  199. ctx->accumulator_started = 1;
  200. if( ( ret = mbedtls_sha256_update_ret( &ctx->accumulator, header, 2 ) ) != 0 )
  201. goto cleanup;
  202. ret = mbedtls_sha256_update_ret( &ctx->accumulator, p, use_len );
  203. #endif
  204. cleanup:
  205. mbedtls_zeroize( tmp, sizeof( tmp ) );
  206. return( ret );
  207. }
  208. int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
  209. const unsigned char *data, size_t len )
  210. {
  211. int ret;
  212. #if defined(MBEDTLS_THREADING_C)
  213. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  214. return( ret );
  215. #endif
  216. ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len );
  217. #if defined(MBEDTLS_THREADING_C)
  218. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  219. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  220. #endif
  221. return( ret );
  222. }
  223. /*
  224. * Run through the different sources to add entropy to our accumulator
  225. */
  226. static int entropy_gather_internal( mbedtls_entropy_context *ctx )
  227. {
  228. int ret, i, have_one_strong = 0;
  229. unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
  230. size_t olen;
  231. if( ctx->source_count == 0 )
  232. return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
  233. /*
  234. * Run through our entropy sources
  235. */
  236. for( i = 0; i < ctx->source_count; i++ )
  237. {
  238. if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
  239. have_one_strong = 1;
  240. olen = 0;
  241. if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
  242. buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
  243. {
  244. goto cleanup;
  245. }
  246. /*
  247. * Add if we actually gathered something
  248. */
  249. if( olen > 0 )
  250. {
  251. if( ( ret = entropy_update( ctx, (unsigned char) i,
  252. buf, olen ) ) != 0 )
  253. return( ret );
  254. ctx->source[i].size += olen;
  255. }
  256. }
  257. if( have_one_strong == 0 )
  258. ret = MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE;
  259. cleanup:
  260. mbedtls_zeroize( buf, sizeof( buf ) );
  261. return( ret );
  262. }
  263. /*
  264. * Thread-safe wrapper for entropy_gather_internal()
  265. */
  266. int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
  267. {
  268. int ret;
  269. #if defined(MBEDTLS_THREADING_C)
  270. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  271. return( ret );
  272. #endif
  273. ret = entropy_gather_internal( ctx );
  274. #if defined(MBEDTLS_THREADING_C)
  275. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  276. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  277. #endif
  278. return( ret );
  279. }
  280. int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
  281. {
  282. int ret, count = 0, i, done;
  283. mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
  284. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  285. if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
  286. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  287. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  288. /* Update the NV entropy seed before generating any entropy for outside
  289. * use.
  290. */
  291. if( ctx->initial_entropy_run == 0 )
  292. {
  293. ctx->initial_entropy_run = 1;
  294. if( ( ret = mbedtls_entropy_update_nv_seed( ctx ) ) != 0 )
  295. return( ret );
  296. }
  297. #endif
  298. #if defined(MBEDTLS_THREADING_C)
  299. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  300. return( ret );
  301. #endif
  302. /*
  303. * Always gather extra entropy before a call
  304. */
  305. do
  306. {
  307. if( count++ > ENTROPY_MAX_LOOP )
  308. {
  309. ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  310. goto exit;
  311. }
  312. if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
  313. goto exit;
  314. done = 1;
  315. for( i = 0; i < ctx->source_count; i++ )
  316. if( ctx->source[i].size < ctx->source[i].threshold )
  317. done = 0;
  318. }
  319. while( ! done );
  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_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_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_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 */