blob: 1a7b9ab257e681c098dc84d76476e50a4a490776 [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Entropy accumulator implementation
3 *
Paul Bakker9988d6b2016-06-01 11:29:42 +01004 * Copyright (C) 2006-2016, 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
Janos Follath53de7842016-06-08 15:29:18 +010076#if defined(MBEDTLS_TEST_WO_ENTROPY)
77 mbedtls_entropy_add_source( ctx, mbedtls_zero_entropy_poll, NULL,
78 1, MBEDTLS_ENTROPY_SOURCE_STRONG );
79#endif
80
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
Paul Bakker9988d6b2016-06-01 11:29:42 +0100102#if defined(MBEDTLS_ENTROPY_NV_SEED)
103 mbedtls_entropy_add_source( ctx, mbedtls_nv_seed_poll, NULL,
104 MBEDTLS_ENTROPY_BLOCK_SIZE,
105 MBEDTLS_ENTROPY_SOURCE_STRONG );
106#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +0000108}
109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
Paul Bakker1ffefac2013-09-28 15:23:03 +0200111{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112#if defined(MBEDTLS_HAVEGE_C)
113 mbedtls_havege_free( &ctx->havege_data );
Paul Bakkera317a982014-06-18 16:44:11 +0200114#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#if defined(MBEDTLS_THREADING_C)
116 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200117#endif
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +0100118 mbedtls_zeroize( ctx, sizeof( mbedtls_entropy_context ) );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200119}
120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
122 mbedtls_entropy_f_source_ptr f_source, void *p_source,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200123 size_t threshold, int strong )
Paul Bakker6083fd22011-12-03 21:45:14 +0000124{
Paul Bakker47703a02014-02-06 15:01:20 +0100125 int index, ret = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#if defined(MBEDTLS_THREADING_C)
128 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100129 return( ret );
130#endif
131
132 index = ctx->source_count;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 if( index >= MBEDTLS_ENTROPY_MAX_SOURCES )
Paul Bakker47703a02014-02-06 15:01:20 +0100134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
Paul Bakker47703a02014-02-06 15:01:20 +0100136 goto exit;
137 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000138
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200139 ctx->source[index].f_source = f_source;
140 ctx->source[index].p_source = p_source;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000141 ctx->source[index].threshold = threshold;
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200142 ctx->source[index].strong = strong;
Paul Bakker6083fd22011-12-03 21:45:14 +0000143
144 ctx->source_count++;
145
Paul Bakker47703a02014-02-06 15:01:20 +0100146exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#if defined(MBEDTLS_THREADING_C)
148 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
149 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100150#endif
151
152 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000153}
154
155/*
156 * Entropy accumulator update
157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200159 const unsigned char *data, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000160{
161 unsigned char header[2];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000163 size_t use_len = len;
164 const unsigned char *p = data;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker6083fd22011-12-03 21:45:14 +0000167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
169 mbedtls_sha512( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200170#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_sha256( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200172#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000173 p = tmp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
Paul Bakker6083fd22011-12-03 21:45:14 +0000175 }
176
177 header[0] = source_id;
178 header[1] = use_len & 0xFF;
179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
181 mbedtls_sha512_update( &ctx->accumulator, header, 2 );
182 mbedtls_sha512_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200183#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_sha256_update( &ctx->accumulator, header, 2 );
185 mbedtls_sha256_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200186#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200187
Paul Bakker6083fd22011-12-03 21:45:14 +0000188 return( 0 );
189}
190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
Paul Bakker6083fd22011-12-03 21:45:14 +0000192 const unsigned char *data, size_t len )
193{
Paul Bakker47703a02014-02-06 15:01:20 +0100194 int ret;
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#if defined(MBEDTLS_THREADING_C)
197 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100198 return( ret );
199#endif
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len );
Paul Bakker47703a02014-02-06 15:01:20 +0100202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_THREADING_C)
204 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
205 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100206#endif
207
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200208 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000209}
210
211/*
212 * Run through the different sources to add entropy to our accumulator
213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214static int entropy_gather_internal( mbedtls_entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +0000215{
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200216 int ret, i, have_one_strong = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
Paul Bakker6083fd22011-12-03 21:45:14 +0000218 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100219
Paul Bakker43655f42011-12-15 20:11:16 +0000220 if( ctx->source_count == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
Paul Bakker43655f42011-12-15 20:11:16 +0000222
Paul Bakker6083fd22011-12-03 21:45:14 +0000223 /*
224 * Run through our entropy sources
225 */
226 for( i = 0; i < ctx->source_count; i++ )
227 {
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200228 if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
229 have_one_strong = 1;
230
Paul Bakker6083fd22011-12-03 21:45:14 +0000231 olen = 0;
Paul Bakker66d5d072014-06-17 16:39:18 +0200232 if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
Paul Bakker6083fd22011-12-03 21:45:14 +0000234 {
235 return( ret );
236 }
237
238 /*
239 * Add if we actually gathered something
240 */
241 if( olen > 0 )
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000242 {
Paul Bakker6083fd22011-12-03 21:45:14 +0000243 entropy_update( ctx, (unsigned char) i, buf, olen );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000244 ctx->source[i].size += olen;
245 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000246 }
247
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200248 if( have_one_strong == 0 )
249 return( MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE );
250
Paul Bakker6083fd22011-12-03 21:45:14 +0000251 return( 0 );
252}
253
Paul Bakker47703a02014-02-06 15:01:20 +0100254/*
255 * Thread-safe wrapper for entropy_gather_internal()
256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
Paul Bakker47703a02014-02-06 15:01:20 +0100258{
Paul Bakkerddd427a2014-04-09 14:47:58 +0200259 int ret;
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( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerddd427a2014-04-09 14:47:58 +0200263 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100264#endif
265
Paul Bakkerddd427a2014-04-09 14:47:58 +0200266 ret = entropy_gather_internal( ctx );
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( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
270 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100271#endif
272
Paul Bakkerddd427a2014-04-09 14:47:58 +0200273 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100274}
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000277{
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200278 int ret, count = 0, i, done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
280 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
283 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000284
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100285#if defined(MBEDTLS_ENTROPY_NV_SEED)
286 /* Update the NV entropy seed before generating any entropy for outside
287 * use.
288 */
Paul Bakkerfc9c7c82016-06-01 15:25:50 +0100289 if( ctx->initial_entropy_run == 0 )
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100290 {
Paul Bakkerfc9c7c82016-06-01 15:25:50 +0100291 ctx->initial_entropy_run = 1;
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100292 if( ( ret = mbedtls_entropy_update_nv_seed( ctx ) ) != 0 )
293 return( ret );
294 }
295#endif
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#if defined(MBEDTLS_THREADING_C)
298 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200299 return( ret );
300#endif
301
Paul Bakker6083fd22011-12-03 21:45:14 +0000302 /*
303 * Always gather extra entropy before a call
304 */
305 do
306 {
307 if( count++ > ENTROPY_MAX_LOOP )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200310 goto exit;
311 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000312
Paul Bakker47703a02014-02-06 15:01:20 +0100313 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200314 goto exit;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000315
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200316 done = 1;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000317 for( i = 0; i < ctx->source_count; i++ )
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200318 if( ctx->source[i].size < ctx->source[i].threshold )
319 done = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000320 }
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200321 while( ! done );
Paul Bakker6083fd22011-12-03 21:45:14 +0000322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakker6083fd22011-12-03 21:45:14 +0000324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
326 mbedtls_sha512_finish( &ctx->accumulator, buf );
Paul Bakker9e36f042013-06-30 14:34:05 +0200327
Paul Bakker6083fd22011-12-03 21:45:14 +0000328 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000329 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha512_context ) );
332 mbedtls_sha512_starts( &ctx->accumulator, 0 );
333 mbedtls_sha512_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200334
335 /*
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100336 * Perform second SHA-512 on entropy
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
339#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
340 mbedtls_sha256_finish( &ctx->accumulator, buf );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200341
342 /*
343 * Reset accumulator and counters and recycle existing entropy
344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha256_context ) );
346 mbedtls_sha256_starts( &ctx->accumulator, 0 );
347 mbedtls_sha256_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100348
349 /*
350 * Perform second SHA-256 on entropy
351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_sha256( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
353#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000354
355 for( i = 0; i < ctx->source_count; i++ )
356 ctx->source[i].size = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000357
358 memcpy( output, buf, len );
359
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200360 ret = 0;
361
362exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#if defined(MBEDTLS_THREADING_C)
364 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
365 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200366#endif
367
368 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000369}
370
Paul Bakkerd5c9f6d2016-06-01 11:30:54 +0100371#if defined(MBEDTLS_ENTROPY_NV_SEED)
372int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx )
373{
374 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
375 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
376
377 /* Read new seed and write it to NV */
378 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
379 return( ret );
380
381 if( mbedtls_nv_seed_write( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
382 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
383
384 /* Manually update the remaining stream with a separator value to diverge */
385 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
386 mbedtls_entropy_update_manual( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
387
388 return( 0 );
389}
390#endif /* MBEDTLS_ENTROPY_NV_SEED */
391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#if defined(MBEDTLS_FS_IO)
393int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100394{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100396 FILE *f;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100398
399 if( ( f = fopen( path, "wb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100403 goto exit;
404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100408 goto exit;
409 }
410
411 ret = 0;
412
413exit:
414 fclose( f );
415 return( ret );
416}
417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100419{
420 FILE *f;
421 size_t n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100423
424 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100426
427 fseek( f, 0, SEEK_END );
428 n = (size_t) ftell( f );
429 fseek( f, 0, SEEK_SET );
430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
432 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100433
434 if( fread( buf, 1, n, f ) != n )
435 {
436 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100438 }
439
440 fclose( f );
441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_entropy_update_manual( ctx, buf, n );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 return( mbedtls_entropy_write_seed_file( ctx, path ) );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100445}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#endif /* MBEDTLS_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200449/*
450 * Dummy source function
451 */
452static int entropy_dummy_source( void *data, unsigned char *output,
453 size_t len, size_t *olen )
454{
455 ((void) data);
456
457 memset( output, 0x2a, len );
458 *olen = len;
459
460 return( 0 );
461}
462
463/*
464 * The actual entropy quality is hard to test, but we can at least
465 * test that the functions don't cause errors and write the correct
466 * amount of data to buffers.
467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468int mbedtls_entropy_self_test( int verbose )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200469{
470 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_entropy_context ctx;
472 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
473 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200474 size_t i, j;
475
476 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_printf( " ENTROPY test: " );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200480
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200481 /* First do a gather to make sure we have default sources */
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200482 if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200483 goto cleanup;
484
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200485 ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16,
486 MBEDTLS_ENTROPY_SOURCE_WEAK );
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200487 if( ret != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200488 goto cleanup;
489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200491 goto cleanup;
492
493 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 * To test that mbedtls_entropy_func writes correct number of bytes:
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200495 * - use the whole buffer and rely on ASan to detect overruns
496 * - collect entropy 8 times and OR the result in an accumulator:
497 * any byte should then be 0 with probably 2^(-64), so requiring
498 * each of the 32 or 64 bytes to be non-zero has a false failure rate
499 * of at most 2^(-58) which is acceptable.
500 */
501 for( i = 0; i < 8; i++ )
502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200504 goto cleanup;
505
506 for( j = 0; j < sizeof( buf ); j++ )
507 acc[j] |= buf[j];
508 }
509
510 for( j = 0; j < sizeof( buf ); j++ )
511 {
512 if( acc[j] == 0 )
513 {
514 ret = 1;
515 goto cleanup;
516 }
517 }
518
519cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200521
522 if( verbose != 0 )
523 {
524 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200526 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200530 }
531
532 return( ret != 0 );
533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#endif /* MBEDTLS_ENTROPY_C */