entropy.c 22 KB

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