blob: 4d7a9b32c3e2ed4abd1e083ea4664a1f4f1835f3 [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
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker6083fd22011-12-03 21:45:14 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker6083fd22011-12-03 21:45:14 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000056
Simon Butcherab5df402016-06-11 02:31:21 +010057#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher1ceab6e2016-06-21 10:14:00 +010058#warning "**** WARNING! MBEDTLS_TEST_NULL_ENTROPY defined! "
59#warning "**** THIS BUILD HAS NO DEFINED ENTROPY SOURCES "
60#warning "**** THIS BUILD IS *NOT* SUITABLE FOR PRODUCTION USE "
Simon Butcherab5df402016-06-11 02:31:21 +010061#endif
62
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/entropy.h"
64#include "mbedtls/entropy_poll.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000065
Rich Evans00ab4702015-02-06 13:43:58 +000066#include <string.h>
67
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_FS_IO)
Paul Bakker66ff70d2014-03-26 11:54:05 +010069#include <stdio.h>
70#endif
71
Paul Bakker217efbc2016-07-14 14:30:03 +010072#if defined(MBEDTLS_ENTROPY_NV_SEED)
73#include "mbedtls/platform.h"
74#endif
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if defined(MBEDTLS_SELF_TEST)
77#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000078#include "mbedtls/platform.h"
Rich Evans00ab4702015-02-06 13:43:58 +000079#else
80#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#define mbedtls_printf printf
82#endif /* MBEDTLS_PLATFORM_C */
83#endif /* MBEDTLS_SELF_TEST */
Rich Evans00ab4702015-02-06 13:43:58 +000084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000086#include "mbedtls/havege.h"
Paul Bakker28c7e7f2011-12-15 19:49:30 +000087#endif
88
Paul Bakker34617722014-06-13 17:20:13 +020089/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020091 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
92}
93
Paul Bakker6083fd22011-12-03 21:45:14 +000094#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
95
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096void mbedtls_entropy_init( mbedtls_entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +000097{
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +010098 ctx->source_count = 0;
99 memset( ctx->source, 0, sizeof( ctx->source ) );
Paul Bakker6083fd22011-12-03 21:45:14 +0000100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#if defined(MBEDTLS_THREADING_C)
102 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200103#endif
104
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100105 ctx->accumulator_started = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100107 mbedtls_sha512_init( &ctx->accumulator );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200108#else
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100109 mbedtls_sha256_init( &ctx->accumulator );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200110#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#if defined(MBEDTLS_HAVEGE_C)
112 mbedtls_havege_init( &ctx->havege_data );
Paul Bakker43655f42011-12-15 20:11:16 +0000113#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000114
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100115 /* Reminder: Update ENTROPY_HAVE_STRONG in the test files
Hanno Becker47deec42017-07-24 12:27:09 +0100116 * when adding more strong entropy sources here. */
Hanno Beckerc6deafc2017-07-23 14:06:42 +0100117
Simon Butcherab5df402016-06-11 02:31:21 +0100118#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher4157b602016-06-12 00:31:33 +0100119 mbedtls_entropy_add_source( ctx, mbedtls_null_entropy_poll, NULL,
Janos Follath53de7842016-06-08 15:29:18 +0100120 1, MBEDTLS_ENTROPY_SOURCE_STRONG );
121#endif
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
124#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
125 mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200126 MBEDTLS_ENTROPY_MIN_PLATFORM,
127 MBEDTLS_ENTROPY_SOURCE_STRONG );
Paul Bakker6083fd22011-12-03 21:45:14 +0000128#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200130 mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL,
131 MBEDTLS_ENTROPY_MIN_HARDCLOCK,
132 MBEDTLS_ENTROPY_SOURCE_WEAK );
Paul Bakker6083fd22011-12-03 21:45:14 +0000133#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#if defined(MBEDTLS_HAVEGE_C)
135 mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200136 MBEDTLS_ENTROPY_MIN_HAVEGE,
137 MBEDTLS_ENTROPY_SOURCE_STRONG );
Paul Bakker28c7e7f2011-12-15 19:49:30 +0000138#endif
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +0200139#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Manuel Pégourié-Gonnardfc2ccfe2015-07-10 11:15:50 +0100140 mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200141 MBEDTLS_ENTROPY_MIN_HARDWARE,
142 MBEDTLS_ENTROPY_SOURCE_STRONG );
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +0200143#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +0100144#if defined(MBEDTLS_ENTROPY_NV_SEED)
145 mbedtls_entropy_add_source( ctx, mbedtls_nv_seed_poll, NULL,
146 MBEDTLS_ENTROPY_BLOCK_SIZE,
147 MBEDTLS_ENTROPY_SOURCE_STRONG );
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100148 ctx->initial_entropy_run = 0;
Paul Bakker9988d6b2016-06-01 11:29:42 +0100149#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +0000151}
152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
Paul Bakker1ffefac2013-09-28 15:23:03 +0200154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_HAVEGE_C)
156 mbedtls_havege_free( &ctx->havege_data );
Paul Bakkera317a982014-06-18 16:44:11 +0200157#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#if defined(MBEDTLS_THREADING_C)
159 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200160#endif
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100161#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
162 mbedtls_sha512_free( &ctx->accumulator );
163#else
164 mbedtls_sha256_free( &ctx->accumulator );
165#endif
166#if defined(MBEDTLS_ENTROPY_NV_SEED)
167 ctx->initial_entropy_run = 0;
168#endif
169 ctx->source_count = 0;
170 mbedtls_zeroize( ctx->source, sizeof( ctx->source ) );
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100171 ctx->accumulator_started = 0;
Paul Bakker1ffefac2013-09-28 15:23:03 +0200172}
173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
175 mbedtls_entropy_f_source_ptr f_source, void *p_source,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200176 size_t threshold, int strong )
Paul Bakker6083fd22011-12-03 21:45:14 +0000177{
Hanno Becker61937d42017-04-26 15:01:23 +0100178 int idx, ret = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#if defined(MBEDTLS_THREADING_C)
181 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100182 return( ret );
183#endif
184
Hanno Becker61937d42017-04-26 15:01:23 +0100185 idx = ctx->source_count;
186 if( idx >= MBEDTLS_ENTROPY_MAX_SOURCES )
Paul Bakker47703a02014-02-06 15:01:20 +0100187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
Paul Bakker47703a02014-02-06 15:01:20 +0100189 goto exit;
190 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000191
Hanno Becker61937d42017-04-26 15:01:23 +0100192 ctx->source[idx].f_source = f_source;
193 ctx->source[idx].p_source = p_source;
194 ctx->source[idx].threshold = threshold;
195 ctx->source[idx].strong = strong;
Paul Bakker6083fd22011-12-03 21:45:14 +0000196
197 ctx->source_count++;
198
Paul Bakker47703a02014-02-06 15:01:20 +0100199exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#if defined(MBEDTLS_THREADING_C)
201 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
202 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100203#endif
204
205 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000206}
207
208/*
209 * Entropy accumulator update
210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200212 const unsigned char *data, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000213{
214 unsigned char header[2];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000216 size_t use_len = len;
217 const unsigned char *p = data;
Jaeden Amero66954e12018-01-25 16:05:54 +0000218 int ret = 0;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker6083fd22011-12-03 21:45:14 +0000221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100223 if( ( ret = mbedtls_sha512_ret( data, len, tmp, 0 ) ) != 0 )
Jaeden Amero66954e12018-01-25 16:05:54 +0000224 goto cleanup;
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200225#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100226 if( ( ret = mbedtls_sha256_ret( data, len, tmp, 0 ) ) != 0 )
Jaeden Amero66954e12018-01-25 16:05:54 +0000227 goto cleanup;
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200228#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000229 p = tmp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
Paul Bakker6083fd22011-12-03 21:45:14 +0000231 }
232
233 header[0] = source_id;
234 header[1] = use_len & 0xFF;
235
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100236 /*
237 * Start the accumulator if this has not already happened. Note that
238 * it is sufficient to start the accumulator here only because all calls to
239 * gather entropy eventually execute this code.
240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100242 if( ctx->accumulator_started == 0 &&
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100243 ( ret = mbedtls_sha512_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
Jaeden Amero66954e12018-01-25 16:05:54 +0000244 goto cleanup;
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100245 else
246 ctx->accumulator_started = 1;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100247 if( ( ret = mbedtls_sha512_update_ret( &ctx->accumulator, header, 2 ) ) != 0 )
Jaeden Amero66954e12018-01-25 16:05:54 +0000248 goto cleanup;
249 ret = mbedtls_sha512_update_ret( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200250#else
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100251 if( ctx->accumulator_started == 0 &&
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100252 ( ret = mbedtls_sha256_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
Jaeden Amero66954e12018-01-25 16:05:54 +0000253 goto cleanup;
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100254 else
255 ctx->accumulator_started = 1;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100256 if( ( ret = mbedtls_sha256_update_ret( &ctx->accumulator, header, 2 ) ) != 0 )
Jaeden Amero66954e12018-01-25 16:05:54 +0000257 goto cleanup;
258 ret = mbedtls_sha256_update_ret( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200259#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200260
Jaeden Amero66954e12018-01-25 16:05:54 +0000261cleanup:
Andres Amaya Garcia65121932017-07-05 15:45:47 +0100262 mbedtls_zeroize( tmp, sizeof( tmp ) );
263
Jaeden Amero66954e12018-01-25 16:05:54 +0000264 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000265}
266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
Paul Bakker6083fd22011-12-03 21:45:14 +0000268 const unsigned char *data, size_t len )
269{
Paul Bakker47703a02014-02-06 15:01:20 +0100270 int ret;
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_THREADING_C)
273 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100274 return( ret );
275#endif
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len );
Paul Bakker47703a02014-02-06 15:01:20 +0100278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#if defined(MBEDTLS_THREADING_C)
280 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
281 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100282#endif
283
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200284 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000285}
286
287/*
288 * Run through the different sources to add entropy to our accumulator
289 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290static int entropy_gather_internal( mbedtls_entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +0000291{
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200292 int ret, i, have_one_strong = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
Paul Bakker6083fd22011-12-03 21:45:14 +0000294 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100295
Paul Bakker43655f42011-12-15 20:11:16 +0000296 if( ctx->source_count == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
Paul Bakker43655f42011-12-15 20:11:16 +0000298
Paul Bakker6083fd22011-12-03 21:45:14 +0000299 /*
300 * Run through our entropy sources
301 */
302 for( i = 0; i < ctx->source_count; i++ )
303 {
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200304 if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
305 have_one_strong = 1;
306
Paul Bakker6083fd22011-12-03 21:45:14 +0000307 olen = 0;
Paul Bakker66d5d072014-06-17 16:39:18 +0200308 if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
Paul Bakker6083fd22011-12-03 21:45:14 +0000310 {
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100311 goto cleanup;
Paul Bakker6083fd22011-12-03 21:45:14 +0000312 }
313
314 /*
315 * Add if we actually gathered something
316 */
317 if( olen > 0 )
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000318 {
Andres Amaya Garcia95869c42017-06-29 16:31:44 +0100319 if( ( ret = entropy_update( ctx, (unsigned char) i,
320 buf, olen ) ) != 0 )
321 return( ret );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000322 ctx->source[i].size += olen;
323 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000324 }
325
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200326 if( have_one_strong == 0 )
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100327 ret = MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE;
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200328
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100329cleanup:
330 mbedtls_zeroize( buf, sizeof( buf ) );
331
332 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000333}
334
Paul Bakker47703a02014-02-06 15:01:20 +0100335/*
336 * Thread-safe wrapper for entropy_gather_internal()
337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
Paul Bakker47703a02014-02-06 15:01:20 +0100339{
Paul Bakkerddd427a2014-04-09 14:47:58 +0200340 int ret;
Paul Bakker47703a02014-02-06 15:01:20 +0100341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#if defined(MBEDTLS_THREADING_C)
343 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerddd427a2014-04-09 14:47:58 +0200344 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100345#endif
346
Paul Bakkerddd427a2014-04-09 14:47:58 +0200347 ret = entropy_gather_internal( ctx );
Paul Bakker47703a02014-02-06 15:01:20 +0100348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349#if defined(MBEDTLS_THREADING_C)
350 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
351 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100352#endif
353
Paul Bakkerddd427a2014-04-09 14:47:58 +0200354 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100355}
356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000358{
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200359 int ret, count = 0, i, done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
361 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
364 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000365
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100366#if defined(MBEDTLS_ENTROPY_NV_SEED)
367 /* Update the NV entropy seed before generating any entropy for outside
368 * use.
369 */
Paul Bakkerfc9c7c82016-06-01 15:25:50 +0100370 if( ctx->initial_entropy_run == 0 )
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100371 {
Paul Bakkerfc9c7c82016-06-01 15:25:50 +0100372 ctx->initial_entropy_run = 1;
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100373 if( ( ret = mbedtls_entropy_update_nv_seed( ctx ) ) != 0 )
374 return( ret );
375 }
376#endif
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378#if defined(MBEDTLS_THREADING_C)
379 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200380 return( ret );
381#endif
382
Paul Bakker6083fd22011-12-03 21:45:14 +0000383 /*
384 * Always gather extra entropy before a call
385 */
386 do
387 {
388 if( count++ > ENTROPY_MAX_LOOP )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200391 goto exit;
392 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000393
Paul Bakker47703a02014-02-06 15:01:20 +0100394 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200395 goto exit;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000396
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200397 done = 1;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000398 for( i = 0; i < ctx->source_count; i++ )
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200399 if( ctx->source[i].size < ctx->source[i].threshold )
400 done = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000401 }
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200402 while( ! done );
Paul Bakker6083fd22011-12-03 21:45:14 +0000403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakker6083fd22011-12-03 21:45:14 +0000405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Andres Amaya Garciab2b063f2017-07-20 16:45:24 +0100407 /*
408 * Note that at this stage it is assumed that the accumulator was started
409 * in a previous call to entropy_update(). If this is not guaranteed, the
410 * code below will fail.
411 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100412 if( ( ret = mbedtls_sha512_finish_ret( &ctx->accumulator, buf ) ) != 0 )
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100413 goto exit;
Paul Bakker9e36f042013-06-30 14:34:05 +0200414
Paul Bakker6083fd22011-12-03 21:45:14 +0000415 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000416 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000417 */
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100418 mbedtls_sha512_free( &ctx->accumulator );
419 mbedtls_sha512_init( &ctx->accumulator );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100420 if( ( ret = mbedtls_sha512_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100421 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100422 if( ( ret = mbedtls_sha512_update_ret( &ctx->accumulator, buf,
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100423 MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
424 goto exit;
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200425
426 /*
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100427 * Perform second SHA-512 on entropy
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200428 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100429 if( ( ret = mbedtls_sha512_ret( buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100430 buf, 0 ) ) != 0 )
431 goto exit;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100433 if( ( ret = mbedtls_sha256_finish_ret( &ctx->accumulator, buf ) ) != 0 )
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100434 goto exit;
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200435
436 /*
437 * Reset accumulator and counters and recycle existing entropy
438 */
Andres Amaya Garciaa7559cb2017-06-29 16:12:31 +0100439 mbedtls_sha256_free( &ctx->accumulator );
440 mbedtls_sha256_init( &ctx->accumulator );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100441 if( ( ret = mbedtls_sha256_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100442 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100443 if( ( ret = mbedtls_sha256_update_ret( &ctx->accumulator, buf,
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100444 MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
445 goto exit;
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100446
447 /*
448 * Perform second SHA-256 on entropy
449 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100450 if( ( ret = mbedtls_sha256_ret( buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
Andres Amaya Garcia207cea52017-06-29 13:28:13 +0100451 buf, 0 ) ) != 0 )
452 goto exit;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000454
455 for( i = 0; i < ctx->source_count; i++ )
456 ctx->source[i].size = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000457
458 memcpy( output, buf, len );
459
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200460 ret = 0;
461
462exit:
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100463 mbedtls_zeroize( buf, sizeof( buf ) );
464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465#if defined(MBEDTLS_THREADING_C)
466 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
467 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200468#endif
469
470 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000471}
472
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100473#if defined(MBEDTLS_ENTROPY_NV_SEED)
474int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx )
475{
476 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Andres Amaya Garciaaf0b31d2017-07-05 14:23:54 +0100477 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100478
479 /* Read new seed and write it to NV */
480 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
481 return( ret );
482
483 if( mbedtls_nv_seed_write( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
484 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
485
486 /* Manually update the remaining stream with a separator value to diverge */
487 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100488 ret = mbedtls_entropy_update_manual( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100489
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100490 return( ret );
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100491}
492#endif /* MBEDTLS_ENTROPY_NV_SEED */
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#if defined(MBEDTLS_FS_IO)
495int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100496{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100498 FILE *f;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100500
501 if( ( f = fopen( path, "wb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100505 goto exit;
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100510 goto exit;
511 }
512
513 ret = 0;
514
515exit:
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100516 mbedtls_zeroize( buf, sizeof( buf ) );
517
Paul Bakker66ff70d2014-03-26 11:54:05 +0100518 fclose( f );
519 return( ret );
520}
521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100523{
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100524 int ret = 0;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100525 FILE *f;
526 size_t n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100528
529 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100531
532 fseek( f, 0, SEEK_END );
533 n = (size_t) ftell( f );
534 fseek( f, 0, SEEK_SET );
535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
537 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100538
539 if( fread( buf, 1, n, f ) != n )
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100540 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
541 else
542 ret = mbedtls_entropy_update_manual( ctx, buf, n );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100543
544 fclose( f );
545
Andres Amaya Garcia1adcd952017-06-26 09:58:59 +0100546 mbedtls_zeroize( buf, sizeof( buf ) );
547
548 if( ret != 0 )
549 return( ret );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 return( mbedtls_entropy_write_seed_file( ctx, path ) );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100552}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#endif /* MBEDTLS_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555#if defined(MBEDTLS_SELF_TEST)
Simon Butcher669c6352016-09-15 18:57:34 +0100556#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200557/*
558 * Dummy source function
559 */
560static int entropy_dummy_source( void *data, unsigned char *output,
561 size_t len, size_t *olen )
562{
563 ((void) data);
564
565 memset( output, 0x2a, len );
566 *olen = len;
567
568 return( 0 );
569}
Simon Butcher669c6352016-09-15 18:57:34 +0100570#endif /* !MBEDTLS_TEST_NULL_ENTROPY */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200571
Andres AGe7723ec2016-08-25 10:18:50 +0100572#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Andres AGb34e42e2016-08-22 11:08:50 +0100573
Andres AGe7723ec2016-08-25 10:18:50 +0100574static int mbedtls_entropy_source_self_test_gather( unsigned char *buf, size_t buf_len )
575{
576 int ret = 0;
577 size_t entropy_len = 0;
578 size_t olen = 0;
579 size_t attempts = buf_len;
580
581 while( attempts > 0 && entropy_len < buf_len )
582 {
583 if( ( ret = mbedtls_hardware_poll( NULL, buf + entropy_len,
584 buf_len - entropy_len, &olen ) ) != 0 )
585 return( ret );
586
587 entropy_len += olen;
588 attempts--;
589 }
590
591 if( entropy_len < buf_len )
592 {
593 ret = 1;
594 }
595
596 return( ret );
597}
598
599
600static int mbedtls_entropy_source_self_test_check_bits( const unsigned char *buf,
601 size_t buf_len )
602{
603 unsigned char set= 0xFF;
604 unsigned char unset = 0x00;
605 size_t i;
606
607 for( i = 0; i < buf_len; i++ )
608 {
609 set &= buf[i];
610 unset |= buf[i];
611 }
612
613 return( set == 0xFF || unset == 0x00 );
614}
Andres AGb34e42e2016-08-22 11:08:50 +0100615
616/*
Andres AGe7723ec2016-08-25 10:18:50 +0100617 * A test to ensure hat the entropy sources are functioning correctly
618 * and there is no obvious failure. The test performs the following checks:
Andres AGb34e42e2016-08-22 11:08:50 +0100619 * - The entropy source is not providing only 0s (all bits unset) or 1s (all
620 * bits set).
621 * - The entropy source is not providing values in a pattern. Because the
Andres AGe7723ec2016-08-25 10:18:50 +0100622 * hardware could be providing data in an arbitrary length, this check polls
623 * the hardware entropy source twice and compares the result to ensure they
624 * are not equal.
Andres AGb34e42e2016-08-22 11:08:50 +0100625 * - The error code returned by the entropy source is not an error.
626 */
Andres AGe7723ec2016-08-25 10:18:50 +0100627int mbedtls_entropy_source_self_test( int verbose )
Andres AGb34e42e2016-08-22 11:08:50 +0100628{
629 int ret = 0;
Andres AGe7723ec2016-08-25 10:18:50 +0100630 unsigned char buf0[2 * sizeof( unsigned long long int )];
631 unsigned char buf1[2 * sizeof( unsigned long long int )];
Andres AGb34e42e2016-08-22 11:08:50 +0100632
633 if( verbose != 0 )
634 mbedtls_printf( " ENTROPY_BIAS test: " );
635
Andres AGe7723ec2016-08-25 10:18:50 +0100636 memset( buf0, 0x00, sizeof( buf0 ) );
637 memset( buf1, 0x00, sizeof( buf1 ) );
Andres AGb34e42e2016-08-22 11:08:50 +0100638
Andres AGe7723ec2016-08-25 10:18:50 +0100639 if( ( ret = mbedtls_entropy_source_self_test_gather( buf0, sizeof( buf0 ) ) ) != 0 )
Andres AGb34e42e2016-08-22 11:08:50 +0100640 goto cleanup;
Andres AGe7723ec2016-08-25 10:18:50 +0100641 if( ( ret = mbedtls_entropy_source_self_test_gather( buf1, sizeof( buf1 ) ) ) != 0 )
642 goto cleanup;
643
644 /* Make sure that the returned values are not all 0 or 1 */
645 if( ( ret = mbedtls_entropy_source_self_test_check_bits( buf0, sizeof( buf0 ) ) ) != 0 )
646 goto cleanup;
647 if( ( ret = mbedtls_entropy_source_self_test_check_bits( buf1, sizeof( buf1 ) ) ) != 0 )
648 goto cleanup;
Andres AGb34e42e2016-08-22 11:08:50 +0100649
650 /* Make sure that the entropy source is not returning values in a
651 * pattern */
Andres AGe7723ec2016-08-25 10:18:50 +0100652 ret = memcmp( buf0, buf1, sizeof( buf0 ) ) == 0;
Andres AGb34e42e2016-08-22 11:08:50 +0100653
654cleanup:
Andres AGb34e42e2016-08-22 11:08:50 +0100655 if( verbose != 0 )
656 {
657 if( ret != 0 )
658 mbedtls_printf( "failed\n" );
659 else
660 mbedtls_printf( "passed\n" );
661
662 mbedtls_printf( "\n" );
663 }
664
665 return( ret != 0 );
666}
Andres AGe7723ec2016-08-25 10:18:50 +0100667
668#endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
Andres AGb34e42e2016-08-22 11:08:50 +0100669
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200670/*
671 * The actual entropy quality is hard to test, but we can at least
672 * test that the functions don't cause errors and write the correct
673 * amount of data to buffers.
674 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675int mbedtls_entropy_self_test( int verbose )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200676{
Andres Amaya Garciaa928e672016-09-13 13:30:02 +0100677 int ret = 1;
Simon Butcher669c6352016-09-15 18:57:34 +0100678#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_entropy_context ctx;
680 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
681 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200682 size_t i, j;
Simon Butcher669c6352016-09-15 18:57:34 +0100683#endif /* !MBEDTLS_TEST_NULL_ENTROPY */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200684
685 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 mbedtls_printf( " ENTROPY test: " );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200687
Andres Amaya Garciaa928e672016-09-13 13:30:02 +0100688#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200690
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200691 /* First do a gather to make sure we have default sources */
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200692 if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200693 goto cleanup;
694
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200695 ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16,
696 MBEDTLS_ENTROPY_SOURCE_WEAK );
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200697 if( ret != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200698 goto cleanup;
699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200701 goto cleanup;
702
703 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 * To test that mbedtls_entropy_func writes correct number of bytes:
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200705 * - use the whole buffer and rely on ASan to detect overruns
706 * - collect entropy 8 times and OR the result in an accumulator:
707 * any byte should then be 0 with probably 2^(-64), so requiring
708 * each of the 32 or 64 bytes to be non-zero has a false failure rate
709 * of at most 2^(-58) which is acceptable.
710 */
711 for( i = 0; i < 8; i++ )
712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200714 goto cleanup;
715
716 for( j = 0; j < sizeof( buf ); j++ )
717 acc[j] |= buf[j];
718 }
719
720 for( j = 0; j < sizeof( buf ); j++ )
721 {
722 if( acc[j] == 0 )
723 {
724 ret = 1;
725 goto cleanup;
726 }
727 }
728
Andres AGe7723ec2016-08-25 10:18:50 +0100729#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
730 if( ( ret = mbedtls_entropy_source_self_test( 0 ) ) != 0 )
731 goto cleanup;
732#endif
733
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200734cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_entropy_free( &ctx );
Andres Amaya Garciaa928e672016-09-13 13:30:02 +0100736#endif /* !MBEDTLS_TEST_NULL_ENTROPY */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200737
738 if( verbose != 0 )
739 {
740 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200742 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200746 }
747
748 return( ret != 0 );
749}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752#endif /* MBEDTLS_ENTROPY_C */