blob: e9a7ae63d37be320a721736e1bb16bcbeb0c0128 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker6083fd22011-12-03 21:45:14 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000023
Simon Butcherab5df402016-06-11 02:31:21 +010024#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher1ceab6e2016-06-21 10:14:00 +010025#warning "**** WARNING! MBEDTLS_TEST_NULL_ENTROPY defined! "
26#warning "**** THIS BUILD HAS NO DEFINED ENTROPY SOURCES "
27#warning "**** THIS BUILD IS *NOT* SUITABLE FOR PRODUCTION USE "
Simon Butcherab5df402016-06-11 02:31:21 +010028#endif
29
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/entropy.h"
31#include "mbedtls/entropy_poll.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050032#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000033#include "mbedtls/error.h"
Dave Rodgmanbae79fa2023-06-28 11:54:53 +010034#include "mbedtls/sha256.h"
35#include "mbedtls/sha512.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000036
Rich Evans00ab4702015-02-06 13:43:58 +000037#include <string.h>
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_FS_IO)
Paul Bakker66ff70d2014-03-26 11:54:05 +010040#include <stdio.h>
41#endif
42
Paul Bakker217efbc2016-07-14 14:30:03 +010043#include "mbedtls/platform.h"
Paul Bakker217efbc2016-07-14 14:30:03 +010044
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/platform.h"
Rich Evans00ab4702015-02-06 13:43:58 +000046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/havege.h"
Paul Bakker28c7e7f2011-12-15 19:49:30 +000049#endif
50
Paul Bakker6083fd22011-12-03 21:45:14 +000051#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
52
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053void mbedtls_entropy_init(mbedtls_entropy_context *ctx)
Paul Bakker6083fd22011-12-03 21:45:14 +000054{
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +010055 ctx->source_count = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010056 memset(ctx->source, 0, sizeof(ctx->source));
Paul Bakker6083fd22011-12-03 21:45:14 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010059 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020060#endif
61
Andres Amaya Garcia95869c42017-06-29 16:31:44 +010062 ctx->accumulator_started = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064 mbedtls_sha512_init(&ctx->accumulator);
Paul Bakkerfb08fd22013-08-27 15:06:26 +020065#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010066 mbedtls_sha256_init(&ctx->accumulator);
Paul Bakkerfb08fd22013-08-27 15:06:26 +020067#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_HAVEGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069 mbedtls_havege_init(&ctx->havege_data);
Paul Bakker43655f42011-12-15 20:11:16 +000070#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000071
Hanno Beckerd4a872e2017-09-07 08:09:33 +010072 /* Reminder: Update ENTROPY_HAVE_STRONG in the test files
Hanno Becker47deec42017-07-24 12:27:09 +010073 * when adding more strong entropy sources here. */
Hanno Beckerc6deafc2017-07-23 14:06:42 +010074
Simon Butcherab5df402016-06-11 02:31:21 +010075#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 mbedtls_entropy_add_source(ctx, mbedtls_null_entropy_poll, NULL,
77 1, MBEDTLS_ENTROPY_SOURCE_STRONG);
Janos Follath53de7842016-06-08 15:29:18 +010078#endif
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
81#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082 mbedtls_entropy_add_source(ctx, mbedtls_platform_entropy_poll, NULL,
83 MBEDTLS_ENTROPY_MIN_PLATFORM,
84 MBEDTLS_ENTROPY_SOURCE_STRONG);
Paul Bakker6083fd22011-12-03 21:45:14 +000085#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if defined(MBEDTLS_TIMING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087 mbedtls_entropy_add_source(ctx, mbedtls_hardclock_poll, NULL,
88 MBEDTLS_ENTROPY_MIN_HARDCLOCK,
89 MBEDTLS_ENTROPY_SOURCE_WEAK);
Paul Bakker6083fd22011-12-03 21:45:14 +000090#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#if defined(MBEDTLS_HAVEGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 mbedtls_entropy_add_source(ctx, mbedtls_havege_poll, &ctx->havege_data,
93 MBEDTLS_ENTROPY_MIN_HAVEGE,
94 MBEDTLS_ENTROPY_SOURCE_STRONG);
Paul Bakker28c7e7f2011-12-15 19:49:30 +000095#endif
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +020096#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 mbedtls_entropy_add_source(ctx, mbedtls_hardware_poll, NULL,
98 MBEDTLS_ENTROPY_MIN_HARDWARE,
99 MBEDTLS_ENTROPY_SOURCE_STRONG);
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +0200100#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +0100101#if defined(MBEDTLS_ENTROPY_NV_SEED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102 mbedtls_entropy_add_source(ctx, mbedtls_nv_seed_poll, NULL,
103 MBEDTLS_ENTROPY_BLOCK_SIZE,
104 MBEDTLS_ENTROPY_SOURCE_STRONG);
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100105 ctx->initial_entropy_run = 0;
Paul Bakker9988d6b2016-06-01 11:29:42 +0100106#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +0000108}
109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110void mbedtls_entropy_free(mbedtls_entropy_context *ctx)
Paul Bakker1ffefac2013-09-28 15:23:03 +0200111{
Gilles Peskineb1583212021-02-22 21:26:54 +0100112 /* If the context was already free, don't call free() again.
113 * This is important for mutexes which don't allow double-free. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 if (ctx->accumulator_started == -1) {
Gilles Peskineb1583212021-02-22 21:26:54 +0100115 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 }
Gilles Peskineb1583212021-02-22 21:26:54 +0100117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_HAVEGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 mbedtls_havege_free(&ctx->havege_data);
Paul Bakkera317a982014-06-18 16:44:11 +0200120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 mbedtls_mutex_free(&ctx->mutex);
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200123#endif
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100124#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 mbedtls_sha512_free(&ctx->accumulator);
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100126#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 mbedtls_sha256_free(&ctx->accumulator);
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100128#endif
129#if defined(MBEDTLS_ENTROPY_NV_SEED)
130 ctx->initial_entropy_run = 0;
131#endif
132 ctx->source_count = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 mbedtls_platform_zeroize(ctx->source, sizeof(ctx->source));
Gilles Peskineb1583212021-02-22 21:26:54 +0100134 ctx->accumulator_started = -1;
Paul Bakker1ffefac2013-09-28 15:23:03 +0200135}
136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137int mbedtls_entropy_add_source(mbedtls_entropy_context *ctx,
138 mbedtls_entropy_f_source_ptr f_source, void *p_source,
139 size_t threshold, int strong)
Paul Bakker6083fd22011-12-03 21:45:14 +0000140{
Hanno Becker61937d42017-04-26 15:01:23 +0100141 int idx, ret = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
145 return ret;
146 }
Paul Bakker47703a02014-02-06 15:01:20 +0100147#endif
148
Hanno Becker61937d42017-04-26 15:01:23 +0100149 idx = ctx->source_count;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 if (idx >= MBEDTLS_ENTROPY_MAX_SOURCES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
Paul Bakker47703a02014-02-06 15:01:20 +0100152 goto exit;
153 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000154
Hanno Becker61937d42017-04-26 15:01:23 +0100155 ctx->source[idx].f_source = f_source;
156 ctx->source[idx].p_source = p_source;
157 ctx->source[idx].threshold = threshold;
158 ctx->source[idx].strong = strong;
Paul Bakker6083fd22011-12-03 21:45:14 +0000159
160 ctx->source_count++;
161
Paul Bakker47703a02014-02-06 15:01:20 +0100162exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
165 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
166 }
Paul Bakker47703a02014-02-06 15:01:20 +0100167#endif
168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000170}
171
172/*
173 * Entropy accumulator update
174 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175static int entropy_update(mbedtls_entropy_context *ctx, unsigned char source_id,
176 const unsigned char *data, size_t len)
Paul Bakker6083fd22011-12-03 21:45:14 +0000177{
178 unsigned char header[2];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000180 size_t use_len = len;
181 const unsigned char *p = data;
Jaeden Amero66954e12018-01-25 16:05:54 +0000182 int ret = 0;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 if (use_len > MBEDTLS_ENTROPY_BLOCK_SIZE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 if ((ret = mbedtls_sha512_ret(data, len, tmp, 0)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000187 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100188 }
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200189#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 if ((ret = mbedtls_sha256_ret(data, len, tmp, 0)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000191 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 }
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200193#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000194 p = tmp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
Paul Bakker6083fd22011-12-03 21:45:14 +0000196 }
197
198 header[0] = source_id;
199 header[1] = use_len & 0xFF;
200
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100201 /*
202 * Start the accumulator if this has not already happened. Note that
203 * it is sufficient to start the accumulator here only because all calls to
204 * gather entropy eventually execute this code.
205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 if (ctx->accumulator_started == 0 &&
208 (ret = mbedtls_sha512_starts_ret(&ctx->accumulator, 0)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000209 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 } else {
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100211 ctx->accumulator_started = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212 }
213 if ((ret = mbedtls_sha512_update_ret(&ctx->accumulator, header, 2)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000214 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215 }
216 ret = mbedtls_sha512_update_ret(&ctx->accumulator, p, use_len);
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200217#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 if (ctx->accumulator_started == 0 &&
219 (ret = mbedtls_sha256_starts_ret(&ctx->accumulator, 0)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000220 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 } else {
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100222 ctx->accumulator_started = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 }
224 if ((ret = mbedtls_sha256_update_ret(&ctx->accumulator, header, 2)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000225 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 }
227 ret = mbedtls_sha256_update_ret(&ctx->accumulator, p, use_len);
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200228#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200229
Jaeden Amero66954e12018-01-25 16:05:54 +0000230cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Andres Amaya Garcia65121932017-07-05 15:45:47 +0100232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000234}
235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx,
237 const unsigned char *data, size_t len)
Paul Bakker6083fd22011-12-03 21:45:14 +0000238{
Janos Follath24eed8d2019-11-22 13:21:35 +0000239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker47703a02014-02-06 15:01:20 +0100240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
243 return ret;
244 }
Paul Bakker47703a02014-02-06 15:01:20 +0100245#endif
246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 ret = entropy_update(ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len);
Paul Bakker47703a02014-02-06 15:01:20 +0100248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
251 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
252 }
Paul Bakker47703a02014-02-06 15:01:20 +0100253#endif
254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000256}
257
258/*
259 * Run through the different sources to add entropy to our accumulator
260 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261static int entropy_gather_internal(mbedtls_entropy_context *ctx)
Paul Bakker6083fd22011-12-03 21:45:14 +0000262{
Gilles Peskine006c1b52019-09-30 17:29:54 +0200263 int ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
264 int i;
265 int have_one_strong = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
Paul Bakker6083fd22011-12-03 21:45:14 +0000267 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 if (ctx->source_count == 0) {
270 return MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED;
271 }
Paul Bakker43655f42011-12-15 20:11:16 +0000272
Paul Bakker6083fd22011-12-03 21:45:14 +0000273 /*
274 * Run through our entropy sources
275 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 for (i = 0; i < ctx->source_count; i++) {
277 if (ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG) {
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200278 have_one_strong = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 }
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200280
Paul Bakker6083fd22011-12-03 21:45:14 +0000281 olen = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 if ((ret = ctx->source[i].f_source(ctx->source[i].p_source,
283 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen)) != 0) {
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100284 goto cleanup;
Paul Bakker6083fd22011-12-03 21:45:14 +0000285 }
286
287 /*
288 * Add if we actually gathered something
289 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 if (olen > 0) {
291 if ((ret = entropy_update(ctx, (unsigned char) i,
292 buf, olen)) != 0) {
293 return ret;
294 }
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000295 ctx->source[i].size += olen;
296 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000297 }
298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 if (have_one_strong == 0) {
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100300 ret = MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 }
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200302
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100303cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304 mbedtls_platform_zeroize(buf, sizeof(buf));
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000307}
308
Paul Bakker47703a02014-02-06 15:01:20 +0100309/*
310 * Thread-safe wrapper for entropy_gather_internal()
311 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312int mbedtls_entropy_gather(mbedtls_entropy_context *ctx)
Paul Bakker47703a02014-02-06 15:01:20 +0100313{
Janos Follath24eed8d2019-11-22 13:21:35 +0000314 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker47703a02014-02-06 15:01:20 +0100315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
318 return ret;
319 }
Paul Bakker47703a02014-02-06 15:01:20 +0100320#endif
321
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 ret = entropy_gather_internal(ctx);
Paul Bakker47703a02014-02-06 15:01:20 +0100323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
326 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
327 }
Paul Bakker47703a02014-02-06 15:01:20 +0100328#endif
329
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 return ret;
Paul Bakker47703a02014-02-06 15:01:20 +0100331}
332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333int mbedtls_entropy_func(void *data, unsigned char *output, size_t len)
Paul Bakker6083fd22011-12-03 21:45:14 +0000334{
Gilles Peskine85485c72019-10-08 15:04:16 +0200335 int ret, count = 0, i, thresholds_reached;
336 size_t strong_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
338 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100340 if (len > MBEDTLS_ENTROPY_BLOCK_SIZE) {
341 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
342 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000343
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100344#if defined(MBEDTLS_ENTROPY_NV_SEED)
345 /* Update the NV entropy seed before generating any entropy for outside
346 * use.
347 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 if (ctx->initial_entropy_run == 0) {
Paul Bakkerfc9c7c82016-06-01 15:25:50 +0100349 ctx->initial_entropy_run = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 if ((ret = mbedtls_entropy_update_nv_seed(ctx)) != 0) {
351 return ret;
352 }
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100353 }
354#endif
355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
358 return ret;
359 }
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200360#endif
361
Paul Bakker6083fd22011-12-03 21:45:14 +0000362 /*
363 * Always gather extra entropy before a call
364 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365 do {
366 if (count++ > ENTROPY_MAX_LOOP) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200368 goto exit;
369 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 if ((ret = entropy_gather_internal(ctx)) != 0) {
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200372 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 }
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000374
Gilles Peskine85485c72019-10-08 15:04:16 +0200375 thresholds_reached = 1;
376 strong_size = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100377 for (i = 0; i < ctx->source_count; i++) {
378 if (ctx->source[i].size < ctx->source[i].threshold) {
Gilles Peskine85485c72019-10-08 15:04:16 +0200379 thresholds_reached = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 }
381 if (ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG) {
Gilles Peskine85485c72019-10-08 15:04:16 +0200382 strong_size += ctx->source[i].size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 }
Gilles Peskine85485c72019-10-08 15:04:16 +0200384 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 } while (!thresholds_reached || strong_size < MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakker6083fd22011-12-03 21:45:14 +0000386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387 memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakker6083fd22011-12-03 21:45:14 +0000388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Andres Amaya Garciab2b063f2017-07-20 16:45:24 +0100390 /*
391 * Note that at this stage it is assumed that the accumulator was started
392 * in a previous call to entropy_update(). If this is not guaranteed, the
393 * code below will fail.
394 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 if ((ret = mbedtls_sha512_finish_ret(&ctx->accumulator, buf)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100396 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397 }
Paul Bakker9e36f042013-06-30 14:34:05 +0200398
Paul Bakker6083fd22011-12-03 21:45:14 +0000399 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000400 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000401 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100402 mbedtls_sha512_free(&ctx->accumulator);
403 mbedtls_sha512_init(&ctx->accumulator);
404 if ((ret = mbedtls_sha512_starts_ret(&ctx->accumulator, 0)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100405 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100406 }
407 if ((ret = mbedtls_sha512_update_ret(&ctx->accumulator, buf,
408 MBEDTLS_ENTROPY_BLOCK_SIZE)) != 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 /*
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100413 * Perform second SHA-512 on entropy
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200414 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100415 if ((ret = mbedtls_sha512_ret(buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
416 buf, 0)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100417 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 if ((ret = mbedtls_sha256_finish_ret(&ctx->accumulator, buf)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100421 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422 }
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200423
424 /*
425 * Reset accumulator and counters and recycle existing entropy
426 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100427 mbedtls_sha256_free(&ctx->accumulator);
428 mbedtls_sha256_init(&ctx->accumulator);
429 if ((ret = mbedtls_sha256_starts_ret(&ctx->accumulator, 0)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100430 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100431 }
432 if ((ret = mbedtls_sha256_update_ret(&ctx->accumulator, buf,
433 MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100434 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 }
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100436
437 /*
438 * Perform second SHA-256 on entropy
439 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100440 if ((ret = mbedtls_sha256_ret(buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
441 buf, 0)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100442 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100443 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000445
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446 for (i = 0; i < ctx->source_count; i++) {
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000447 ctx->source[i].size = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000449
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 memcpy(output, buf, len);
Paul Bakker6083fd22011-12-03 21:45:14 +0000451
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200452 ret = 0;
453
454exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455 mbedtls_platform_zeroize(buf, sizeof(buf));
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100458 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
459 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
460 }
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200461#endif
462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000464}
465
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100466#if defined(MBEDTLS_ENTROPY_NV_SEED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100467int mbedtls_entropy_update_nv_seed(mbedtls_entropy_context *ctx)
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100468{
469 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Andres Amaya Garciaaf0b31d2017-07-05 14:23:54 +0100470 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100471
472 /* Read new seed and write it to NV */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100473 if ((ret = mbedtls_entropy_func(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
474 return ret;
475 }
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100476
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100477 if (mbedtls_nv_seed_write(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) < 0) {
478 return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
479 }
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100480
481 /* Manually update the remaining stream with a separator value to diverge */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100482 memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
483 ret = mbedtls_entropy_update_manual(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485 return ret;
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100486}
487#endif /* MBEDTLS_ENTROPY_NV_SEED */
488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489#if defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100490int mbedtls_entropy_write_seed_file(mbedtls_entropy_context *ctx, const char *path)
Paul Bakker66ff70d2014-03-26 11:54:05 +0100491{
Victor Krasnoshchoka0c2d192020-09-03 00:07:05 +0300492 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Victor Krasnoshchoke79812e2020-08-27 00:19:55 +0300493 FILE *f = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100496 if ((ret = mbedtls_entropy_func(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
Victor Krasnoshchokb3129ba2020-08-29 22:54:37 +0300497 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100498 goto exit;
Victor Krasnoshchokb3129ba2020-08-29 22:54:37 +0300499 }
500
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100501 if ((f = fopen(path, "wb")) == NULL) {
Victor Krasnoshchokb3129ba2020-08-29 22:54:37 +0300502 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
503 goto exit;
504 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100505
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100506 if (fwrite(buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f) != MBEDTLS_ENTROPY_BLOCK_SIZE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100508 goto exit;
509 }
510
511 ret = 0;
512
513exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100514 mbedtls_platform_zeroize(buf, sizeof(buf));
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516 if (f != NULL) {
517 fclose(f);
518 }
Victor Krasnoshchoke79812e2020-08-27 00:19:55 +0300519
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100520 return ret;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100521}
522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100523int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *path)
Paul Bakker66ff70d2014-03-26 11:54:05 +0100524{
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100525 int ret = 0;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100526 FILE *f;
527 size_t n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100528 unsigned char buf[MBEDTLS_ENTROPY_MAX_SEED_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100530 if ((f = fopen(path, "rb")) == NULL) {
531 return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
532 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100534 fseek(f, 0, SEEK_END);
535 n = (size_t) ftell(f);
536 fseek(f, 0, SEEK_SET);
Paul Bakker66ff70d2014-03-26 11:54:05 +0100537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 if (n > MBEDTLS_ENTROPY_MAX_SEED_SIZE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100540 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100542 if (fread(buf, 1, n, f) != n) {
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100543 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100544 } else {
545 ret = mbedtls_entropy_update_manual(ctx, buf, n);
546 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100548 fclose(f);
Paul Bakker66ff70d2014-03-26 11:54:05 +0100549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550 mbedtls_platform_zeroize(buf, sizeof(buf));
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100552 if (ret != 0) {
553 return ret;
554 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100556 return mbedtls_entropy_write_seed_file(ctx, path);
Paul Bakker66ff70d2014-03-26 11:54:05 +0100557}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558#endif /* MBEDTLS_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560#if defined(MBEDTLS_SELF_TEST)
Simon Butcher669c6352016-09-15 18:57:34 +0100561#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200562/*
563 * Dummy source function
564 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100565static int entropy_dummy_source(void *data, unsigned char *output,
566 size_t len, size_t *olen)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200567{
568 ((void) data);
569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100570 memset(output, 0x2a, len);
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200571 *olen = len;
572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100573 return 0;
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200574}
Simon Butcher669c6352016-09-15 18:57:34 +0100575#endif /* !MBEDTLS_TEST_NULL_ENTROPY */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200576
Andres AGe7723ec2016-08-25 10:18:50 +0100577#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Andres AGb34e42e2016-08-22 11:08:50 +0100578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100579static int mbedtls_entropy_source_self_test_gather(unsigned char *buf, size_t buf_len)
Andres AGe7723ec2016-08-25 10:18:50 +0100580{
581 int ret = 0;
582 size_t entropy_len = 0;
583 size_t olen = 0;
584 size_t attempts = buf_len;
585
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100586 while (attempts > 0 && entropy_len < buf_len) {
587 if ((ret = mbedtls_hardware_poll(NULL, buf + entropy_len,
588 buf_len - entropy_len, &olen)) != 0) {
589 return ret;
590 }
Andres AGe7723ec2016-08-25 10:18:50 +0100591
592 entropy_len += olen;
593 attempts--;
594 }
595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100596 if (entropy_len < buf_len) {
Andres AGe7723ec2016-08-25 10:18:50 +0100597 ret = 1;
598 }
599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100600 return ret;
Andres AGe7723ec2016-08-25 10:18:50 +0100601}
602
603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100604static int mbedtls_entropy_source_self_test_check_bits(const unsigned char *buf,
605 size_t buf_len)
Andres AGe7723ec2016-08-25 10:18:50 +0100606{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100607 unsigned char set = 0xFF;
Andres AGe7723ec2016-08-25 10:18:50 +0100608 unsigned char unset = 0x00;
609 size_t i;
610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100611 for (i = 0; i < buf_len; i++) {
Andres AGe7723ec2016-08-25 10:18:50 +0100612 set &= buf[i];
613 unset |= buf[i];
614 }
615
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100616 return set == 0xFF || unset == 0x00;
Andres AGe7723ec2016-08-25 10:18:50 +0100617}
Andres AGb34e42e2016-08-22 11:08:50 +0100618
619/*
Zachary Fleckenstein4364fc92022-12-09 09:26:42 -0500620 * A test to ensure that the entropy sources are functioning correctly
Andres AGe7723ec2016-08-25 10:18:50 +0100621 * and there is no obvious failure. The test performs the following checks:
Andres AGb34e42e2016-08-22 11:08:50 +0100622 * - The entropy source is not providing only 0s (all bits unset) or 1s (all
623 * bits set).
624 * - The entropy source is not providing values in a pattern. Because the
Andres AGe7723ec2016-08-25 10:18:50 +0100625 * hardware could be providing data in an arbitrary length, this check polls
626 * the hardware entropy source twice and compares the result to ensure they
627 * are not equal.
Andres AGb34e42e2016-08-22 11:08:50 +0100628 * - The error code returned by the entropy source is not an error.
629 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100630int mbedtls_entropy_source_self_test(int verbose)
Andres AGb34e42e2016-08-22 11:08:50 +0100631{
632 int ret = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100633 unsigned char buf0[2 * sizeof(unsigned long long int)];
634 unsigned char buf1[2 * sizeof(unsigned long long int)];
Andres AGb34e42e2016-08-22 11:08:50 +0100635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 if (verbose != 0) {
637 mbedtls_printf(" ENTROPY_BIAS test: ");
638 }
Andres AGb34e42e2016-08-22 11:08:50 +0100639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100640 memset(buf0, 0x00, sizeof(buf0));
641 memset(buf1, 0x00, sizeof(buf1));
Andres AGb34e42e2016-08-22 11:08:50 +0100642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100643 if ((ret = mbedtls_entropy_source_self_test_gather(buf0, sizeof(buf0))) != 0) {
Andres AGb34e42e2016-08-22 11:08:50 +0100644 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100645 }
646 if ((ret = mbedtls_entropy_source_self_test_gather(buf1, sizeof(buf1))) != 0) {
Andres AGe7723ec2016-08-25 10:18:50 +0100647 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100648 }
Andres AGe7723ec2016-08-25 10:18:50 +0100649
650 /* Make sure that the returned values are not all 0 or 1 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100651 if ((ret = mbedtls_entropy_source_self_test_check_bits(buf0, sizeof(buf0))) != 0) {
Andres AGe7723ec2016-08-25 10:18:50 +0100652 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100653 }
654 if ((ret = mbedtls_entropy_source_self_test_check_bits(buf1, sizeof(buf1))) != 0) {
Andres AGe7723ec2016-08-25 10:18:50 +0100655 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100656 }
Andres AGb34e42e2016-08-22 11:08:50 +0100657
658 /* Make sure that the entropy source is not returning values in a
659 * pattern */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100660 ret = memcmp(buf0, buf1, sizeof(buf0)) == 0;
Andres AGb34e42e2016-08-22 11:08:50 +0100661
662cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100663 if (verbose != 0) {
664 if (ret != 0) {
665 mbedtls_printf("failed\n");
666 } else {
667 mbedtls_printf("passed\n");
668 }
Andres AGb34e42e2016-08-22 11:08:50 +0100669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100670 mbedtls_printf("\n");
Andres AGb34e42e2016-08-22 11:08:50 +0100671 }
672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100673 return ret != 0;
Andres AGb34e42e2016-08-22 11:08:50 +0100674}
Andres AGe7723ec2016-08-25 10:18:50 +0100675
676#endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
Andres AGb34e42e2016-08-22 11:08:50 +0100677
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200678/*
679 * The actual entropy quality is hard to test, but we can at least
680 * test that the functions don't cause errors and write the correct
681 * amount of data to buffers.
682 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100683int mbedtls_entropy_self_test(int verbose)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200684{
Andres Amaya Garciaa928e672016-09-13 13:30:02 +0100685 int ret = 1;
Simon Butcher669c6352016-09-15 18:57:34 +0100686#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_entropy_context ctx;
688 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
689 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200690 size_t i, j;
Simon Butcher669c6352016-09-15 18:57:34 +0100691#endif /* !MBEDTLS_TEST_NULL_ENTROPY */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100693 if (verbose != 0) {
694 mbedtls_printf(" ENTROPY test: ");
695 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200696
Andres Amaya Garciaa928e672016-09-13 13:30:02 +0100697#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100698 mbedtls_entropy_init(&ctx);
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200699
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200700 /* First do a gather to make sure we have default sources */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100701 if ((ret = mbedtls_entropy_gather(&ctx)) != 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200702 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100703 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100705 ret = mbedtls_entropy_add_source(&ctx, entropy_dummy_source, NULL, 16,
706 MBEDTLS_ENTROPY_SOURCE_WEAK);
707 if (ret != 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200708 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100709 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200710
Dave Rodgman18688702023-02-02 12:40:50 +0000711 if ((ret = mbedtls_entropy_update_manual(&ctx, buf, sizeof(buf))) != 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200712 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100713 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200714
715 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 * To test that mbedtls_entropy_func writes correct number of bytes:
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200717 * - use the whole buffer and rely on ASan to detect overruns
718 * - collect entropy 8 times and OR the result in an accumulator:
719 * any byte should then be 0 with probably 2^(-64), so requiring
720 * each of the 32 or 64 bytes to be non-zero has a false failure rate
721 * of at most 2^(-58) which is acceptable.
722 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100723 for (i = 0; i < 8; i++) {
724 if ((ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf))) != 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200725 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100726 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100728 for (j = 0; j < sizeof(buf); j++) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200729 acc[j] |= buf[j];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100730 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200731 }
732
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100733 for (j = 0; j < sizeof(buf); j++) {
734 if (acc[j] == 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200735 ret = 1;
736 goto cleanup;
737 }
738 }
739
Andres AGe7723ec2016-08-25 10:18:50 +0100740#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100741 if ((ret = mbedtls_entropy_source_self_test(0)) != 0) {
Andres AGe7723ec2016-08-25 10:18:50 +0100742 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100743 }
Andres AGe7723ec2016-08-25 10:18:50 +0100744#endif
745
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200746cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100747 mbedtls_entropy_free(&ctx);
Andres Amaya Garciaa928e672016-09-13 13:30:02 +0100748#endif /* !MBEDTLS_TEST_NULL_ENTROPY */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100750 if (verbose != 0) {
751 if (ret != 0) {
752 mbedtls_printf("failed\n");
753 } else {
754 mbedtls_printf("passed\n");
755 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200756
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100757 mbedtls_printf("\n");
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200758 }
759
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100760 return ret != 0;
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200761}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#endif /* MBEDTLS_ENTROPY_C */