2011-12-03 21:45:14 +00:00
|
|
|
/*
|
|
|
|
* Entropy accumulator implementation
|
|
|
|
*
|
2015-01-23 09:45:19 +00:00
|
|
|
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
2011-12-03 21:45:14 +00:00
|
|
|
*
|
2015-03-06 13:17:10 +00:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2011-12-03 21:45:14 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 10:39:06 +00:00
|
|
|
#endif
|
2011-12-03 21:45:14 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_ENTROPY_C)
|
2011-12-03 21:45:14 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/entropy.h"
|
|
|
|
#include "mbedtls/entropy_poll.h"
|
2011-12-03 21:45:14 +00:00
|
|
|
|
2015-02-06 13:43:58 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_FS_IO)
|
2014-03-26 10:54:05 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/platform.h"
|
2015-02-06 13:43:58 +00:00
|
|
|
#else
|
|
|
|
#include <stdio.h>
|
2015-04-08 10:49:31 +00:00
|
|
|
#define mbedtls_printf printf
|
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2015-02-06 13:43:58 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_HAVEGE_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/havege.h"
|
2011-12-15 19:49:30 +00:00
|
|
|
#endif
|
|
|
|
|
2014-06-13 15:20:13 +00:00
|
|
|
/* Implementation that should never be optimized out by the compiler */
|
2015-04-08 10:49:31 +00:00
|
|
|
static void mbedtls_zeroize( void *v, size_t n ) {
|
2014-06-13 15:20:13 +00:00
|
|
|
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
|
|
|
}
|
|
|
|
|
2011-12-03 21:45:14 +00:00
|
|
|
#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_entropy_init( mbedtls_entropy_context *ctx )
|
2011-12-03 21:45:14 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
memset( ctx, 0, sizeof(mbedtls_entropy_context) );
|
2011-12-03 21:45:14 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
mbedtls_mutex_init( &ctx->mutex );
|
2013-09-28 13:23:57 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
|
|
|
|
mbedtls_sha512_starts( &ctx->accumulator, 0 );
|
2013-08-27 13:06:26 +00:00
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_sha256_starts( &ctx->accumulator, 0 );
|
2013-08-27 13:06:26 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_HAVEGE_C)
|
|
|
|
mbedtls_havege_init( &ctx->havege_data );
|
2011-12-15 20:11:16 +00:00
|
|
|
#endif
|
2011-12-03 21:45:14 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
|
|
|
|
#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
|
|
|
|
mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL,
|
2015-06-19 08:26:32 +00:00
|
|
|
MBEDTLS_ENTROPY_MIN_PLATFORM,
|
|
|
|
MBEDTLS_ENTROPY_SOURCE_STRONG );
|
2011-12-03 21:45:14 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_TIMING_C)
|
2015-06-19 08:26:32 +00:00
|
|
|
mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL,
|
|
|
|
MBEDTLS_ENTROPY_MIN_HARDCLOCK,
|
|
|
|
MBEDTLS_ENTROPY_SOURCE_WEAK );
|
2011-12-03 21:45:14 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_HAVEGE_C)
|
|
|
|
mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data,
|
2015-06-19 08:26:32 +00:00
|
|
|
MBEDTLS_ENTROPY_MIN_HAVEGE,
|
|
|
|
MBEDTLS_ENTROPY_SOURCE_STRONG );
|
2011-12-15 19:49:30 +00:00
|
|
|
#endif
|
2015-06-19 08:06:21 +00:00
|
|
|
#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
|
|
|
|
mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL
|
2015-06-19 08:26:32 +00:00
|
|
|
MBEDTLS_ENTROPY_MIN_HARDWARE,
|
|
|
|
MBEDTLS_ENTROPY_SOURCE_STRONG );
|
2015-06-19 08:06:21 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
|
2011-12-03 21:45:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
|
2013-09-28 13:23:03 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_HAVEGE_C)
|
|
|
|
mbedtls_havege_free( &ctx->havege_data );
|
2014-06-18 14:44:11 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
mbedtls_mutex_free( &ctx->mutex );
|
2013-09-28 13:23:57 +00:00
|
|
|
#endif
|
2015-06-02 08:59:29 +00:00
|
|
|
mbedtls_zeroize( ctx, sizeof( mbedtls_entropy_context ) );
|
2013-09-28 13:23:03 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
|
|
|
|
mbedtls_entropy_f_source_ptr f_source, void *p_source,
|
2015-06-19 08:26:32 +00:00
|
|
|
size_t threshold, int strong )
|
2011-12-03 21:45:14 +00:00
|
|
|
{
|
2014-02-06 14:01:20 +00:00
|
|
|
int index, ret = 0;
|
2011-12-10 17:02:19 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
|
2014-02-06 14:01:20 +00:00
|
|
|
return( ret );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
index = ctx->source_count;
|
2015-04-08 10:49:31 +00:00
|
|
|
if( index >= MBEDTLS_ENTROPY_MAX_SOURCES )
|
2014-02-06 14:01:20 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
|
2014-02-06 14:01:20 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
2011-12-03 21:45:14 +00:00
|
|
|
|
2015-06-19 08:26:32 +00:00
|
|
|
ctx->source[index].f_source = f_source;
|
|
|
|
ctx->source[index].p_source = p_source;
|
2011-12-10 17:02:19 +00:00
|
|
|
ctx->source[index].threshold = threshold;
|
2015-06-19 08:26:32 +00:00
|
|
|
ctx->source[index].strong = strong;
|
2011-12-03 21:45:14 +00:00
|
|
|
|
|
|
|
ctx->source_count++;
|
|
|
|
|
2014-02-06 14:01:20 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
|
2014-02-06 14:01:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return( ret );
|
2011-12-03 21:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Entropy accumulator update
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
|
2013-06-25 14:25:17 +00:00
|
|
|
const unsigned char *data, size_t len )
|
2011-12-03 21:45:14 +00:00
|
|
|
{
|
|
|
|
unsigned char header[2];
|
2015-04-08 10:49:31 +00:00
|
|
|
unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
2011-12-03 21:45:14 +00:00
|
|
|
size_t use_len = len;
|
|
|
|
const unsigned char *p = data;
|
2013-06-25 14:25:17 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
|
2011-12-03 21:45:14 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
|
|
|
|
mbedtls_sha512( data, len, tmp, 0 );
|
2013-08-27 13:06:26 +00:00
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_sha256( data, len, tmp, 0 );
|
2013-08-27 13:06:26 +00:00
|
|
|
#endif
|
2011-12-03 21:45:14 +00:00
|
|
|
p = tmp;
|
2015-04-08 10:49:31 +00:00
|
|
|
use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
|
2011-12-03 21:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
header[0] = source_id;
|
|
|
|
header[1] = use_len & 0xFF;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
|
|
|
|
mbedtls_sha512_update( &ctx->accumulator, header, 2 );
|
|
|
|
mbedtls_sha512_update( &ctx->accumulator, p, use_len );
|
2013-08-27 13:06:26 +00:00
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_sha256_update( &ctx->accumulator, header, 2 );
|
|
|
|
mbedtls_sha256_update( &ctx->accumulator, p, use_len );
|
2013-08-27 13:06:26 +00:00
|
|
|
#endif
|
2013-06-25 14:25:17 +00:00
|
|
|
|
2011-12-03 21:45:14 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
|
2011-12-03 21:45:14 +00:00
|
|
|
const unsigned char *data, size_t len )
|
|
|
|
{
|
2014-02-06 14:01:20 +00:00
|
|
|
int ret;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
|
2014-02-06 14:01:20 +00:00
|
|
|
return( ret );
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len );
|
2014-02-06 14:01:20 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
|
2014-02-06 14:01:20 +00:00
|
|
|
#endif
|
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( ret );
|
2011-12-03 21:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run through the different sources to add entropy to our accumulator
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
static int entropy_gather_internal( mbedtls_entropy_context *ctx )
|
2011-12-03 21:45:14 +00:00
|
|
|
{
|
2015-06-19 08:26:32 +00:00
|
|
|
int ret, i, have_one_strong = 0;
|
2015-04-08 10:49:31 +00:00
|
|
|
unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
|
2011-12-03 21:45:14 +00:00
|
|
|
size_t olen;
|
2014-02-06 14:01:20 +00:00
|
|
|
|
2011-12-15 20:11:16 +00:00
|
|
|
if( ctx->source_count == 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
|
2011-12-15 20:11:16 +00:00
|
|
|
|
2011-12-03 21:45:14 +00:00
|
|
|
/*
|
|
|
|
* Run through our entropy sources
|
|
|
|
*/
|
|
|
|
for( i = 0; i < ctx->source_count; i++ )
|
|
|
|
{
|
2015-06-19 08:26:32 +00:00
|
|
|
if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
|
|
|
|
have_one_strong = 1;
|
|
|
|
|
2011-12-03 21:45:14 +00:00
|
|
|
olen = 0;
|
2014-06-17 14:39:18 +00:00
|
|
|
if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
|
2015-04-08 10:49:31 +00:00
|
|
|
buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
|
2011-12-03 21:45:14 +00:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add if we actually gathered something
|
|
|
|
*/
|
|
|
|
if( olen > 0 )
|
2011-12-10 17:02:19 +00:00
|
|
|
{
|
2011-12-03 21:45:14 +00:00
|
|
|
entropy_update( ctx, (unsigned char) i, buf, olen );
|
2011-12-10 17:02:19 +00:00
|
|
|
ctx->source[i].size += olen;
|
|
|
|
}
|
2011-12-03 21:45:14 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 08:26:32 +00:00
|
|
|
if( have_one_strong == 0 )
|
|
|
|
return( MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE );
|
|
|
|
|
2011-12-03 21:45:14 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2014-02-06 14:01:20 +00:00
|
|
|
/*
|
|
|
|
* Thread-safe wrapper for entropy_gather_internal()
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
|
2014-02-06 14:01:20 +00:00
|
|
|
{
|
2014-04-09 12:47:58 +00:00
|
|
|
int ret;
|
2014-02-06 14:01:20 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
|
2014-04-09 12:47:58 +00:00
|
|
|
return( ret );
|
2014-02-06 14:01:20 +00:00
|
|
|
#endif
|
|
|
|
|
2014-04-09 12:47:58 +00:00
|
|
|
ret = entropy_gather_internal( ctx );
|
2014-02-06 14:01:20 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
|
2014-02-06 14:01:20 +00:00
|
|
|
#endif
|
|
|
|
|
2014-04-09 12:47:58 +00:00
|
|
|
return( ret );
|
2014-02-06 14:01:20 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
|
2011-12-03 21:45:14 +00:00
|
|
|
{
|
2015-06-19 07:40:51 +00:00
|
|
|
int ret, count = 0, i, done;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
|
|
|
|
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
2011-12-03 21:45:14 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
|
|
|
|
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
|
2011-12-03 21:45:14 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
|
2013-09-28 13:23:57 +00:00
|
|
|
return( ret );
|
|
|
|
#endif
|
|
|
|
|
2011-12-03 21:45:14 +00:00
|
|
|
/*
|
|
|
|
* Always gather extra entropy before a call
|
|
|
|
*/
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if( count++ > ENTROPY_MAX_LOOP )
|
2013-09-28 13:23:57 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
|
2013-09-28 13:23:57 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
2011-12-03 21:45:14 +00:00
|
|
|
|
2014-02-06 14:01:20 +00:00
|
|
|
if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
|
2013-09-28 13:23:57 +00:00
|
|
|
goto exit;
|
2011-12-10 17:02:19 +00:00
|
|
|
|
2015-06-19 07:40:51 +00:00
|
|
|
done = 1;
|
2011-12-10 17:02:19 +00:00
|
|
|
for( i = 0; i < ctx->source_count; i++ )
|
2015-06-19 07:40:51 +00:00
|
|
|
if( ctx->source[i].size < ctx->source[i].threshold )
|
|
|
|
done = 0;
|
2011-12-03 21:45:14 +00:00
|
|
|
}
|
2015-06-19 07:40:51 +00:00
|
|
|
while( ! done );
|
2011-12-03 21:45:14 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
2011-12-03 21:45:14 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
|
|
|
|
mbedtls_sha512_finish( &ctx->accumulator, buf );
|
2013-06-30 12:34:05 +00:00
|
|
|
|
2011-12-03 21:45:14 +00:00
|
|
|
/*
|
2011-12-10 17:02:19 +00:00
|
|
|
* Reset accumulator and counters and recycle existing entropy
|
2011-12-03 21:45:14 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
memset( &ctx->accumulator, 0, sizeof( mbedtls_sha512_context ) );
|
|
|
|
mbedtls_sha512_starts( &ctx->accumulator, 0 );
|
|
|
|
mbedtls_sha512_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
2013-08-27 13:06:26 +00:00
|
|
|
|
|
|
|
/*
|
2014-03-26 11:51:25 +00:00
|
|
|
* Perform second SHA-512 on entropy
|
2013-08-27 13:06:26 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
|
|
|
|
#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
|
|
|
|
mbedtls_sha256_finish( &ctx->accumulator, buf );
|
2013-08-27 13:06:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset accumulator and counters and recycle existing entropy
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
memset( &ctx->accumulator, 0, sizeof( mbedtls_sha256_context ) );
|
|
|
|
mbedtls_sha256_starts( &ctx->accumulator, 0 );
|
|
|
|
mbedtls_sha256_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
2014-03-26 11:51:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform second SHA-256 on entropy
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_sha256( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
|
|
|
|
#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
|
2011-12-10 17:02:19 +00:00
|
|
|
|
|
|
|
for( i = 0; i < ctx->source_count; i++ )
|
|
|
|
ctx->source[i].size = 0;
|
2011-12-03 21:45:14 +00:00
|
|
|
|
|
|
|
memcpy( output, buf, len );
|
|
|
|
|
2013-09-28 13:23:57 +00:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
|
2013-09-28 13:23:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return( ret );
|
2011-12-03 21:45:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_FS_IO)
|
|
|
|
int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
|
2014-03-26 10:54:05 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
|
2014-03-26 10:54:05 +00:00
|
|
|
FILE *f;
|
2015-04-08 10:49:31 +00:00
|
|
|
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
2014-03-26 10:54:05 +00:00
|
|
|
|
|
|
|
if( ( f = fopen( path, "wb" ) ) == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
|
2014-03-26 10:54:05 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
|
2014-03-26 10:54:05 +00:00
|
|
|
goto exit;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
|
2014-03-26 10:54:05 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
|
2014-03-26 10:54:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
fclose( f );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
|
2014-03-26 10:54:05 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
size_t n;
|
2015-04-08 10:49:31 +00:00
|
|
|
unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
|
2014-03-26 10:54:05 +00:00
|
|
|
|
|
|
|
if( ( f = fopen( path, "rb" ) ) == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
|
2014-03-26 10:54:05 +00:00
|
|
|
|
|
|
|
fseek( f, 0, SEEK_END );
|
|
|
|
n = (size_t) ftell( f );
|
|
|
|
fseek( f, 0, SEEK_SET );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
|
|
|
|
n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
|
2014-03-26 10:54:05 +00:00
|
|
|
|
|
|
|
if( fread( buf, 1, n, f ) != n )
|
|
|
|
{
|
|
|
|
fclose( f );
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
|
2014-03-26 10:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose( f );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_update_manual( ctx, buf, n );
|
2014-03-26 10:54:05 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
return( mbedtls_entropy_write_seed_file( ctx, path ) );
|
2014-03-26 10:54:05 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_FS_IO */
|
2014-03-26 10:54:05 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2014-05-30 08:34:15 +00:00
|
|
|
/*
|
|
|
|
* Dummy source function
|
|
|
|
*/
|
|
|
|
static int entropy_dummy_source( void *data, unsigned char *output,
|
|
|
|
size_t len, size_t *olen )
|
|
|
|
{
|
|
|
|
((void) data);
|
|
|
|
|
|
|
|
memset( output, 0x2a, len );
|
|
|
|
*olen = len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The actual entropy quality is hard to test, but we can at least
|
|
|
|
* test that the functions don't cause errors and write the correct
|
|
|
|
* amount of data to buffers.
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_entropy_self_test( int verbose )
|
2014-05-30 08:34:15 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_context ctx;
|
|
|
|
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
|
|
|
|
unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
|
2014-05-30 08:34:15 +00:00
|
|
|
size_t i, j;
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " ENTROPY test: " );
|
2014-05-30 08:34:15 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_init( &ctx );
|
2014-05-30 08:34:15 +00:00
|
|
|
|
2015-06-19 08:26:32 +00:00
|
|
|
/* First do a gather to make sure we have default sources */
|
2015-05-14 11:57:50 +00:00
|
|
|
if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
|
2014-05-30 08:34:15 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2015-06-19 08:26:32 +00:00
|
|
|
ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16,
|
|
|
|
MBEDTLS_ENTROPY_SOURCE_WEAK );
|
2015-05-14 11:57:50 +00:00
|
|
|
if( ret != 0 )
|
2014-05-30 08:34:15 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
|
2014-05-30 08:34:15 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/*
|
2015-04-08 10:49:31 +00:00
|
|
|
* To test that mbedtls_entropy_func writes correct number of bytes:
|
2014-05-30 08:34:15 +00:00
|
|
|
* - use the whole buffer and rely on ASan to detect overruns
|
|
|
|
* - collect entropy 8 times and OR the result in an accumulator:
|
|
|
|
* any byte should then be 0 with probably 2^(-64), so requiring
|
|
|
|
* each of the 32 or 64 bytes to be non-zero has a false failure rate
|
|
|
|
* of at most 2^(-58) which is acceptable.
|
|
|
|
*/
|
|
|
|
for( i = 0; i < 8; i++ )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
|
2014-05-30 08:34:15 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
for( j = 0; j < sizeof( buf ); j++ )
|
|
|
|
acc[j] |= buf[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
for( j = 0; j < sizeof( buf ); j++ )
|
|
|
|
{
|
|
|
|
if( acc[j] == 0 )
|
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_free( &ctx );
|
2014-05-30 08:34:15 +00:00
|
|
|
|
|
|
|
if( verbose != 0 )
|
|
|
|
{
|
|
|
|
if( ret != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "failed\n" );
|
2014-05-30 08:34:15 +00:00
|
|
|
else
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "passed\n" );
|
2014-05-30 08:34:15 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "\n" );
|
2014-05-30 08:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return( ret != 0 );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2014-05-30 08:34:15 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_ENTROPY_C */
|