diff --git a/ChangeLog b/ChangeLog index de16609cf..de84df058 100644 --- a/ChangeLog +++ b/ChangeLog @@ -13,6 +13,8 @@ Features Changes * Deprecated the Memory layer + * entropy_add_source(), entropy_update_manual() and entropy_gather() + now thread-safe if POLARSSL_THREADING_C defined Bugfix * ecp_gen_keypair() does more tries to prevent failure because of diff --git a/include/polarssl/entropy.h b/include/polarssl/entropy.h index 7e65b780a..2b824ef6d 100644 --- a/include/polarssl/entropy.h +++ b/include/polarssl/entropy.h @@ -133,6 +133,7 @@ void entropy_free( entropy_context *ctx ); /** * \brief Adds an entropy source to poll + * (Thread-safe if POLARSSL_THREADING_C is enabled) * * \param ctx Entropy context * \param f_source Entropy function @@ -148,6 +149,7 @@ int entropy_add_source( entropy_context *ctx, /** * \brief Trigger an extra gather poll for the accumulator + * (Thread-safe if POLARSSL_THREADING_C is enabled) * * \param ctx Entropy context * @@ -169,6 +171,7 @@ int entropy_func( void *data, unsigned char *output, size_t len ); /** * \brief Add data to the accumulator manually + * (Thread-safe if POLARSSL_THREADING_C is enabled) * * \param ctx Entropy context * \param data Data to add diff --git a/library/entropy.c b/library/entropy.c index c5fac26e9..38171fc1e 100644 --- a/library/entropy.c +++ b/library/entropy.c @@ -80,10 +80,19 @@ int entropy_add_source( entropy_context *ctx, f_source_ptr f_source, void *p_source, size_t threshold ) { - int index = ctx->source_count; + int index, ret = 0; +#if defined(POLARSSL_THREADING_C) + if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 ) + return( ret ); +#endif + + index = ctx->source_count; if( index >= ENTROPY_MAX_SOURCES ) - return( POLARSSL_ERR_ENTROPY_MAX_SOURCES ); + { + ret = POLARSSL_ERR_ENTROPY_MAX_SOURCES; + goto exit; + } ctx->source[index].f_source = f_source; ctx->source[index].p_source = p_source; @@ -91,7 +100,13 @@ int entropy_add_source( entropy_context *ctx, ctx->source_count++; - return( 0 ); +exit: +#if defined(POLARSSL_THREADING_C) + if( polarssl_mutex_unlock( &ctx->mutex ) != 0 ) + return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); +#endif + + return( ret ); } /* @@ -133,18 +148,32 @@ static int entropy_update( entropy_context *ctx, unsigned char source_id, int entropy_update_manual( entropy_context *ctx, const unsigned char *data, size_t len ) { - return entropy_update( ctx, ENTROPY_SOURCE_MANUAL, data, len ); + int ret; + +#if defined(POLARSSL_THREADING_C) + if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 ) + return( ret ); +#endif + + ret = entropy_update( ctx, ENTROPY_SOURCE_MANUAL, data, len ); + +#if defined(POLARSSL_THREADING_C) + if( polarssl_mutex_unlock( &ctx->mutex ) != 0 ) + return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); +#endif + + return ( ret ); } /* * Run through the different sources to add entropy to our accumulator */ -int entropy_gather( entropy_context *ctx ) +static int entropy_gather_internal( entropy_context *ctx ) { int ret, i; unsigned char buf[ENTROPY_MAX_GATHER]; size_t olen; - + if( ctx->source_count == 0 ) return( POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED ); @@ -173,6 +202,28 @@ int entropy_gather( entropy_context *ctx ) return( 0 ); } +/* + * Thread-safe wrapper for entropy_gather_internal() + */ +int entropy_gather( entropy_context *ctx ) +{ + int ret; + +#if defined(POLARSSL_THREADING_C) + if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 ) + return( ret ); +#endif + + ret = entropy_gather_internal( ctx ); + +#if defined(POLARSSL_THREADING_C) + if( polarssl_mutex_unlock( &ctx->mutex ) != 0 ) + return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); +#endif + + return ( ret ); +} + int entropy_func( void *data, unsigned char *output, size_t len ) { int ret, count = 0, i, reached; @@ -198,7 +249,7 @@ int entropy_func( void *data, unsigned char *output, size_t len ) goto exit; } - if( ( ret = entropy_gather( ctx ) ) != 0 ) + if( ( ret = entropy_gather_internal( ctx ) ) != 0 ) goto exit; reached = 0;