blob: 0d9a8150ba63ed2b6b2acc5ba627101efa6fd942 [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Entropy accumulator implementation
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, 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 Bakker6083fd22011-12-03 21:45:14 +000057#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059void mbedtls_entropy_init( mbedtls_entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +000060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 memset( ctx, 0, sizeof(mbedtls_entropy_context) );
Paul Bakker6083fd22011-12-03 21:45:14 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_THREADING_C)
64 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020065#endif
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
68 mbedtls_sha512_starts( &ctx->accumulator, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +020069#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_sha256_starts( &ctx->accumulator, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +020071#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_HAVEGE_C)
73 mbedtls_havege_init( &ctx->havege_data );
Paul Bakker43655f42011-12-15 20:11:16 +000074#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
77#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
78 mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020079 MBEDTLS_ENTROPY_MIN_PLATFORM,
80 MBEDTLS_ENTROPY_SOURCE_STRONG );
Paul Bakker6083fd22011-12-03 21:45:14 +000081#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020083 mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL,
84 MBEDTLS_ENTROPY_MIN_HARDCLOCK,
85 MBEDTLS_ENTROPY_SOURCE_WEAK );
Paul Bakker6083fd22011-12-03 21:45:14 +000086#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if defined(MBEDTLS_HAVEGE_C)
88 mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020089 MBEDTLS_ENTROPY_MIN_HAVEGE,
90 MBEDTLS_ENTROPY_SOURCE_STRONG );
Paul Bakker28c7e7f2011-12-15 19:49:30 +000091#endif
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +020092#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Manuel Pégourié-Gonnardfc2ccfe2015-07-10 11:15:50 +010093 mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020094 MBEDTLS_ENTROPY_MIN_HARDWARE,
95 MBEDTLS_ENTROPY_SOURCE_STRONG );
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +020096#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +000098}
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
Paul Bakker1ffefac2013-09-28 15:23:03 +0200101{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#if defined(MBEDTLS_HAVEGE_C)
103 mbedtls_havege_free( &ctx->havege_data );
Paul Bakkera317a982014-06-18 16:44:11 +0200104#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#if defined(MBEDTLS_THREADING_C)
106 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200107#endif
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +0100108 mbedtls_zeroize( ctx, sizeof( mbedtls_entropy_context ) );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200109}
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
112 mbedtls_entropy_f_source_ptr f_source, void *p_source,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200113 size_t threshold, int strong )
Paul Bakker6083fd22011-12-03 21:45:14 +0000114{
Paul Bakker47703a02014-02-06 15:01:20 +0100115 int index, ret = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if defined(MBEDTLS_THREADING_C)
118 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100119 return( ret );
120#endif
121
122 index = ctx->source_count;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 if( index >= MBEDTLS_ENTROPY_MAX_SOURCES )
Paul Bakker47703a02014-02-06 15:01:20 +0100124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
Paul Bakker47703a02014-02-06 15:01:20 +0100126 goto exit;
127 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000128
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200129 ctx->source[index].f_source = f_source;
130 ctx->source[index].p_source = p_source;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000131 ctx->source[index].threshold = threshold;
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200132 ctx->source[index].strong = strong;
Paul Bakker6083fd22011-12-03 21:45:14 +0000133
134 ctx->source_count++;
135
Paul Bakker47703a02014-02-06 15:01:20 +0100136exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#if defined(MBEDTLS_THREADING_C)
138 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
139 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100140#endif
141
142 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000143}
144
145/*
146 * Entropy accumulator update
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200149 const unsigned char *data, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000150{
151 unsigned char header[2];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000153 size_t use_len = len;
154 const unsigned char *p = data;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker6083fd22011-12-03 21:45:14 +0000157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
159 mbedtls_sha512( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200160#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_sha256( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200162#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000163 p = tmp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
Paul Bakker6083fd22011-12-03 21:45:14 +0000165 }
166
167 header[0] = source_id;
168 header[1] = use_len & 0xFF;
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
171 mbedtls_sha512_update( &ctx->accumulator, header, 2 );
172 mbedtls_sha512_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200173#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_sha256_update( &ctx->accumulator, header, 2 );
175 mbedtls_sha256_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200176#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200177
Andres Amaya Garciabab1edc2017-07-05 15:45:47 +0100178 mbedtls_zeroize( tmp, sizeof( tmp ) );
179
Paul Bakker6083fd22011-12-03 21:45:14 +0000180 return( 0 );
181}
182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
Paul Bakker6083fd22011-12-03 21:45:14 +0000184 const unsigned char *data, size_t len )
185{
Paul Bakker47703a02014-02-06 15:01:20 +0100186 int ret;
187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#if defined(MBEDTLS_THREADING_C)
189 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100190 return( ret );
191#endif
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len );
Paul Bakker47703a02014-02-06 15:01:20 +0100194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_THREADING_C)
196 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
197 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100198#endif
199
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200200 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000201}
202
203/*
204 * Run through the different sources to add entropy to our accumulator
205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206static int entropy_gather_internal( mbedtls_entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +0000207{
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200208 int ret, i, have_one_strong = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
Paul Bakker6083fd22011-12-03 21:45:14 +0000210 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100211
Paul Bakker43655f42011-12-15 20:11:16 +0000212 if( ctx->source_count == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
Paul Bakker43655f42011-12-15 20:11:16 +0000214
Paul Bakker6083fd22011-12-03 21:45:14 +0000215 /*
216 * Run through our entropy sources
217 */
218 for( i = 0; i < ctx->source_count; i++ )
219 {
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200220 if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
221 have_one_strong = 1;
222
Paul Bakker6083fd22011-12-03 21:45:14 +0000223 olen = 0;
Paul Bakker66d5d072014-06-17 16:39:18 +0200224 if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
Paul Bakker6083fd22011-12-03 21:45:14 +0000226 {
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100227 goto cleanup;
Paul Bakker6083fd22011-12-03 21:45:14 +0000228 }
229
230 /*
231 * Add if we actually gathered something
232 */
233 if( olen > 0 )
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000234 {
Paul Bakker6083fd22011-12-03 21:45:14 +0000235 entropy_update( ctx, (unsigned char) i, buf, olen );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000236 ctx->source[i].size += olen;
237 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000238 }
239
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200240 if( have_one_strong == 0 )
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100241 ret = MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE;
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200242
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100243cleanup:
244 mbedtls_zeroize( buf, sizeof( buf ) );
245
246 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000247}
248
Paul Bakker47703a02014-02-06 15:01:20 +0100249/*
250 * Thread-safe wrapper for entropy_gather_internal()
251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
Paul Bakker47703a02014-02-06 15:01:20 +0100253{
Paul Bakkerddd427a2014-04-09 14:47:58 +0200254 int ret;
Paul Bakker47703a02014-02-06 15:01:20 +0100255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#if defined(MBEDTLS_THREADING_C)
257 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerddd427a2014-04-09 14:47:58 +0200258 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100259#endif
260
Paul Bakkerddd427a2014-04-09 14:47:58 +0200261 ret = entropy_gather_internal( ctx );
Paul Bakker47703a02014-02-06 15:01:20 +0100262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#if defined(MBEDTLS_THREADING_C)
264 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
265 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100266#endif
267
Paul Bakkerddd427a2014-04-09 14:47:58 +0200268 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100269}
270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000272{
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200273 int ret, count = 0, i, done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
275 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
278 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#if defined(MBEDTLS_THREADING_C)
281 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200282 return( ret );
283#endif
284
Paul Bakker6083fd22011-12-03 21:45:14 +0000285 /*
286 * Always gather extra entropy before a call
287 */
288 do
289 {
290 if( count++ > ENTROPY_MAX_LOOP )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200293 goto exit;
294 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000295
Paul Bakker47703a02014-02-06 15:01:20 +0100296 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200297 goto exit;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000298
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200299 done = 1;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000300 for( i = 0; i < ctx->source_count; i++ )
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200301 if( ctx->source[i].size < ctx->source[i].threshold )
302 done = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000303 }
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200304 while( ! done );
Paul Bakker6083fd22011-12-03 21:45:14 +0000305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakker6083fd22011-12-03 21:45:14 +0000307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
309 mbedtls_sha512_finish( &ctx->accumulator, buf );
Paul Bakker9e36f042013-06-30 14:34:05 +0200310
Paul Bakker6083fd22011-12-03 21:45:14 +0000311 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000312 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha512_context ) );
315 mbedtls_sha512_starts( &ctx->accumulator, 0 );
316 mbedtls_sha512_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200317
318 /*
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100319 * Perform second SHA-512 on entropy
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
322#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
323 mbedtls_sha256_finish( &ctx->accumulator, buf );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200324
325 /*
326 * Reset accumulator and counters and recycle existing entropy
327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha256_context ) );
329 mbedtls_sha256_starts( &ctx->accumulator, 0 );
330 mbedtls_sha256_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100331
332 /*
333 * Perform second SHA-256 on entropy
334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_sha256( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
336#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000337
338 for( i = 0; i < ctx->source_count; i++ )
339 ctx->source[i].size = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000340
341 memcpy( output, buf, len );
342
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200343 ret = 0;
344
345exit:
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100346 mbedtls_zeroize( buf, sizeof( buf ) );
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#if defined(MBEDTLS_THREADING_C)
349 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
350 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200351#endif
352
353 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000354}
355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#if defined(MBEDTLS_FS_IO)
357int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100358{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100360 FILE *f;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100362
363 if( ( f = fopen( path, "wb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100367 goto exit;
368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100372 goto exit;
373 }
374
375 ret = 0;
376
377exit:
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100378 mbedtls_zeroize( buf, sizeof( buf ) );
379
Paul Bakker66ff70d2014-03-26 11:54:05 +0100380 fclose( f );
381 return( ret );
382}
383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100385{
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100386 int ret = 0;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100387 FILE *f;
388 size_t n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100390
391 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100393
394 fseek( f, 0, SEEK_END );
395 n = (size_t) ftell( f );
396 fseek( f, 0, SEEK_SET );
397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
399 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100400
401 if( fread( buf, 1, n, f ) != n )
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100402 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
403 else
404 ret = mbedtls_entropy_update_manual( ctx, buf, n );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100405
406 fclose( f );
407
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100408 mbedtls_zeroize( buf, sizeof( buf ) );
409
410 if( ret != 0 )
411 return( ret );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 return( mbedtls_entropy_write_seed_file( ctx, path ) );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100414}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415#endif /* MBEDTLS_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200418/*
419 * Dummy source function
420 */
421static int entropy_dummy_source( void *data, unsigned char *output,
422 size_t len, size_t *olen )
423{
424 ((void) data);
425
426 memset( output, 0x2a, len );
427 *olen = len;
428
429 return( 0 );
430}
431
432/*
433 * The actual entropy quality is hard to test, but we can at least
434 * test that the functions don't cause errors and write the correct
435 * amount of data to buffers.
436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437int mbedtls_entropy_self_test( int verbose )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200438{
439 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_entropy_context ctx;
441 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
442 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200443 size_t i, j;
444
445 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_printf( " ENTROPY test: " );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200449
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200450 /* First do a gather to make sure we have default sources */
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200451 if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200452 goto cleanup;
453
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200454 ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16,
455 MBEDTLS_ENTROPY_SOURCE_WEAK );
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200456 if( ret != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200457 goto cleanup;
458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200460 goto cleanup;
461
462 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 * To test that mbedtls_entropy_func writes correct number of bytes:
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200464 * - use the whole buffer and rely on ASan to detect overruns
465 * - collect entropy 8 times and OR the result in an accumulator:
466 * any byte should then be 0 with probably 2^(-64), so requiring
467 * each of the 32 or 64 bytes to be non-zero has a false failure rate
468 * of at most 2^(-58) which is acceptable.
469 */
470 for( i = 0; i < 8; i++ )
471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200473 goto cleanup;
474
475 for( j = 0; j < sizeof( buf ); j++ )
476 acc[j] |= buf[j];
477 }
478
479 for( j = 0; j < sizeof( buf ); j++ )
480 {
481 if( acc[j] == 0 )
482 {
483 ret = 1;
484 goto cleanup;
485 }
486 }
487
488cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200490
491 if( verbose != 0 )
492 {
493 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200495 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200499 }
500
501 return( ret != 0 );
502}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#endif /* MBEDTLS_ENTROPY_C */