blob: d8c5c510429da92340b69cb0e7e8ca13e2a65d1c [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
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 Bakkerd5c9f6d2016-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
Janos Follath53de7842016-06-08 15:29:18 +010080#if defined(MBEDTLS_TEST_WO_ENTROPY)
81 mbedtls_entropy_add_source( ctx, mbedtls_zero_entropy_poll, NULL,
82 1, MBEDTLS_ENTROPY_SOURCE_STRONG );
83#endif
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
86#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
87 mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020088 MBEDTLS_ENTROPY_MIN_PLATFORM,
89 MBEDTLS_ENTROPY_SOURCE_STRONG );
Paul Bakker6083fd22011-12-03 21:45:14 +000090#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020092 mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL,
93 MBEDTLS_ENTROPY_MIN_HARDCLOCK,
94 MBEDTLS_ENTROPY_SOURCE_WEAK );
Paul Bakker6083fd22011-12-03 21:45:14 +000095#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#if defined(MBEDTLS_HAVEGE_C)
97 mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020098 MBEDTLS_ENTROPY_MIN_HAVEGE,
99 MBEDTLS_ENTROPY_SOURCE_STRONG );
Paul Bakker28c7e7f2011-12-15 19:49:30 +0000100#endif
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +0200101#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Manuel Pégourié-Gonnardfc2ccfe2015-07-10 11:15:50 +0100102 mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200103 MBEDTLS_ENTROPY_MIN_HARDWARE,
104 MBEDTLS_ENTROPY_SOURCE_STRONG );
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +0200105#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +0100106#if defined(MBEDTLS_ENTROPY_NV_SEED)
107 mbedtls_entropy_add_source( ctx, mbedtls_nv_seed_poll, NULL,
108 MBEDTLS_ENTROPY_BLOCK_SIZE,
109 MBEDTLS_ENTROPY_SOURCE_STRONG );
110#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +0000112}
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
Paul Bakker1ffefac2013-09-28 15:23:03 +0200115{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116#if defined(MBEDTLS_HAVEGE_C)
117 mbedtls_havege_free( &ctx->havege_data );
Paul Bakkera317a982014-06-18 16:44:11 +0200118#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119#if defined(MBEDTLS_THREADING_C)
120 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200121#endif
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +0100122 mbedtls_zeroize( ctx, sizeof( mbedtls_entropy_context ) );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200123}
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
126 mbedtls_entropy_f_source_ptr f_source, void *p_source,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200127 size_t threshold, int strong )
Paul Bakker6083fd22011-12-03 21:45:14 +0000128{
Paul Bakker47703a02014-02-06 15:01:20 +0100129 int index, ret = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_THREADING_C)
132 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100133 return( ret );
134#endif
135
136 index = ctx->source_count;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 if( index >= MBEDTLS_ENTROPY_MAX_SOURCES )
Paul Bakker47703a02014-02-06 15:01:20 +0100138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
Paul Bakker47703a02014-02-06 15:01:20 +0100140 goto exit;
141 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000142
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200143 ctx->source[index].f_source = f_source;
144 ctx->source[index].p_source = p_source;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000145 ctx->source[index].threshold = threshold;
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200146 ctx->source[index].strong = strong;
Paul Bakker6083fd22011-12-03 21:45:14 +0000147
148 ctx->source_count++;
149
Paul Bakker47703a02014-02-06 15:01:20 +0100150exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#if defined(MBEDTLS_THREADING_C)
152 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
153 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100154#endif
155
156 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000157}
158
159/*
160 * Entropy accumulator update
161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200163 const unsigned char *data, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000164{
165 unsigned char header[2];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000167 size_t use_len = len;
168 const unsigned char *p = data;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker6083fd22011-12-03 21:45:14 +0000171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
173 mbedtls_sha512( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200174#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_sha256( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200176#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000177 p = tmp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
Paul Bakker6083fd22011-12-03 21:45:14 +0000179 }
180
181 header[0] = source_id;
182 header[1] = use_len & 0xFF;
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
185 mbedtls_sha512_update( &ctx->accumulator, header, 2 );
186 mbedtls_sha512_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200187#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_sha256_update( &ctx->accumulator, header, 2 );
189 mbedtls_sha256_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200190#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200191
Paul Bakker6083fd22011-12-03 21:45:14 +0000192 return( 0 );
193}
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
Paul Bakker6083fd22011-12-03 21:45:14 +0000196 const unsigned char *data, size_t len )
197{
Paul Bakker47703a02014-02-06 15:01:20 +0100198 int ret;
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#if defined(MBEDTLS_THREADING_C)
201 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100202 return( ret );
203#endif
204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len );
Paul Bakker47703a02014-02-06 15:01:20 +0100206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#if defined(MBEDTLS_THREADING_C)
208 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
209 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100210#endif
211
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200212 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000213}
214
215/*
216 * Run through the different sources to add entropy to our accumulator
217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218static int entropy_gather_internal( mbedtls_entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +0000219{
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200220 int ret, i, have_one_strong = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
Paul Bakker6083fd22011-12-03 21:45:14 +0000222 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100223
Paul Bakker43655f42011-12-15 20:11:16 +0000224 if( ctx->source_count == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
Paul Bakker43655f42011-12-15 20:11:16 +0000226
Paul Bakker6083fd22011-12-03 21:45:14 +0000227 /*
228 * Run through our entropy sources
229 */
230 for( i = 0; i < ctx->source_count; i++ )
231 {
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200232 if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
233 have_one_strong = 1;
234
Paul Bakker6083fd22011-12-03 21:45:14 +0000235 olen = 0;
Paul Bakker66d5d072014-06-17 16:39:18 +0200236 if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
Paul Bakker6083fd22011-12-03 21:45:14 +0000238 {
239 return( ret );
240 }
241
242 /*
243 * Add if we actually gathered something
244 */
245 if( olen > 0 )
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000246 {
Paul Bakker6083fd22011-12-03 21:45:14 +0000247 entropy_update( ctx, (unsigned char) i, buf, olen );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000248 ctx->source[i].size += olen;
249 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000250 }
251
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200252 if( have_one_strong == 0 )
253 return( MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE );
254
Paul Bakker6083fd22011-12-03 21:45:14 +0000255 return( 0 );
256}
257
Paul Bakker47703a02014-02-06 15:01:20 +0100258/*
259 * Thread-safe wrapper for entropy_gather_internal()
260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
Paul Bakker47703a02014-02-06 15:01:20 +0100262{
Paul Bakkerddd427a2014-04-09 14:47:58 +0200263 int ret;
Paul Bakker47703a02014-02-06 15:01:20 +0100264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#if defined(MBEDTLS_THREADING_C)
266 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerddd427a2014-04-09 14:47:58 +0200267 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100268#endif
269
Paul Bakkerddd427a2014-04-09 14:47:58 +0200270 ret = entropy_gather_internal( ctx );
Paul Bakker47703a02014-02-06 15:01:20 +0100271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_THREADING_C)
273 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
274 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100275#endif
276
Paul Bakkerddd427a2014-04-09 14:47:58 +0200277 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100278}
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000281{
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200282 int ret, count = 0, i, done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
284 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
287 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000288
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100289#if defined(MBEDTLS_ENTROPY_NV_SEED)
290 /* Update the NV entropy seed before generating any entropy for outside
291 * use.
292 */
293 if( initial_entropy_run == 0 )
294 {
295 initial_entropy_run = 1;
296 if( ( ret = mbedtls_entropy_update_nv_seed( ctx ) ) != 0 )
297 return( ret );
298 }
299#endif
300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#if defined(MBEDTLS_THREADING_C)
302 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200303 return( ret );
304#endif
305
Paul Bakker6083fd22011-12-03 21:45:14 +0000306 /*
307 * Always gather extra entropy before a call
308 */
309 do
310 {
311 if( count++ > ENTROPY_MAX_LOOP )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200314 goto exit;
315 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000316
Paul Bakker47703a02014-02-06 15:01:20 +0100317 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200318 goto exit;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000319
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200320 done = 1;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000321 for( i = 0; i < ctx->source_count; i++ )
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200322 if( ctx->source[i].size < ctx->source[i].threshold )
323 done = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000324 }
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200325 while( ! done );
Paul Bakker6083fd22011-12-03 21:45:14 +0000326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakker6083fd22011-12-03 21:45:14 +0000328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
330 mbedtls_sha512_finish( &ctx->accumulator, buf );
Paul Bakker9e36f042013-06-30 14:34:05 +0200331
Paul Bakker6083fd22011-12-03 21:45:14 +0000332 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000333 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha512_context ) );
336 mbedtls_sha512_starts( &ctx->accumulator, 0 );
337 mbedtls_sha512_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200338
339 /*
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100340 * Perform second SHA-512 on entropy
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
343#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
344 mbedtls_sha256_finish( &ctx->accumulator, buf );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200345
346 /*
347 * Reset accumulator and counters and recycle existing entropy
348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha256_context ) );
350 mbedtls_sha256_starts( &ctx->accumulator, 0 );
351 mbedtls_sha256_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100352
353 /*
354 * Perform second SHA-256 on entropy
355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_sha256( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
357#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000358
359 for( i = 0; i < ctx->source_count; i++ )
360 ctx->source[i].size = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000361
362 memcpy( output, buf, len );
363
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200364 ret = 0;
365
366exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#if defined(MBEDTLS_THREADING_C)
368 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
369 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200370#endif
371
372 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000373}
374
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100375#if defined(MBEDTLS_ENTROPY_NV_SEED)
376int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx )
377{
378 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
379 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
380
381 /* Read new seed and write it to NV */
382 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
383 return( ret );
384
385 if( mbedtls_nv_seed_write( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
386 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
387
388 /* Manually update the remaining stream with a separator value to diverge */
389 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
390 mbedtls_entropy_update_manual( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
391
392 return( 0 );
393}
394#endif /* MBEDTLS_ENTROPY_NV_SEED */
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#if defined(MBEDTLS_FS_IO)
397int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100398{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100400 FILE *f;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100402
403 if( ( f = fopen( path, "wb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100407 goto exit;
408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100412 goto exit;
413 }
414
415 ret = 0;
416
417exit:
418 fclose( f );
419 return( ret );
420}
421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100423{
424 FILE *f;
425 size_t n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100427
428 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100430
431 fseek( f, 0, SEEK_END );
432 n = (size_t) ftell( f );
433 fseek( f, 0, SEEK_SET );
434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
436 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100437
438 if( fread( buf, 1, n, f ) != n )
439 {
440 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100442 }
443
444 fclose( f );
445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_entropy_update_manual( ctx, buf, n );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 return( mbedtls_entropy_write_seed_file( ctx, path ) );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100449}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450#endif /* MBEDTLS_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200453/*
454 * Dummy source function
455 */
456static int entropy_dummy_source( void *data, unsigned char *output,
457 size_t len, size_t *olen )
458{
459 ((void) data);
460
461 memset( output, 0x2a, len );
462 *olen = len;
463
464 return( 0 );
465}
466
467/*
468 * The actual entropy quality is hard to test, but we can at least
469 * test that the functions don't cause errors and write the correct
470 * amount of data to buffers.
471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472int mbedtls_entropy_self_test( int verbose )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200473{
474 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_entropy_context ctx;
476 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
477 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200478 size_t i, j;
479
480 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 mbedtls_printf( " ENTROPY test: " );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200484
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200485 /* First do a gather to make sure we have default sources */
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200486 if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200487 goto cleanup;
488
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200489 ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16,
490 MBEDTLS_ENTROPY_SOURCE_WEAK );
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200491 if( ret != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200492 goto cleanup;
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200495 goto cleanup;
496
497 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 * To test that mbedtls_entropy_func writes correct number of bytes:
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200499 * - use the whole buffer and rely on ASan to detect overruns
500 * - collect entropy 8 times and OR the result in an accumulator:
501 * any byte should then be 0 with probably 2^(-64), so requiring
502 * each of the 32 or 64 bytes to be non-zero has a false failure rate
503 * of at most 2^(-58) which is acceptable.
504 */
505 for( i = 0; i < 8; i++ )
506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200508 goto cleanup;
509
510 for( j = 0; j < sizeof( buf ); j++ )
511 acc[j] |= buf[j];
512 }
513
514 for( j = 0; j < sizeof( buf ); j++ )
515 {
516 if( acc[j] == 0 )
517 {
518 ret = 1;
519 goto cleanup;
520 }
521 }
522
523cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200525
526 if( verbose != 0 )
527 {
528 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200530 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200534 }
535
536 return( ret != 0 );
537}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#endif /* MBEDTLS_ENTROPY_C */