entropy.c 21 KB

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