blob: f68d68656b348a68b790250862ab74929ea8affe [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
Paul Bakker6083fd22011-12-03 21:45:14 +0000178 return( 0 );
179}
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
Paul Bakker6083fd22011-12-03 21:45:14 +0000182 const unsigned char *data, size_t len )
183{
Paul Bakker47703a02014-02-06 15:01:20 +0100184 int ret;
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#if defined(MBEDTLS_THREADING_C)
187 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100188 return( ret );
189#endif
190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len );
Paul Bakker47703a02014-02-06 15:01:20 +0100192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_THREADING_C)
194 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
195 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100196#endif
197
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200198 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000199}
200
201/*
202 * Run through the different sources to add entropy to our accumulator
203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204static int entropy_gather_internal( mbedtls_entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +0000205{
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200206 int ret, i, have_one_strong = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
Paul Bakker6083fd22011-12-03 21:45:14 +0000208 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100209
Paul Bakker43655f42011-12-15 20:11:16 +0000210 if( ctx->source_count == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
Paul Bakker43655f42011-12-15 20:11:16 +0000212
Paul Bakker6083fd22011-12-03 21:45:14 +0000213 /*
214 * Run through our entropy sources
215 */
216 for( i = 0; i < ctx->source_count; i++ )
217 {
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200218 if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
219 have_one_strong = 1;
220
Paul Bakker6083fd22011-12-03 21:45:14 +0000221 olen = 0;
Paul Bakker66d5d072014-06-17 16:39:18 +0200222 if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
Paul Bakker6083fd22011-12-03 21:45:14 +0000224 {
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100225 goto cleanup;
Paul Bakker6083fd22011-12-03 21:45:14 +0000226 }
227
228 /*
229 * Add if we actually gathered something
230 */
231 if( olen > 0 )
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000232 {
Paul Bakker6083fd22011-12-03 21:45:14 +0000233 entropy_update( ctx, (unsigned char) i, buf, olen );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000234 ctx->source[i].size += olen;
235 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000236 }
237
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200238 if( have_one_strong == 0 )
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100239 ret = MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE;
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200240
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100241cleanup:
242 mbedtls_zeroize( buf, sizeof( buf ) );
243
244 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000245}
246
Paul Bakker47703a02014-02-06 15:01:20 +0100247/*
248 * Thread-safe wrapper for entropy_gather_internal()
249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
Paul Bakker47703a02014-02-06 15:01:20 +0100251{
Paul Bakkerddd427a2014-04-09 14:47:58 +0200252 int ret;
Paul Bakker47703a02014-02-06 15:01:20 +0100253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#if defined(MBEDTLS_THREADING_C)
255 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerddd427a2014-04-09 14:47:58 +0200256 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100257#endif
258
Paul Bakkerddd427a2014-04-09 14:47:58 +0200259 ret = entropy_gather_internal( ctx );
Paul Bakker47703a02014-02-06 15:01:20 +0100260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#if defined(MBEDTLS_THREADING_C)
262 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
263 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100264#endif
265
Paul Bakkerddd427a2014-04-09 14:47:58 +0200266 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100267}
268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000270{
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200271 int ret, count = 0, i, done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
273 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
276 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278#if defined(MBEDTLS_THREADING_C)
279 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200280 return( ret );
281#endif
282
Paul Bakker6083fd22011-12-03 21:45:14 +0000283 /*
284 * Always gather extra entropy before a call
285 */
286 do
287 {
288 if( count++ > ENTROPY_MAX_LOOP )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200291 goto exit;
292 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000293
Paul Bakker47703a02014-02-06 15:01:20 +0100294 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200295 goto exit;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000296
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200297 done = 1;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000298 for( i = 0; i < ctx->source_count; i++ )
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200299 if( ctx->source[i].size < ctx->source[i].threshold )
300 done = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000301 }
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200302 while( ! done );
Paul Bakker6083fd22011-12-03 21:45:14 +0000303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakker6083fd22011-12-03 21:45:14 +0000305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
307 mbedtls_sha512_finish( &ctx->accumulator, buf );
Paul Bakker9e36f042013-06-30 14:34:05 +0200308
Paul Bakker6083fd22011-12-03 21:45:14 +0000309 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000310 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha512_context ) );
313 mbedtls_sha512_starts( &ctx->accumulator, 0 );
314 mbedtls_sha512_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200315
316 /*
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100317 * Perform second SHA-512 on entropy
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
320#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
321 mbedtls_sha256_finish( &ctx->accumulator, buf );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200322
323 /*
324 * Reset accumulator and counters and recycle existing entropy
325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha256_context ) );
327 mbedtls_sha256_starts( &ctx->accumulator, 0 );
328 mbedtls_sha256_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100329
330 /*
331 * Perform second SHA-256 on entropy
332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 mbedtls_sha256( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
334#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000335
336 for( i = 0; i < ctx->source_count; i++ )
337 ctx->source[i].size = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000338
339 memcpy( output, buf, len );
340
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200341 ret = 0;
342
343exit:
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100344 mbedtls_zeroize( buf, sizeof( buf ) );
345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346#if defined(MBEDTLS_THREADING_C)
347 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
348 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200349#endif
350
351 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000352}
353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#if defined(MBEDTLS_FS_IO)
355int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100356{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100358 FILE *f;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100360
361 if( ( f = fopen( path, "wb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100365 goto exit;
366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100370 goto exit;
371 }
372
373 ret = 0;
374
375exit:
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100376 mbedtls_zeroize( buf, sizeof( buf ) );
377
Paul Bakker66ff70d2014-03-26 11:54:05 +0100378 fclose( f );
379 return( ret );
380}
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100383{
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100384 int ret = 0;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100385 FILE *f;
386 size_t n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100388
389 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100391
392 fseek( f, 0, SEEK_END );
393 n = (size_t) ftell( f );
394 fseek( f, 0, SEEK_SET );
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
397 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100398
399 if( fread( buf, 1, n, f ) != n )
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100400 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
401 else
402 ret = mbedtls_entropy_update_manual( ctx, buf, n );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100403
404 fclose( f );
405
Andres Amaya Garcia81284ad2017-06-26 09:58:59 +0100406 mbedtls_zeroize( buf, sizeof( buf ) );
407
408 if( ret != 0 )
409 return( ret );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 return( mbedtls_entropy_write_seed_file( ctx, path ) );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100412}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#endif /* MBEDTLS_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200416/*
417 * Dummy source function
418 */
419static int entropy_dummy_source( void *data, unsigned char *output,
420 size_t len, size_t *olen )
421{
422 ((void) data);
423
424 memset( output, 0x2a, len );
425 *olen = len;
426
427 return( 0 );
428}
429
430/*
431 * The actual entropy quality is hard to test, but we can at least
432 * test that the functions don't cause errors and write the correct
433 * amount of data to buffers.
434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435int mbedtls_entropy_self_test( int verbose )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200436{
437 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_entropy_context ctx;
439 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
440 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200441 size_t i, j;
442
443 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_printf( " ENTROPY test: " );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200447
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200448 /* First do a gather to make sure we have default sources */
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200449 if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200450 goto cleanup;
451
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200452 ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16,
453 MBEDTLS_ENTROPY_SOURCE_WEAK );
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200454 if( ret != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200455 goto cleanup;
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200458 goto cleanup;
459
460 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 * To test that mbedtls_entropy_func writes correct number of bytes:
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200462 * - use the whole buffer and rely on ASan to detect overruns
463 * - collect entropy 8 times and OR the result in an accumulator:
464 * any byte should then be 0 with probably 2^(-64), so requiring
465 * each of the 32 or 64 bytes to be non-zero has a false failure rate
466 * of at most 2^(-58) which is acceptable.
467 */
468 for( i = 0; i < 8; i++ )
469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200471 goto cleanup;
472
473 for( j = 0; j < sizeof( buf ); j++ )
474 acc[j] |= buf[j];
475 }
476
477 for( j = 0; j < sizeof( buf ); j++ )
478 {
479 if( acc[j] == 0 )
480 {
481 ret = 1;
482 goto cleanup;
483 }
484 }
485
486cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200488
489 if( verbose != 0 )
490 {
491 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200493 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200497 }
498
499 return( ret != 0 );
500}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#endif /* MBEDTLS_ENTROPY_C */