blob: 1a4ac96457cde5c0f5d08e809b9718fd95050e50 [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"
Paul Bakker6083fd22011-12-03 21:45:14 +000034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <string.h>
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_FS_IO)
Paul Bakker66ff70d2014-03-26 11:54:05 +010038#include <stdio.h>
39#endif
40
Paul Bakker217efbc2016-07-14 14:30:03 +010041#include "mbedtls/platform.h"
Paul Bakker217efbc2016-07-14 14:30:03 +010042
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/platform.h"
Rich Evans00ab4702015-02-06 13:43:58 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/havege.h"
Paul Bakker28c7e7f2011-12-15 19:49:30 +000047#endif
48
Paul Bakker6083fd22011-12-03 21:45:14 +000049#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
50
David Horstmannceeaeb92023-01-05 15:44:23 +000051void mbedtls_entropy_init(mbedtls_entropy_context *ctx)
Paul Bakker6083fd22011-12-03 21:45:14 +000052{
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +010053 ctx->source_count = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +000054 memset(ctx->source, 0, sizeof(ctx->source));
Paul Bakker6083fd22011-12-03 21:45:14 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_THREADING_C)
David Horstmannceeaeb92023-01-05 15:44:23 +000057 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020058#endif
59
Andres Amaya Garcia95869c42017-06-29 16:31:44 +010060 ctx->accumulator_started = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
David Horstmannceeaeb92023-01-05 15:44:23 +000062 mbedtls_sha512_init(&ctx->accumulator);
Paul Bakkerfb08fd22013-08-27 15:06:26 +020063#else
David Horstmannceeaeb92023-01-05 15:44:23 +000064 mbedtls_sha256_init(&ctx->accumulator);
Paul Bakkerfb08fd22013-08-27 15:06:26 +020065#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_HAVEGE_C)
David Horstmannceeaeb92023-01-05 15:44:23 +000067 mbedtls_havege_init(&ctx->havege_data);
Paul Bakker43655f42011-12-15 20:11:16 +000068#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000069
Hanno Beckerd4a872e2017-09-07 08:09:33 +010070 /* Reminder: Update ENTROPY_HAVE_STRONG in the test files
Hanno Becker47deec42017-07-24 12:27:09 +010071 * when adding more strong entropy sources here. */
Hanno Beckerc6deafc2017-07-23 14:06:42 +010072
Simon Butcherab5df402016-06-11 02:31:21 +010073#if defined(MBEDTLS_TEST_NULL_ENTROPY)
David Horstmannceeaeb92023-01-05 15:44:23 +000074 mbedtls_entropy_add_source(ctx, mbedtls_null_entropy_poll, NULL,
75 1, MBEDTLS_ENTROPY_SOURCE_STRONG);
Janos Follath53de7842016-06-08 15:29:18 +010076#endif
77
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
79#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
David Horstmannceeaeb92023-01-05 15:44:23 +000080 mbedtls_entropy_add_source(ctx, mbedtls_platform_entropy_poll, NULL,
81 MBEDTLS_ENTROPY_MIN_PLATFORM,
82 MBEDTLS_ENTROPY_SOURCE_STRONG);
Paul Bakker6083fd22011-12-03 21:45:14 +000083#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#if defined(MBEDTLS_TIMING_C)
David Horstmannceeaeb92023-01-05 15:44:23 +000085 mbedtls_entropy_add_source(ctx, mbedtls_hardclock_poll, NULL,
86 MBEDTLS_ENTROPY_MIN_HARDCLOCK,
87 MBEDTLS_ENTROPY_SOURCE_WEAK);
Paul Bakker6083fd22011-12-03 21:45:14 +000088#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if defined(MBEDTLS_HAVEGE_C)
David Horstmannceeaeb92023-01-05 15:44:23 +000090 mbedtls_entropy_add_source(ctx, mbedtls_havege_poll, &ctx->havege_data,
91 MBEDTLS_ENTROPY_MIN_HAVEGE,
92 MBEDTLS_ENTROPY_SOURCE_STRONG);
Paul Bakker28c7e7f2011-12-15 19:49:30 +000093#endif
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +020094#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +000095 mbedtls_entropy_add_source(ctx, mbedtls_hardware_poll, NULL,
96 MBEDTLS_ENTROPY_MIN_HARDWARE,
97 MBEDTLS_ENTROPY_SOURCE_STRONG);
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +020098#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +010099#if defined(MBEDTLS_ENTROPY_NV_SEED)
David Horstmannceeaeb92023-01-05 15:44:23 +0000100 mbedtls_entropy_add_source(ctx, mbedtls_nv_seed_poll, NULL,
101 MBEDTLS_ENTROPY_BLOCK_SIZE,
102 MBEDTLS_ENTROPY_SOURCE_STRONG);
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100103 ctx->initial_entropy_run = 0;
Paul Bakker9988d6b2016-06-01 11:29:42 +0100104#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +0000106}
107
David Horstmannceeaeb92023-01-05 15:44:23 +0000108void mbedtls_entropy_free(mbedtls_entropy_context *ctx)
Paul Bakker1ffefac2013-09-28 15:23:03 +0200109{
Gilles Peskineb1583212021-02-22 21:26:54 +0100110 /* If the context was already free, don't call free() again.
111 * This is important for mutexes which don't allow double-free. */
David Horstmannceeaeb92023-01-05 15:44:23 +0000112 if (ctx->accumulator_started == -1) {
Gilles Peskineb1583212021-02-22 21:26:54 +0100113 return;
David Horstmannceeaeb92023-01-05 15:44:23 +0000114 }
Gilles Peskineb1583212021-02-22 21:26:54 +0100115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116#if defined(MBEDTLS_HAVEGE_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000117 mbedtls_havege_free(&ctx->havege_data);
Paul Bakkera317a982014-06-18 16:44:11 +0200118#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119#if defined(MBEDTLS_THREADING_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000120 mbedtls_mutex_free(&ctx->mutex);
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200121#endif
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100122#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
David Horstmannceeaeb92023-01-05 15:44:23 +0000123 mbedtls_sha512_free(&ctx->accumulator);
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100124#else
David Horstmannceeaeb92023-01-05 15:44:23 +0000125 mbedtls_sha256_free(&ctx->accumulator);
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100126#endif
127#if defined(MBEDTLS_ENTROPY_NV_SEED)
128 ctx->initial_entropy_run = 0;
129#endif
130 ctx->source_count = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +0000131 mbedtls_platform_zeroize(ctx->source, sizeof(ctx->source));
Gilles Peskineb1583212021-02-22 21:26:54 +0100132 ctx->accumulator_started = -1;
Paul Bakker1ffefac2013-09-28 15:23:03 +0200133}
134
David Horstmannceeaeb92023-01-05 15:44:23 +0000135int mbedtls_entropy_add_source(mbedtls_entropy_context *ctx,
136 mbedtls_entropy_f_source_ptr f_source, void *p_source,
137 size_t threshold, int strong)
Paul Bakker6083fd22011-12-03 21:45:14 +0000138{
Hanno Becker61937d42017-04-26 15:01:23 +0100139 int idx, ret = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#if defined(MBEDTLS_THREADING_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000142 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
143 return ret;
144 }
Paul Bakker47703a02014-02-06 15:01:20 +0100145#endif
146
Hanno Becker61937d42017-04-26 15:01:23 +0100147 idx = ctx->source_count;
David Horstmannceeaeb92023-01-05 15:44:23 +0000148 if (idx >= MBEDTLS_ENTROPY_MAX_SOURCES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
Paul Bakker47703a02014-02-06 15:01:20 +0100150 goto exit;
151 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000152
Hanno Becker61937d42017-04-26 15:01:23 +0100153 ctx->source[idx].f_source = f_source;
154 ctx->source[idx].p_source = p_source;
155 ctx->source[idx].threshold = threshold;
156 ctx->source[idx].strong = strong;
Paul Bakker6083fd22011-12-03 21:45:14 +0000157
158 ctx->source_count++;
159
Paul Bakker47703a02014-02-06 15:01:20 +0100160exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_THREADING_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000162 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
163 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
164 }
Paul Bakker47703a02014-02-06 15:01:20 +0100165#endif
166
David Horstmannceeaeb92023-01-05 15:44:23 +0000167 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000168}
169
170/*
171 * Entropy accumulator update
172 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000173static int entropy_update(mbedtls_entropy_context *ctx, unsigned char source_id,
174 const unsigned char *data, size_t len)
Paul Bakker6083fd22011-12-03 21:45:14 +0000175{
176 unsigned char header[2];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000178 size_t use_len = len;
179 const unsigned char *p = data;
Jaeden Amero66954e12018-01-25 16:05:54 +0000180 int ret = 0;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200181
David Horstmannceeaeb92023-01-05 15:44:23 +0000182 if (use_len > MBEDTLS_ENTROPY_BLOCK_SIZE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
David Horstmannceeaeb92023-01-05 15:44:23 +0000184 if ((ret = mbedtls_sha512_ret(data, len, tmp, 0)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000185 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +0000186 }
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200187#else
David Horstmannceeaeb92023-01-05 15:44:23 +0000188 if ((ret = mbedtls_sha256_ret(data, len, tmp, 0)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000189 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +0000190 }
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200191#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000192 p = tmp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
Paul Bakker6083fd22011-12-03 21:45:14 +0000194 }
195
196 header[0] = source_id;
197 header[1] = use_len & 0xFF;
198
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100199 /*
200 * Start the accumulator if this has not already happened. Note that
201 * it is sufficient to start the accumulator here only because all calls to
202 * gather entropy eventually execute this code.
203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
David Horstmannceeaeb92023-01-05 15:44:23 +0000205 if (ctx->accumulator_started == 0 &&
206 (ret = mbedtls_sha512_starts_ret(&ctx->accumulator, 0)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000207 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +0000208 } else {
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100209 ctx->accumulator_started = 1;
David Horstmannceeaeb92023-01-05 15:44:23 +0000210 }
211 if ((ret = mbedtls_sha512_update_ret(&ctx->accumulator, header, 2)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000212 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +0000213 }
214 ret = mbedtls_sha512_update_ret(&ctx->accumulator, p, use_len);
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200215#else
David Horstmannceeaeb92023-01-05 15:44:23 +0000216 if (ctx->accumulator_started == 0 &&
217 (ret = mbedtls_sha256_starts_ret(&ctx->accumulator, 0)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000218 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +0000219 } else {
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100220 ctx->accumulator_started = 1;
David Horstmannceeaeb92023-01-05 15:44:23 +0000221 }
222 if ((ret = mbedtls_sha256_update_ret(&ctx->accumulator, header, 2)) != 0) {
Jaeden Amero66954e12018-01-25 16:05:54 +0000223 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +0000224 }
225 ret = mbedtls_sha256_update_ret(&ctx->accumulator, p, use_len);
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200226#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200227
Jaeden Amero66954e12018-01-25 16:05:54 +0000228cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +0000229 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Andres Amaya Garcia65121932017-07-05 15:45:47 +0100230
David Horstmannceeaeb92023-01-05 15:44:23 +0000231 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000232}
233
David Horstmannceeaeb92023-01-05 15:44:23 +0000234int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx,
235 const unsigned char *data, size_t len)
Paul Bakker6083fd22011-12-03 21:45:14 +0000236{
Janos Follath24eed8d2019-11-22 13:21:35 +0000237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker47703a02014-02-06 15:01:20 +0100238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_THREADING_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000240 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
241 return ret;
242 }
Paul Bakker47703a02014-02-06 15:01:20 +0100243#endif
244
David Horstmannceeaeb92023-01-05 15:44:23 +0000245 ret = entropy_update(ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len);
Paul Bakker47703a02014-02-06 15:01:20 +0100246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_THREADING_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000248 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
249 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
250 }
Paul Bakker47703a02014-02-06 15:01:20 +0100251#endif
252
David Horstmannceeaeb92023-01-05 15:44:23 +0000253 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000254}
255
256/*
257 * Run through the different sources to add entropy to our accumulator
258 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000259static int entropy_gather_internal(mbedtls_entropy_context *ctx)
Paul Bakker6083fd22011-12-03 21:45:14 +0000260{
Gilles Peskine006c1b52019-09-30 17:29:54 +0200261 int ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
262 int i;
263 int have_one_strong = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
Paul Bakker6083fd22011-12-03 21:45:14 +0000265 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100266
David Horstmannceeaeb92023-01-05 15:44:23 +0000267 if (ctx->source_count == 0) {
268 return MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED;
269 }
Paul Bakker43655f42011-12-15 20:11:16 +0000270
Paul Bakker6083fd22011-12-03 21:45:14 +0000271 /*
272 * Run through our entropy sources
273 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000274 for (i = 0; i < ctx->source_count; i++) {
275 if (ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG) {
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200276 have_one_strong = 1;
David Horstmannceeaeb92023-01-05 15:44:23 +0000277 }
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200278
Paul Bakker6083fd22011-12-03 21:45:14 +0000279 olen = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +0000280 if ((ret = ctx->source[i].f_source(ctx->source[i].p_source,
281 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen)) != 0) {
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100282 goto cleanup;
Paul Bakker6083fd22011-12-03 21:45:14 +0000283 }
284
285 /*
286 * Add if we actually gathered something
287 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000288 if (olen > 0) {
289 if ((ret = entropy_update(ctx, (unsigned char) i,
290 buf, olen)) != 0) {
291 return ret;
292 }
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000293 ctx->source[i].size += olen;
294 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000295 }
296
David Horstmannceeaeb92023-01-05 15:44:23 +0000297 if (have_one_strong == 0) {
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100298 ret = MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE;
David Horstmannceeaeb92023-01-05 15:44:23 +0000299 }
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200300
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100301cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +0000302 mbedtls_platform_zeroize(buf, sizeof(buf));
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100303
David Horstmannceeaeb92023-01-05 15:44:23 +0000304 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000305}
306
Paul Bakker47703a02014-02-06 15:01:20 +0100307/*
308 * Thread-safe wrapper for entropy_gather_internal()
309 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000310int mbedtls_entropy_gather(mbedtls_entropy_context *ctx)
Paul Bakker47703a02014-02-06 15:01:20 +0100311{
Janos Follath24eed8d2019-11-22 13:21:35 +0000312 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker47703a02014-02-06 15:01:20 +0100313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314#if defined(MBEDTLS_THREADING_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000315 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
316 return ret;
317 }
Paul Bakker47703a02014-02-06 15:01:20 +0100318#endif
319
David Horstmannceeaeb92023-01-05 15:44:23 +0000320 ret = entropy_gather_internal(ctx);
Paul Bakker47703a02014-02-06 15:01:20 +0100321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#if defined(MBEDTLS_THREADING_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000323 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
324 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
325 }
Paul Bakker47703a02014-02-06 15:01:20 +0100326#endif
327
David Horstmannceeaeb92023-01-05 15:44:23 +0000328 return ret;
Paul Bakker47703a02014-02-06 15:01:20 +0100329}
330
David Horstmannceeaeb92023-01-05 15:44:23 +0000331int mbedtls_entropy_func(void *data, unsigned char *output, size_t len)
Paul Bakker6083fd22011-12-03 21:45:14 +0000332{
Gilles Peskine85485c72019-10-08 15:04:16 +0200333 int ret, count = 0, i, thresholds_reached;
334 size_t strong_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
336 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000337
David Horstmannceeaeb92023-01-05 15:44:23 +0000338 if (len > MBEDTLS_ENTROPY_BLOCK_SIZE) {
339 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
340 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000341
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100342#if defined(MBEDTLS_ENTROPY_NV_SEED)
343 /* Update the NV entropy seed before generating any entropy for outside
344 * use.
345 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000346 if (ctx->initial_entropy_run == 0) {
Paul Bakkerfc9c7c82016-06-01 15:25:50 +0100347 ctx->initial_entropy_run = 1;
David Horstmannceeaeb92023-01-05 15:44:23 +0000348 if ((ret = mbedtls_entropy_update_nv_seed(ctx)) != 0) {
349 return ret;
350 }
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100351 }
352#endif
353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#if defined(MBEDTLS_THREADING_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000355 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
356 return ret;
357 }
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200358#endif
359
Paul Bakker6083fd22011-12-03 21:45:14 +0000360 /*
361 * Always gather extra entropy before a call
362 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000363 do {
364 if (count++ > ENTROPY_MAX_LOOP) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200366 goto exit;
367 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000368
David Horstmannceeaeb92023-01-05 15:44:23 +0000369 if ((ret = entropy_gather_internal(ctx)) != 0) {
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200370 goto exit;
David Horstmannceeaeb92023-01-05 15:44:23 +0000371 }
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000372
Gilles Peskine85485c72019-10-08 15:04:16 +0200373 thresholds_reached = 1;
374 strong_size = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +0000375 for (i = 0; i < ctx->source_count; i++) {
376 if (ctx->source[i].size < ctx->source[i].threshold) {
Gilles Peskine85485c72019-10-08 15:04:16 +0200377 thresholds_reached = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +0000378 }
379 if (ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG) {
Gilles Peskine85485c72019-10-08 15:04:16 +0200380 strong_size += ctx->source[i].size;
David Horstmannceeaeb92023-01-05 15:44:23 +0000381 }
Gilles Peskine85485c72019-10-08 15:04:16 +0200382 }
David Horstmannceeaeb92023-01-05 15:44:23 +0000383 } while (!thresholds_reached || strong_size < MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakker6083fd22011-12-03 21:45:14 +0000384
David Horstmannceeaeb92023-01-05 15:44:23 +0000385 memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakker6083fd22011-12-03 21:45:14 +0000386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Andres Amaya Garciab2b063f2017-07-20 16:45:24 +0100388 /*
389 * Note that at this stage it is assumed that the accumulator was started
390 * in a previous call to entropy_update(). If this is not guaranteed, the
391 * code below will fail.
392 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000393 if ((ret = mbedtls_sha512_finish_ret(&ctx->accumulator, buf)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100394 goto exit;
David Horstmannceeaeb92023-01-05 15:44:23 +0000395 }
Paul Bakker9e36f042013-06-30 14:34:05 +0200396
Paul Bakker6083fd22011-12-03 21:45:14 +0000397 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000398 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000399 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000400 mbedtls_sha512_free(&ctx->accumulator);
401 mbedtls_sha512_init(&ctx->accumulator);
402 if ((ret = mbedtls_sha512_starts_ret(&ctx->accumulator, 0)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100403 goto exit;
David Horstmannceeaeb92023-01-05 15:44:23 +0000404 }
405 if ((ret = mbedtls_sha512_update_ret(&ctx->accumulator, buf,
406 MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100407 goto exit;
David Horstmannceeaeb92023-01-05 15:44:23 +0000408 }
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200409
410 /*
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100411 * Perform second SHA-512 on entropy
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200412 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000413 if ((ret = mbedtls_sha512_ret(buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
414 buf, 0)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100415 goto exit;
David Horstmannceeaeb92023-01-05 15:44:23 +0000416 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
David Horstmannceeaeb92023-01-05 15:44:23 +0000418 if ((ret = mbedtls_sha256_finish_ret(&ctx->accumulator, buf)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100419 goto exit;
David Horstmannceeaeb92023-01-05 15:44:23 +0000420 }
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200421
422 /*
423 * Reset accumulator and counters and recycle existing entropy
424 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000425 mbedtls_sha256_free(&ctx->accumulator);
426 mbedtls_sha256_init(&ctx->accumulator);
427 if ((ret = mbedtls_sha256_starts_ret(&ctx->accumulator, 0)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100428 goto exit;
David Horstmannceeaeb92023-01-05 15:44:23 +0000429 }
430 if ((ret = mbedtls_sha256_update_ret(&ctx->accumulator, buf,
431 MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100432 goto exit;
David Horstmannceeaeb92023-01-05 15:44:23 +0000433 }
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100434
435 /*
436 * Perform second SHA-256 on entropy
437 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000438 if ((ret = mbedtls_sha256_ret(buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
439 buf, 0)) != 0) {
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100440 goto exit;
David Horstmannceeaeb92023-01-05 15:44:23 +0000441 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000443
David Horstmannceeaeb92023-01-05 15:44:23 +0000444 for (i = 0; i < ctx->source_count; i++) {
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000445 ctx->source[i].size = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +0000446 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000447
David Horstmannceeaeb92023-01-05 15:44:23 +0000448 memcpy(output, buf, len);
Paul Bakker6083fd22011-12-03 21:45:14 +0000449
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200450 ret = 0;
451
452exit:
David Horstmannceeaeb92023-01-05 15:44:23 +0000453 mbedtls_platform_zeroize(buf, sizeof(buf));
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455#if defined(MBEDTLS_THREADING_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000456 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
457 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
458 }
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200459#endif
460
David Horstmannceeaeb92023-01-05 15:44:23 +0000461 return ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000462}
463
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100464#if defined(MBEDTLS_ENTROPY_NV_SEED)
David Horstmannceeaeb92023-01-05 15:44:23 +0000465int mbedtls_entropy_update_nv_seed(mbedtls_entropy_context *ctx)
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100466{
467 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Andres Amaya Garciaaf0b31d2017-07-05 14:23:54 +0100468 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100469
470 /* Read new seed and write it to NV */
David Horstmannceeaeb92023-01-05 15:44:23 +0000471 if ((ret = mbedtls_entropy_func(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
472 return ret;
473 }
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100474
David Horstmannceeaeb92023-01-05 15:44:23 +0000475 if (mbedtls_nv_seed_write(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) < 0) {
476 return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
477 }
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100478
479 /* Manually update the remaining stream with a separator value to diverge */
David Horstmannceeaeb92023-01-05 15:44:23 +0000480 memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
481 ret = mbedtls_entropy_update_manual(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100482
David Horstmannceeaeb92023-01-05 15:44:23 +0000483 return ret;
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100484}
485#endif /* MBEDTLS_ENTROPY_NV_SEED */
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#if defined(MBEDTLS_FS_IO)
David Horstmannceeaeb92023-01-05 15:44:23 +0000488int mbedtls_entropy_write_seed_file(mbedtls_entropy_context *ctx, const char *path)
Paul Bakker66ff70d2014-03-26 11:54:05 +0100489{
Victor Krasnoshchoka0c2d192020-09-03 00:07:05 +0300490 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Victor Krasnoshchoke79812e2020-08-27 00:19:55 +0300491 FILE *f = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100493
David Horstmannceeaeb92023-01-05 15:44:23 +0000494 if ((ret = mbedtls_entropy_func(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
Victor Krasnoshchokb3129ba2020-08-29 22:54:37 +0300495 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100496 goto exit;
Victor Krasnoshchokb3129ba2020-08-29 22:54:37 +0300497 }
498
David Horstmannceeaeb92023-01-05 15:44:23 +0000499 if ((f = fopen(path, "wb")) == NULL) {
Victor Krasnoshchokb3129ba2020-08-29 22:54:37 +0300500 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
501 goto exit;
502 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100503
David Horstmannceeaeb92023-01-05 15:44:23 +0000504 if (fwrite(buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f) != MBEDTLS_ENTROPY_BLOCK_SIZE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100506 goto exit;
507 }
508
509 ret = 0;
510
511exit:
David Horstmannceeaeb92023-01-05 15:44:23 +0000512 mbedtls_platform_zeroize(buf, sizeof(buf));
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100513
David Horstmannceeaeb92023-01-05 15:44:23 +0000514 if (f != NULL) {
515 fclose(f);
516 }
Victor Krasnoshchoke79812e2020-08-27 00:19:55 +0300517
David Horstmannceeaeb92023-01-05 15:44:23 +0000518 return ret;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100519}
520
David Horstmannceeaeb92023-01-05 15:44:23 +0000521int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *path)
Paul Bakker66ff70d2014-03-26 11:54:05 +0100522{
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100523 int ret = 0;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100524 FILE *f;
525 size_t n;
David Horstmannceeaeb92023-01-05 15:44:23 +0000526 unsigned char buf[MBEDTLS_ENTROPY_MAX_SEED_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100527
David Horstmannceeaeb92023-01-05 15:44:23 +0000528 if ((f = fopen(path, "rb")) == NULL) {
529 return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
530 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100531
David Horstmannceeaeb92023-01-05 15:44:23 +0000532 fseek(f, 0, SEEK_END);
533 n = (size_t) ftell(f);
534 fseek(f, 0, SEEK_SET);
Paul Bakker66ff70d2014-03-26 11:54:05 +0100535
David Horstmannceeaeb92023-01-05 15:44:23 +0000536 if (n > MBEDTLS_ENTROPY_MAX_SEED_SIZE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
David Horstmannceeaeb92023-01-05 15:44:23 +0000538 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100539
David Horstmannceeaeb92023-01-05 15:44:23 +0000540 if (fread(buf, 1, n, f) != n) {
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100541 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
David Horstmannceeaeb92023-01-05 15:44:23 +0000542 } else {
543 ret = mbedtls_entropy_update_manual(ctx, buf, n);
544 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100545
David Horstmannceeaeb92023-01-05 15:44:23 +0000546 fclose(f);
Paul Bakker66ff70d2014-03-26 11:54:05 +0100547
David Horstmannceeaeb92023-01-05 15:44:23 +0000548 mbedtls_platform_zeroize(buf, sizeof(buf));
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100549
David Horstmannceeaeb92023-01-05 15:44:23 +0000550 if (ret != 0) {
551 return ret;
552 }
Paul Bakker66ff70d2014-03-26 11:54:05 +0100553
David Horstmannceeaeb92023-01-05 15:44:23 +0000554 return mbedtls_entropy_write_seed_file(ctx, path);
Paul Bakker66ff70d2014-03-26 11:54:05 +0100555}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556#endif /* MBEDTLS_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558#if defined(MBEDTLS_SELF_TEST)
Simon Butcher669c6352016-09-15 18:57:34 +0100559#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200560/*
561 * Dummy source function
562 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000563static int entropy_dummy_source(void *data, unsigned char *output,
564 size_t len, size_t *olen)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200565{
566 ((void) data);
567
David Horstmannceeaeb92023-01-05 15:44:23 +0000568 memset(output, 0x2a, len);
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200569 *olen = len;
570
David Horstmannceeaeb92023-01-05 15:44:23 +0000571 return 0;
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200572}
Simon Butcher669c6352016-09-15 18:57:34 +0100573#endif /* !MBEDTLS_TEST_NULL_ENTROPY */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200574
Andres AGe7723ec2016-08-25 10:18:50 +0100575#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Andres AGb34e42e2016-08-22 11:08:50 +0100576
David Horstmannceeaeb92023-01-05 15:44:23 +0000577static int mbedtls_entropy_source_self_test_gather(unsigned char *buf, size_t buf_len)
Andres AGe7723ec2016-08-25 10:18:50 +0100578{
579 int ret = 0;
580 size_t entropy_len = 0;
581 size_t olen = 0;
582 size_t attempts = buf_len;
583
David Horstmannceeaeb92023-01-05 15:44:23 +0000584 while (attempts > 0 && entropy_len < buf_len) {
585 if ((ret = mbedtls_hardware_poll(NULL, buf + entropy_len,
586 buf_len - entropy_len, &olen)) != 0) {
587 return ret;
588 }
Andres AGe7723ec2016-08-25 10:18:50 +0100589
590 entropy_len += olen;
591 attempts--;
592 }
593
David Horstmannceeaeb92023-01-05 15:44:23 +0000594 if (entropy_len < buf_len) {
Andres AGe7723ec2016-08-25 10:18:50 +0100595 ret = 1;
596 }
597
David Horstmannceeaeb92023-01-05 15:44:23 +0000598 return ret;
Andres AGe7723ec2016-08-25 10:18:50 +0100599}
600
601
David Horstmannceeaeb92023-01-05 15:44:23 +0000602static int mbedtls_entropy_source_self_test_check_bits(const unsigned char *buf,
603 size_t buf_len)
Andres AGe7723ec2016-08-25 10:18:50 +0100604{
David Horstmannceeaeb92023-01-05 15:44:23 +0000605 unsigned char set = 0xFF;
Andres AGe7723ec2016-08-25 10:18:50 +0100606 unsigned char unset = 0x00;
607 size_t i;
608
David Horstmannceeaeb92023-01-05 15:44:23 +0000609 for (i = 0; i < buf_len; i++) {
Andres AGe7723ec2016-08-25 10:18:50 +0100610 set &= buf[i];
611 unset |= buf[i];
612 }
613
David Horstmannceeaeb92023-01-05 15:44:23 +0000614 return set == 0xFF || unset == 0x00;
Andres AGe7723ec2016-08-25 10:18:50 +0100615}
Andres AGb34e42e2016-08-22 11:08:50 +0100616
617/*
Zachary Fleckenstein4364fc92022-12-09 09:26:42 -0500618 * A test to ensure that the entropy sources are functioning correctly
Andres AGe7723ec2016-08-25 10:18:50 +0100619 * and there is no obvious failure. The test performs the following checks:
Andres AGb34e42e2016-08-22 11:08:50 +0100620 * - The entropy source is not providing only 0s (all bits unset) or 1s (all
621 * bits set).
622 * - The entropy source is not providing values in a pattern. Because the
Andres AGe7723ec2016-08-25 10:18:50 +0100623 * hardware could be providing data in an arbitrary length, this check polls
624 * the hardware entropy source twice and compares the result to ensure they
625 * are not equal.
Andres AGb34e42e2016-08-22 11:08:50 +0100626 * - The error code returned by the entropy source is not an error.
627 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000628int mbedtls_entropy_source_self_test(int verbose)
Andres AGb34e42e2016-08-22 11:08:50 +0100629{
630 int ret = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +0000631 unsigned char buf0[2 * sizeof(unsigned long long int)];
632 unsigned char buf1[2 * sizeof(unsigned long long int)];
Andres AGb34e42e2016-08-22 11:08:50 +0100633
David Horstmannceeaeb92023-01-05 15:44:23 +0000634 if (verbose != 0) {
635 mbedtls_printf(" ENTROPY_BIAS test: ");
636 }
Andres AGb34e42e2016-08-22 11:08:50 +0100637
David Horstmannceeaeb92023-01-05 15:44:23 +0000638 memset(buf0, 0x00, sizeof(buf0));
639 memset(buf1, 0x00, sizeof(buf1));
Andres AGb34e42e2016-08-22 11:08:50 +0100640
David Horstmannceeaeb92023-01-05 15:44:23 +0000641 if ((ret = mbedtls_entropy_source_self_test_gather(buf0, sizeof(buf0))) != 0) {
Andres AGb34e42e2016-08-22 11:08:50 +0100642 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +0000643 }
644 if ((ret = mbedtls_entropy_source_self_test_gather(buf1, sizeof(buf1))) != 0) {
Andres AGe7723ec2016-08-25 10:18:50 +0100645 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +0000646 }
Andres AGe7723ec2016-08-25 10:18:50 +0100647
648 /* Make sure that the returned values are not all 0 or 1 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000649 if ((ret = mbedtls_entropy_source_self_test_check_bits(buf0, sizeof(buf0))) != 0) {
Andres AGe7723ec2016-08-25 10:18:50 +0100650 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +0000651 }
652 if ((ret = mbedtls_entropy_source_self_test_check_bits(buf1, sizeof(buf1))) != 0) {
Andres AGe7723ec2016-08-25 10:18:50 +0100653 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +0000654 }
Andres AGb34e42e2016-08-22 11:08:50 +0100655
656 /* Make sure that the entropy source is not returning values in a
657 * pattern */
David Horstmannceeaeb92023-01-05 15:44:23 +0000658 ret = memcmp(buf0, buf1, sizeof(buf0)) == 0;
Andres AGb34e42e2016-08-22 11:08:50 +0100659
660cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +0000661 if (verbose != 0) {
662 if (ret != 0) {
663 mbedtls_printf("failed\n");
664 } else {
665 mbedtls_printf("passed\n");
666 }
Andres AGb34e42e2016-08-22 11:08:50 +0100667
David Horstmannceeaeb92023-01-05 15:44:23 +0000668 mbedtls_printf("\n");
Andres AGb34e42e2016-08-22 11:08:50 +0100669 }
670
David Horstmannceeaeb92023-01-05 15:44:23 +0000671 return ret != 0;
Andres AGb34e42e2016-08-22 11:08:50 +0100672}
Andres AGe7723ec2016-08-25 10:18:50 +0100673
674#endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
Andres AGb34e42e2016-08-22 11:08:50 +0100675
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200676/*
677 * The actual entropy quality is hard to test, but we can at least
678 * test that the functions don't cause errors and write the correct
679 * amount of data to buffers.
680 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000681int mbedtls_entropy_self_test(int verbose)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200682{
Andres Amaya Garciaa928e672016-09-13 13:30:02 +0100683 int ret = 1;
Simon Butcher669c6352016-09-15 18:57:34 +0100684#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_entropy_context ctx;
686 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
687 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200688 size_t i, j;
Simon Butcher669c6352016-09-15 18:57:34 +0100689#endif /* !MBEDTLS_TEST_NULL_ENTROPY */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200690
David Horstmannceeaeb92023-01-05 15:44:23 +0000691 if (verbose != 0) {
692 mbedtls_printf(" ENTROPY test: ");
693 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200694
Andres Amaya Garciaa928e672016-09-13 13:30:02 +0100695#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
David Horstmannceeaeb92023-01-05 15:44:23 +0000696 mbedtls_entropy_init(&ctx);
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200697
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200698 /* First do a gather to make sure we have default sources */
David Horstmannceeaeb92023-01-05 15:44:23 +0000699 if ((ret = mbedtls_entropy_gather(&ctx)) != 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200700 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +0000701 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200702
David Horstmannceeaeb92023-01-05 15:44:23 +0000703 ret = mbedtls_entropy_add_source(&ctx, entropy_dummy_source, NULL, 16,
704 MBEDTLS_ENTROPY_SOURCE_WEAK);
705 if (ret != 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200706 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +0000707 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200708
David Horstmannceeaeb92023-01-05 15:44:23 +0000709 if ((ret = mbedtls_entropy_update_manual(&ctx, buf, sizeof buf)) != 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200710 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +0000711 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200712
713 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 * To test that mbedtls_entropy_func writes correct number of bytes:
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200715 * - use the whole buffer and rely on ASan to detect overruns
716 * - collect entropy 8 times and OR the result in an accumulator:
717 * any byte should then be 0 with probably 2^(-64), so requiring
718 * each of the 32 or 64 bytes to be non-zero has a false failure rate
719 * of at most 2^(-58) which is acceptable.
720 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000721 for (i = 0; i < 8; i++) {
722 if ((ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf))) != 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200723 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +0000724 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200725
David Horstmannceeaeb92023-01-05 15:44:23 +0000726 for (j = 0; j < sizeof(buf); j++) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200727 acc[j] |= buf[j];
David Horstmannceeaeb92023-01-05 15:44:23 +0000728 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200729 }
730
David Horstmannceeaeb92023-01-05 15:44:23 +0000731 for (j = 0; j < sizeof(buf); j++) {
732 if (acc[j] == 0) {
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200733 ret = 1;
734 goto cleanup;
735 }
736 }
737
Andres AGe7723ec2016-08-25 10:18:50 +0100738#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
David Horstmannceeaeb92023-01-05 15:44:23 +0000739 if ((ret = mbedtls_entropy_source_self_test(0)) != 0) {
Andres AGe7723ec2016-08-25 10:18:50 +0100740 goto cleanup;
David Horstmannceeaeb92023-01-05 15:44:23 +0000741 }
Andres AGe7723ec2016-08-25 10:18:50 +0100742#endif
743
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200744cleanup:
David Horstmannceeaeb92023-01-05 15:44:23 +0000745 mbedtls_entropy_free(&ctx);
Andres Amaya Garciaa928e672016-09-13 13:30:02 +0100746#endif /* !MBEDTLS_TEST_NULL_ENTROPY */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200747
David Horstmannceeaeb92023-01-05 15:44:23 +0000748 if (verbose != 0) {
749 if (ret != 0) {
750 mbedtls_printf("failed\n");
751 } else {
752 mbedtls_printf("passed\n");
753 }
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200754
David Horstmannceeaeb92023-01-05 15:44:23 +0000755 mbedtls_printf("\n");
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200756 }
757
David Horstmannceeaeb92023-01-05 15:44:23 +0000758 return ret != 0;
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200759}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#endif /* MBEDTLS_ENTROPY_C */