entropy.c 21 KB

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