blob: b45384dbe46476d967fbb4c76ff6939991ae2612 [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Entropy accumulator implementation
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker6083fd22011-12-03 21:45:14 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker6083fd22011-12-03 21:45:14 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/entropy.h"
31#include "mbedtls/entropy_poll.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <string.h>
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_FS_IO)
Paul Bakker66ff70d2014-03-26 11:54:05 +010036#include <stdio.h>
37#endif
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_SELF_TEST)
40#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/platform.h"
Rich Evans00ab4702015-02-06 13:43:58 +000042#else
43#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#define mbedtls_printf printf
45#endif /* MBEDTLS_PLATFORM_C */
46#endif /* MBEDTLS_SELF_TEST */
Rich Evans00ab4702015-02-06 13:43:58 +000047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/havege.h"
Paul Bakker28c7e7f2011-12-15 19:49:30 +000050#endif
51
Paul Bakker34617722014-06-13 17:20:13 +020052/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020054 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
55}
56
Paul Bakker6083fd22011-12-03 21:45:14 +000057#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059void mbedtls_entropy_init( mbedtls_entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +000060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 memset( ctx, 0, sizeof(mbedtls_entropy_context) );
Paul Bakker6083fd22011-12-03 21:45:14 +000062
Hanno Becker66580d22017-09-08 10:06:41 +010063 /* Reminder: Update ENTROPY_HAVE_STRONG in the test files
64 * when adding more strong entropy sources here. */
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_THREADING_C)
67 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020068#endif
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
71 mbedtls_sha512_starts( &ctx->accumulator, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +020072#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_sha256_starts( &ctx->accumulator, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +020074#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_HAVEGE_C)
76 mbedtls_havege_init( &ctx->havege_data );
Paul Bakker43655f42011-12-15 20:11:16 +000077#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
80#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
81 mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020082 MBEDTLS_ENTROPY_MIN_PLATFORM,
83 MBEDTLS_ENTROPY_SOURCE_STRONG );
Paul Bakker6083fd22011-12-03 21:45:14 +000084#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020086 mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL,
87 MBEDTLS_ENTROPY_MIN_HARDCLOCK,
88 MBEDTLS_ENTROPY_SOURCE_WEAK );
Paul Bakker6083fd22011-12-03 21:45:14 +000089#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#if defined(MBEDTLS_HAVEGE_C)
91 mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020092 MBEDTLS_ENTROPY_MIN_HAVEGE,
93 MBEDTLS_ENTROPY_SOURCE_STRONG );
Paul Bakker28c7e7f2011-12-15 19:49:30 +000094#endif
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +020095#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Manuel Pégourié-Gonnardfc2ccfe2015-07-10 11:15:50 +010096 mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020097 MBEDTLS_ENTROPY_MIN_HARDWARE,
98 MBEDTLS_ENTROPY_SOURCE_STRONG );
Manuel Pégourié-Gonnard3f77dfb2015-06-19 10:06:21 +020099#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +0000101}
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
Paul Bakker1ffefac2013-09-28 15:23:03 +0200104{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#if defined(MBEDTLS_HAVEGE_C)
106 mbedtls_havege_free( &ctx->havege_data );
Paul Bakkera317a982014-06-18 16:44:11 +0200107#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#if defined(MBEDTLS_THREADING_C)
109 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200110#endif
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +0100111 mbedtls_zeroize( ctx, sizeof( mbedtls_entropy_context ) );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200112}
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
115 mbedtls_entropy_f_source_ptr f_source, void *p_source,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200116 size_t threshold, int strong )
Paul Bakker6083fd22011-12-03 21:45:14 +0000117{
Hanno Becker6ad82d72017-04-26 15:01:23 +0100118 int idx, ret = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#if defined(MBEDTLS_THREADING_C)
121 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100122 return( ret );
123#endif
124
Hanno Becker6ad82d72017-04-26 15:01:23 +0100125 idx = ctx->source_count;
126 if( idx >= MBEDTLS_ENTROPY_MAX_SOURCES )
Paul Bakker47703a02014-02-06 15:01:20 +0100127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
Paul Bakker47703a02014-02-06 15:01:20 +0100129 goto exit;
130 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000131
Hanno Becker6ad82d72017-04-26 15:01:23 +0100132 ctx->source[idx].f_source = f_source;
133 ctx->source[idx].p_source = p_source;
134 ctx->source[idx].threshold = threshold;
135 ctx->source[idx].strong = strong;
Paul Bakker6083fd22011-12-03 21:45:14 +0000136
137 ctx->source_count++;
138
Paul Bakker47703a02014-02-06 15:01:20 +0100139exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#if defined(MBEDTLS_THREADING_C)
141 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
142 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100143#endif
144
145 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000146}
147
148/*
149 * Entropy accumulator update
150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200152 const unsigned char *data, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000153{
154 unsigned char header[2];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker6083fd22011-12-03 21:45:14 +0000156 size_t use_len = len;
157 const unsigned char *p = data;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker6083fd22011-12-03 21:45:14 +0000160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
162 mbedtls_sha512( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200163#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_sha256( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200165#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000166 p = tmp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
Paul Bakker6083fd22011-12-03 21:45:14 +0000168 }
169
170 header[0] = source_id;
171 header[1] = use_len & 0xFF;
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
174 mbedtls_sha512_update( &ctx->accumulator, header, 2 );
175 mbedtls_sha512_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200176#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_sha256_update( &ctx->accumulator, header, 2 );
178 mbedtls_sha256_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200179#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200180
Paul Bakker6083fd22011-12-03 21:45:14 +0000181 return( 0 );
182}
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
Paul Bakker6083fd22011-12-03 21:45:14 +0000185 const unsigned char *data, size_t len )
186{
Paul Bakker47703a02014-02-06 15:01:20 +0100187 int ret;
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if defined(MBEDTLS_THREADING_C)
190 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Paul Bakker47703a02014-02-06 15:01:20 +0100191 return( ret );
192#endif
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len );
Paul Bakker47703a02014-02-06 15:01:20 +0100195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#if defined(MBEDTLS_THREADING_C)
197 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
198 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100199#endif
200
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200201 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000202}
203
204/*
205 * Run through the different sources to add entropy to our accumulator
206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207static int entropy_gather_internal( mbedtls_entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +0000208{
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200209 int ret, i, have_one_strong = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
Paul Bakker6083fd22011-12-03 21:45:14 +0000211 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100212
Paul Bakker43655f42011-12-15 20:11:16 +0000213 if( ctx->source_count == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
Paul Bakker43655f42011-12-15 20:11:16 +0000215
Paul Bakker6083fd22011-12-03 21:45:14 +0000216 /*
217 * Run through our entropy sources
218 */
219 for( i = 0; i < ctx->source_count; i++ )
220 {
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200221 if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
222 have_one_strong = 1;
223
Paul Bakker6083fd22011-12-03 21:45:14 +0000224 olen = 0;
Paul Bakker66d5d072014-06-17 16:39:18 +0200225 if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
Paul Bakker6083fd22011-12-03 21:45:14 +0000227 {
228 return( ret );
229 }
230
231 /*
232 * Add if we actually gathered something
233 */
234 if( olen > 0 )
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000235 {
Paul Bakker6083fd22011-12-03 21:45:14 +0000236 entropy_update( ctx, (unsigned char) i, buf, olen );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000237 ctx->source[i].size += olen;
238 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000239 }
240
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200241 if( have_one_strong == 0 )
242 return( MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE );
243
Paul Bakker6083fd22011-12-03 21:45:14 +0000244 return( 0 );
245}
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:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344#if defined(MBEDTLS_THREADING_C)
345 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
346 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200347#endif
348
349 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000350}
351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352#if defined(MBEDTLS_FS_IO)
353int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100354{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100356 FILE *f;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100358
359 if( ( f = fopen( path, "wb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100363 goto exit;
364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100368 goto exit;
369 }
370
371 ret = 0;
372
373exit:
374 fclose( f );
375 return( ret );
376}
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
Paul Bakker66ff70d2014-03-26 11:54:05 +0100379{
380 FILE *f;
381 size_t n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
Paul Bakker66ff70d2014-03-26 11:54:05 +0100383
384 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100386
387 fseek( f, 0, SEEK_END );
388 n = (size_t) ftell( f );
389 fseek( f, 0, SEEK_SET );
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
392 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
Paul Bakker66ff70d2014-03-26 11:54:05 +0100393
394 if( fread( buf, 1, n, f ) != n )
395 {
396 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100398 }
399
400 fclose( f );
401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_entropy_update_manual( ctx, buf, n );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 return( mbedtls_entropy_write_seed_file( ctx, path ) );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100405}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#endif /* MBEDTLS_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200409/*
410 * Dummy source function
411 */
412static int entropy_dummy_source( void *data, unsigned char *output,
413 size_t len, size_t *olen )
414{
415 ((void) data);
416
417 memset( output, 0x2a, len );
418 *olen = len;
419
420 return( 0 );
421}
422
423/*
424 * The actual entropy quality is hard to test, but we can at least
425 * test that the functions don't cause errors and write the correct
426 * amount of data to buffers.
427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428int mbedtls_entropy_self_test( int verbose )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200429{
430 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_entropy_context ctx;
432 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
433 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200434 size_t i, j;
435
436 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_printf( " ENTROPY test: " );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200440
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200441 /* First do a gather to make sure we have default sources */
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200442 if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200443 goto cleanup;
444
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200445 ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16,
446 MBEDTLS_ENTROPY_SOURCE_WEAK );
Manuel Pégourié-Gonnarde94bfe62015-05-14 13:57:50 +0200447 if( ret != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200448 goto cleanup;
449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200451 goto cleanup;
452
453 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 * To test that mbedtls_entropy_func writes correct number of bytes:
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200455 * - use the whole buffer and rely on ASan to detect overruns
456 * - collect entropy 8 times and OR the result in an accumulator:
457 * any byte should then be 0 with probably 2^(-64), so requiring
458 * each of the 32 or 64 bytes to be non-zero has a false failure rate
459 * of at most 2^(-58) which is acceptable.
460 */
461 for( i = 0; i < 8; i++ )
462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200464 goto cleanup;
465
466 for( j = 0; j < sizeof( buf ); j++ )
467 acc[j] |= buf[j];
468 }
469
470 for( j = 0; j < sizeof( buf ); j++ )
471 {
472 if( acc[j] == 0 )
473 {
474 ret = 1;
475 goto cleanup;
476 }
477 }
478
479cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200481
482 if( verbose != 0 )
483 {
484 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200486 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200490 }
491
492 return( ret != 0 );
493}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496#endif /* MBEDTLS_ENTROPY_C */