blob: e3bc8516e2f29b9026a7ddf4c511fb6f3b71f89d [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Entropy accumulator implementation
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker6083fd22011-12-03 21:45:14 +00006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Paul Bakker6083fd22011-12-03 21:45:14 +00009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/entropy.h"
Chris Jonesea0a8652021-03-09 19:11:19 +000013#include "entropy_poll.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050014#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000015#include "mbedtls/error.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000016
Rich Evans00ab4702015-02-06 13:43:58 +000017#include <string.h>
18
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019#if defined(MBEDTLS_FS_IO)
Paul Bakker66ff70d2014-03-26 11:54:05 +010020#include <stdio.h>
21#endif
22
Paul Bakker217efbc2016-07-14 14:30:03 +010023#include "mbedtls/platform.h"
Paul Bakker217efbc2016-07-14 14:30:03 +010024
Paul Bakker6083fd22011-12-03 21:45:14 +000025#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
26
Gilles Peskine449bd832023-01-11 14:50:10 +010027void mbedtls_entropy_init(mbedtls_entropy_context *ctx)
Paul Bakker6083fd22011-12-03 21:45:14 +000028{
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +010029 ctx->source_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010030 memset(ctx->source, 0, sizeof(ctx->source));
Paul Bakker6083fd22011-12-03 21:45:14 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010033 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020034#endif
35
Andres Amaya Garcia95869c42017-06-29 16:31:44 +010036 ctx->accumulator_started = 0;
Manuel Pégourié-Gonnard5cd4b642023-02-02 13:14:59 +010037 mbedtls_md_init(&ctx->accumulator);
Paul Bakker6083fd22011-12-03 21:45:14 +000038
Hanno Beckerd4a872e2017-09-07 08:09:33 +010039 /* Reminder: Update ENTROPY_HAVE_STRONG in the test files
Hanno Becker47deec42017-07-24 12:27:09 +010040 * when adding more strong entropy sources here. */
Hanno Beckerc6deafc2017-07-23 14:06:42 +010041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
43#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +010044 mbedtls_entropy_add_source(ctx, mbedtls_platform_entropy_poll, NULL,
45 MBEDTLS_ENTROPY_MIN_PLATFORM,
46 MBEDTLS_ENTROPY_SOURCE_STRONG);
Paul Bakker6083fd22011-12-03 21:45:14 +000047#endif
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +020048#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +010049 mbedtls_entropy_add_source(ctx, mbedtls_hardware_poll, NULL,
50 MBEDTLS_ENTROPY_MIN_HARDWARE,
51 MBEDTLS_ENTROPY_SOURCE_STRONG);
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +020052#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +010053#if defined(MBEDTLS_ENTROPY_NV_SEED)
Gilles Peskine449bd832023-01-11 14:50:10 +010054 mbedtls_entropy_add_source(ctx, mbedtls_nv_seed_poll, NULL,
55 MBEDTLS_ENTROPY_BLOCK_SIZE,
56 MBEDTLS_ENTROPY_SOURCE_STRONG);
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +010057 ctx->initial_entropy_run = 0;
Paul Bakker9988d6b2016-06-01 11:29:42 +010058#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +000060}
61
Gilles Peskine449bd832023-01-11 14:50:10 +010062void mbedtls_entropy_free(mbedtls_entropy_context *ctx)
Paul Bakker1ffefac2013-09-28 15:23:03 +020063{
Gilles Peskineb1583212021-02-22 21:26:54 +010064 /* If the context was already free, don't call free() again.
65 * This is important for mutexes which don't allow double-free. */
Gilles Peskine449bd832023-01-11 14:50:10 +010066 if (ctx->accumulator_started == -1) {
Gilles Peskineb1583212021-02-22 21:26:54 +010067 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010068 }
Gilles Peskineb1583212021-02-22 21:26:54 +010069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010071 mbedtls_mutex_free(&ctx->mutex);
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020072#endif
Manuel Pégourié-Gonnard5cd4b642023-02-02 13:14:59 +010073 mbedtls_md_free(&ctx->accumulator);
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +010074#if defined(MBEDTLS_ENTROPY_NV_SEED)
75 ctx->initial_entropy_run = 0;
76#endif
77 ctx->source_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010078 mbedtls_platform_zeroize(ctx->source, sizeof(ctx->source));
Gilles Peskineb1583212021-02-22 21:26:54 +010079 ctx->accumulator_started = -1;
Paul Bakker1ffefac2013-09-28 15:23:03 +020080}
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082int mbedtls_entropy_add_source(mbedtls_entropy_context *ctx,
83 mbedtls_entropy_f_source_ptr f_source, void *p_source,
84 size_t threshold, int strong)
Paul Bakker6083fd22011-12-03 21:45:14 +000085{
Hanno Becker61937d42017-04-26 15:01:23 +010086 int idx, ret = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010089 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
90 return ret;
91 }
Paul Bakker47703a02014-02-06 15:01:20 +010092#endif
93
Hanno Becker61937d42017-04-26 15:01:23 +010094 idx = ctx->source_count;
Gilles Peskine449bd832023-01-11 14:50:10 +010095 if (idx >= MBEDTLS_ENTROPY_MAX_SOURCES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
Paul Bakker47703a02014-02-06 15:01:20 +010097 goto exit;
98 }
Paul Bakker6083fd22011-12-03 21:45:14 +000099
Hanno Becker61937d42017-04-26 15:01:23 +0100100 ctx->source[idx].f_source = f_source;
101 ctx->source[idx].p_source = p_source;
102 ctx->source[idx].threshold = threshold;
103 ctx->source[idx].strong = strong;
Paul Bakker6083fd22011-12-03 21:45:14 +0000104
105 ctx->source_count++;
106
Paul Bakker47703a02014-02-06 15:01:20 +0100107exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
110 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
111 }
Paul Bakker47703a02014-02-06 15:01:20 +0100112#endif
113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000115}
116
117/*
118 * Entropy accumulator update
119 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120static int entropy_update(mbedtls_entropy_context *ctx, unsigned char source_id,
121 const unsigned char *data, size_t len)
Paul Bakker6083fd22011-12-03 21:45:14 +0000122{
123 unsigned char header[2];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000125 size_t use_len = len;
126 const unsigned char *p = data;
Jaeden Amero66954e12018-01-25 16:05:54 +0000127 int ret = 0;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 if (use_len > MBEDTLS_ENTROPY_BLOCK_SIZE) {
Manuel Pégourié-Gonnard5cd4b642023-02-02 13:14:59 +0100130 if ((ret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_ENTROPY_MD),
131 data, len, tmp)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000132 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000134 p = tmp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
Paul Bakker6083fd22011-12-03 21:45:14 +0000136 }
137
138 header[0] = source_id;
139 header[1] = use_len & 0xFF;
140
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100141 /*
142 * Start the accumulator if this has not already happened. Note that
143 * it is sufficient to start the accumulator here only because all calls to
144 * gather entropy eventually execute this code.
145 */
Manuel Pégourié-Gonnard5cd4b642023-02-02 13:14:59 +0100146 if (ctx->accumulator_started == 0) {
147 ret = mbedtls_md_setup(&ctx->accumulator,
148 mbedtls_md_info_from_type(MBEDTLS_ENTROPY_MD), 0);
149 if (ret != 0) {
150 goto cleanup;
151 }
152 ret = mbedtls_md_starts(&ctx->accumulator);
153 if (ret != 0) {
154 goto cleanup;
155 }
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100156 ctx->accumulator_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 }
Manuel Pégourié-Gonnard5cd4b642023-02-02 13:14:59 +0100158 if ((ret = mbedtls_md_update(&ctx->accumulator, header, 2)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000159 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 }
Manuel Pégourié-Gonnard5cd4b642023-02-02 13:14:59 +0100161 ret = mbedtls_md_update(&ctx->accumulator, p, use_len);
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200162
Jaeden Amero66954e12018-01-25 16:05:54 +0000163cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Andres Amaya Garcia65121932017-07-05 15:45:47 +0100165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000167}
168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx,
170 const unsigned char *data, size_t len)
Paul Bakker6083fd22011-12-03 21:45:14 +0000171{
Janos Follath24eed8d2019-11-22 13:21:35 +0000172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker47703a02014-02-06 15:01:20 +0100173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
176 return ret;
177 }
Paul Bakker47703a02014-02-06 15:01:20 +0100178#endif
179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 ret = entropy_update(ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len);
Paul Bakker47703a02014-02-06 15:01:20 +0100181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
184 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
185 }
Paul Bakker47703a02014-02-06 15:01:20 +0100186#endif
187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000189}
190
191/*
192 * Run through the different sources to add entropy to our accumulator
193 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100194static int entropy_gather_internal(mbedtls_entropy_context *ctx)
Paul Bakker6083fd22011-12-03 21:45:14 +0000195{
Gilles Peskine006c1b52019-09-30 17:29:54 +0200196 int ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
197 int i;
198 int have_one_strong = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
Paul Bakker6083fd22011-12-03 21:45:14 +0000200 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 if (ctx->source_count == 0) {
203 return MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED;
204 }
Paul Bakker43655f42011-12-15 20:11:16 +0000205
Paul Bakker6083fd22011-12-03 21:45:14 +0000206 /*
207 * Run through our entropy sources
208 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 for (i = 0; i < ctx->source_count; i++) {
210 if (ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG) {
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200211 have_one_strong = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 }
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200213
Paul Bakker6083fd22011-12-03 21:45:14 +0000214 olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 if ((ret = ctx->source[i].f_source(ctx->source[i].p_source,
216 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen)) != 0) {
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100217 goto cleanup;
Paul Bakker6083fd22011-12-03 21:45:14 +0000218 }
219
220 /*
221 * Add if we actually gathered something
222 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if (olen > 0) {
224 if ((ret = entropy_update(ctx, (unsigned char) i,
225 buf, olen)) != 0) {
226 return ret;
227 }
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000228 ctx->source[i].size += olen;
229 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000230 }
231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 if (have_one_strong == 0) {
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100233 ret = MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 }
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200235
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100236cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 mbedtls_platform_zeroize(buf, sizeof(buf));
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000240}
241
Paul Bakker47703a02014-02-06 15:01:20 +0100242/*
243 * Thread-safe wrapper for entropy_gather_internal()
244 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100245int mbedtls_entropy_gather(mbedtls_entropy_context *ctx)
Paul Bakker47703a02014-02-06 15:01:20 +0100246{
Janos Follath24eed8d2019-11-22 13:21:35 +0000247 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker47703a02014-02-06 15:01:20 +0100248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
251 return ret;
252 }
Paul Bakker47703a02014-02-06 15:01:20 +0100253#endif
254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 ret = entropy_gather_internal(ctx);
Paul Bakker47703a02014-02-06 15:01:20 +0100256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
259 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
260 }
Paul Bakker47703a02014-02-06 15:01:20 +0100261#endif
262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return ret;
Paul Bakker47703a02014-02-06 15:01:20 +0100264}
265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266int mbedtls_entropy_func(void *data, unsigned char *output, size_t len)
Paul Bakker6083fd22011-12-03 21:45:14 +0000267{
Gilles Peskine85485c72019-10-08 15:04:16 +0200268 int ret, count = 0, i, thresholds_reached;
269 size_t strong_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
271 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (len > MBEDTLS_ENTROPY_BLOCK_SIZE) {
274 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
275 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000276
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100277#if defined(MBEDTLS_ENTROPY_NV_SEED)
278 /* Update the NV entropy seed before generating any entropy for outside
279 * use.
280 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (ctx->initial_entropy_run == 0) {
Paul Bakkerfc9c7c82016-06-01 15:25:50 +0100282 ctx->initial_entropy_run = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 if ((ret = mbedtls_entropy_update_nv_seed(ctx)) != 0) {
284 return ret;
285 }
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100286 }
287#endif
288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
291 return ret;
292 }
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200293#endif
294
Paul Bakker6083fd22011-12-03 21:45:14 +0000295 /*
296 * Always gather extra entropy before a call
297 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 do {
299 if (count++ > ENTROPY_MAX_LOOP) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200301 goto exit;
302 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if ((ret = entropy_gather_internal(ctx)) != 0) {
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200305 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 }
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000307
Gilles Peskine85485c72019-10-08 15:04:16 +0200308 thresholds_reached = 1;
309 strong_size = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 for (i = 0; i < ctx->source_count; i++) {
311 if (ctx->source[i].size < ctx->source[i].threshold) {
Gilles Peskine85485c72019-10-08 15:04:16 +0200312 thresholds_reached = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 }
314 if (ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG) {
Gilles Peskine85485c72019-10-08 15:04:16 +0200315 strong_size += ctx->source[i].size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 }
Gilles Peskine85485c72019-10-08 15:04:16 +0200317 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 } while (!thresholds_reached || strong_size < MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakker6083fd22011-12-03 21:45:14 +0000319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakker6083fd22011-12-03 21:45:14 +0000321
Andres Amaya Garciab2b063f2017-07-20 16:45:24 +0100322 /*
323 * Note that at this stage it is assumed that the accumulator was started
324 * in a previous call to entropy_update(). If this is not guaranteed, the
325 * code below will fail.
326 */
Manuel Pégourié-Gonnard5cd4b642023-02-02 13:14:59 +0100327 if ((ret = mbedtls_md_finish(&ctx->accumulator, buf)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100328 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 }
Paul Bakker9e36f042013-06-30 14:34:05 +0200330
Paul Bakker6083fd22011-12-03 21:45:14 +0000331 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000332 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000333 */
Manuel Pégourié-Gonnard5cd4b642023-02-02 13:14:59 +0100334 mbedtls_md_free(&ctx->accumulator);
335 mbedtls_md_init(&ctx->accumulator);
336 ret = mbedtls_md_setup(&ctx->accumulator,
337 mbedtls_md_info_from_type(MBEDTLS_ENTROPY_MD), 0);
338 if (ret != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100339 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 }
Manuel Pégourié-Gonnard5cd4b642023-02-02 13:14:59 +0100341 ret = mbedtls_md_starts(&ctx->accumulator);
342 if (ret != 0) {
343 goto exit;
344 }
345 if ((ret = mbedtls_md_update(&ctx->accumulator, buf,
346 MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100347 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 }
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200349
350 /*
Manuel Pégourié-Gonnard5cd4b642023-02-02 13:14:59 +0100351 * Perform second hashing on entropy
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200352 */
Manuel Pégourié-Gonnard5cd4b642023-02-02 13:14:59 +0100353 if ((ret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_ENTROPY_MD),
354 buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100355 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 }
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 for (i = 0; i < ctx->source_count; i++) {
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000359 ctx->source[i].size = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 memcpy(output, buf, len);
Paul Bakker6083fd22011-12-03 21:45:14 +0000363
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200364 ret = 0;
365
366exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 mbedtls_platform_zeroize(buf, sizeof(buf));
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
371 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
372 }
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200373#endif
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000376}
377
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100378#if defined(MBEDTLS_ENTROPY_NV_SEED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100379int mbedtls_entropy_update_nv_seed(mbedtls_entropy_context *ctx)
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100380{
381 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Andres Amaya Garciaaf0b31d2017-07-05 14:23:54 +0100382 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100383
384 /* Read new seed and write it to NV */
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if ((ret = mbedtls_entropy_func(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
386 return ret;
387 }
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (mbedtls_nv_seed_write(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) < 0) {
390 return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
391 }
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100392
393 /* Manually update the remaining stream with a separator value to diverge */
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
395 ret = mbedtls_entropy_update_manual(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 return ret;
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100398}
399#endif /* MBEDTLS_ENTROPY_NV_SEED */
400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401#if defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100402int mbedtls_entropy_write_seed_file(mbedtls_entropy_context *ctx, const char *path)
Paul Bakker66ff70d2014-03-26 11:54:05 +0100403{
Victor Krasnoshchoka0c2d192020-09-03 00:07:05 +0300404 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Victor Krasnoshchoke79812e2020-08-27 00:19:55 +0300405 FILE *f = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if ((ret = mbedtls_entropy_func(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
Victor Krasnoshchokb3129ba2020-08-29 22:54:37 +0300409 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100410 goto exit;
Victor Krasnoshchokb3129ba2020-08-29 22:54:37 +0300411 }
412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 if ((f = fopen(path, "wb")) == NULL) {
Victor Krasnoshchokb3129ba2020-08-29 22:54:37 +0300414 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
415 goto exit;
416 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100417
Gilles Peskineda0913b2022-06-30 17:03:40 +0200418 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 mbedtls_setbuf(f, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +0200420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 if (fwrite(buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f) != MBEDTLS_ENTROPY_BLOCK_SIZE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100423 goto exit;
424 }
425
426 ret = 0;
427
428exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 mbedtls_platform_zeroize(buf, sizeof(buf));
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if (f != NULL) {
432 fclose(f);
433 }
Victor Krasnoshchoke79812e2020-08-27 00:19:55 +0300434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 return ret;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100436}
437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *path)
Paul Bakker66ff70d2014-03-26 11:54:05 +0100439{
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100440 int ret = 0;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100441 FILE *f;
442 size_t n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 unsigned char buf[MBEDTLS_ENTROPY_MAX_SEED_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if ((f = fopen(path, "rb")) == NULL) {
446 return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
447 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100448
Gilles Peskineda0913b2022-06-30 17:03:40 +0200449 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 mbedtls_setbuf(f, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +0200451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 fseek(f, 0, SEEK_END);
453 n = (size_t) ftell(f);
454 fseek(f, 0, SEEK_SET);
Paul Bakker66ff70d2014-03-26 11:54:05 +0100455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 if (n > MBEDTLS_ENTROPY_MAX_SEED_SIZE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if (fread(buf, 1, n, f) != n) {
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100461 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 } else {
463 ret = mbedtls_entropy_update_manual(ctx, buf, n);
464 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 fclose(f);
Paul Bakker66ff70d2014-03-26 11:54:05 +0100467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 mbedtls_platform_zeroize(buf, sizeof(buf));
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (ret != 0) {
471 return ret;
472 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 return mbedtls_entropy_write_seed_file(ctx, path);
Paul Bakker66ff70d2014-03-26 11:54:05 +0100475}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476#endif /* MBEDTLS_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200479/*
480 * Dummy source function
481 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100482static int entropy_dummy_source(void *data, unsigned char *output,
483 size_t len, size_t *olen)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200484{
485 ((void) data);
486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 memset(output, 0x2a, len);
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200488 *olen = len;
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 return 0;
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200491}
492
Andres AGe7723ec2016-08-25 10:18:50 +0100493#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Andres AGb34e42e2016-08-22 11:08:50 +0100494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495static int mbedtls_entropy_source_self_test_gather(unsigned char *buf, size_t buf_len)
Andres AGe7723ec2016-08-25 10:18:50 +0100496{
497 int ret = 0;
498 size_t entropy_len = 0;
499 size_t olen = 0;
500 size_t attempts = buf_len;
501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 while (attempts > 0 && entropy_len < buf_len) {
503 if ((ret = mbedtls_hardware_poll(NULL, buf + entropy_len,
504 buf_len - entropy_len, &olen)) != 0) {
505 return ret;
506 }
Andres AGe7723ec2016-08-25 10:18:50 +0100507
508 entropy_len += olen;
509 attempts--;
510 }
511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 if (entropy_len < buf_len) {
Andres AGe7723ec2016-08-25 10:18:50 +0100513 ret = 1;
514 }
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return ret;
Andres AGe7723ec2016-08-25 10:18:50 +0100517}
518
519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520static int mbedtls_entropy_source_self_test_check_bits(const unsigned char *buf,
521 size_t buf_len)
Andres AGe7723ec2016-08-25 10:18:50 +0100522{
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 unsigned char set = 0xFF;
Andres AGe7723ec2016-08-25 10:18:50 +0100524 unsigned char unset = 0x00;
525 size_t i;
526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 for (i = 0; i < buf_len; i++) {
Andres AGe7723ec2016-08-25 10:18:50 +0100528 set &= buf[i];
529 unset |= buf[i];
530 }
531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 return set == 0xFF || unset == 0x00;
Andres AGe7723ec2016-08-25 10:18:50 +0100533}
Andres AGb34e42e2016-08-22 11:08:50 +0100534
535/*
Zachary Fleckenstein73defe42022-12-08 07:28:29 -0500536 * A test to ensure that the entropy sources are functioning correctly
Andres AGe7723ec2016-08-25 10:18:50 +0100537 * and there is no obvious failure. The test performs the following checks:
Andres AGb34e42e2016-08-22 11:08:50 +0100538 * - The entropy source is not providing only 0s (all bits unset) or 1s (all
539 * bits set).
540 * - The entropy source is not providing values in a pattern. Because the
Andres AGe7723ec2016-08-25 10:18:50 +0100541 * hardware could be providing data in an arbitrary length, this check polls
542 * the hardware entropy source twice and compares the result to ensure they
543 * are not equal.
Andres AGb34e42e2016-08-22 11:08:50 +0100544 * - The error code returned by the entropy source is not an error.
545 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100546int mbedtls_entropy_source_self_test(int verbose)
Andres AGb34e42e2016-08-22 11:08:50 +0100547{
548 int ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 unsigned char buf0[2 * sizeof(unsigned long long int)];
550 unsigned char buf1[2 * sizeof(unsigned long long int)];
Andres AGb34e42e2016-08-22 11:08:50 +0100551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 if (verbose != 0) {
553 mbedtls_printf(" ENTROPY_BIAS test: ");
554 }
Andres AGb34e42e2016-08-22 11:08:50 +0100555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 memset(buf0, 0x00, sizeof(buf0));
557 memset(buf1, 0x00, sizeof(buf1));
Andres AGb34e42e2016-08-22 11:08:50 +0100558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if ((ret = mbedtls_entropy_source_self_test_gather(buf0, sizeof(buf0))) != 0) {
Andres AGb34e42e2016-08-22 11:08:50 +0100560 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 }
562 if ((ret = mbedtls_entropy_source_self_test_gather(buf1, sizeof(buf1))) != 0) {
Andres AGe7723ec2016-08-25 10:18:50 +0100563 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 }
Andres AGe7723ec2016-08-25 10:18:50 +0100565
566 /* Make sure that the returned values are not all 0 or 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if ((ret = mbedtls_entropy_source_self_test_check_bits(buf0, sizeof(buf0))) != 0) {
Andres AGe7723ec2016-08-25 10:18:50 +0100568 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 }
570 if ((ret = mbedtls_entropy_source_self_test_check_bits(buf1, sizeof(buf1))) != 0) {
Andres AGe7723ec2016-08-25 10:18:50 +0100571 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 }
Andres AGb34e42e2016-08-22 11:08:50 +0100573
574 /* Make sure that the entropy source is not returning values in a
575 * pattern */
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 ret = memcmp(buf0, buf1, sizeof(buf0)) == 0;
Andres AGb34e42e2016-08-22 11:08:50 +0100577
578cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 if (verbose != 0) {
580 if (ret != 0) {
581 mbedtls_printf("failed\n");
582 } else {
583 mbedtls_printf("passed\n");
584 }
Andres AGb34e42e2016-08-22 11:08:50 +0100585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 mbedtls_printf("\n");
Andres AGb34e42e2016-08-22 11:08:50 +0100587 }
588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return ret != 0;
Andres AGb34e42e2016-08-22 11:08:50 +0100590}
Andres AGe7723ec2016-08-25 10:18:50 +0100591
592#endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
Andres AGb34e42e2016-08-22 11:08:50 +0100593
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200594/*
595 * The actual entropy quality is hard to test, but we can at least
596 * test that the functions don't cause errors and write the correct
597 * amount of data to buffers.
598 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100599int mbedtls_entropy_self_test(int verbose)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200600{
Andres Amaya Garciaa928e672016-09-13 13:30:02 +0100601 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_entropy_context ctx;
603 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
604 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200605 size_t i, j;
606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 if (verbose != 0) {
608 mbedtls_printf(" ENTROPY test: ");
609 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 mbedtls_entropy_init(&ctx);
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200612
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200613 /* First do a gather to make sure we have default sources */
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if ((ret = mbedtls_entropy_gather(&ctx)) != 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200615 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 ret = mbedtls_entropy_add_source(&ctx, entropy_dummy_source, NULL, 16,
619 MBEDTLS_ENTROPY_SOURCE_WEAK);
620 if (ret != 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200621 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200623
Dave Rodgman6dd757a2023-02-02 12:40:50 +0000624 if ((ret = mbedtls_entropy_update_manual(&ctx, buf, sizeof(buf))) != 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200625 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200627
628 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 * To test that mbedtls_entropy_func writes correct number of bytes:
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200630 * - use the whole buffer and rely on ASan to detect overruns
631 * - collect entropy 8 times and OR the result in an accumulator:
632 * any byte should then be 0 with probably 2^(-64), so requiring
633 * each of the 32 or 64 bytes to be non-zero has a false failure rate
634 * of at most 2^(-58) which is acceptable.
635 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 for (i = 0; i < 8; i++) {
637 if ((ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf))) != 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200638 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 for (j = 0; j < sizeof(buf); j++) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200642 acc[j] |= buf[j];
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200644 }
645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 for (j = 0; j < sizeof(buf); j++) {
647 if (acc[j] == 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200648 ret = 1;
649 goto cleanup;
650 }
651 }
652
Andres AGe7723ec2016-08-25 10:18:50 +0100653#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if ((ret = mbedtls_entropy_source_self_test(0)) != 0) {
Andres AGe7723ec2016-08-25 10:18:50 +0100655 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 }
Andres AGe7723ec2016-08-25 10:18:50 +0100657#endif
658
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200659cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 mbedtls_entropy_free(&ctx);
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 if (verbose != 0) {
663 if (ret != 0) {
664 mbedtls_printf("failed\n");
665 } else {
666 mbedtls_printf("passed\n");
667 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 mbedtls_printf("\n");
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200670 }
671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 return ret != 0;
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200673}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676#endif /* MBEDTLS_ENTROPY_C */