blob: 339dc0e038fc276bd35b24398a20d42006238e05 [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 Rodgman7ff79652023-11-03 12:04:52 +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
Simon Butcherab5df402016-06-11 02:31:21 +010012#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher1ceab6e2016-06-21 10:14:00 +010013#warning "**** WARNING! MBEDTLS_TEST_NULL_ENTROPY defined! "
14#warning "**** THIS BUILD HAS NO DEFINED ENTROPY SOURCES "
15#warning "**** THIS BUILD IS *NOT* SUITABLE FOR PRODUCTION USE "
Simon Butcherab5df402016-06-11 02:31:21 +010016#endif
17
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000018#include "mbedtls/entropy.h"
19#include "mbedtls/entropy_poll.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050020#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000021#include "mbedtls/error.h"
Dave Rodgmanbae79fa2023-06-28 11:54:53 +010022#include "mbedtls/sha256.h"
23#include "mbedtls/sha512.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000024
Rich Evans00ab4702015-02-06 13:43:58 +000025#include <string.h>
26
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if defined(MBEDTLS_FS_IO)
Paul Bakker66ff70d2014-03-26 11:54:05 +010028#include <stdio.h>
29#endif
30
Paul Bakker217efbc2016-07-14 14:30:03 +010031#include "mbedtls/platform.h"
Paul Bakker217efbc2016-07-14 14:30:03 +010032
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/platform.h"
Rich Evans00ab4702015-02-06 13:43:58 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/havege.h"
Paul Bakker28c7e7f2011-12-15 19:49:30 +000037#endif
38
Paul Bakker6083fd22011-12-03 21:45:14 +000039#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
40
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041void mbedtls_entropy_init(mbedtls_entropy_context *ctx)
Paul Bakker6083fd22011-12-03 21:45:14 +000042{
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +010043 ctx->source_count = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010044 memset(ctx->source, 0, sizeof(ctx->source));
Paul Bakker6083fd22011-12-03 21:45:14 +000045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010047 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020048#endif
49
Andres Amaya Garcia95869c42017-06-29 16:31:44 +010050 ctx->accumulator_started = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010052 mbedtls_sha512_init(&ctx->accumulator);
Paul Bakkerfb08fd22013-08-27 15:06:26 +020053#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010054 mbedtls_sha256_init(&ctx->accumulator);
Paul Bakkerfb08fd22013-08-27 15:06:26 +020055#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_HAVEGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057 mbedtls_havege_init(&ctx->havege_data);
Paul Bakker43655f42011-12-15 20:11:16 +000058#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000059
Hanno Beckerd4a872e2017-09-07 08:09:33 +010060 /* Reminder: Update ENTROPY_HAVE_STRONG in the test files
Hanno Becker47deec42017-07-24 12:27:09 +010061 * when adding more strong entropy sources here. */
Hanno Beckerc6deafc2017-07-23 14:06:42 +010062
Simon Butcherab5df402016-06-11 02:31:21 +010063#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064 mbedtls_entropy_add_source(ctx, mbedtls_null_entropy_poll, NULL,
65 1, MBEDTLS_ENTROPY_SOURCE_STRONG);
Janos Follath53de7842016-06-08 15:29:18 +010066#endif
67
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
69#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010070 mbedtls_entropy_add_source(ctx, mbedtls_platform_entropy_poll, NULL,
71 MBEDTLS_ENTROPY_MIN_PLATFORM,
72 MBEDTLS_ENTROPY_SOURCE_STRONG);
Paul Bakker6083fd22011-12-03 21:45:14 +000073#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_TIMING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075 mbedtls_entropy_add_source(ctx, mbedtls_hardclock_poll, NULL,
76 MBEDTLS_ENTROPY_MIN_HARDCLOCK,
77 MBEDTLS_ENTROPY_SOURCE_WEAK);
Paul Bakker6083fd22011-12-03 21:45:14 +000078#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_HAVEGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 mbedtls_entropy_add_source(ctx, mbedtls_havege_poll, &ctx->havege_data,
81 MBEDTLS_ENTROPY_MIN_HAVEGE,
82 MBEDTLS_ENTROPY_SOURCE_STRONG);
Paul Bakker28c7e7f2011-12-15 19:49:30 +000083#endif
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +020084#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085 mbedtls_entropy_add_source(ctx, mbedtls_hardware_poll, NULL,
86 MBEDTLS_ENTROPY_MIN_HARDWARE,
87 MBEDTLS_ENTROPY_SOURCE_STRONG);
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +020088#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +010089#if defined(MBEDTLS_ENTROPY_NV_SEED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 mbedtls_entropy_add_source(ctx, mbedtls_nv_seed_poll, NULL,
91 MBEDTLS_ENTROPY_BLOCK_SIZE,
92 MBEDTLS_ENTROPY_SOURCE_STRONG);
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +010093 ctx->initial_entropy_run = 0;
Paul Bakker9988d6b2016-06-01 11:29:42 +010094#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +000096}
97
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098void mbedtls_entropy_free(mbedtls_entropy_context *ctx)
Paul Bakker1ffefac2013-09-28 15:23:03 +020099{
Gilles Peskineb1583212021-02-22 21:26:54 +0100100 /* If the context was already free, don't call free() again.
101 * This is important for mutexes which don't allow double-free. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102 if (ctx->accumulator_started == -1) {
Gilles Peskineb1583212021-02-22 21:26:54 +0100103 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 }
Gilles Peskineb1583212021-02-22 21:26:54 +0100105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_HAVEGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 mbedtls_havege_free(&ctx->havege_data);
Paul Bakkera317a982014-06-18 16:44:11 +0200108#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 mbedtls_mutex_free(&ctx->mutex);
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200111#endif
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100112#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 mbedtls_sha512_free(&ctx->accumulator);
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100114#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 mbedtls_sha256_free(&ctx->accumulator);
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100116#endif
117#if defined(MBEDTLS_ENTROPY_NV_SEED)
118 ctx->initial_entropy_run = 0;
119#endif
120 ctx->source_count = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 mbedtls_platform_zeroize(ctx->source, sizeof(ctx->source));
Gilles Peskineb1583212021-02-22 21:26:54 +0100122 ctx->accumulator_started = -1;
Paul Bakker1ffefac2013-09-28 15:23:03 +0200123}
124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125int mbedtls_entropy_add_source(mbedtls_entropy_context *ctx,
126 mbedtls_entropy_f_source_ptr f_source, void *p_source,
127 size_t threshold, int strong)
Paul Bakker6083fd22011-12-03 21:45:14 +0000128{
Hanno Becker61937d42017-04-26 15:01:23 +0100129 int idx, ret = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
133 return ret;
134 }
Paul Bakker47703a02014-02-06 15:01:20 +0100135#endif
136
Hanno Becker61937d42017-04-26 15:01:23 +0100137 idx = ctx->source_count;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 if (idx >= MBEDTLS_ENTROPY_MAX_SOURCES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
Paul Bakker47703a02014-02-06 15:01:20 +0100140 goto exit;
141 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000142
Hanno Becker61937d42017-04-26 15:01:23 +0100143 ctx->source[idx].f_source = f_source;
144 ctx->source[idx].p_source = p_source;
145 ctx->source[idx].threshold = threshold;
146 ctx->source[idx].strong = strong;
Paul Bakker6083fd22011-12-03 21:45:14 +0000147
148 ctx->source_count++;
149
Paul Bakker47703a02014-02-06 15:01:20 +0100150exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
153 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
154 }
Paul Bakker47703a02014-02-06 15:01:20 +0100155#endif
156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000158}
159
160/*
161 * Entropy accumulator update
162 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163static int entropy_update(mbedtls_entropy_context *ctx, unsigned char source_id,
164 const unsigned char *data, size_t len)
Paul Bakker6083fd22011-12-03 21:45:14 +0000165{
166 unsigned char header[2];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000168 size_t use_len = len;
169 const unsigned char *p = data;
Jaeden Amero66954e12018-01-25 16:05:54 +0000170 int ret = 0;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 if (use_len > MBEDTLS_ENTROPY_BLOCK_SIZE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174 if ((ret = mbedtls_sha512_ret(data, len, tmp, 0)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000175 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176 }
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200177#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 if ((ret = mbedtls_sha256_ret(data, len, tmp, 0)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000179 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 }
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200181#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000182 p = tmp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
Paul Bakker6083fd22011-12-03 21:45:14 +0000184 }
185
186 header[0] = source_id;
187 header[1] = use_len & 0xFF;
188
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100189 /*
190 * Start the accumulator if this has not already happened. Note that
191 * it is sufficient to start the accumulator here only because all calls to
192 * gather entropy eventually execute this code.
193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195 if (ctx->accumulator_started == 0 &&
196 (ret = mbedtls_sha512_starts_ret(&ctx->accumulator, 0)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000197 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 } else {
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100199 ctx->accumulator_started = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100200 }
201 if ((ret = mbedtls_sha512_update_ret(&ctx->accumulator, header, 2)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000202 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 }
204 ret = mbedtls_sha512_update_ret(&ctx->accumulator, p, use_len);
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200205#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 if (ctx->accumulator_started == 0 &&
207 (ret = mbedtls_sha256_starts_ret(&ctx->accumulator, 0)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000208 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 } else {
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100210 ctx->accumulator_started = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 }
212 if ((ret = mbedtls_sha256_update_ret(&ctx->accumulator, header, 2)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000213 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214 }
215 ret = mbedtls_sha256_update_ret(&ctx->accumulator, p, use_len);
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200216#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200217
Jaeden Amero66954e12018-01-25 16:05:54 +0000218cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Andres Amaya Garcia65121932017-07-05 15:45:47 +0100220
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000222}
223
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx,
225 const unsigned char *data, size_t len)
Paul Bakker6083fd22011-12-03 21:45:14 +0000226{
Janos Follath24eed8d2019-11-22 13:21:35 +0000227 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker47703a02014-02-06 15:01:20 +0100228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
231 return ret;
232 }
Paul Bakker47703a02014-02-06 15:01:20 +0100233#endif
234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 ret = entropy_update(ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len);
Paul Bakker47703a02014-02-06 15:01:20 +0100236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
239 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
240 }
Paul Bakker47703a02014-02-06 15:01:20 +0100241#endif
242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000244}
245
246/*
247 * Run through the different sources to add entropy to our accumulator
248 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249static int entropy_gather_internal(mbedtls_entropy_context *ctx)
Paul Bakker6083fd22011-12-03 21:45:14 +0000250{
Gilles Peskine006c1b52019-09-30 17:29:54 +0200251 int ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
252 int i;
253 int have_one_strong = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
Paul Bakker6083fd22011-12-03 21:45:14 +0000255 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 if (ctx->source_count == 0) {
258 return MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED;
259 }
Paul Bakker43655f42011-12-15 20:11:16 +0000260
Paul Bakker6083fd22011-12-03 21:45:14 +0000261 /*
262 * Run through our entropy sources
263 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 for (i = 0; i < ctx->source_count; i++) {
265 if (ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG) {
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200266 have_one_strong = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267 }
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200268
Paul Bakker6083fd22011-12-03 21:45:14 +0000269 olen = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 if ((ret = ctx->source[i].f_source(ctx->source[i].p_source,
271 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen)) != 0) {
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100272 goto cleanup;
Paul Bakker6083fd22011-12-03 21:45:14 +0000273 }
274
275 /*
276 * Add if we actually gathered something
277 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 if (olen > 0) {
279 if ((ret = entropy_update(ctx, (unsigned char) i,
280 buf, olen)) != 0) {
281 return ret;
282 }
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000283 ctx->source[i].size += olen;
284 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000285 }
286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 if (have_one_strong == 0) {
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100288 ret = MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 }
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200290
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100291cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292 mbedtls_platform_zeroize(buf, sizeof(buf));
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000295}
296
Paul Bakker47703a02014-02-06 15:01:20 +0100297/*
298 * Thread-safe wrapper for entropy_gather_internal()
299 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300int mbedtls_entropy_gather(mbedtls_entropy_context *ctx)
Paul Bakker47703a02014-02-06 15:01:20 +0100301{
Janos Follath24eed8d2019-11-22 13:21:35 +0000302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker47703a02014-02-06 15:01:20 +0100303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
306 return ret;
307 }
Paul Bakker47703a02014-02-06 15:01:20 +0100308#endif
309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 ret = entropy_gather_internal(ctx);
Paul Bakker47703a02014-02-06 15:01:20 +0100311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
314 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
315 }
Paul Bakker47703a02014-02-06 15:01:20 +0100316#endif
317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 return ret;
Paul Bakker47703a02014-02-06 15:01:20 +0100319}
320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321int mbedtls_entropy_func(void *data, unsigned char *output, size_t len)
Paul Bakker6083fd22011-12-03 21:45:14 +0000322{
Gilles Peskine85485c72019-10-08 15:04:16 +0200323 int ret, count = 0, i, thresholds_reached;
324 size_t strong_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
326 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000327
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328 if (len > MBEDTLS_ENTROPY_BLOCK_SIZE) {
329 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
330 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000331
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100332#if defined(MBEDTLS_ENTROPY_NV_SEED)
333 /* Update the NV entropy seed before generating any entropy for outside
334 * use.
335 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 if (ctx->initial_entropy_run == 0) {
Paul Bakkerfc9c7c82016-06-01 15:25:50 +0100337 ctx->initial_entropy_run = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338 if ((ret = mbedtls_entropy_update_nv_seed(ctx)) != 0) {
339 return ret;
340 }
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100341 }
342#endif
343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
346 return ret;
347 }
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200348#endif
349
Paul Bakker6083fd22011-12-03 21:45:14 +0000350 /*
351 * Always gather extra entropy before a call
352 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 do {
354 if (count++ > ENTROPY_MAX_LOOP) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200356 goto exit;
357 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 if ((ret = entropy_gather_internal(ctx)) != 0) {
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200360 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 }
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000362
Gilles Peskine85485c72019-10-08 15:04:16 +0200363 thresholds_reached = 1;
364 strong_size = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365 for (i = 0; i < ctx->source_count; i++) {
366 if (ctx->source[i].size < ctx->source[i].threshold) {
Gilles Peskine85485c72019-10-08 15:04:16 +0200367 thresholds_reached = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 }
369 if (ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG) {
Gilles Peskine85485c72019-10-08 15:04:16 +0200370 strong_size += ctx->source[i].size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 }
Gilles Peskine85485c72019-10-08 15:04:16 +0200372 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 } while (!thresholds_reached || strong_size < MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakker6083fd22011-12-03 21:45:14 +0000374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375 memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakker6083fd22011-12-03 21:45:14 +0000376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Andres Amaya Garciab2b063f2017-07-20 16:45:24 +0100378 /*
379 * Note that at this stage it is assumed that the accumulator was started
380 * in a previous call to entropy_update(). If this is not guaranteed, the
381 * code below will fail.
382 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 if ((ret = mbedtls_sha512_finish_ret(&ctx->accumulator, buf)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100384 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 }
Paul Bakker9e36f042013-06-30 14:34:05 +0200386
Paul Bakker6083fd22011-12-03 21:45:14 +0000387 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000388 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000389 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100390 mbedtls_sha512_free(&ctx->accumulator);
391 mbedtls_sha512_init(&ctx->accumulator);
392 if ((ret = mbedtls_sha512_starts_ret(&ctx->accumulator, 0)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100393 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100394 }
395 if ((ret = mbedtls_sha512_update_ret(&ctx->accumulator, buf,
396 MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100397 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 }
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200399
400 /*
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100401 * Perform second SHA-512 on entropy
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200402 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403 if ((ret = mbedtls_sha512_ret(buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
404 buf, 0)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100405 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100406 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100408 if ((ret = mbedtls_sha256_finish_ret(&ctx->accumulator, buf)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100409 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 }
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200411
412 /*
413 * Reset accumulator and counters and recycle existing entropy
414 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100415 mbedtls_sha256_free(&ctx->accumulator);
416 mbedtls_sha256_init(&ctx->accumulator);
417 if ((ret = mbedtls_sha256_starts_ret(&ctx->accumulator, 0)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100418 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100419 }
420 if ((ret = mbedtls_sha256_update_ret(&ctx->accumulator, buf,
421 MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100422 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 }
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100424
425 /*
426 * Perform second SHA-256 on entropy
427 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 if ((ret = mbedtls_sha256_ret(buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
429 buf, 0)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100430 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100431 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 for (i = 0; i < ctx->source_count; i++) {
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000435 ctx->source[i].size = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100436 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100438 memcpy(output, buf, len);
Paul Bakker6083fd22011-12-03 21:45:14 +0000439
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200440 ret = 0;
441
442exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100443 mbedtls_platform_zeroize(buf, sizeof(buf));
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
447 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
448 }
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200449#endif
450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000452}
453
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100454#if defined(MBEDTLS_ENTROPY_NV_SEED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455int mbedtls_entropy_update_nv_seed(mbedtls_entropy_context *ctx)
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100456{
457 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Andres Amaya Garciaaf0b31d2017-07-05 14:23:54 +0100458 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100459
460 /* Read new seed and write it to NV */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 if ((ret = mbedtls_entropy_func(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
462 return ret;
463 }
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100464
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100465 if (mbedtls_nv_seed_write(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) < 0) {
466 return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
467 }
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100468
469 /* Manually update the remaining stream with a separator value to diverge */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100470 memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
471 ret = mbedtls_entropy_update_manual(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100473 return ret;
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100474}
475#endif /* MBEDTLS_ENTROPY_NV_SEED */
476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#if defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478int mbedtls_entropy_write_seed_file(mbedtls_entropy_context *ctx, const char *path)
Paul Bakker66ff70d2014-03-26 11:54:05 +0100479{
Victor Krasnoshchoka0c2d192020-09-03 00:07:05 +0300480 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Victor Krasnoshchoke79812e2020-08-27 00:19:55 +0300481 FILE *f = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100484 if ((ret = mbedtls_entropy_func(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
Victor Krasnoshchokb3129ba2020-08-29 22:54:37 +0300485 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100486 goto exit;
Victor Krasnoshchokb3129ba2020-08-29 22:54:37 +0300487 }
488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100489 if ((f = fopen(path, "wb")) == NULL) {
Victor Krasnoshchokb3129ba2020-08-29 22:54:37 +0300490 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
491 goto exit;
492 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100494 if (fwrite(buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f) != MBEDTLS_ENTROPY_BLOCK_SIZE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100496 goto exit;
497 }
498
499 ret = 0;
500
501exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100502 mbedtls_platform_zeroize(buf, sizeof(buf));
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100504 if (f != NULL) {
505 fclose(f);
506 }
Victor Krasnoshchoke79812e2020-08-27 00:19:55 +0300507
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100508 return ret;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100509}
510
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100511int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *path)
Paul Bakker66ff70d2014-03-26 11:54:05 +0100512{
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100513 int ret = 0;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100514 FILE *f;
515 size_t n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516 unsigned char buf[MBEDTLS_ENTROPY_MAX_SEED_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100518 if ((f = fopen(path, "rb")) == NULL) {
519 return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
520 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100522 fseek(f, 0, SEEK_END);
523 n = (size_t) ftell(f);
524 fseek(f, 0, SEEK_SET);
Paul Bakker66ff70d2014-03-26 11:54:05 +0100525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100526 if (n > MBEDTLS_ENTROPY_MAX_SEED_SIZE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100528 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100530 if (fread(buf, 1, n, f) != n) {
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100531 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100532 } else {
533 ret = mbedtls_entropy_update_manual(ctx, buf, n);
534 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100536 fclose(f);
Paul Bakker66ff70d2014-03-26 11:54:05 +0100537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 mbedtls_platform_zeroize(buf, sizeof(buf));
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100540 if (ret != 0) {
541 return ret;
542 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100544 return mbedtls_entropy_write_seed_file(ctx, path);
Paul Bakker66ff70d2014-03-26 11:54:05 +0100545}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#endif /* MBEDTLS_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548#if defined(MBEDTLS_SELF_TEST)
Simon Butcher669c6352016-09-15 18:57:34 +0100549#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200550/*
551 * Dummy source function
552 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100553static int entropy_dummy_source(void *data, unsigned char *output,
554 size_t len, size_t *olen)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200555{
556 ((void) data);
557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100558 memset(output, 0x2a, len);
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200559 *olen = len;
560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100561 return 0;
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200562}
Simon Butcher669c6352016-09-15 18:57:34 +0100563#endif /* !MBEDTLS_TEST_NULL_ENTROPY */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200564
Andres AGe7723ec2016-08-25 10:18:50 +0100565#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Andres AGb34e42e2016-08-22 11:08:50 +0100566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100567static int mbedtls_entropy_source_self_test_gather(unsigned char *buf, size_t buf_len)
Andres AGe7723ec2016-08-25 10:18:50 +0100568{
569 int ret = 0;
570 size_t entropy_len = 0;
571 size_t olen = 0;
572 size_t attempts = buf_len;
573
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100574 while (attempts > 0 && entropy_len < buf_len) {
575 if ((ret = mbedtls_hardware_poll(NULL, buf + entropy_len,
576 buf_len - entropy_len, &olen)) != 0) {
577 return ret;
578 }
Andres AGe7723ec2016-08-25 10:18:50 +0100579
580 entropy_len += olen;
581 attempts--;
582 }
583
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100584 if (entropy_len < buf_len) {
Andres AGe7723ec2016-08-25 10:18:50 +0100585 ret = 1;
586 }
587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100588 return ret;
Andres AGe7723ec2016-08-25 10:18:50 +0100589}
590
591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100592static int mbedtls_entropy_source_self_test_check_bits(const unsigned char *buf,
593 size_t buf_len)
Andres AGe7723ec2016-08-25 10:18:50 +0100594{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100595 unsigned char set = 0xFF;
Andres AGe7723ec2016-08-25 10:18:50 +0100596 unsigned char unset = 0x00;
597 size_t i;
598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100599 for (i = 0; i < buf_len; i++) {
Andres AGe7723ec2016-08-25 10:18:50 +0100600 set &= buf[i];
601 unset |= buf[i];
602 }
603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100604 return set == 0xFF || unset == 0x00;
Andres AGe7723ec2016-08-25 10:18:50 +0100605}
Andres AGb34e42e2016-08-22 11:08:50 +0100606
607/*
Zachary Fleckenstein4364fc92022-12-09 09:26:42 -0500608 * A test to ensure that the entropy sources are functioning correctly
Andres AGe7723ec2016-08-25 10:18:50 +0100609 * and there is no obvious failure. The test performs the following checks:
Andres AGb34e42e2016-08-22 11:08:50 +0100610 * - The entropy source is not providing only 0s (all bits unset) or 1s (all
611 * bits set).
612 * - The entropy source is not providing values in a pattern. Because the
Andres AGe7723ec2016-08-25 10:18:50 +0100613 * hardware could be providing data in an arbitrary length, this check polls
614 * the hardware entropy source twice and compares the result to ensure they
615 * are not equal.
Andres AGb34e42e2016-08-22 11:08:50 +0100616 * - The error code returned by the entropy source is not an error.
617 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100618int mbedtls_entropy_source_self_test(int verbose)
Andres AGb34e42e2016-08-22 11:08:50 +0100619{
620 int ret = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100621 unsigned char buf0[2 * sizeof(unsigned long long int)];
622 unsigned char buf1[2 * sizeof(unsigned long long int)];
Andres AGb34e42e2016-08-22 11:08:50 +0100623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100624 if (verbose != 0) {
625 mbedtls_printf(" ENTROPY_BIAS test: ");
626 }
Andres AGb34e42e2016-08-22 11:08:50 +0100627
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100628 memset(buf0, 0x00, sizeof(buf0));
629 memset(buf1, 0x00, sizeof(buf1));
Andres AGb34e42e2016-08-22 11:08:50 +0100630
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100631 if ((ret = mbedtls_entropy_source_self_test_gather(buf0, sizeof(buf0))) != 0) {
Andres AGb34e42e2016-08-22 11:08:50 +0100632 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100633 }
634 if ((ret = mbedtls_entropy_source_self_test_gather(buf1, sizeof(buf1))) != 0) {
Andres AGe7723ec2016-08-25 10:18:50 +0100635 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 }
Andres AGe7723ec2016-08-25 10:18:50 +0100637
638 /* Make sure that the returned values are not all 0 or 1 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100639 if ((ret = mbedtls_entropy_source_self_test_check_bits(buf0, sizeof(buf0))) != 0) {
Andres AGe7723ec2016-08-25 10:18:50 +0100640 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100641 }
642 if ((ret = mbedtls_entropy_source_self_test_check_bits(buf1, sizeof(buf1))) != 0) {
Andres AGe7723ec2016-08-25 10:18:50 +0100643 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100644 }
Andres AGb34e42e2016-08-22 11:08:50 +0100645
646 /* Make sure that the entropy source is not returning values in a
647 * pattern */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100648 ret = memcmp(buf0, buf1, sizeof(buf0)) == 0;
Andres AGb34e42e2016-08-22 11:08:50 +0100649
650cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100651 if (verbose != 0) {
652 if (ret != 0) {
653 mbedtls_printf("failed\n");
654 } else {
655 mbedtls_printf("passed\n");
656 }
Andres AGb34e42e2016-08-22 11:08:50 +0100657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100658 mbedtls_printf("\n");
Andres AGb34e42e2016-08-22 11:08:50 +0100659 }
660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100661 return ret != 0;
Andres AGb34e42e2016-08-22 11:08:50 +0100662}
Andres AGe7723ec2016-08-25 10:18:50 +0100663
664#endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
Andres AGb34e42e2016-08-22 11:08:50 +0100665
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200666/*
667 * The actual entropy quality is hard to test, but we can at least
668 * test that the functions don't cause errors and write the correct
669 * amount of data to buffers.
670 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100671int mbedtls_entropy_self_test(int verbose)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200672{
Andres Amaya Garciaa928e672016-09-13 13:30:02 +0100673 int ret = 1;
Simon Butcher669c6352016-09-15 18:57:34 +0100674#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_entropy_context ctx;
676 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
677 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200678 size_t i, j;
Simon Butcher669c6352016-09-15 18:57:34 +0100679#endif /* !MBEDTLS_TEST_NULL_ENTROPY */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100681 if (verbose != 0) {
682 mbedtls_printf(" ENTROPY test: ");
683 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200684
Andres Amaya Garciaa928e672016-09-13 13:30:02 +0100685#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100686 mbedtls_entropy_init(&ctx);
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200687
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200688 /* First do a gather to make sure we have default sources */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100689 if ((ret = mbedtls_entropy_gather(&ctx)) != 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200690 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100691 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100693 ret = mbedtls_entropy_add_source(&ctx, entropy_dummy_source, NULL, 16,
694 MBEDTLS_ENTROPY_SOURCE_WEAK);
695 if (ret != 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200696 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100697 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200698
Dave Rodgman18688702023-02-02 12:40:50 +0000699 if ((ret = mbedtls_entropy_update_manual(&ctx, buf, sizeof(buf))) != 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200700 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100701 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200702
703 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 * To test that mbedtls_entropy_func writes correct number of bytes:
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200705 * - use the whole buffer and rely on ASan to detect overruns
706 * - collect entropy 8 times and OR the result in an accumulator:
707 * any byte should then be 0 with probably 2^(-64), so requiring
708 * each of the 32 or 64 bytes to be non-zero has a false failure rate
709 * of at most 2^(-58) which is acceptable.
710 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100711 for (i = 0; i < 8; i++) {
712 if ((ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf))) != 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200713 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100714 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100716 for (j = 0; j < sizeof(buf); j++) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200717 acc[j] |= buf[j];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100718 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200719 }
720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100721 for (j = 0; j < sizeof(buf); j++) {
722 if (acc[j] == 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200723 ret = 1;
724 goto cleanup;
725 }
726 }
727
Andres AGe7723ec2016-08-25 10:18:50 +0100728#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100729 if ((ret = mbedtls_entropy_source_self_test(0)) != 0) {
Andres AGe7723ec2016-08-25 10:18:50 +0100730 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100731 }
Andres AGe7723ec2016-08-25 10:18:50 +0100732#endif
733
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200734cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100735 mbedtls_entropy_free(&ctx);
Andres Amaya Garciaa928e672016-09-13 13:30:02 +0100736#endif /* !MBEDTLS_TEST_NULL_ENTROPY */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100738 if (verbose != 0) {
739 if (ret != 0) {
740 mbedtls_printf("failed\n");
741 } else {
742 mbedtls_printf("passed\n");
743 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200744
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100745 mbedtls_printf("\n");
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200746 }
747
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100748 return ret != 0;
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200749}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752#endif /* MBEDTLS_ENTROPY_C */