blob: 1982b10960dc29e417d79bbaaea5b0fa08186202 [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Entropy accumulator implementation
3 *
Paul Bakker54c43fc2016-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
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/entropy.h"
31#include "mbedtls/entropy_poll.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <string.h>
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_FS_IO)
Paul Bakker66ff70d2014-03-26 11:54:05 +010036#include <stdio.h>
37#endif
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_SELF_TEST)
40#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/platform.h"
Rich Evans00ab4702015-02-06 13:43:58 +000042#else
43#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#define mbedtls_printf printf
45#endif /* MBEDTLS_PLATFORM_C */
46#endif /* MBEDTLS_SELF_TEST */
Rich Evans00ab4702015-02-06 13:43:58 +000047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/havege.h"
Paul Bakker28c7e7f2011-12-15 19:49:30 +000050#endif
51
Paul Bakker34617722014-06-13 17:20:13 +020052/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020054 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
55}
56
Paul Bakker7da30712016-06-01 11:30:54 +010057#if defined(MBEDTLS_ENTROPY_NV_SEED)
58static int initial_entropy_run = 0;
59#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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 memset( ctx, 0, sizeof(mbedtls_entropy_context) );
Paul Bakker6083fd22011-12-03 21:45:14 +000066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_THREADING_C)
68 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020069#endif
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
72 mbedtls_sha512_starts( &ctx->accumulator, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +020073#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_sha256_starts( &ctx->accumulator, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +020075#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if defined(MBEDTLS_HAVEGE_C)
77 mbedtls_havege_init( &ctx->havege_data );
Paul Bakker43655f42011-12-15 20:11:16 +000078#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
81#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
82 mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020083 MBEDTLS_ENTROPY_MIN_PLATFORM,
84 MBEDTLS_ENTROPY_SOURCE_STRONG );
Paul Bakker6083fd22011-12-03 21:45:14 +000085#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020087 mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL,
88 MBEDTLS_ENTROPY_MIN_HARDCLOCK,
89 MBEDTLS_ENTROPY_SOURCE_WEAK );
Paul Bakker6083fd22011-12-03 21:45:14 +000090#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#if defined(MBEDTLS_HAVEGE_C)
92 mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020093 MBEDTLS_ENTROPY_MIN_HAVEGE,
94 MBEDTLS_ENTROPY_SOURCE_STRONG );
Paul Bakker28c7e7f2011-12-15 19:49:30 +000095#endif
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +020096#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Manuel Pégourié-Gonnardfc2ccfe2015-07-10 11:15:50 +010097 mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020098 MBEDTLS_ENTROPY_MIN_HARDWARE,
99 MBEDTLS_ENTROPY_SOURCE_STRONG );
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +0200100#endif
Paul Bakker54c43fc2016-06-01 11:29:42 +0100101#if defined(MBEDTLS_ENTROPY_NV_SEED)
102 mbedtls_entropy_add_source( ctx, mbedtls_nv_seed_poll, NULL,
103 MBEDTLS_ENTROPY_BLOCK_SIZE,
104 MBEDTLS_ENTROPY_SOURCE_STRONG );
105#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +0000107}
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
Paul Bakker1ffefac2013-09-28 15:23:03 +0200110{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#if defined(MBEDTLS_HAVEGE_C)
112 mbedtls_havege_free( &ctx->havege_data );
Paul Bakkera317a982014-06-18 16:44:11 +0200113#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_THREADING_C)
115 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200116#endif
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +0100117 mbedtls_zeroize( ctx, sizeof( mbedtls_entropy_context ) );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200118}
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
121 mbedtls_entropy_f_source_ptr f_source, void *p_source,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200122 size_t threshold, int strong )
Paul Bakker6083fd22011-12-03 21:45:14 +0000123{
Paul Bakker47703a02014-02-06 15:01:20 +0100124 int index, ret = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#if defined(MBEDTLS_THREADING_C)
127 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100128 return( ret );
129#endif
130
131 index = ctx->source_count;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 if( index >= MBEDTLS_ENTROPY_MAX_SOURCES )
Paul Bakker47703a02014-02-06 15:01:20 +0100133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
Paul Bakker47703a02014-02-06 15:01:20 +0100135 goto exit;
136 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000137
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200138 ctx->source[index].f_source = f_source;
139 ctx->source[index].p_source = p_source;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000140 ctx->source[index].threshold = threshold;
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200141 ctx->source[index].strong = strong;
Paul Bakker6083fd22011-12-03 21:45:14 +0000142
143 ctx->source_count++;
144
Paul Bakker47703a02014-02-06 15:01:20 +0100145exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#if defined(MBEDTLS_THREADING_C)
147 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
148 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100149#endif
150
151 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000152}
153
154/*
155 * Entropy accumulator update
156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200158 const unsigned char *data, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000159{
160 unsigned char header[2];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000162 size_t use_len = len;
163 const unsigned char *p = data;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker6083fd22011-12-03 21:45:14 +0000166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
168 mbedtls_sha512( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200169#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_sha256( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200171#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000172 p = tmp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
Paul Bakker6083fd22011-12-03 21:45:14 +0000174 }
175
176 header[0] = source_id;
177 header[1] = use_len & 0xFF;
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
180 mbedtls_sha512_update( &ctx->accumulator, header, 2 );
181 mbedtls_sha512_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200182#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_sha256_update( &ctx->accumulator, header, 2 );
184 mbedtls_sha256_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200185#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200186
Paul Bakker6083fd22011-12-03 21:45:14 +0000187 return( 0 );
188}
189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
Paul Bakker6083fd22011-12-03 21:45:14 +0000191 const unsigned char *data, size_t len )
192{
Paul Bakker47703a02014-02-06 15:01:20 +0100193 int ret;
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_THREADING_C)
196 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100197 return( ret );
198#endif
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len );
Paul Bakker47703a02014-02-06 15:01:20 +0100201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if defined(MBEDTLS_THREADING_C)
203 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
204 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100205#endif
206
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200207 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000208}
209
210/*
211 * Run through the different sources to add entropy to our accumulator
212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213static int entropy_gather_internal( mbedtls_entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +0000214{
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200215 int ret, i, have_one_strong = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
Paul Bakker6083fd22011-12-03 21:45:14 +0000217 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100218
Paul Bakker43655f42011-12-15 20:11:16 +0000219 if( ctx->source_count == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
Paul Bakker43655f42011-12-15 20:11:16 +0000221
Paul Bakker6083fd22011-12-03 21:45:14 +0000222 /*
223 * Run through our entropy sources
224 */
225 for( i = 0; i < ctx->source_count; i++ )
226 {
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200227 if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
228 have_one_strong = 1;
229
Paul Bakker6083fd22011-12-03 21:45:14 +0000230 olen = 0;
Paul Bakker66d5d072014-06-17 16:39:18 +0200231 if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
Paul Bakker6083fd22011-12-03 21:45:14 +0000233 {
234 return( ret );
235 }
236
237 /*
238 * Add if we actually gathered something
239 */
240 if( olen > 0 )
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000241 {
Paul Bakker6083fd22011-12-03 21:45:14 +0000242 entropy_update( ctx, (unsigned char) i, buf, olen );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000243 ctx->source[i].size += olen;
244 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000245 }
246
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200247 if( have_one_strong == 0 )
248 return( MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE );
249
Paul Bakker6083fd22011-12-03 21:45:14 +0000250 return( 0 );
251}
252
Paul Bakker47703a02014-02-06 15:01:20 +0100253/*
254 * Thread-safe wrapper for entropy_gather_internal()
255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
Paul Bakker47703a02014-02-06 15:01:20 +0100257{
Paul Bakkerddd427a2014-04-09 14:47:58 +0200258 int ret;
Paul Bakker47703a02014-02-06 15:01:20 +0100259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260#if defined(MBEDTLS_THREADING_C)
261 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerddd427a2014-04-09 14:47:58 +0200262 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100263#endif
264
Paul Bakkerddd427a2014-04-09 14:47:58 +0200265 ret = entropy_gather_internal( ctx );
Paul Bakker47703a02014-02-06 15:01:20 +0100266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#if defined(MBEDTLS_THREADING_C)
268 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
269 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100270#endif
271
Paul Bakkerddd427a2014-04-09 14:47:58 +0200272 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100273}
274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000276{
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200277 int ret, count = 0, i, done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
279 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
282 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000283
Paul Bakker7da30712016-06-01 11:30:54 +0100284#if defined(MBEDTLS_ENTROPY_NV_SEED)
285 /* Update the NV entropy seed before generating any entropy for outside
286 * use.
287 */
288 if( initial_entropy_run == 0 )
289 {
290 initial_entropy_run = 1;
291 if( ( ret = mbedtls_entropy_update_nv_seed( ctx ) ) != 0 )
292 return( ret );
293 }
294#endif
295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#if defined(MBEDTLS_THREADING_C)
297 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200298 return( ret );
299#endif
300
Paul Bakker6083fd22011-12-03 21:45:14 +0000301 /*
302 * Always gather extra entropy before a call
303 */
304 do
305 {
306 if( count++ > ENTROPY_MAX_LOOP )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200309 goto exit;
310 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000311
Paul Bakker47703a02014-02-06 15:01:20 +0100312 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200313 goto exit;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000314
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200315 done = 1;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000316 for( i = 0; i < ctx->source_count; i++ )
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200317 if( ctx->source[i].size < ctx->source[i].threshold )
318 done = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000319 }
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200320 while( ! done );
Paul Bakker6083fd22011-12-03 21:45:14 +0000321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakker6083fd22011-12-03 21:45:14 +0000323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
325 mbedtls_sha512_finish( &ctx->accumulator, buf );
Paul Bakker9e36f042013-06-30 14:34:05 +0200326
Paul Bakker6083fd22011-12-03 21:45:14 +0000327 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000328 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha512_context ) );
331 mbedtls_sha512_starts( &ctx->accumulator, 0 );
332 mbedtls_sha512_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200333
334 /*
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100335 * Perform second SHA-512 on entropy
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
338#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
339 mbedtls_sha256_finish( &ctx->accumulator, buf );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200340
341 /*
342 * Reset accumulator and counters and recycle existing entropy
343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha256_context ) );
345 mbedtls_sha256_starts( &ctx->accumulator, 0 );
346 mbedtls_sha256_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100347
348 /*
349 * Perform second SHA-256 on entropy
350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_sha256( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
352#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000353
354 for( i = 0; i < ctx->source_count; i++ )
355 ctx->source[i].size = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000356
357 memcpy( output, buf, len );
358
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200359 ret = 0;
360
361exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#if defined(MBEDTLS_THREADING_C)
363 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
364 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200365#endif
366
367 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000368}
369
Paul Bakker7da30712016-06-01 11:30:54 +0100370#if defined(MBEDTLS_ENTROPY_NV_SEED)
371int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx )
372{
373 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
374 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
375
376 /* Read new seed and write it to NV */
377 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
378 return( ret );
379
380 if( mbedtls_nv_seed_write( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
381 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
382
383 /* Manually update the remaining stream with a separator value to diverge */
384 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
385 mbedtls_entropy_update_manual( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
386
387 return( 0 );
388}
389#endif /* MBEDTLS_ENTROPY_NV_SEED */
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#if defined(MBEDTLS_FS_IO)
392int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100393{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100395 FILE *f;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100397
398 if( ( f = fopen( path, "wb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100402 goto exit;
403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100407 goto exit;
408 }
409
410 ret = 0;
411
412exit:
413 fclose( f );
414 return( ret );
415}
416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100418{
419 FILE *f;
420 size_t n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100422
423 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100425
426 fseek( f, 0, SEEK_END );
427 n = (size_t) ftell( f );
428 fseek( f, 0, SEEK_SET );
429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
431 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100432
433 if( fread( buf, 1, n, f ) != n )
434 {
435 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100437 }
438
439 fclose( f );
440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_entropy_update_manual( ctx, buf, n );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 return( mbedtls_entropy_write_seed_file( ctx, path ) );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100444}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445#endif /* MBEDTLS_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200448/*
449 * Dummy source function
450 */
451static int entropy_dummy_source( void *data, unsigned char *output,
452 size_t len, size_t *olen )
453{
454 ((void) data);
455
456 memset( output, 0x2a, len );
457 *olen = len;
458
459 return( 0 );
460}
461
462/*
463 * The actual entropy quality is hard to test, but we can at least
464 * test that the functions don't cause errors and write the correct
465 * amount of data to buffers.
466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467int mbedtls_entropy_self_test( int verbose )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200468{
469 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_entropy_context ctx;
471 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
472 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200473 size_t i, j;
474
475 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_printf( " ENTROPY test: " );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200479
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200480 /* First do a gather to make sure we have default sources */
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200481 if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200482 goto cleanup;
483
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200484 ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16,
485 MBEDTLS_ENTROPY_SOURCE_WEAK );
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200486 if( ret != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200487 goto cleanup;
488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200490 goto cleanup;
491
492 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 * To test that mbedtls_entropy_func writes correct number of bytes:
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200494 * - use the whole buffer and rely on ASan to detect overruns
495 * - collect entropy 8 times and OR the result in an accumulator:
496 * any byte should then be 0 with probably 2^(-64), so requiring
497 * each of the 32 or 64 bytes to be non-zero has a false failure rate
498 * of at most 2^(-58) which is acceptable.
499 */
500 for( i = 0; i < 8; i++ )
501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200503 goto cleanup;
504
505 for( j = 0; j < sizeof( buf ); j++ )
506 acc[j] |= buf[j];
507 }
508
509 for( j = 0; j < sizeof( buf ); j++ )
510 {
511 if( acc[j] == 0 )
512 {
513 ret = 1;
514 goto cleanup;
515 }
516 }
517
518cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200520
521 if( verbose != 0 )
522 {
523 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200525 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200529 }
530
531 return( ret != 0 );
532}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535#endif /* MBEDTLS_ENTROPY_C */