blob: e3bc8516e2f29b9026a7ddf4c511fb6f3b71f89d [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/*
2 * Entropy accumulator implementation
3 *
Jerome Forissier79013242021-07-28 10:24:04 +02004 * Copyright The Mbed TLS Contributors
Tom Van Eyckc1633172024-04-09 18:44:13 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Jens Wiklander817466c2018-05-22 13:49:31 +02006 */
7
Jerome Forissier79013242021-07-28 10:24:04 +02008#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +02009
10#if defined(MBEDTLS_ENTROPY_C)
11
Jens Wiklander817466c2018-05-22 13:49:31 +020012#include "mbedtls/entropy.h"
Jens Wiklander32b31802023-10-06 16:59:46 +020013#include "entropy_poll.h"
Jens Wiklander3d3b0592019-03-20 15:30:29 +010014#include "mbedtls/platform_util.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020015#include "mbedtls/error.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020016
17#include <string.h>
18
19#if defined(MBEDTLS_FS_IO)
20#include <stdio.h>
21#endif
22
Jens Wiklander817466c2018-05-22 13:49:31 +020023#include "mbedtls/platform.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020024
Jens Wiklander817466c2018-05-22 13:49:31 +020025#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
26
Jens Wiklander32b31802023-10-06 16:59:46 +020027void mbedtls_entropy_init(mbedtls_entropy_context *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +020028{
Jens Wiklander3d3b0592019-03-20 15:30:29 +010029 ctx->source_count = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +020030 memset(ctx->source, 0, sizeof(ctx->source));
Jens Wiklander817466c2018-05-22 13:49:31 +020031
32#if defined(MBEDTLS_THREADING_C)
Jens Wiklander32b31802023-10-06 16:59:46 +020033 mbedtls_mutex_init(&ctx->mutex);
Jens Wiklander817466c2018-05-22 13:49:31 +020034#endif
35
Jens Wiklander3d3b0592019-03-20 15:30:29 +010036 ctx->accumulator_started = 0;
Tom Van Eyckc1633172024-04-09 18:44:13 +020037 mbedtls_md_init(&ctx->accumulator);
Jens Wiklander817466c2018-05-22 13:49:31 +020038
Jens Wiklander3d3b0592019-03-20 15:30:29 +010039 /* Reminder: Update ENTROPY_HAVE_STRONG in the test files
40 * when adding more strong entropy sources here. */
41
Jens Wiklander817466c2018-05-22 13:49:31 +020042#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
43#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Jens Wiklander32b31802023-10-06 16:59:46 +020044 mbedtls_entropy_add_source(ctx, mbedtls_platform_entropy_poll, NULL,
45 MBEDTLS_ENTROPY_MIN_PLATFORM,
46 MBEDTLS_ENTROPY_SOURCE_STRONG);
Jens Wiklander817466c2018-05-22 13:49:31 +020047#endif
48#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Jens Wiklander32b31802023-10-06 16:59:46 +020049 mbedtls_entropy_add_source(ctx, mbedtls_hardware_poll, NULL,
50 MBEDTLS_ENTROPY_MIN_HARDWARE,
51 MBEDTLS_ENTROPY_SOURCE_STRONG);
Jens Wiklander817466c2018-05-22 13:49:31 +020052#endif
53#if defined(MBEDTLS_ENTROPY_NV_SEED)
Jens Wiklander32b31802023-10-06 16:59:46 +020054 mbedtls_entropy_add_source(ctx, mbedtls_nv_seed_poll, NULL,
55 MBEDTLS_ENTROPY_BLOCK_SIZE,
56 MBEDTLS_ENTROPY_SOURCE_STRONG);
Jens Wiklander3d3b0592019-03-20 15:30:29 +010057 ctx->initial_entropy_run = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +020058#endif
59#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
60}
61
Jens Wiklander32b31802023-10-06 16:59:46 +020062void mbedtls_entropy_free(mbedtls_entropy_context *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +020063{
Jerome Forissier79013242021-07-28 10:24:04 +020064 /* If the context was already free, don't call free() again.
65 * This is important for mutexes which don't allow double-free. */
Jens Wiklander32b31802023-10-06 16:59:46 +020066 if (ctx->accumulator_started == -1) {
Jerome Forissier79013242021-07-28 10:24:04 +020067 return;
Jens Wiklander32b31802023-10-06 16:59:46 +020068 }
Jerome Forissier79013242021-07-28 10:24:04 +020069
Jens Wiklander817466c2018-05-22 13:49:31 +020070#if defined(MBEDTLS_THREADING_C)
Jens Wiklander32b31802023-10-06 16:59:46 +020071 mbedtls_mutex_free(&ctx->mutex);
Jens Wiklander817466c2018-05-22 13:49:31 +020072#endif
Tom Van Eyckc1633172024-04-09 18:44:13 +020073 mbedtls_md_free(&ctx->accumulator);
Jens Wiklander3d3b0592019-03-20 15:30:29 +010074#if defined(MBEDTLS_ENTROPY_NV_SEED)
75 ctx->initial_entropy_run = 0;
76#endif
77 ctx->source_count = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +020078 mbedtls_platform_zeroize(ctx->source, sizeof(ctx->source));
Jerome Forissier79013242021-07-28 10:24:04 +020079 ctx->accumulator_started = -1;
Jens Wiklander817466c2018-05-22 13:49:31 +020080}
81
Jens Wiklander32b31802023-10-06 16:59:46 +020082int 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)
Jens Wiklander817466c2018-05-22 13:49:31 +020085{
86 int idx, ret = 0;
87
88#if defined(MBEDTLS_THREADING_C)
Jens Wiklander32b31802023-10-06 16:59:46 +020089 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
90 return ret;
91 }
Jens Wiklander817466c2018-05-22 13:49:31 +020092#endif
93
94 idx = ctx->source_count;
Jens Wiklander32b31802023-10-06 16:59:46 +020095 if (idx >= MBEDTLS_ENTROPY_MAX_SOURCES) {
Jens Wiklander817466c2018-05-22 13:49:31 +020096 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
97 goto exit;
98 }
99
100 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;
104
105 ctx->source_count++;
106
107exit:
108#if defined(MBEDTLS_THREADING_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200109 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
110 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
111 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200112#endif
113
Jens Wiklander32b31802023-10-06 16:59:46 +0200114 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200115}
116
117/*
118 * Entropy accumulator update
119 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200120static int entropy_update(mbedtls_entropy_context *ctx, unsigned char source_id,
121 const unsigned char *data, size_t len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200122{
123 unsigned char header[2];
124 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
125 size_t use_len = len;
126 const unsigned char *p = data;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100127 int ret = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200128
Jens Wiklander32b31802023-10-06 16:59:46 +0200129 if (use_len > MBEDTLS_ENTROPY_BLOCK_SIZE) {
Tom Van Eyckc1633172024-04-09 18:44:13 +0200130 if ((ret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_ENTROPY_MD),
131 data, len, tmp)) != 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100132 goto cleanup;
Jens Wiklander32b31802023-10-06 16:59:46 +0200133 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200134 p = tmp;
135 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
136 }
137
138 header[0] = source_id;
139 header[1] = use_len & 0xFF;
140
Jens Wiklander3d3b0592019-03-20 15:30:29 +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 */
Tom Van Eyckc1633172024-04-09 18:44:13 +0200146 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 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100156 ctx->accumulator_started = 1;
Jens Wiklander32b31802023-10-06 16:59:46 +0200157 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200158 if ((ret = mbedtls_md_update(&ctx->accumulator, header, 2)) != 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100159 goto cleanup;
Jens Wiklander32b31802023-10-06 16:59:46 +0200160 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200161 ret = mbedtls_md_update(&ctx->accumulator, p, use_len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200162
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100163cleanup:
Jens Wiklander32b31802023-10-06 16:59:46 +0200164 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100165
Jens Wiklander32b31802023-10-06 16:59:46 +0200166 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200167}
168
Jens Wiklander32b31802023-10-06 16:59:46 +0200169int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx,
170 const unsigned char *data, size_t len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200171{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200173
174#if defined(MBEDTLS_THREADING_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200175 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
176 return ret;
177 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200178#endif
179
Jens Wiklander32b31802023-10-06 16:59:46 +0200180 ret = entropy_update(ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200181
182#if defined(MBEDTLS_THREADING_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200183 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
184 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
185 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200186#endif
187
Jens Wiklander32b31802023-10-06 16:59:46 +0200188 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200189}
190
191/*
192 * Run through the different sources to add entropy to our accumulator
193 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200194static int entropy_gather_internal(mbedtls_entropy_context *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200195{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200196 int ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
197 int i;
198 int have_one_strong = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200199 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
200 size_t olen;
201
Jens Wiklander32b31802023-10-06 16:59:46 +0200202 if (ctx->source_count == 0) {
203 return MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED;
204 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200205
206 /*
207 * Run through our entropy sources
208 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200209 for (i = 0; i < ctx->source_count; i++) {
210 if (ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200211 have_one_strong = 1;
Jens Wiklander32b31802023-10-06 16:59:46 +0200212 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200213
214 olen = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +0200215 if ((ret = ctx->source[i].f_source(ctx->source[i].p_source,
216 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen)) != 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100217 goto cleanup;
Jens Wiklander817466c2018-05-22 13:49:31 +0200218 }
219
220 /*
221 * Add if we actually gathered something
222 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200223 if (olen > 0) {
224 if ((ret = entropy_update(ctx, (unsigned char) i,
225 buf, olen)) != 0) {
226 return ret;
227 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200228 ctx->source[i].size += olen;
229 }
230 }
231
Jens Wiklander32b31802023-10-06 16:59:46 +0200232 if (have_one_strong == 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100233 ret = MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE;
Jens Wiklander32b31802023-10-06 16:59:46 +0200234 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200235
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100236cleanup:
Jens Wiklander32b31802023-10-06 16:59:46 +0200237 mbedtls_platform_zeroize(buf, sizeof(buf));
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100238
Jens Wiklander32b31802023-10-06 16:59:46 +0200239 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200240}
241
242/*
243 * Thread-safe wrapper for entropy_gather_internal()
244 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200245int mbedtls_entropy_gather(mbedtls_entropy_context *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200246{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200247 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200248
249#if defined(MBEDTLS_THREADING_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200250 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
251 return ret;
252 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200253#endif
254
Jens Wiklander32b31802023-10-06 16:59:46 +0200255 ret = entropy_gather_internal(ctx);
Jens Wiklander817466c2018-05-22 13:49:31 +0200256
257#if defined(MBEDTLS_THREADING_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200258 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
259 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
260 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200261#endif
262
Jens Wiklander32b31802023-10-06 16:59:46 +0200263 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200264}
265
Jens Wiklander32b31802023-10-06 16:59:46 +0200266int mbedtls_entropy_func(void *data, unsigned char *output, size_t len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200267{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200268 int ret, count = 0, i, thresholds_reached;
269 size_t strong_size;
Jens Wiklander817466c2018-05-22 13:49:31 +0200270 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
271 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
272
Jens Wiklander32b31802023-10-06 16:59:46 +0200273 if (len > MBEDTLS_ENTROPY_BLOCK_SIZE) {
274 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
275 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200276
277#if defined(MBEDTLS_ENTROPY_NV_SEED)
278 /* Update the NV entropy seed before generating any entropy for outside
279 * use.
280 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200281 if (ctx->initial_entropy_run == 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200282 ctx->initial_entropy_run = 1;
Jens Wiklander32b31802023-10-06 16:59:46 +0200283 if ((ret = mbedtls_entropy_update_nv_seed(ctx)) != 0) {
284 return ret;
285 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200286 }
287#endif
288
289#if defined(MBEDTLS_THREADING_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200290 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
291 return ret;
292 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200293#endif
294
295 /*
296 * Always gather extra entropy before a call
297 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200298 do {
299 if (count++ > ENTROPY_MAX_LOOP) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200300 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
301 goto exit;
302 }
303
Jens Wiklander32b31802023-10-06 16:59:46 +0200304 if ((ret = entropy_gather_internal(ctx)) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200305 goto exit;
Jens Wiklander32b31802023-10-06 16:59:46 +0200306 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200307
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200308 thresholds_reached = 1;
309 strong_size = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +0200310 for (i = 0; i < ctx->source_count; i++) {
311 if (ctx->source[i].size < ctx->source[i].threshold) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200312 thresholds_reached = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +0200313 }
314 if (ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200315 strong_size += ctx->source[i].size;
Jens Wiklander32b31802023-10-06 16:59:46 +0200316 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200317 }
Jens Wiklander32b31802023-10-06 16:59:46 +0200318 } while (!thresholds_reached || strong_size < MBEDTLS_ENTROPY_BLOCK_SIZE);
Jens Wiklander817466c2018-05-22 13:49:31 +0200319
Jens Wiklander32b31802023-10-06 16:59:46 +0200320 memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
Jens Wiklander817466c2018-05-22 13:49:31 +0200321
Jens Wiklander3d3b0592019-03-20 15:30:29 +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 */
Tom Van Eyckc1633172024-04-09 18:44:13 +0200327 if ((ret = mbedtls_md_finish(&ctx->accumulator, buf)) != 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100328 goto exit;
Jens Wiklander32b31802023-10-06 16:59:46 +0200329 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200330
331 /*
332 * Reset accumulator and counters and recycle existing entropy
333 */
Tom Van Eyckc1633172024-04-09 18:44:13 +0200334 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) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100339 goto exit;
Jens Wiklander32b31802023-10-06 16:59:46 +0200340 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200341 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) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100347 goto exit;
Jens Wiklander32b31802023-10-06 16:59:46 +0200348 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200349
350 /*
Tom Van Eyckc1633172024-04-09 18:44:13 +0200351 * Perform second hashing on entropy
Jens Wiklander817466c2018-05-22 13:49:31 +0200352 */
Tom Van Eyckc1633172024-04-09 18:44:13 +0200353 if ((ret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_ENTROPY_MD),
354 buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf)) != 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100355 goto exit;
Jens Wiklander32b31802023-10-06 16:59:46 +0200356 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200357
Jens Wiklander32b31802023-10-06 16:59:46 +0200358 for (i = 0; i < ctx->source_count; i++) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200359 ctx->source[i].size = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +0200360 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200361
Jens Wiklander32b31802023-10-06 16:59:46 +0200362 memcpy(output, buf, len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200363
364 ret = 0;
365
366exit:
Jens Wiklander32b31802023-10-06 16:59:46 +0200367 mbedtls_platform_zeroize(buf, sizeof(buf));
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100368
Jens Wiklander817466c2018-05-22 13:49:31 +0200369#if defined(MBEDTLS_THREADING_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200370 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
371 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
372 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200373#endif
374
Jens Wiklander32b31802023-10-06 16:59:46 +0200375 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200376}
377
378#if defined(MBEDTLS_ENTROPY_NV_SEED)
Jens Wiklander32b31802023-10-06 16:59:46 +0200379int mbedtls_entropy_update_nv_seed(mbedtls_entropy_context *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200380{
381 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100382 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Jens Wiklander817466c2018-05-22 13:49:31 +0200383
384 /* Read new seed and write it to NV */
Jens Wiklander32b31802023-10-06 16:59:46 +0200385 if ((ret = mbedtls_entropy_func(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
386 return ret;
387 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200388
Jens Wiklander32b31802023-10-06 16:59:46 +0200389 if (mbedtls_nv_seed_write(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) < 0) {
390 return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
391 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200392
393 /* Manually update the remaining stream with a separator value to diverge */
Jens Wiklander32b31802023-10-06 16:59:46 +0200394 memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
395 ret = mbedtls_entropy_update_manual(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE);
Jens Wiklander817466c2018-05-22 13:49:31 +0200396
Jens Wiklander32b31802023-10-06 16:59:46 +0200397 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200398}
399#endif /* MBEDTLS_ENTROPY_NV_SEED */
400
401#if defined(MBEDTLS_FS_IO)
Jens Wiklander32b31802023-10-06 16:59:46 +0200402int mbedtls_entropy_write_seed_file(mbedtls_entropy_context *ctx, const char *path)
Jens Wiklander817466c2018-05-22 13:49:31 +0200403{
Jerome Forissier79013242021-07-28 10:24:04 +0200404 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
405 FILE *f = NULL;
Jens Wiklander817466c2018-05-22 13:49:31 +0200406 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
407
Jens Wiklander32b31802023-10-06 16:59:46 +0200408 if ((ret = mbedtls_entropy_func(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
Jerome Forissier79013242021-07-28 10:24:04 +0200409 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200410 goto exit;
Jerome Forissier79013242021-07-28 10:24:04 +0200411 }
412
Jens Wiklander32b31802023-10-06 16:59:46 +0200413 if ((f = fopen(path, "wb")) == NULL) {
Jerome Forissier79013242021-07-28 10:24:04 +0200414 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
415 goto exit;
416 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200417
Jens Wiklander32b31802023-10-06 16:59:46 +0200418 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
419 mbedtls_setbuf(f, NULL);
420
421 if (fwrite(buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f) != MBEDTLS_ENTROPY_BLOCK_SIZE) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200422 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
423 goto exit;
424 }
425
426 ret = 0;
427
428exit:
Jens Wiklander32b31802023-10-06 16:59:46 +0200429 mbedtls_platform_zeroize(buf, sizeof(buf));
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100430
Jens Wiklander32b31802023-10-06 16:59:46 +0200431 if (f != NULL) {
432 fclose(f);
433 }
Jerome Forissier79013242021-07-28 10:24:04 +0200434
Jens Wiklander32b31802023-10-06 16:59:46 +0200435 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200436}
437
Jens Wiklander32b31802023-10-06 16:59:46 +0200438int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *path)
Jens Wiklander817466c2018-05-22 13:49:31 +0200439{
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100440 int ret = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200441 FILE *f;
442 size_t n;
Jens Wiklander32b31802023-10-06 16:59:46 +0200443 unsigned char buf[MBEDTLS_ENTROPY_MAX_SEED_SIZE];
Jens Wiklander817466c2018-05-22 13:49:31 +0200444
Jens Wiklander32b31802023-10-06 16:59:46 +0200445 if ((f = fopen(path, "rb")) == NULL) {
446 return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
447 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200448
Jens Wiklander32b31802023-10-06 16:59:46 +0200449 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
450 mbedtls_setbuf(f, NULL);
Jens Wiklander817466c2018-05-22 13:49:31 +0200451
Jens Wiklander32b31802023-10-06 16:59:46 +0200452 fseek(f, 0, SEEK_END);
453 n = (size_t) ftell(f);
454 fseek(f, 0, SEEK_SET);
455
456 if (n > MBEDTLS_ENTROPY_MAX_SEED_SIZE) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200457 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
Jens Wiklander32b31802023-10-06 16:59:46 +0200458 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200459
Jens Wiklander32b31802023-10-06 16:59:46 +0200460 if (fread(buf, 1, n, f) != n) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100461 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Jens Wiklander32b31802023-10-06 16:59:46 +0200462 } else {
463 ret = mbedtls_entropy_update_manual(ctx, buf, n);
464 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200465
Jens Wiklander32b31802023-10-06 16:59:46 +0200466 fclose(f);
Jens Wiklander817466c2018-05-22 13:49:31 +0200467
Jens Wiklander32b31802023-10-06 16:59:46 +0200468 mbedtls_platform_zeroize(buf, sizeof(buf));
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100469
Jens Wiklander32b31802023-10-06 16:59:46 +0200470 if (ret != 0) {
471 return ret;
472 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200473
Jens Wiklander32b31802023-10-06 16:59:46 +0200474 return mbedtls_entropy_write_seed_file(ctx, path);
Jens Wiklander817466c2018-05-22 13:49:31 +0200475}
476#endif /* MBEDTLS_FS_IO */
477
478#if defined(MBEDTLS_SELF_TEST)
Jens Wiklander817466c2018-05-22 13:49:31 +0200479/*
480 * Dummy source function
481 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200482static int entropy_dummy_source(void *data, unsigned char *output,
483 size_t len, size_t *olen)
Jens Wiklander817466c2018-05-22 13:49:31 +0200484{
485 ((void) data);
486
Jens Wiklander32b31802023-10-06 16:59:46 +0200487 memset(output, 0x2a, len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200488 *olen = len;
489
Jens Wiklander32b31802023-10-06 16:59:46 +0200490 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200491}
Jens Wiklander817466c2018-05-22 13:49:31 +0200492
493#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
494
Jens Wiklander32b31802023-10-06 16:59:46 +0200495static int mbedtls_entropy_source_self_test_gather(unsigned char *buf, size_t buf_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200496{
497 int ret = 0;
498 size_t entropy_len = 0;
499 size_t olen = 0;
500 size_t attempts = buf_len;
501
Jens Wiklander32b31802023-10-06 16:59:46 +0200502 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 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200507
508 entropy_len += olen;
509 attempts--;
510 }
511
Jens Wiklander32b31802023-10-06 16:59:46 +0200512 if (entropy_len < buf_len) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200513 ret = 1;
514 }
515
Jens Wiklander32b31802023-10-06 16:59:46 +0200516 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200517}
518
519
Jens Wiklander32b31802023-10-06 16:59:46 +0200520static int mbedtls_entropy_source_self_test_check_bits(const unsigned char *buf,
521 size_t buf_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200522{
Jens Wiklander32b31802023-10-06 16:59:46 +0200523 unsigned char set = 0xFF;
Jens Wiklander817466c2018-05-22 13:49:31 +0200524 unsigned char unset = 0x00;
525 size_t i;
526
Jens Wiklander32b31802023-10-06 16:59:46 +0200527 for (i = 0; i < buf_len; i++) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200528 set &= buf[i];
529 unset |= buf[i];
530 }
531
Jens Wiklander32b31802023-10-06 16:59:46 +0200532 return set == 0xFF || unset == 0x00;
Jens Wiklander817466c2018-05-22 13:49:31 +0200533}
534
535/*
Jens Wiklander32b31802023-10-06 16:59:46 +0200536 * A test to ensure that the entropy sources are functioning correctly
Jens Wiklander817466c2018-05-22 13:49:31 +0200537 * and there is no obvious failure. The test performs the following checks:
538 * - 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
541 * 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.
544 * - The error code returned by the entropy source is not an error.
545 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200546int mbedtls_entropy_source_self_test(int verbose)
Jens Wiklander817466c2018-05-22 13:49:31 +0200547{
548 int ret = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +0200549 unsigned char buf0[2 * sizeof(unsigned long long int)];
550 unsigned char buf1[2 * sizeof(unsigned long long int)];
Jens Wiklander817466c2018-05-22 13:49:31 +0200551
Jens Wiklander32b31802023-10-06 16:59:46 +0200552 if (verbose != 0) {
553 mbedtls_printf(" ENTROPY_BIAS test: ");
554 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200555
Jens Wiklander32b31802023-10-06 16:59:46 +0200556 memset(buf0, 0x00, sizeof(buf0));
557 memset(buf1, 0x00, sizeof(buf1));
Jens Wiklander817466c2018-05-22 13:49:31 +0200558
Jens Wiklander32b31802023-10-06 16:59:46 +0200559 if ((ret = mbedtls_entropy_source_self_test_gather(buf0, sizeof(buf0))) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200560 goto cleanup;
Jens Wiklander32b31802023-10-06 16:59:46 +0200561 }
562 if ((ret = mbedtls_entropy_source_self_test_gather(buf1, sizeof(buf1))) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200563 goto cleanup;
Jens Wiklander32b31802023-10-06 16:59:46 +0200564 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200565
566 /* Make sure that the returned values are not all 0 or 1 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200567 if ((ret = mbedtls_entropy_source_self_test_check_bits(buf0, sizeof(buf0))) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200568 goto cleanup;
Jens Wiklander32b31802023-10-06 16:59:46 +0200569 }
570 if ((ret = mbedtls_entropy_source_self_test_check_bits(buf1, sizeof(buf1))) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200571 goto cleanup;
Jens Wiklander32b31802023-10-06 16:59:46 +0200572 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200573
574 /* Make sure that the entropy source is not returning values in a
575 * pattern */
Jens Wiklander32b31802023-10-06 16:59:46 +0200576 ret = memcmp(buf0, buf1, sizeof(buf0)) == 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200577
578cleanup:
Jens Wiklander32b31802023-10-06 16:59:46 +0200579 if (verbose != 0) {
580 if (ret != 0) {
581 mbedtls_printf("failed\n");
582 } else {
583 mbedtls_printf("passed\n");
584 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200585
Jens Wiklander32b31802023-10-06 16:59:46 +0200586 mbedtls_printf("\n");
Jens Wiklander817466c2018-05-22 13:49:31 +0200587 }
588
Jens Wiklander32b31802023-10-06 16:59:46 +0200589 return ret != 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200590}
591
592#endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
593
594/*
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 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200599int mbedtls_entropy_self_test(int verbose)
Jens Wiklander817466c2018-05-22 13:49:31 +0200600{
601 int ret = 1;
Jens Wiklander817466c2018-05-22 13: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 };
605 size_t i, j;
Jens Wiklander817466c2018-05-22 13:49:31 +0200606
Jens Wiklander32b31802023-10-06 16:59:46 +0200607 if (verbose != 0) {
608 mbedtls_printf(" ENTROPY test: ");
609 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200610
Jens Wiklander32b31802023-10-06 16:59:46 +0200611 mbedtls_entropy_init(&ctx);
Jens Wiklander817466c2018-05-22 13:49:31 +0200612
613 /* First do a gather to make sure we have default sources */
Jens Wiklander32b31802023-10-06 16:59:46 +0200614 if ((ret = mbedtls_entropy_gather(&ctx)) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200615 goto cleanup;
Jens Wiklander32b31802023-10-06 16:59:46 +0200616 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200617
Jens Wiklander32b31802023-10-06 16:59:46 +0200618 ret = mbedtls_entropy_add_source(&ctx, entropy_dummy_source, NULL, 16,
619 MBEDTLS_ENTROPY_SOURCE_WEAK);
620 if (ret != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200621 goto cleanup;
Jens Wiklander32b31802023-10-06 16:59:46 +0200622 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200623
Jens Wiklander32b31802023-10-06 16:59:46 +0200624 if ((ret = mbedtls_entropy_update_manual(&ctx, buf, sizeof(buf))) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200625 goto cleanup;
Jens Wiklander32b31802023-10-06 16:59:46 +0200626 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200627
628 /*
629 * To test that mbedtls_entropy_func writes correct number of bytes:
630 * - 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 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200636 for (i = 0; i < 8; i++) {
637 if ((ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf))) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200638 goto cleanup;
Jens Wiklander32b31802023-10-06 16:59:46 +0200639 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200640
Jens Wiklander32b31802023-10-06 16:59:46 +0200641 for (j = 0; j < sizeof(buf); j++) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200642 acc[j] |= buf[j];
Jens Wiklander32b31802023-10-06 16:59:46 +0200643 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200644 }
645
Jens Wiklander32b31802023-10-06 16:59:46 +0200646 for (j = 0; j < sizeof(buf); j++) {
647 if (acc[j] == 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200648 ret = 1;
649 goto cleanup;
650 }
651 }
652
653#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Jens Wiklander32b31802023-10-06 16:59:46 +0200654 if ((ret = mbedtls_entropy_source_self_test(0)) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200655 goto cleanup;
Jens Wiklander32b31802023-10-06 16:59:46 +0200656 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200657#endif
658
659cleanup:
Jens Wiklander32b31802023-10-06 16:59:46 +0200660 mbedtls_entropy_free(&ctx);
Jens Wiklander817466c2018-05-22 13:49:31 +0200661
Jens Wiklander32b31802023-10-06 16:59:46 +0200662 if (verbose != 0) {
663 if (ret != 0) {
664 mbedtls_printf("failed\n");
665 } else {
666 mbedtls_printf("passed\n");
667 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200668
Jens Wiklander32b31802023-10-06 16:59:46 +0200669 mbedtls_printf("\n");
Jens Wiklander817466c2018-05-22 13:49:31 +0200670 }
671
Jens Wiklander32b31802023-10-06 16:59:46 +0200672 return ret != 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200673}
674#endif /* MBEDTLS_SELF_TEST */
675
676#endif /* MBEDTLS_ENTROPY_C */