blob: f5d7d4021c8796a19b7c57f5e3a019d6d03ecc1e [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Entropy accumulator implementation
3 *
Paul Bakker9988d6b2016-06-01 11:29:42 +01004 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker6083fd22011-12-03 21:45:14 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000029
Simon Butcherab5df402016-06-11 02:31:21 +010030#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher1ceab6e2016-06-21 10:14:00 +010031#warning "**** WARNING! MBEDTLS_TEST_NULL_ENTROPY defined! "
32#warning "**** THIS BUILD HAS NO DEFINED ENTROPY SOURCES "
33#warning "**** THIS BUILD IS *NOT* SUITABLE FOR PRODUCTION USE "
Simon Butcherab5df402016-06-11 02:31:21 +010034#endif
35
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/entropy.h"
37#include "mbedtls/entropy_poll.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050038#include "mbedtls/platform_util.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000039
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <string.h>
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_FS_IO)
Paul Bakker66ff70d2014-03-26 11:54:05 +010043#include <stdio.h>
44#endif
45
Paul Bakker217efbc2016-07-14 14:30:03 +010046#include "mbedtls/platform.h"
Paul Bakker217efbc2016-07-14 14:30:03 +010047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_SELF_TEST)
49#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/platform.h"
Rich Evans00ab4702015-02-06 13:43:58 +000051#else
52#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#define mbedtls_printf printf
54#endif /* MBEDTLS_PLATFORM_C */
55#endif /* MBEDTLS_SELF_TEST */
Rich Evans00ab4702015-02-06 13:43:58 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/havege.h"
Paul Bakker28c7e7f2011-12-15 19:49:30 +000059#endif
60
Paul Bakker6083fd22011-12-03 21:45:14 +000061#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063void mbedtls_entropy_init( mbedtls_entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +000064{
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +010065 ctx->source_count = 0;
Manuel Pégourié-Gonnard99419332019-10-03 10:40:57 +020066 memset( ctx->source, 0, sizeof( ctx->source ) );
Paul Bakker6083fd22011-12-03 21:45:14 +000067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_THREADING_C)
69 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020070#endif
71
Andres Amaya Garcia95869c42017-06-29 16:31:44 +010072 ctx->accumulator_started = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +010074 mbedtls_sha512_init( &ctx->accumulator );
Paul Bakkerfb08fd22013-08-27 15:06:26 +020075#else
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +010076 mbedtls_sha256_init( &ctx->accumulator );
Paul Bakkerfb08fd22013-08-27 15:06:26 +020077#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#if defined(MBEDTLS_HAVEGE_C)
79 mbedtls_havege_init( &ctx->havege_data );
Paul Bakker43655f42011-12-15 20:11:16 +000080#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000081
Hanno Beckerd4a872e2017-09-07 08:09:33 +010082 /* Reminder: Update ENTROPY_HAVE_STRONG in the test files
Hanno Becker47deec42017-07-24 12:27:09 +010083 * when adding more strong entropy sources here. */
Hanno Beckerc6deafc2017-07-23 14:06:42 +010084
Simon Butcherab5df402016-06-11 02:31:21 +010085#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher4157b602016-06-12 00:31:33 +010086 mbedtls_entropy_add_source( ctx, mbedtls_null_entropy_poll, NULL,
Janos Follath53de7842016-06-08 15:29:18 +010087 1, MBEDTLS_ENTROPY_SOURCE_STRONG );
88#endif
89
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
91#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
92 mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020093 MBEDTLS_ENTROPY_MIN_PLATFORM,
94 MBEDTLS_ENTROPY_SOURCE_STRONG );
Paul Bakker6083fd22011-12-03 21:45:14 +000095#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020097 mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL,
98 MBEDTLS_ENTROPY_MIN_HARDCLOCK,
99 MBEDTLS_ENTROPY_SOURCE_WEAK );
Paul Bakker6083fd22011-12-03 21:45:14 +0000100#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#if defined(MBEDTLS_HAVEGE_C)
102 mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200103 MBEDTLS_ENTROPY_MIN_HAVEGE,
104 MBEDTLS_ENTROPY_SOURCE_STRONG );
Paul Bakker28c7e7f2011-12-15 19:49:30 +0000105#endif
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +0200106#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Manuel Pégourié-Gonnardfc2ccfe2015-07-10 11:15:50 +0100107 mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200108 MBEDTLS_ENTROPY_MIN_HARDWARE,
109 MBEDTLS_ENTROPY_SOURCE_STRONG );
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +0200110#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +0100111#if defined(MBEDTLS_ENTROPY_NV_SEED)
112 mbedtls_entropy_add_source( ctx, mbedtls_nv_seed_poll, NULL,
113 MBEDTLS_ENTROPY_BLOCK_SIZE,
114 MBEDTLS_ENTROPY_SOURCE_STRONG );
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100115 ctx->initial_entropy_run = 0;
Paul Bakker9988d6b2016-06-01 11:29:42 +0100116#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +0000118}
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
Paul Bakker1ffefac2013-09-28 15:23:03 +0200121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if defined(MBEDTLS_HAVEGE_C)
123 mbedtls_havege_free( &ctx->havege_data );
Paul Bakkera317a982014-06-18 16:44:11 +0200124#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#if defined(MBEDTLS_THREADING_C)
126 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200127#endif
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100128#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
129 mbedtls_sha512_free( &ctx->accumulator );
130#else
131 mbedtls_sha256_free( &ctx->accumulator );
132#endif
133#if defined(MBEDTLS_ENTROPY_NV_SEED)
134 ctx->initial_entropy_run = 0;
135#endif
136 ctx->source_count = 0;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500137 mbedtls_platform_zeroize( ctx->source, sizeof( ctx->source ) );
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100138 ctx->accumulator_started = 0;
Paul Bakker1ffefac2013-09-28 15:23:03 +0200139}
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
142 mbedtls_entropy_f_source_ptr f_source, void *p_source,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200143 size_t threshold, int strong )
Paul Bakker6083fd22011-12-03 21:45:14 +0000144{
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400145 int idx, ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#if defined(MBEDTLS_THREADING_C)
148 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100149 return( ret );
150#endif
151
Hanno Becker61937d42017-04-26 15:01:23 +0100152 idx = ctx->source_count;
153 if( idx >= MBEDTLS_ENTROPY_MAX_SOURCES )
Paul Bakker47703a02014-02-06 15:01:20 +0100154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
Paul Bakker47703a02014-02-06 15:01:20 +0100156 goto exit;
157 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000158
Hanno Becker61937d42017-04-26 15:01:23 +0100159 ctx->source[idx].f_source = f_source;
160 ctx->source[idx].p_source = p_source;
161 ctx->source[idx].threshold = threshold;
162 ctx->source[idx].strong = strong;
Paul Bakker6083fd22011-12-03 21:45:14 +0000163
164 ctx->source_count++;
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400165 ret = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000166
Paul Bakker47703a02014-02-06 15:01:20 +0100167exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_THREADING_C)
169 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
170 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100171#endif
172
173 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000174}
175
176/*
177 * Entropy accumulator update
178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200180 const unsigned char *data, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000181{
182 unsigned char header[2];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000184 size_t use_len = len;
185 const unsigned char *p = data;
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400186 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker6083fd22011-12-03 21:45:14 +0000189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100191 if( ( ret = mbedtls_sha512_ret( data, len, tmp, 0 ) ) != 0 )
Jaeden Amero66954e12018-01-25 16:05:54 +0000192 goto cleanup;
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200193#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100194 if( ( ret = mbedtls_sha256_ret( data, len, tmp, 0 ) ) != 0 )
Jaeden Amero66954e12018-01-25 16:05:54 +0000195 goto cleanup;
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200196#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000197 p = tmp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
Paul Bakker6083fd22011-12-03 21:45:14 +0000199 }
200
201 header[0] = source_id;
202 header[1] = use_len & 0xFF;
203
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100204 /*
205 * Start the accumulator if this has not already happened. Note that
206 * it is sufficient to start the accumulator here only because all calls to
207 * gather entropy eventually execute this code.
208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100210 if( ctx->accumulator_started == 0 &&
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100211 ( ret = mbedtls_sha512_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
Jaeden Amero66954e12018-01-25 16:05:54 +0000212 goto cleanup;
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100213 else
214 ctx->accumulator_started = 1;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100215 if( ( ret = mbedtls_sha512_update_ret( &ctx->accumulator, header, 2 ) ) != 0 )
Jaeden Amero66954e12018-01-25 16:05:54 +0000216 goto cleanup;
217 ret = mbedtls_sha512_update_ret( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200218#else
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100219 if( ctx->accumulator_started == 0 &&
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100220 ( ret = mbedtls_sha256_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
Jaeden Amero66954e12018-01-25 16:05:54 +0000221 goto cleanup;
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100222 else
223 ctx->accumulator_started = 1;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100224 if( ( ret = mbedtls_sha256_update_ret( &ctx->accumulator, header, 2 ) ) != 0 )
Jaeden Amero66954e12018-01-25 16:05:54 +0000225 goto cleanup;
226 ret = mbedtls_sha256_update_ret( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200227#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200228
Jaeden Amero66954e12018-01-25 16:05:54 +0000229cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500230 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Andres Amaya Garcia65121932017-07-05 15:45:47 +0100231
Jaeden Amero66954e12018-01-25 16:05:54 +0000232 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000233}
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
Paul Bakker6083fd22011-12-03 21:45:14 +0000236 const unsigned char *data, size_t len )
237{
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400238 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Paul Bakker47703a02014-02-06 15:01:20 +0100239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_THREADING_C)
241 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100242 return( ret );
243#endif
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 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)
248 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
249 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100250#endif
251
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200252 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000253}
254
255/*
256 * Run through the different sources to add entropy to our accumulator
257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258static int entropy_gather_internal( mbedtls_entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +0000259{
Jarno Lamsa6122b592019-12-17 10:06:46 +0200260 int i;
261 volatile int ret = MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE;
Jarno Lamsa5aa4c072019-12-20 12:42:49 +0200262 volatile int have_one_strong_fi = MBEDTLS_ENTROPY_SOURCE_WEAK;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
Paul Bakker6083fd22011-12-03 21:45:14 +0000264 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100265
Paul Bakker43655f42011-12-15 20:11:16 +0000266 if( ctx->source_count == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
Paul Bakker43655f42011-12-15 20:11:16 +0000268
Paul Bakker6083fd22011-12-03 21:45:14 +0000269 /*
270 * Run through our entropy sources
271 */
272 for( i = 0; i < ctx->source_count; i++ )
273 {
Jarno Lamsad05da1f2019-11-14 10:12:36 +0200274 volatile int strong_fi = ctx->source[i].strong;
275 if( strong_fi == MBEDTLS_ENTROPY_SOURCE_STRONG )
Jarno Lamsaf5b6af02019-12-19 14:46:40 +0200276 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +0200277 mbedtls_platform_random_delay();
Jarno Lamsad05da1f2019-11-14 10:12:36 +0200278
Jarno Lamsaf5b6af02019-12-19 14:46:40 +0200279 if( strong_fi == MBEDTLS_ENTROPY_SOURCE_STRONG )
Jarno Lamsa5aa4c072019-12-20 12:42:49 +0200280 have_one_strong_fi = MBEDTLS_ENTROPY_SOURCE_STRONG;
Jarno Lamsaf5b6af02019-12-19 14:46:40 +0200281 else
282 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
283 }
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200284
Paul Bakker6083fd22011-12-03 21:45:14 +0000285 olen = 0;
Paul Bakker66d5d072014-06-17 16:39:18 +0200286 if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
Paul Bakker6083fd22011-12-03 21:45:14 +0000288 {
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100289 goto cleanup;
Paul Bakker6083fd22011-12-03 21:45:14 +0000290 }
291
292 /*
293 * Add if we actually gathered something
294 */
295 if( olen > 0 )
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000296 {
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100297 if( ( ret = entropy_update( ctx, (unsigned char) i,
298 buf, olen ) ) != 0 )
299 return( ret );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000300 ctx->source[i].size += olen;
301 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000302 }
303
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100304cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500305 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100306
Jarno Lamsa5aa4c072019-12-20 12:42:49 +0200307 if( have_one_strong_fi == MBEDTLS_ENTROPY_SOURCE_STRONG )
Jarno Lamsa6122b592019-12-17 10:06:46 +0200308 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +0200309 mbedtls_platform_random_delay();
Jarno Lamsa5aa4c072019-12-20 12:42:49 +0200310 if( have_one_strong_fi == MBEDTLS_ENTROPY_SOURCE_STRONG )
Jarno Lamsa6122b592019-12-17 10:06:46 +0200311 {
312 return( ret );
313 }
Jarno Lamsaf5b6af02019-12-19 14:46:40 +0200314 else
315 {
316 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
317 }
Jarno Lamsaaf60cd72019-12-19 16:45:23 +0200318
Jarno Lamsa6122b592019-12-17 10:06:46 +0200319 }
320
321 return( MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE );
Paul Bakker6083fd22011-12-03 21:45:14 +0000322}
323
Paul Bakker47703a02014-02-06 15:01:20 +0100324/*
325 * Thread-safe wrapper for entropy_gather_internal()
326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
Paul Bakker47703a02014-02-06 15:01:20 +0100328{
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400329 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Paul Bakker47703a02014-02-06 15:01:20 +0100330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331#if defined(MBEDTLS_THREADING_C)
332 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerddd427a2014-04-09 14:47:58 +0200333 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100334#endif
335
Paul Bakkerddd427a2014-04-09 14:47:58 +0200336 ret = entropy_gather_internal( ctx );
Paul Bakker47703a02014-02-06 15:01:20 +0100337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338#if defined(MBEDTLS_THREADING_C)
339 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
340 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100341#endif
342
Paul Bakkerddd427a2014-04-09 14:47:58 +0200343 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100344}
345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000347{
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400348 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
349 int count = 0, i, done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
351 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
354 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000355
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100356#if defined(MBEDTLS_ENTROPY_NV_SEED)
357 /* Update the NV entropy seed before generating any entropy for outside
358 * use.
359 */
Paul Bakkerfc9c7c82016-06-01 15:25:50 +0100360 if( ctx->initial_entropy_run == 0 )
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100361 {
Paul Bakkerfc9c7c82016-06-01 15:25:50 +0100362 ctx->initial_entropy_run = 1;
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100363 if( ( ret = mbedtls_entropy_update_nv_seed( ctx ) ) != 0 )
364 return( ret );
365 }
366#endif
367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368#if defined(MBEDTLS_THREADING_C)
369 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200370 return( ret );
371#endif
372
Paul Bakker6083fd22011-12-03 21:45:14 +0000373 /*
374 * Always gather extra entropy before a call
375 */
376 do
377 {
378 if( count++ > ENTROPY_MAX_LOOP )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200381 goto exit;
382 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000383
Paul Bakker47703a02014-02-06 15:01:20 +0100384 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200385 goto exit;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000386
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200387 done = 1;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000388 for( i = 0; i < ctx->source_count; i++ )
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200389 if( ctx->source[i].size < ctx->source[i].threshold )
390 done = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000391 }
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200392 while( ! done );
Paul Bakker6083fd22011-12-03 21:45:14 +0000393
Manuel Pégourié-Gonnard54526c32019-10-03 11:06:55 +0200394 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakker6083fd22011-12-03 21:45:14 +0000395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Andres Amaya Garciab2b063f2017-07-20 16:45:24 +0100397 /*
398 * Note that at this stage it is assumed that the accumulator was started
399 * in a previous call to entropy_update(). If this is not guaranteed, the
400 * code below will fail.
401 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100402 if( ( ret = mbedtls_sha512_finish_ret( &ctx->accumulator, buf ) ) != 0 )
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100403 goto exit;
Paul Bakker9e36f042013-06-30 14:34:05 +0200404
Paul Bakker6083fd22011-12-03 21:45:14 +0000405 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000406 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000407 */
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100408 mbedtls_sha512_free( &ctx->accumulator );
409 mbedtls_sha512_init( &ctx->accumulator );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100410 if( ( ret = mbedtls_sha512_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100411 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100412 if( ( ret = mbedtls_sha512_update_ret( &ctx->accumulator, buf,
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100413 MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
414 goto exit;
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200415
416 /*
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100417 * Perform second SHA-512 on entropy
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200418 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100419 if( ( ret = mbedtls_sha512_ret( buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100420 buf, 0 ) ) != 0 )
421 goto exit;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100423 if( ( ret = mbedtls_sha256_finish_ret( &ctx->accumulator, buf ) ) != 0 )
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100424 goto exit;
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200425
426 /*
427 * Reset accumulator and counters and recycle existing entropy
428 */
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100429 mbedtls_sha256_free( &ctx->accumulator );
430 mbedtls_sha256_init( &ctx->accumulator );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100431 if( ( ret = mbedtls_sha256_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100432 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100433 if( ( ret = mbedtls_sha256_update_ret( &ctx->accumulator, buf,
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100434 MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
435 goto exit;
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100436
437 /*
438 * Perform second SHA-256 on entropy
439 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100440 if( ( ret = mbedtls_sha256_ret( buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100441 buf, 0 ) ) != 0 )
442 goto exit;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000444
445 for( i = 0; i < ctx->source_count; i++ )
446 ctx->source[i].size = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000447
Teppo Järvelin91d79382019-10-02 09:09:31 +0300448 mbedtls_platform_memcpy( output, buf, len );
Paul Bakker6083fd22011-12-03 21:45:14 +0000449
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200450 ret = 0;
451
452exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500453 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)
456 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
457 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200458#endif
459
460 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000461}
462
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100463#if defined(MBEDTLS_ENTROPY_NV_SEED)
464int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx )
465{
466 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Andres Amaya Garciaaf0b31d2017-07-05 14:23:54 +0100467 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100468
469 /* Read new seed and write it to NV */
470 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
471 return( ret );
472
473 if( mbedtls_nv_seed_write( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
474 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
475
476 /* Manually update the remaining stream with a separator value to diverge */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200477 mbedtls_platform_memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100478 ret = mbedtls_entropy_update_manual( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100479
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100480 return( ret );
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100481}
482#endif /* MBEDTLS_ENTROPY_NV_SEED */
483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#if defined(MBEDTLS_FS_IO)
485int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100486{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100488 FILE *f;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100490
491 if( ( f = fopen( path, "wb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100495 goto exit;
496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100500 goto exit;
501 }
502
503 ret = 0;
504
505exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500506 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100507
Paul Bakker66ff70d2014-03-26 11:54:05 +0100508 fclose( f );
509 return( ret );
510}
511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100513{
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100514 int ret = 0;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100515 FILE *f;
516 size_t n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100518
519 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100521
522 fseek( f, 0, SEEK_END );
523 n = (size_t) ftell( f );
524 fseek( f, 0, SEEK_SET );
525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
527 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100528
529 if( fread( buf, 1, n, f ) != n )
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100530 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
531 else
532 ret = mbedtls_entropy_update_manual( ctx, buf, n );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100533
534 fclose( f );
535
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500536 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100537
538 if( ret != 0 )
539 return( ret );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 return( mbedtls_entropy_write_seed_file( ctx, path ) );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100542}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543#endif /* MBEDTLS_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#if defined(MBEDTLS_SELF_TEST)
Simon Butcher669c6352016-09-15 18:57:34 +0100546#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200547/*
548 * Dummy source function
549 */
550static int entropy_dummy_source( void *data, unsigned char *output,
551 size_t len, size_t *olen )
552{
553 ((void) data);
554
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200555 mbedtls_platform_memset( output, 0x2a, len );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200556 *olen = len;
557
558 return( 0 );
559}
Simon Butcher669c6352016-09-15 18:57:34 +0100560#endif /* !MBEDTLS_TEST_NULL_ENTROPY */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200561
Andres AGe7723ec2016-08-25 10:18:50 +0100562#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Andres AGb34e42e2016-08-22 11:08:50 +0100563
Andres AGe7723ec2016-08-25 10:18:50 +0100564static int mbedtls_entropy_source_self_test_gather( unsigned char *buf, size_t buf_len )
565{
566 int ret = 0;
567 size_t entropy_len = 0;
568 size_t olen = 0;
569 size_t attempts = buf_len;
570
571 while( attempts > 0 && entropy_len < buf_len )
572 {
573 if( ( ret = mbedtls_hardware_poll( NULL, buf + entropy_len,
574 buf_len - entropy_len, &olen ) ) != 0 )
575 return( ret );
576
577 entropy_len += olen;
578 attempts--;
579 }
580
581 if( entropy_len < buf_len )
582 {
583 ret = 1;
584 }
585
586 return( ret );
587}
588
589
590static int mbedtls_entropy_source_self_test_check_bits( const unsigned char *buf,
591 size_t buf_len )
592{
593 unsigned char set= 0xFF;
594 unsigned char unset = 0x00;
595 size_t i;
596
597 for( i = 0; i < buf_len; i++ )
598 {
599 set &= buf[i];
600 unset |= buf[i];
601 }
602
603 return( set == 0xFF || unset == 0x00 );
604}
Andres AGb34e42e2016-08-22 11:08:50 +0100605
606/*
Andres AGe7723ec2016-08-25 10:18:50 +0100607 * A test to ensure hat the entropy sources are functioning correctly
608 * and there is no obvious failure. The test performs the following checks:
Andres AGb34e42e2016-08-22 11:08:50 +0100609 * - The entropy source is not providing only 0s (all bits unset) or 1s (all
610 * bits set).
611 * - The entropy source is not providing values in a pattern. Because the
Andres AGe7723ec2016-08-25 10:18:50 +0100612 * hardware could be providing data in an arbitrary length, this check polls
613 * the hardware entropy source twice and compares the result to ensure they
614 * are not equal.
Andres AGb34e42e2016-08-22 11:08:50 +0100615 * - The error code returned by the entropy source is not an error.
616 */
Andres AGe7723ec2016-08-25 10:18:50 +0100617int mbedtls_entropy_source_self_test( int verbose )
Andres AGb34e42e2016-08-22 11:08:50 +0100618{
619 int ret = 0;
Andres AGe7723ec2016-08-25 10:18:50 +0100620 unsigned char buf0[2 * sizeof( unsigned long long int )];
621 unsigned char buf1[2 * sizeof( unsigned long long int )];
Andres AGb34e42e2016-08-22 11:08:50 +0100622
623 if( verbose != 0 )
624 mbedtls_printf( " ENTROPY_BIAS test: " );
625
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200626 mbedtls_platform_memset( buf0, 0x00, sizeof( buf0 ) );
627 mbedtls_platform_memset( buf1, 0x00, sizeof( buf1 ) );
Andres AGb34e42e2016-08-22 11:08:50 +0100628
Andres AGe7723ec2016-08-25 10:18:50 +0100629 if( ( ret = mbedtls_entropy_source_self_test_gather( buf0, sizeof( buf0 ) ) ) != 0 )
Andres AGb34e42e2016-08-22 11:08:50 +0100630 goto cleanup;
Andres AGe7723ec2016-08-25 10:18:50 +0100631 if( ( ret = mbedtls_entropy_source_self_test_gather( buf1, sizeof( buf1 ) ) ) != 0 )
632 goto cleanup;
633
634 /* Make sure that the returned values are not all 0 or 1 */
635 if( ( ret = mbedtls_entropy_source_self_test_check_bits( buf0, sizeof( buf0 ) ) ) != 0 )
636 goto cleanup;
637 if( ( ret = mbedtls_entropy_source_self_test_check_bits( buf1, sizeof( buf1 ) ) ) != 0 )
638 goto cleanup;
Andres AGb34e42e2016-08-22 11:08:50 +0100639
640 /* Make sure that the entropy source is not returning values in a
641 * pattern */
Andres AGe7723ec2016-08-25 10:18:50 +0100642 ret = memcmp( buf0, buf1, sizeof( buf0 ) ) == 0;
Andres AGb34e42e2016-08-22 11:08:50 +0100643
644cleanup:
Andres AGb34e42e2016-08-22 11:08:50 +0100645 if( verbose != 0 )
646 {
647 if( ret != 0 )
648 mbedtls_printf( "failed\n" );
649 else
650 mbedtls_printf( "passed\n" );
651
652 mbedtls_printf( "\n" );
653 }
654
655 return( ret != 0 );
656}
Andres AGe7723ec2016-08-25 10:18:50 +0100657
658#endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
Andres AGb34e42e2016-08-22 11:08:50 +0100659
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200660/*
661 * The actual entropy quality is hard to test, but we can at least
662 * test that the functions don't cause errors and write the correct
663 * amount of data to buffers.
664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665int mbedtls_entropy_self_test( int verbose )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200666{
Andres Amaya Garciaa928e672016-09-13 13:30:02 +0100667 int ret = 1;
Simon Butcher669c6352016-09-15 18:57:34 +0100668#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_entropy_context ctx;
670 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
671 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200672 size_t i, j;
Simon Butcher669c6352016-09-15 18:57:34 +0100673#endif /* !MBEDTLS_TEST_NULL_ENTROPY */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200674
675 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_printf( " ENTROPY test: " );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200677
Andres Amaya Garciaa928e672016-09-13 13:30:02 +0100678#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200680
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200681 /* First do a gather to make sure we have default sources */
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200682 if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200683 goto cleanup;
684
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200685 ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16,
686 MBEDTLS_ENTROPY_SOURCE_WEAK );
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200687 if( ret != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200688 goto cleanup;
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200691 goto cleanup;
692
693 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 * To test that mbedtls_entropy_func writes correct number of bytes:
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200695 * - use the whole buffer and rely on ASan to detect overruns
696 * - collect entropy 8 times and OR the result in an accumulator:
697 * any byte should then be 0 with probably 2^(-64), so requiring
698 * each of the 32 or 64 bytes to be non-zero has a false failure rate
699 * of at most 2^(-58) which is acceptable.
700 */
701 for( i = 0; i < 8; i++ )
702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200704 goto cleanup;
705
706 for( j = 0; j < sizeof( buf ); j++ )
707 acc[j] |= buf[j];
708 }
709
710 for( j = 0; j < sizeof( buf ); j++ )
711 {
712 if( acc[j] == 0 )
713 {
714 ret = 1;
715 goto cleanup;
716 }
717 }
718
Andres AGe7723ec2016-08-25 10:18:50 +0100719#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
720 if( ( ret = mbedtls_entropy_source_self_test( 0 ) ) != 0 )
721 goto cleanup;
722#endif
723
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200724cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_entropy_free( &ctx );
Andres Amaya Garciaa928e672016-09-13 13:30:02 +0100726#endif /* !MBEDTLS_TEST_NULL_ENTROPY */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200727
728 if( verbose != 0 )
729 {
730 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200732 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200736 }
737
738 return( ret != 0 );
739}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742#endif /* MBEDTLS_ENTROPY_C */