entropy.c 21 KB

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