blob: dd5693775d8546a13524bf5285005c7ac0b55a52 [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
Paul Bakker6083fd22011-12-03 21:45:14 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker6083fd22011-12-03 21:45:14 +00007 *
Paul Bakker6083fd22011-12-03 21:45:14 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/entropy.h"
32#include "mbedtls/entropy_poll.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <string.h>
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_FS_IO)
Paul Bakker66ff70d2014-03-26 11:54:05 +010037#include <stdio.h>
38#endif
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_SELF_TEST)
41#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/platform.h"
Rich Evans00ab4702015-02-06 13:43:58 +000043#else
44#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#define mbedtls_printf printf
46#endif /* MBEDTLS_PLATFORM_C */
47#endif /* MBEDTLS_SELF_TEST */
Rich Evans00ab4702015-02-06 13:43:58 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/havege.h"
Paul Bakker28c7e7f2011-12-15 19:49:30 +000051#endif
52
Paul Bakker34617722014-06-13 17:20:13 +020053/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020055 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
56}
57
Paul Bakker6083fd22011-12-03 21:45:14 +000058#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060void mbedtls_entropy_init( mbedtls_entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +000061{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 memset( ctx, 0, sizeof(mbedtls_entropy_context) );
Paul Bakker6083fd22011-12-03 21:45:14 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_THREADING_C)
65 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020066#endif
67
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
69 mbedtls_sha512_starts( &ctx->accumulator, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +020070#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 mbedtls_sha256_starts( &ctx->accumulator, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +020072#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_HAVEGE_C)
74 mbedtls_havege_init( &ctx->havege_data );
Paul Bakker43655f42011-12-15 20:11:16 +000075#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
78#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
79 mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020080 MBEDTLS_ENTROPY_MIN_PLATFORM,
81 MBEDTLS_ENTROPY_SOURCE_STRONG );
Paul Bakker6083fd22011-12-03 21:45:14 +000082#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020084 mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL,
85 MBEDTLS_ENTROPY_MIN_HARDCLOCK,
86 MBEDTLS_ENTROPY_SOURCE_WEAK );
Paul Bakker6083fd22011-12-03 21:45:14 +000087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#if defined(MBEDTLS_HAVEGE_C)
89 mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020090 MBEDTLS_ENTROPY_MIN_HAVEGE,
91 MBEDTLS_ENTROPY_SOURCE_STRONG );
Paul Bakker28c7e7f2011-12-15 19:49:30 +000092#endif
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +020093#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Manuel Pégourié-Gonnardfc2ccfe2015-07-10 11:15:50 +010094 mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020095 MBEDTLS_ENTROPY_MIN_HARDWARE,
96 MBEDTLS_ENTROPY_SOURCE_STRONG );
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +020097#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +000099}
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
Paul Bakker1ffefac2013-09-28 15:23:03 +0200102{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#if defined(MBEDTLS_HAVEGE_C)
104 mbedtls_havege_free( &ctx->havege_data );
Paul Bakkera317a982014-06-18 16:44:11 +0200105#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_THREADING_C)
107 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200108#endif
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +0100109 mbedtls_zeroize( ctx, sizeof( mbedtls_entropy_context ) );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200110}
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
113 mbedtls_entropy_f_source_ptr f_source, void *p_source,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200114 size_t threshold, int strong )
Paul Bakker6083fd22011-12-03 21:45:14 +0000115{
Paul Bakker47703a02014-02-06 15:01:20 +0100116 int index, ret = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_THREADING_C)
119 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100120 return( ret );
121#endif
122
123 index = ctx->source_count;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 if( index >= MBEDTLS_ENTROPY_MAX_SOURCES )
Paul Bakker47703a02014-02-06 15:01:20 +0100125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
Paul Bakker47703a02014-02-06 15:01:20 +0100127 goto exit;
128 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000129
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200130 ctx->source[index].f_source = f_source;
131 ctx->source[index].p_source = p_source;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000132 ctx->source[index].threshold = threshold;
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200133 ctx->source[index].strong = strong;
Paul Bakker6083fd22011-12-03 21:45:14 +0000134
135 ctx->source_count++;
136
Paul Bakker47703a02014-02-06 15:01:20 +0100137exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_THREADING_C)
139 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
140 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100141#endif
142
143 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000144}
145
146/*
147 * Entropy accumulator update
148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200150 const unsigned char *data, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000151{
152 unsigned char header[2];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000154 size_t use_len = len;
155 const unsigned char *p = data;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker6083fd22011-12-03 21:45:14 +0000158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
160 mbedtls_sha512( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200161#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_sha256( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200163#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000164 p = tmp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
Paul Bakker6083fd22011-12-03 21:45:14 +0000166 }
167
168 header[0] = source_id;
169 header[1] = use_len & 0xFF;
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
172 mbedtls_sha512_update( &ctx->accumulator, header, 2 );
173 mbedtls_sha512_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200174#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_sha256_update( &ctx->accumulator, header, 2 );
176 mbedtls_sha256_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200177#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200178
Paul Bakker6083fd22011-12-03 21:45:14 +0000179 return( 0 );
180}
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
Paul Bakker6083fd22011-12-03 21:45:14 +0000183 const unsigned char *data, size_t len )
184{
Paul Bakker47703a02014-02-06 15:01:20 +0100185 int ret;
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_THREADING_C)
188 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100189 return( ret );
190#endif
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len );
Paul Bakker47703a02014-02-06 15:01:20 +0100193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_THREADING_C)
195 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
196 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100197#endif
198
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200199 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000200}
201
202/*
203 * Run through the different sources to add entropy to our accumulator
204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205static int entropy_gather_internal( mbedtls_entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +0000206{
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200207 int ret, i, have_one_strong = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
Paul Bakker6083fd22011-12-03 21:45:14 +0000209 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100210
Paul Bakker43655f42011-12-15 20:11:16 +0000211 if( ctx->source_count == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
Paul Bakker43655f42011-12-15 20:11:16 +0000213
Paul Bakker6083fd22011-12-03 21:45:14 +0000214 /*
215 * Run through our entropy sources
216 */
217 for( i = 0; i < ctx->source_count; i++ )
218 {
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200219 if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
220 have_one_strong = 1;
221
Paul Bakker6083fd22011-12-03 21:45:14 +0000222 olen = 0;
Paul Bakker66d5d072014-06-17 16:39:18 +0200223 if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
Paul Bakker6083fd22011-12-03 21:45:14 +0000225 {
226 return( ret );
227 }
228
229 /*
230 * Add if we actually gathered something
231 */
232 if( olen > 0 )
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000233 {
Paul Bakker6083fd22011-12-03 21:45:14 +0000234 entropy_update( ctx, (unsigned char) i, buf, olen );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000235 ctx->source[i].size += olen;
236 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000237 }
238
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200239 if( have_one_strong == 0 )
240 return( MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE );
241
Paul Bakker6083fd22011-12-03 21:45:14 +0000242 return( 0 );
243}
244
Paul Bakker47703a02014-02-06 15:01:20 +0100245/*
246 * Thread-safe wrapper for entropy_gather_internal()
247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
Paul Bakker47703a02014-02-06 15:01:20 +0100249{
Paul Bakkerddd427a2014-04-09 14:47:58 +0200250 int ret;
Paul Bakker47703a02014-02-06 15:01:20 +0100251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#if defined(MBEDTLS_THREADING_C)
253 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerddd427a2014-04-09 14:47:58 +0200254 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100255#endif
256
Paul Bakkerddd427a2014-04-09 14:47:58 +0200257 ret = entropy_gather_internal( ctx );
Paul Bakker47703a02014-02-06 15:01:20 +0100258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259#if defined(MBEDTLS_THREADING_C)
260 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
261 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100262#endif
263
Paul Bakkerddd427a2014-04-09 14:47:58 +0200264 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100265}
266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000268{
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200269 int ret, count = 0, i, done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
271 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
274 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#if defined(MBEDTLS_THREADING_C)
277 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200278 return( ret );
279#endif
280
Paul Bakker6083fd22011-12-03 21:45:14 +0000281 /*
282 * Always gather extra entropy before a call
283 */
284 do
285 {
286 if( count++ > ENTROPY_MAX_LOOP )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200289 goto exit;
290 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000291
Paul Bakker47703a02014-02-06 15:01:20 +0100292 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200293 goto exit;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000294
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200295 done = 1;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000296 for( i = 0; i < ctx->source_count; i++ )
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200297 if( ctx->source[i].size < ctx->source[i].threshold )
298 done = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000299 }
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200300 while( ! done );
Paul Bakker6083fd22011-12-03 21:45:14 +0000301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakker6083fd22011-12-03 21:45:14 +0000303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
305 mbedtls_sha512_finish( &ctx->accumulator, buf );
Paul Bakker9e36f042013-06-30 14:34:05 +0200306
Paul Bakker6083fd22011-12-03 21:45:14 +0000307 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000308 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha512_context ) );
311 mbedtls_sha512_starts( &ctx->accumulator, 0 );
312 mbedtls_sha512_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200313
314 /*
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100315 * Perform second SHA-512 on entropy
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
318#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
319 mbedtls_sha256_finish( &ctx->accumulator, buf );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200320
321 /*
322 * Reset accumulator and counters and recycle existing entropy
323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha256_context ) );
325 mbedtls_sha256_starts( &ctx->accumulator, 0 );
326 mbedtls_sha256_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100327
328 /*
329 * Perform second SHA-256 on entropy
330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_sha256( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
332#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000333
334 for( i = 0; i < ctx->source_count; i++ )
335 ctx->source[i].size = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000336
337 memcpy( output, buf, len );
338
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200339 ret = 0;
340
341exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#if defined(MBEDTLS_THREADING_C)
343 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
344 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200345#endif
346
347 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000348}
349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350#if defined(MBEDTLS_FS_IO)
351int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100352{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100354 FILE *f;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100356
357 if( ( f = fopen( path, "wb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100361 goto exit;
362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100366 goto exit;
367 }
368
369 ret = 0;
370
371exit:
372 fclose( f );
373 return( ret );
374}
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100377{
378 FILE *f;
379 size_t n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100381
382 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100384
385 fseek( f, 0, SEEK_END );
386 n = (size_t) ftell( f );
387 fseek( f, 0, SEEK_SET );
388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
390 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100391
392 if( fread( buf, 1, n, f ) != n )
393 {
394 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100396 }
397
398 fclose( f );
399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_entropy_update_manual( ctx, buf, n );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 return( mbedtls_entropy_write_seed_file( ctx, path ) );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100403}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404#endif /* MBEDTLS_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200407/*
408 * Dummy source function
409 */
410static int entropy_dummy_source( void *data, unsigned char *output,
411 size_t len, size_t *olen )
412{
413 ((void) data);
414
415 memset( output, 0x2a, len );
416 *olen = len;
417
418 return( 0 );
419}
420
421/*
422 * The actual entropy quality is hard to test, but we can at least
423 * test that the functions don't cause errors and write the correct
424 * amount of data to buffers.
425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426int mbedtls_entropy_self_test( int verbose )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200427{
428 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_entropy_context ctx;
430 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
431 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200432 size_t i, j;
433
434 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_printf( " ENTROPY test: " );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200438
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200439 /* First do a gather to make sure we have default sources */
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200440 if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200441 goto cleanup;
442
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200443 ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16,
444 MBEDTLS_ENTROPY_SOURCE_WEAK );
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200445 if( ret != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200446 goto cleanup;
447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200449 goto cleanup;
450
451 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 * To test that mbedtls_entropy_func writes correct number of bytes:
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200453 * - use the whole buffer and rely on ASan to detect overruns
454 * - collect entropy 8 times and OR the result in an accumulator:
455 * any byte should then be 0 with probably 2^(-64), so requiring
456 * each of the 32 or 64 bytes to be non-zero has a false failure rate
457 * of at most 2^(-58) which is acceptable.
458 */
459 for( i = 0; i < 8; i++ )
460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200462 goto cleanup;
463
464 for( j = 0; j < sizeof( buf ); j++ )
465 acc[j] |= buf[j];
466 }
467
468 for( j = 0; j < sizeof( buf ); j++ )
469 {
470 if( acc[j] == 0 )
471 {
472 ret = 1;
473 goto cleanup;
474 }
475 }
476
477cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200479
480 if( verbose != 0 )
481 {
482 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200484 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200488 }
489
490 return( ret != 0 );
491}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#endif /* MBEDTLS_ENTROPY_C */