blob: 4dddb7507fb2e1481c71433c9d5de8386c081d51 [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Entropy accumulator implementation
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, 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,
80 MBEDTLS_ENTROPY_MIN_PLATFORM );
Paul Bakker6083fd22011-12-03 21:45:14 +000081#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_TIMING_C)
83 mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL, MBEDTLS_ENTROPY_MIN_HARDCLOCK );
Paul Bakker6083fd22011-12-03 21:45:14 +000084#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_HAVEGE_C)
86 mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data,
87 MBEDTLS_ENTROPY_MIN_HAVEGE );
Paul Bakker28c7e7f2011-12-15 19:49:30 +000088#endif
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +020089#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
90 mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL
91 MBEDTLS_ENTROPY_MIN_HARDWARE );
92#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +000094}
95
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
Paul Bakker1ffefac2013-09-28 15:23:03 +020097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#if defined(MBEDTLS_HAVEGE_C)
99 mbedtls_havege_free( &ctx->havege_data );
Paul Bakkera317a982014-06-18 16:44:11 +0200100#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#if defined(MBEDTLS_THREADING_C)
102 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200103#endif
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +0100104 mbedtls_zeroize( ctx, sizeof( mbedtls_entropy_context ) );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200105}
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
108 mbedtls_entropy_f_source_ptr f_source, void *p_source,
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000109 size_t threshold )
Paul Bakker6083fd22011-12-03 21:45:14 +0000110{
Paul Bakker47703a02014-02-06 15:01:20 +0100111 int index, ret = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_THREADING_C)
114 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100115 return( ret );
116#endif
117
118 index = ctx->source_count;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 if( index >= MBEDTLS_ENTROPY_MAX_SOURCES )
Paul Bakker47703a02014-02-06 15:01:20 +0100120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
Paul Bakker47703a02014-02-06 15:01:20 +0100122 goto exit;
123 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000124
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000125 ctx->source[index].f_source = f_source;
126 ctx->source[index].p_source = p_source;
127 ctx->source[index].threshold = threshold;
Paul Bakker6083fd22011-12-03 21:45:14 +0000128
129 ctx->source_count++;
130
Paul Bakker47703a02014-02-06 15:01:20 +0100131exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#if defined(MBEDTLS_THREADING_C)
133 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
134 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100135#endif
136
137 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000138}
139
140/*
141 * Entropy accumulator update
142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200144 const unsigned char *data, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000145{
146 unsigned char header[2];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000148 size_t use_len = len;
149 const unsigned char *p = data;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker6083fd22011-12-03 21:45:14 +0000152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
154 mbedtls_sha512( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200155#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_sha256( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200157#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000158 p = tmp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
Paul Bakker6083fd22011-12-03 21:45:14 +0000160 }
161
162 header[0] = source_id;
163 header[1] = use_len & 0xFF;
164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
166 mbedtls_sha512_update( &ctx->accumulator, header, 2 );
167 mbedtls_sha512_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200168#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 mbedtls_sha256_update( &ctx->accumulator, header, 2 );
170 mbedtls_sha256_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200171#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200172
Paul Bakker6083fd22011-12-03 21:45:14 +0000173 return( 0 );
174}
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
Paul Bakker6083fd22011-12-03 21:45:14 +0000177 const unsigned char *data, size_t len )
178{
Paul Bakker47703a02014-02-06 15:01:20 +0100179 int ret;
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_THREADING_C)
182 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100183 return( ret );
184#endif
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len );
Paul Bakker47703a02014-02-06 15:01:20 +0100187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#if defined(MBEDTLS_THREADING_C)
189 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
190 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100191#endif
192
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200193 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000194}
195
196/*
197 * Run through the different sources to add entropy to our accumulator
198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199static int entropy_gather_internal( mbedtls_entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +0000200{
201 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
Paul Bakker6083fd22011-12-03 21:45:14 +0000203 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100204
Paul Bakker43655f42011-12-15 20:11:16 +0000205 if( ctx->source_count == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
Paul Bakker43655f42011-12-15 20:11:16 +0000207
Paul Bakker6083fd22011-12-03 21:45:14 +0000208 /*
209 * Run through our entropy sources
210 */
211 for( i = 0; i < ctx->source_count; i++ )
212 {
213 olen = 0;
Paul Bakker66d5d072014-06-17 16:39:18 +0200214 if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
Paul Bakker6083fd22011-12-03 21:45:14 +0000216 {
217 return( ret );
218 }
219
220 /*
221 * Add if we actually gathered something
222 */
223 if( olen > 0 )
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000224 {
Paul Bakker6083fd22011-12-03 21:45:14 +0000225 entropy_update( ctx, (unsigned char) i, buf, olen );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000226 ctx->source[i].size += olen;
227 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000228 }
229
230 return( 0 );
231}
232
Paul Bakker47703a02014-02-06 15:01:20 +0100233/*
234 * Thread-safe wrapper for entropy_gather_internal()
235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
Paul Bakker47703a02014-02-06 15:01:20 +0100237{
Paul Bakkerddd427a2014-04-09 14:47:58 +0200238 int ret;
Paul Bakker47703a02014-02-06 15:01:20 +0100239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_THREADING_C)
241 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerddd427a2014-04-09 14:47:58 +0200242 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100243#endif
244
Paul Bakkerddd427a2014-04-09 14:47:58 +0200245 ret = entropy_gather_internal( ctx );
Paul Bakker47703a02014-02-06 15:01:20 +0100246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_THREADING_C)
248 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
249 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100250#endif
251
Paul Bakkerddd427a2014-04-09 14:47:58 +0200252 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100253}
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000256{
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200257 int ret, count = 0, i, done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
259 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
262 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#if defined(MBEDTLS_THREADING_C)
265 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200266 return( ret );
267#endif
268
Paul Bakker6083fd22011-12-03 21:45:14 +0000269 /*
270 * Always gather extra entropy before a call
271 */
272 do
273 {
274 if( count++ > ENTROPY_MAX_LOOP )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200277 goto exit;
278 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000279
Paul Bakker47703a02014-02-06 15:01:20 +0100280 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200281 goto exit;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000282
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200283 done = 1;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000284 for( i = 0; i < ctx->source_count; i++ )
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200285 if( ctx->source[i].size < ctx->source[i].threshold )
286 done = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000287 }
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200288 while( ! done );
Paul Bakker6083fd22011-12-03 21:45:14 +0000289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakker6083fd22011-12-03 21:45:14 +0000291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
293 mbedtls_sha512_finish( &ctx->accumulator, buf );
Paul Bakker9e36f042013-06-30 14:34:05 +0200294
Paul Bakker6083fd22011-12-03 21:45:14 +0000295 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000296 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha512_context ) );
299 mbedtls_sha512_starts( &ctx->accumulator, 0 );
300 mbedtls_sha512_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200301
302 /*
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100303 * Perform second SHA-512 on entropy
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
306#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
307 mbedtls_sha256_finish( &ctx->accumulator, buf );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200308
309 /*
310 * Reset accumulator and counters and recycle existing entropy
311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha256_context ) );
313 mbedtls_sha256_starts( &ctx->accumulator, 0 );
314 mbedtls_sha256_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100315
316 /*
317 * Perform second SHA-256 on entropy
318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_sha256( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
320#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000321
322 for( i = 0; i < ctx->source_count; i++ )
323 ctx->source[i].size = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000324
325 memcpy( output, buf, len );
326
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200327 ret = 0;
328
329exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330#if defined(MBEDTLS_THREADING_C)
331 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
332 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200333#endif
334
335 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000336}
337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338#if defined(MBEDTLS_FS_IO)
339int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100340{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100342 FILE *f;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100344
345 if( ( f = fopen( path, "wb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100349 goto exit;
350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100354 goto exit;
355 }
356
357 ret = 0;
358
359exit:
360 fclose( f );
361 return( ret );
362}
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100365{
366 FILE *f;
367 size_t n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100369
370 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100372
373 fseek( f, 0, SEEK_END );
374 n = (size_t) ftell( f );
375 fseek( f, 0, SEEK_SET );
376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
378 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100379
380 if( fread( buf, 1, n, f ) != n )
381 {
382 fclose( f );
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
386 fclose( f );
387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_entropy_update_manual( ctx, buf, n );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 return( mbedtls_entropy_write_seed_file( ctx, path ) );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100391}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#endif /* MBEDTLS_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200395/*
396 * Dummy source function
397 */
398static int entropy_dummy_source( void *data, unsigned char *output,
399 size_t len, size_t *olen )
400{
401 ((void) data);
402
403 memset( output, 0x2a, len );
404 *olen = len;
405
406 return( 0 );
407}
408
409/*
410 * The actual entropy quality is hard to test, but we can at least
411 * test that the functions don't cause errors and write the correct
412 * amount of data to buffers.
413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414int mbedtls_entropy_self_test( int verbose )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200415{
416 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_entropy_context ctx;
418 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
419 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200420 size_t i, j;
421
422 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_printf( " ENTROPY test: " );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200426
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200427 /* First do a gather to mek sure we have default sources */
428 if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200429 goto cleanup;
430
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200431 ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16 );
432 if( ret != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200433 goto cleanup;
434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200436 goto cleanup;
437
438 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 * To test that mbedtls_entropy_func writes correct number of bytes:
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200440 * - use the whole buffer and rely on ASan to detect overruns
441 * - collect entropy 8 times and OR the result in an accumulator:
442 * any byte should then be 0 with probably 2^(-64), so requiring
443 * each of the 32 or 64 bytes to be non-zero has a false failure rate
444 * of at most 2^(-58) which is acceptable.
445 */
446 for( i = 0; i < 8; i++ )
447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200449 goto cleanup;
450
451 for( j = 0; j < sizeof( buf ); j++ )
452 acc[j] |= buf[j];
453 }
454
455 for( j = 0; j < sizeof( buf ); j++ )
456 {
457 if( acc[j] == 0 )
458 {
459 ret = 1;
460 goto cleanup;
461 }
462 }
463
464cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200466
467 if( verbose != 0 )
468 {
469 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200471 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200475 }
476
477 return( ret != 0 );
478}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481#endif /* MBEDTLS_ENTROPY_C */