mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-21 13:20:27 -06:00
Externals: Update mbedtls to 2.16.1
On a few of our buildbot instances, we get warnings about the usage of deprecated functions. We should correct these, especially if we're delegating to system versions of the libraries if they're available. However, in order to do that, we need to update our library variant from 2.1.1 so that the non-deprecated alternatives are actually available.
This commit is contained in:
156
Externals/mbedtls/library/entropy.c
vendored
156
Externals/mbedtls/library/entropy.c
vendored
@ -35,6 +35,7 @@
|
||||
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/entropy_poll.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
@ -59,30 +60,30 @@
|
||||
#include "mbedtls/havege.h"
|
||||
#endif
|
||||
|
||||
/* Implementation that should never be optimized out by the compiler */
|
||||
static void mbedtls_zeroize( void *v, size_t n ) {
|
||||
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
||||
}
|
||||
|
||||
#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
|
||||
|
||||
void mbedtls_entropy_init( mbedtls_entropy_context *ctx )
|
||||
{
|
||||
memset( ctx, 0, sizeof(mbedtls_entropy_context) );
|
||||
ctx->source_count = 0;
|
||||
memset( ctx->source, 0, sizeof( ctx->source ) );
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
mbedtls_mutex_init( &ctx->mutex );
|
||||
#endif
|
||||
|
||||
ctx->accumulator_started = 0;
|
||||
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
|
||||
mbedtls_sha512_starts( &ctx->accumulator, 0 );
|
||||
mbedtls_sha512_init( &ctx->accumulator );
|
||||
#else
|
||||
mbedtls_sha256_starts( &ctx->accumulator, 0 );
|
||||
mbedtls_sha256_init( &ctx->accumulator );
|
||||
#endif
|
||||
#if defined(MBEDTLS_HAVEGE_C)
|
||||
mbedtls_havege_init( &ctx->havege_data );
|
||||
#endif
|
||||
|
||||
/* Reminder: Update ENTROPY_HAVE_STRONG in the test files
|
||||
* when adding more strong entropy sources here. */
|
||||
|
||||
#if defined(MBEDTLS_TEST_NULL_ENTROPY)
|
||||
mbedtls_entropy_add_source( ctx, mbedtls_null_entropy_poll, NULL,
|
||||
1, MBEDTLS_ENTROPY_SOURCE_STRONG );
|
||||
@ -113,6 +114,7 @@ void mbedtls_entropy_init( mbedtls_entropy_context *ctx )
|
||||
mbedtls_entropy_add_source( ctx, mbedtls_nv_seed_poll, NULL,
|
||||
MBEDTLS_ENTROPY_BLOCK_SIZE,
|
||||
MBEDTLS_ENTROPY_SOURCE_STRONG );
|
||||
ctx->initial_entropy_run = 0;
|
||||
#endif
|
||||
#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
|
||||
}
|
||||
@ -125,31 +127,41 @@ void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
mbedtls_mutex_free( &ctx->mutex );
|
||||
#endif
|
||||
mbedtls_zeroize( ctx, sizeof( mbedtls_entropy_context ) );
|
||||
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
|
||||
mbedtls_sha512_free( &ctx->accumulator );
|
||||
#else
|
||||
mbedtls_sha256_free( &ctx->accumulator );
|
||||
#endif
|
||||
#if defined(MBEDTLS_ENTROPY_NV_SEED)
|
||||
ctx->initial_entropy_run = 0;
|
||||
#endif
|
||||
ctx->source_count = 0;
|
||||
mbedtls_platform_zeroize( ctx->source, sizeof( ctx->source ) );
|
||||
ctx->accumulator_started = 0;
|
||||
}
|
||||
|
||||
int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
|
||||
mbedtls_entropy_f_source_ptr f_source, void *p_source,
|
||||
size_t threshold, int strong )
|
||||
{
|
||||
int index, ret = 0;
|
||||
int idx, ret = 0;
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
|
||||
return( ret );
|
||||
#endif
|
||||
|
||||
index = ctx->source_count;
|
||||
if( index >= MBEDTLS_ENTROPY_MAX_SOURCES )
|
||||
idx = ctx->source_count;
|
||||
if( idx >= MBEDTLS_ENTROPY_MAX_SOURCES )
|
||||
{
|
||||
ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ctx->source[index].f_source = f_source;
|
||||
ctx->source[index].p_source = p_source;
|
||||
ctx->source[index].threshold = threshold;
|
||||
ctx->source[index].strong = strong;
|
||||
ctx->source[idx].f_source = f_source;
|
||||
ctx->source[idx].p_source = p_source;
|
||||
ctx->source[idx].threshold = threshold;
|
||||
ctx->source[idx].strong = strong;
|
||||
|
||||
ctx->source_count++;
|
||||
|
||||
@ -172,13 +184,16 @@ static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id
|
||||
unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
||||
size_t use_len = len;
|
||||
const unsigned char *p = data;
|
||||
int ret = 0;
|
||||
|
||||
if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
|
||||
{
|
||||
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
|
||||
mbedtls_sha512( data, len, tmp, 0 );
|
||||
if( ( ret = mbedtls_sha512_ret( data, len, tmp, 0 ) ) != 0 )
|
||||
goto cleanup;
|
||||
#else
|
||||
mbedtls_sha256( data, len, tmp, 0 );
|
||||
if( ( ret = mbedtls_sha256_ret( data, len, tmp, 0 ) ) != 0 )
|
||||
goto cleanup;
|
||||
#endif
|
||||
p = tmp;
|
||||
use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
|
||||
@ -187,15 +202,35 @@ static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id
|
||||
header[0] = source_id;
|
||||
header[1] = use_len & 0xFF;
|
||||
|
||||
/*
|
||||
* Start the accumulator if this has not already happened. Note that
|
||||
* it is sufficient to start the accumulator here only because all calls to
|
||||
* gather entropy eventually execute this code.
|
||||
*/
|
||||
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
|
||||
mbedtls_sha512_update( &ctx->accumulator, header, 2 );
|
||||
mbedtls_sha512_update( &ctx->accumulator, p, use_len );
|
||||
if( ctx->accumulator_started == 0 &&
|
||||
( ret = mbedtls_sha512_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
|
||||
goto cleanup;
|
||||
else
|
||||
ctx->accumulator_started = 1;
|
||||
if( ( ret = mbedtls_sha512_update_ret( &ctx->accumulator, header, 2 ) ) != 0 )
|
||||
goto cleanup;
|
||||
ret = mbedtls_sha512_update_ret( &ctx->accumulator, p, use_len );
|
||||
#else
|
||||
mbedtls_sha256_update( &ctx->accumulator, header, 2 );
|
||||
mbedtls_sha256_update( &ctx->accumulator, p, use_len );
|
||||
if( ctx->accumulator_started == 0 &&
|
||||
( ret = mbedtls_sha256_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
|
||||
goto cleanup;
|
||||
else
|
||||
ctx->accumulator_started = 1;
|
||||
if( ( ret = mbedtls_sha256_update_ret( &ctx->accumulator, header, 2 ) ) != 0 )
|
||||
goto cleanup;
|
||||
ret = mbedtls_sha256_update_ret( &ctx->accumulator, p, use_len );
|
||||
#endif
|
||||
|
||||
return( 0 );
|
||||
cleanup:
|
||||
mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
|
||||
int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
|
||||
@ -242,7 +277,7 @@ static int entropy_gather_internal( mbedtls_entropy_context *ctx )
|
||||
if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
|
||||
buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
|
||||
{
|
||||
return( ret );
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -250,15 +285,20 @@ static int entropy_gather_internal( mbedtls_entropy_context *ctx )
|
||||
*/
|
||||
if( olen > 0 )
|
||||
{
|
||||
entropy_update( ctx, (unsigned char) i, buf, olen );
|
||||
if( ( ret = entropy_update( ctx, (unsigned char) i,
|
||||
buf, olen ) ) != 0 )
|
||||
return( ret );
|
||||
ctx->source[i].size += olen;
|
||||
}
|
||||
}
|
||||
|
||||
if( have_one_strong == 0 )
|
||||
return( MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE );
|
||||
ret = MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE;
|
||||
|
||||
return( 0 );
|
||||
cleanup:
|
||||
mbedtls_platform_zeroize( buf, sizeof( buf ) );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
|
||||
/*
|
||||
@ -333,33 +373,52 @@ int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
|
||||
memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||
|
||||
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
|
||||
mbedtls_sha512_finish( &ctx->accumulator, buf );
|
||||
/*
|
||||
* Note that at this stage it is assumed that the accumulator was started
|
||||
* in a previous call to entropy_update(). If this is not guaranteed, the
|
||||
* code below will fail.
|
||||
*/
|
||||
if( ( ret = mbedtls_sha512_finish_ret( &ctx->accumulator, buf ) ) != 0 )
|
||||
goto exit;
|
||||
|
||||
/*
|
||||
* Reset accumulator and counters and recycle existing entropy
|
||||
*/
|
||||
memset( &ctx->accumulator, 0, sizeof( mbedtls_sha512_context ) );
|
||||
mbedtls_sha512_starts( &ctx->accumulator, 0 );
|
||||
mbedtls_sha512_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||
mbedtls_sha512_free( &ctx->accumulator );
|
||||
mbedtls_sha512_init( &ctx->accumulator );
|
||||
if( ( ret = mbedtls_sha512_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
|
||||
goto exit;
|
||||
if( ( ret = mbedtls_sha512_update_ret( &ctx->accumulator, buf,
|
||||
MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
|
||||
goto exit;
|
||||
|
||||
/*
|
||||
* Perform second SHA-512 on entropy
|
||||
*/
|
||||
mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
|
||||
if( ( ret = mbedtls_sha512_ret( buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
|
||||
buf, 0 ) ) != 0 )
|
||||
goto exit;
|
||||
#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
|
||||
mbedtls_sha256_finish( &ctx->accumulator, buf );
|
||||
if( ( ret = mbedtls_sha256_finish_ret( &ctx->accumulator, buf ) ) != 0 )
|
||||
goto exit;
|
||||
|
||||
/*
|
||||
* Reset accumulator and counters and recycle existing entropy
|
||||
*/
|
||||
memset( &ctx->accumulator, 0, sizeof( mbedtls_sha256_context ) );
|
||||
mbedtls_sha256_starts( &ctx->accumulator, 0 );
|
||||
mbedtls_sha256_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||
mbedtls_sha256_free( &ctx->accumulator );
|
||||
mbedtls_sha256_init( &ctx->accumulator );
|
||||
if( ( ret = mbedtls_sha256_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
|
||||
goto exit;
|
||||
if( ( ret = mbedtls_sha256_update_ret( &ctx->accumulator, buf,
|
||||
MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
|
||||
goto exit;
|
||||
|
||||
/*
|
||||
* Perform second SHA-256 on entropy
|
||||
*/
|
||||
mbedtls_sha256( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
|
||||
if( ( ret = mbedtls_sha256_ret( buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
|
||||
buf, 0 ) ) != 0 )
|
||||
goto exit;
|
||||
#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
|
||||
|
||||
for( i = 0; i < ctx->source_count; i++ )
|
||||
@ -370,6 +429,8 @@ int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
|
||||
ret = 0;
|
||||
|
||||
exit:
|
||||
mbedtls_platform_zeroize( buf, sizeof( buf ) );
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
|
||||
return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
|
||||
@ -382,7 +443,7 @@ exit:
|
||||
int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx )
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
|
||||
unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
|
||||
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
||||
|
||||
/* Read new seed and write it to NV */
|
||||
if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
|
||||
@ -393,9 +454,9 @@ int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx )
|
||||
|
||||
/* Manually update the remaining stream with a separator value to diverge */
|
||||
memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||
mbedtls_entropy_update_manual( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||
ret = mbedtls_entropy_update_manual( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||
|
||||
return( 0 );
|
||||
return( ret );
|
||||
}
|
||||
#endif /* MBEDTLS_ENTROPY_NV_SEED */
|
||||
|
||||
@ -421,12 +482,15 @@ int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *p
|
||||
ret = 0;
|
||||
|
||||
exit:
|
||||
mbedtls_platform_zeroize( buf, sizeof( buf ) );
|
||||
|
||||
fclose( f );
|
||||
return( ret );
|
||||
}
|
||||
|
||||
int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
|
||||
{
|
||||
int ret = 0;
|
||||
FILE *f;
|
||||
size_t n;
|
||||
unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
|
||||
@ -442,14 +506,16 @@ int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *
|
||||
n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
|
||||
|
||||
if( fread( buf, 1, n, f ) != n )
|
||||
{
|
||||
fclose( f );
|
||||
return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
|
||||
}
|
||||
ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
|
||||
else
|
||||
ret = mbedtls_entropy_update_manual( ctx, buf, n );
|
||||
|
||||
fclose( f );
|
||||
|
||||
mbedtls_entropy_update_manual( ctx, buf, n );
|
||||
mbedtls_platform_zeroize( buf, sizeof( buf ) );
|
||||
|
||||
if( ret != 0 )
|
||||
return( ret );
|
||||
|
||||
return( mbedtls_entropy_write_seed_file( ctx, path ) );
|
||||
}
|
||||
|
Reference in New Issue
Block a user