blob: b0a94d089d1c1fc688758f8b26abee18f6ad7c18 [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
Hanno Becker66580d22017-09-08 10:06:41 +010063 /* Reminder: Update ENTROPY_HAVE_STRONG in the test files
64 * when adding more strong entropy sources here. */
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_THREADING_C)
67 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020068#endif
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Hanno Becker1cc67a02018-01-17 17:38:28 +000071 mbedtls_sha512_init( &ctx->accumulator );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 mbedtls_sha512_starts( &ctx->accumulator, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +020073#else
Hanno Becker1cc67a02018-01-17 17:38:28 +000074 mbedtls_sha256_init( &ctx->accumulator );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 mbedtls_sha256_starts( &ctx->accumulator, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +020076#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if defined(MBEDTLS_HAVEGE_C)
78 mbedtls_havege_init( &ctx->havege_data );
Paul Bakker43655f42011-12-15 20:11:16 +000079#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
82#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
83 mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020084 MBEDTLS_ENTROPY_MIN_PLATFORM,
85 MBEDTLS_ENTROPY_SOURCE_STRONG );
Paul Bakker6083fd22011-12-03 21:45:14 +000086#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020088 mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL,
89 MBEDTLS_ENTROPY_MIN_HARDCLOCK,
90 MBEDTLS_ENTROPY_SOURCE_WEAK );
Paul Bakker6083fd22011-12-03 21:45:14 +000091#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#if defined(MBEDTLS_HAVEGE_C)
93 mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020094 MBEDTLS_ENTROPY_MIN_HAVEGE,
95 MBEDTLS_ENTROPY_SOURCE_STRONG );
Paul Bakker28c7e7f2011-12-15 19:49:30 +000096#endif
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +020097#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Manuel Pégourié-Gonnardfc2ccfe2015-07-10 11:15:50 +010098 mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020099 MBEDTLS_ENTROPY_MIN_HARDWARE,
100 MBEDTLS_ENTROPY_SOURCE_STRONG );
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +0200101#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +0000103}
104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
Paul Bakker1ffefac2013-09-28 15:23:03 +0200106{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#if defined(MBEDTLS_HAVEGE_C)
108 mbedtls_havege_free( &ctx->havege_data );
Paul Bakkera317a982014-06-18 16:44:11 +0200109#endif
Hanno Becker1cc67a02018-01-17 17:38:28 +0000110
111#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
112 mbedtls_sha512_free( &ctx->accumulator );
113#else
114 mbedtls_sha256_free( &ctx->accumulator );
115#endif
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if defined(MBEDTLS_THREADING_C)
118 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200119#endif
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +0100120 mbedtls_zeroize( ctx, sizeof( mbedtls_entropy_context ) );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200121}
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
124 mbedtls_entropy_f_source_ptr f_source, void *p_source,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200125 size_t threshold, int strong )
Paul Bakker6083fd22011-12-03 21:45:14 +0000126{
Hanno Becker6ad82d72017-04-26 15:01:23 +0100127 int idx, ret = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#if defined(MBEDTLS_THREADING_C)
130 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100131 return( ret );
132#endif
133
Hanno Becker6ad82d72017-04-26 15:01:23 +0100134 idx = ctx->source_count;
135 if( idx >= MBEDTLS_ENTROPY_MAX_SOURCES )
Paul Bakker47703a02014-02-06 15:01:20 +0100136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
Paul Bakker47703a02014-02-06 15:01:20 +0100138 goto exit;
139 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000140
Hanno Becker6ad82d72017-04-26 15:01:23 +0100141 ctx->source[idx].f_source = f_source;
142 ctx->source[idx].p_source = p_source;
143 ctx->source[idx].threshold = threshold;
144 ctx->source[idx].strong = strong;
Paul Bakker6083fd22011-12-03 21:45:14 +0000145
146 ctx->source_count++;
147
Paul Bakker47703a02014-02-06 15:01:20 +0100148exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#if defined(MBEDTLS_THREADING_C)
150 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
151 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100152#endif
153
154 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000155}
156
157/*
158 * Entropy accumulator update
159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200161 const unsigned char *data, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000162{
163 unsigned char header[2];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000165 size_t use_len = len;
166 const unsigned char *p = data;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker6083fd22011-12-03 21:45:14 +0000169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
171 mbedtls_sha512( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200172#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_sha256( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200174#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000175 p = tmp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
Paul Bakker6083fd22011-12-03 21:45:14 +0000177 }
178
179 header[0] = source_id;
180 header[1] = use_len & 0xFF;
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
183 mbedtls_sha512_update( &ctx->accumulator, header, 2 );
184 mbedtls_sha512_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200185#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_sha256_update( &ctx->accumulator, header, 2 );
187 mbedtls_sha256_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200188#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200189
Andres Amaya Garciabab1edc2017-07-05 15:45:47 +0100190 mbedtls_zeroize( tmp, sizeof( tmp ) );
191
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 {
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100239 goto cleanup;
Paul Bakker6083fd22011-12-03 21:45:14 +0000240 }
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 )
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100253 ret = MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE;
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200254
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100255cleanup:
256 mbedtls_zeroize( buf, sizeof( buf ) );
257
258 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000259}
260
Paul Bakker47703a02014-02-06 15:01:20 +0100261/*
262 * Thread-safe wrapper for entropy_gather_internal()
263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
Paul Bakker47703a02014-02-06 15:01:20 +0100265{
Paul Bakkerddd427a2014-04-09 14:47:58 +0200266 int ret;
Paul Bakker47703a02014-02-06 15:01:20 +0100267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#if defined(MBEDTLS_THREADING_C)
269 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerddd427a2014-04-09 14:47:58 +0200270 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100271#endif
272
Paul Bakkerddd427a2014-04-09 14:47:58 +0200273 ret = entropy_gather_internal( ctx );
Paul Bakker47703a02014-02-06 15:01:20 +0100274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275#if defined(MBEDTLS_THREADING_C)
276 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
277 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100278#endif
279
Paul Bakkerddd427a2014-04-09 14:47:58 +0200280 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100281}
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000284{
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200285 int ret, count = 0, i, done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
287 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
290 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#if defined(MBEDTLS_THREADING_C)
293 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200294 return( ret );
295#endif
296
Paul Bakker6083fd22011-12-03 21:45:14 +0000297 /*
298 * Always gather extra entropy before a call
299 */
300 do
301 {
302 if( count++ > ENTROPY_MAX_LOOP )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200305 goto exit;
306 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000307
Paul Bakker47703a02014-02-06 15:01:20 +0100308 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200309 goto exit;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000310
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200311 done = 1;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000312 for( i = 0; i < ctx->source_count; i++ )
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200313 if( ctx->source[i].size < ctx->source[i].threshold )
314 done = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000315 }
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200316 while( ! done );
Paul Bakker6083fd22011-12-03 21:45:14 +0000317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakker6083fd22011-12-03 21:45:14 +0000319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
321 mbedtls_sha512_finish( &ctx->accumulator, buf );
Paul Bakker9e36f042013-06-30 14:34:05 +0200322
Paul Bakker6083fd22011-12-03 21:45:14 +0000323 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000324 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000325 */
Hanno Becker31b37f62018-01-17 23:09:20 +0000326 mbedtls_sha512_free( &ctx->accumulator );
327 mbedtls_sha512_init( &ctx->accumulator );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_sha512_starts( &ctx->accumulator, 0 );
329 mbedtls_sha512_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200330
331 /*
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100332 * Perform second SHA-512 on entropy
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
335#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
336 mbedtls_sha256_finish( &ctx->accumulator, buf );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200337
338 /*
339 * Reset accumulator and counters and recycle existing entropy
340 */
Hanno Becker31b37f62018-01-17 23:09:20 +0000341 mbedtls_sha256_free( &ctx->accumulator );
342 mbedtls_sha256_init( &ctx->accumulator );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_sha256_starts( &ctx->accumulator, 0 );
344 mbedtls_sha256_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100345
346 /*
347 * Perform second SHA-256 on entropy
348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_sha256( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
350#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000351
352 for( i = 0; i < ctx->source_count; i++ )
353 ctx->source[i].size = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000354
355 memcpy( output, buf, len );
356
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200357 ret = 0;
358
359exit:
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100360 mbedtls_zeroize( buf, sizeof( buf ) );
361
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370#if defined(MBEDTLS_FS_IO)
371int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100372{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100374 FILE *f;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100376
377 if( ( f = fopen( path, "wb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100381 goto exit;
382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100386 goto exit;
387 }
388
389 ret = 0;
390
391exit:
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100392 mbedtls_zeroize( buf, sizeof( buf ) );
393
Paul Bakker66ff70d2014-03-26 11:54:05 +0100394 fclose( f );
395 return( ret );
396}
397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100399{
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100400 int ret = 0;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100401 FILE *f;
402 size_t n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100404
405 if( ( f = fopen( path, "rb" ) ) == 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
408 fseek( f, 0, SEEK_END );
409 n = (size_t) ftell( f );
410 fseek( f, 0, SEEK_SET );
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
413 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100414
415 if( fread( buf, 1, n, f ) != n )
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100416 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
417 else
418 ret = mbedtls_entropy_update_manual( ctx, buf, n );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100419
420 fclose( f );
421
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100422 mbedtls_zeroize( buf, sizeof( buf ) );
423
424 if( ret != 0 )
425 return( ret );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 return( mbedtls_entropy_write_seed_file( ctx, path ) );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100428}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#endif /* MBEDTLS_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200432/*
433 * Dummy source function
434 */
435static int entropy_dummy_source( void *data, unsigned char *output,
436 size_t len, size_t *olen )
437{
438 ((void) data);
439
440 memset( output, 0x2a, len );
441 *olen = len;
442
443 return( 0 );
444}
445
446/*
447 * The actual entropy quality is hard to test, but we can at least
448 * test that the functions don't cause errors and write the correct
449 * amount of data to buffers.
450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451int mbedtls_entropy_self_test( int verbose )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200452{
453 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_entropy_context ctx;
455 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
456 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200457 size_t i, j;
458
459 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_printf( " ENTROPY test: " );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200463
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200464 /* First do a gather to make sure we have default sources */
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200465 if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200466 goto cleanup;
467
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200468 ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16,
469 MBEDTLS_ENTROPY_SOURCE_WEAK );
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200470 if( ret != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200471 goto cleanup;
472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200474 goto cleanup;
475
476 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 * To test that mbedtls_entropy_func writes correct number of bytes:
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200478 * - use the whole buffer and rely on ASan to detect overruns
479 * - collect entropy 8 times and OR the result in an accumulator:
480 * any byte should then be 0 with probably 2^(-64), so requiring
481 * each of the 32 or 64 bytes to be non-zero has a false failure rate
482 * of at most 2^(-58) which is acceptable.
483 */
484 for( i = 0; i < 8; i++ )
485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200487 goto cleanup;
488
489 for( j = 0; j < sizeof( buf ); j++ )
490 acc[j] |= buf[j];
491 }
492
493 for( j = 0; j < sizeof( buf ); j++ )
494 {
495 if( acc[j] == 0 )
496 {
497 ret = 1;
498 goto cleanup;
499 }
500 }
501
502cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200504
505 if( verbose != 0 )
506 {
507 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200509 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200513 }
514
515 return( ret != 0 );
516}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#endif /* MBEDTLS_ENTROPY_C */