blob: 282640f2ddd51cefa288d7266cc93e679d88b785 [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Entropy accumulator implementation
3 *
Paul Bakker9988d6b2016-06-01 11:29:42 +01004 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker6083fd22011-12-03 21:45:14 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker6083fd22011-12-03 21:45:14 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000029
Simon Butcherab5df402016-06-11 02:31:21 +010030#if defined(MBEDTLS_TEST_NULL_ENTROPY)
31#warning "**** WARNING! MBEDTLS_TEST_NULL_ENTROPY defined! ****"
32#warning "**** THIS BUILD HAS NO DEFINED ENTROPY SOURCES ****"
33#warning "**** NOT SUITABLE FOR PRODUCTION ****"
34#endif
35
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/entropy.h"
37#include "mbedtls/entropy_poll.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000038
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <string.h>
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_FS_IO)
Paul Bakker66ff70d2014-03-26 11:54:05 +010042#include <stdio.h>
43#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_SELF_TEST)
46#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/platform.h"
Rich Evans00ab4702015-02-06 13:43:58 +000048#else
49#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#define mbedtls_printf printf
51#endif /* MBEDTLS_PLATFORM_C */
52#endif /* MBEDTLS_SELF_TEST */
Rich Evans00ab4702015-02-06 13:43:58 +000053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/havege.h"
Paul Bakker28c7e7f2011-12-15 19:49:30 +000056#endif
57
Paul Bakker34617722014-06-13 17:20:13 +020058/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020060 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
61}
62
Paul Bakker6083fd22011-12-03 21:45:14 +000063#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065void mbedtls_entropy_init( mbedtls_entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +000066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 memset( ctx, 0, sizeof(mbedtls_entropy_context) );
Paul Bakker6083fd22011-12-03 21:45:14 +000068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_THREADING_C)
70 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020071#endif
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
74 mbedtls_sha512_starts( &ctx->accumulator, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +020075#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_sha256_starts( &ctx->accumulator, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +020077#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#if defined(MBEDTLS_HAVEGE_C)
79 mbedtls_havege_init( &ctx->havege_data );
Paul Bakker43655f42011-12-15 20:11:16 +000080#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000081
Simon Butcherab5df402016-06-11 02:31:21 +010082#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher4157b602016-06-12 00:31:33 +010083 mbedtls_entropy_add_source( ctx, mbedtls_null_entropy_poll, NULL,
Janos Follath53de7842016-06-08 15:29:18 +010084 1, MBEDTLS_ENTROPY_SOURCE_STRONG );
85#endif
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
88#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
89 mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020090 MBEDTLS_ENTROPY_MIN_PLATFORM,
91 MBEDTLS_ENTROPY_SOURCE_STRONG );
Paul Bakker6083fd22011-12-03 21:45:14 +000092#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020094 mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL,
95 MBEDTLS_ENTROPY_MIN_HARDCLOCK,
96 MBEDTLS_ENTROPY_SOURCE_WEAK );
Paul Bakker6083fd22011-12-03 21:45:14 +000097#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#if defined(MBEDTLS_HAVEGE_C)
99 mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200100 MBEDTLS_ENTROPY_MIN_HAVEGE,
101 MBEDTLS_ENTROPY_SOURCE_STRONG );
Paul Bakker28c7e7f2011-12-15 19:49:30 +0000102#endif
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +0200103#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Manuel Pégourié-Gonnardfc2ccfe2015-07-10 11:15:50 +0100104 mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200105 MBEDTLS_ENTROPY_MIN_HARDWARE,
106 MBEDTLS_ENTROPY_SOURCE_STRONG );
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +0200107#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +0100108#if defined(MBEDTLS_ENTROPY_NV_SEED)
109 mbedtls_entropy_add_source( ctx, mbedtls_nv_seed_poll, NULL,
110 MBEDTLS_ENTROPY_BLOCK_SIZE,
111 MBEDTLS_ENTROPY_SOURCE_STRONG );
112#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +0000114}
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
Paul Bakker1ffefac2013-09-28 15:23:03 +0200117{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_HAVEGE_C)
119 mbedtls_havege_free( &ctx->havege_data );
Paul Bakkera317a982014-06-18 16:44:11 +0200120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121#if defined(MBEDTLS_THREADING_C)
122 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200123#endif
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +0100124 mbedtls_zeroize( ctx, sizeof( mbedtls_entropy_context ) );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200125}
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
128 mbedtls_entropy_f_source_ptr f_source, void *p_source,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200129 size_t threshold, int strong )
Paul Bakker6083fd22011-12-03 21:45:14 +0000130{
Paul Bakker47703a02014-02-06 15:01:20 +0100131 int index, ret = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#if defined(MBEDTLS_THREADING_C)
134 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100135 return( ret );
136#endif
137
138 index = ctx->source_count;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 if( index >= MBEDTLS_ENTROPY_MAX_SOURCES )
Paul Bakker47703a02014-02-06 15:01:20 +0100140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
Paul Bakker47703a02014-02-06 15:01:20 +0100142 goto exit;
143 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000144
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200145 ctx->source[index].f_source = f_source;
146 ctx->source[index].p_source = p_source;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000147 ctx->source[index].threshold = threshold;
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200148 ctx->source[index].strong = strong;
Paul Bakker6083fd22011-12-03 21:45:14 +0000149
150 ctx->source_count++;
151
Paul Bakker47703a02014-02-06 15:01:20 +0100152exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#if defined(MBEDTLS_THREADING_C)
154 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
155 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100156#endif
157
158 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000159}
160
161/*
162 * Entropy accumulator update
163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200165 const unsigned char *data, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000166{
167 unsigned char header[2];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000169 size_t use_len = len;
170 const unsigned char *p = data;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker6083fd22011-12-03 21:45:14 +0000173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
175 mbedtls_sha512( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200176#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_sha256( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200178#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000179 p = tmp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
Paul Bakker6083fd22011-12-03 21:45:14 +0000181 }
182
183 header[0] = source_id;
184 header[1] = use_len & 0xFF;
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
187 mbedtls_sha512_update( &ctx->accumulator, header, 2 );
188 mbedtls_sha512_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200189#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_sha256_update( &ctx->accumulator, header, 2 );
191 mbedtls_sha256_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200192#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200193
Paul Bakker6083fd22011-12-03 21:45:14 +0000194 return( 0 );
195}
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
Paul Bakker6083fd22011-12-03 21:45:14 +0000198 const unsigned char *data, size_t len )
199{
Paul Bakker47703a02014-02-06 15:01:20 +0100200 int ret;
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if defined(MBEDTLS_THREADING_C)
203 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100204 return( ret );
205#endif
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len );
Paul Bakker47703a02014-02-06 15:01:20 +0100208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#if defined(MBEDTLS_THREADING_C)
210 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
211 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100212#endif
213
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200214 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000215}
216
217/*
218 * Run through the different sources to add entropy to our accumulator
219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220static int entropy_gather_internal( mbedtls_entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +0000221{
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200222 int ret, i, have_one_strong = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
Paul Bakker6083fd22011-12-03 21:45:14 +0000224 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100225
Paul Bakker43655f42011-12-15 20:11:16 +0000226 if( ctx->source_count == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
Paul Bakker43655f42011-12-15 20:11:16 +0000228
Paul Bakker6083fd22011-12-03 21:45:14 +0000229 /*
230 * Run through our entropy sources
231 */
232 for( i = 0; i < ctx->source_count; i++ )
233 {
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200234 if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
235 have_one_strong = 1;
236
Paul Bakker6083fd22011-12-03 21:45:14 +0000237 olen = 0;
Paul Bakker66d5d072014-06-17 16:39:18 +0200238 if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
Paul Bakker6083fd22011-12-03 21:45:14 +0000240 {
241 return( ret );
242 }
243
244 /*
245 * Add if we actually gathered something
246 */
247 if( olen > 0 )
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000248 {
Paul Bakker6083fd22011-12-03 21:45:14 +0000249 entropy_update( ctx, (unsigned char) i, buf, olen );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000250 ctx->source[i].size += olen;
251 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000252 }
253
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200254 if( have_one_strong == 0 )
255 return( MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE );
256
Paul Bakker6083fd22011-12-03 21:45:14 +0000257 return( 0 );
258}
259
Paul Bakker47703a02014-02-06 15:01:20 +0100260/*
261 * Thread-safe wrapper for entropy_gather_internal()
262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
Paul Bakker47703a02014-02-06 15:01:20 +0100264{
Paul Bakkerddd427a2014-04-09 14:47:58 +0200265 int ret;
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( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerddd427a2014-04-09 14:47:58 +0200269 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100270#endif
271
Paul Bakkerddd427a2014-04-09 14:47:58 +0200272 ret = entropy_gather_internal( ctx );
Paul Bakker47703a02014-02-06 15:01:20 +0100273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#if defined(MBEDTLS_THREADING_C)
275 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
276 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100277#endif
278
Paul Bakkerddd427a2014-04-09 14:47:58 +0200279 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100280}
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000283{
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200284 int ret, count = 0, i, done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
286 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
289 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000290
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100291#if defined(MBEDTLS_ENTROPY_NV_SEED)
292 /* Update the NV entropy seed before generating any entropy for outside
293 * use.
294 */
Paul Bakkerfc9c7c82016-06-01 15:25:50 +0100295 if( ctx->initial_entropy_run == 0 )
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100296 {
Paul Bakkerfc9c7c82016-06-01 15:25:50 +0100297 ctx->initial_entropy_run = 1;
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100298 if( ( ret = mbedtls_entropy_update_nv_seed( ctx ) ) != 0 )
299 return( ret );
300 }
301#endif
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#if defined(MBEDTLS_THREADING_C)
304 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200305 return( ret );
306#endif
307
Paul Bakker6083fd22011-12-03 21:45:14 +0000308 /*
309 * Always gather extra entropy before a call
310 */
311 do
312 {
313 if( count++ > ENTROPY_MAX_LOOP )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200316 goto exit;
317 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000318
Paul Bakker47703a02014-02-06 15:01:20 +0100319 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200320 goto exit;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000321
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200322 done = 1;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000323 for( i = 0; i < ctx->source_count; i++ )
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200324 if( ctx->source[i].size < ctx->source[i].threshold )
325 done = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000326 }
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200327 while( ! done );
Paul Bakker6083fd22011-12-03 21:45:14 +0000328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakker6083fd22011-12-03 21:45:14 +0000330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
332 mbedtls_sha512_finish( &ctx->accumulator, buf );
Paul Bakker9e36f042013-06-30 14:34:05 +0200333
Paul Bakker6083fd22011-12-03 21:45:14 +0000334 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000335 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha512_context ) );
338 mbedtls_sha512_starts( &ctx->accumulator, 0 );
339 mbedtls_sha512_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200340
341 /*
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100342 * Perform second SHA-512 on entropy
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
345#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
346 mbedtls_sha256_finish( &ctx->accumulator, buf );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200347
348 /*
349 * Reset accumulator and counters and recycle existing entropy
350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha256_context ) );
352 mbedtls_sha256_starts( &ctx->accumulator, 0 );
353 mbedtls_sha256_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100354
355 /*
356 * Perform second SHA-256 on entropy
357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_sha256( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
359#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000360
361 for( i = 0; i < ctx->source_count; i++ )
362 ctx->source[i].size = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000363
364 memcpy( output, buf, len );
365
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200366 ret = 0;
367
368exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#if defined(MBEDTLS_THREADING_C)
370 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
371 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200372#endif
373
374 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000375}
376
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100377#if defined(MBEDTLS_ENTROPY_NV_SEED)
378int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx )
379{
380 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
381 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
382
383 /* Read new seed and write it to NV */
384 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
385 return( ret );
386
387 if( mbedtls_nv_seed_write( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
388 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
389
390 /* Manually update the remaining stream with a separator value to diverge */
391 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
392 mbedtls_entropy_update_manual( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
393
394 return( 0 );
395}
396#endif /* MBEDTLS_ENTROPY_NV_SEED */
397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398#if defined(MBEDTLS_FS_IO)
399int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100400{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100402 FILE *f;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100404
405 if( ( f = fopen( path, "wb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100409 goto exit;
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100414 goto exit;
415 }
416
417 ret = 0;
418
419exit:
420 fclose( f );
421 return( ret );
422}
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100425{
426 FILE *f;
427 size_t n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100429
430 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100432
433 fseek( f, 0, SEEK_END );
434 n = (size_t) ftell( f );
435 fseek( f, 0, SEEK_SET );
436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
438 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100439
440 if( fread( buf, 1, n, f ) != n )
441 {
442 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100444 }
445
446 fclose( f );
447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_entropy_update_manual( ctx, buf, n );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 return( mbedtls_entropy_write_seed_file( ctx, path ) );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100451}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452#endif /* MBEDTLS_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200455/*
456 * Dummy source function
457 */
458static int entropy_dummy_source( void *data, unsigned char *output,
459 size_t len, size_t *olen )
460{
461 ((void) data);
462
463 memset( output, 0x2a, len );
464 *olen = len;
465
466 return( 0 );
467}
468
469/*
470 * The actual entropy quality is hard to test, but we can at least
471 * test that the functions don't cause errors and write the correct
472 * amount of data to buffers.
473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474int mbedtls_entropy_self_test( int verbose )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200475{
476 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_entropy_context ctx;
478 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
479 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200480 size_t i, j;
481
482 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_printf( " ENTROPY test: " );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200486
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200487 /* First do a gather to make sure we have default sources */
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200488 if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200489 goto cleanup;
490
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200491 ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16,
492 MBEDTLS_ENTROPY_SOURCE_WEAK );
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200493 if( ret != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200494 goto cleanup;
495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200497 goto cleanup;
498
499 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 * To test that mbedtls_entropy_func writes correct number of bytes:
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200501 * - use the whole buffer and rely on ASan to detect overruns
502 * - collect entropy 8 times and OR the result in an accumulator:
503 * any byte should then be 0 with probably 2^(-64), so requiring
504 * each of the 32 or 64 bytes to be non-zero has a false failure rate
505 * of at most 2^(-58) which is acceptable.
506 */
507 for( i = 0; i < 8; i++ )
508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200510 goto cleanup;
511
512 for( j = 0; j < sizeof( buf ); j++ )
513 acc[j] |= buf[j];
514 }
515
516 for( j = 0; j < sizeof( buf ); j++ )
517 {
518 if( acc[j] == 0 )
519 {
520 ret = 1;
521 goto cleanup;
522 }
523 }
524
525cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200527
528 if( verbose != 0 )
529 {
530 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200532 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200536 }
537
538 return( ret != 0 );
539}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542#endif /* MBEDTLS_ENTROPY_C */