diff --git a/library/chacha20.c b/library/chacha20.c index 5a753ebaa..7f7603549 100644 --- a/library/chacha20.c +++ b/library/chacha20.c @@ -22,7 +22,6 @@ * * This file is part of mbed TLS (https://tls.mbed.org) */ -#include "mbedtls/chacha20.h" #if !defined(MBEDTLS_CONFIG_FILE) #include "mbedtls/config.h" @@ -32,7 +31,8 @@ #if defined(MBEDTLS_CHACHA20_C) -#if !defined(MBEDTLS_CHACHA20_ALT) +#include "mbedtls/chacha20.h" +#include "mbedtls/platform_util.h" #include #include @@ -46,6 +46,8 @@ #endif /* MBEDTLS_PLATFORM_C */ #endif /* MBEDTLS_SELF_TEST */ +#if !defined(MBEDTLS_CHACHA20_ALT) + #define BYTES_TO_U32_LE( data, offset ) \ ( (uint32_t) data[offset] \ | (uint32_t) ( (uint32_t) data[( offset ) + 1] << 8 ) \ @@ -59,11 +61,6 @@ #define CHACHA20_BLOCK_SIZE_BYTES ( 4U * 16U ) -/* 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; -} - /** * \brief ChaCha20 quarter round operation. * @@ -182,9 +179,9 @@ void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx ) { if ( ctx != NULL ) { - mbedtls_zeroize( ctx->initial_state, sizeof( ctx->initial_state ) ); - mbedtls_zeroize( ctx->working_state, sizeof( ctx->working_state ) ); - mbedtls_zeroize( ctx->keystream8, sizeof( ctx->keystream8 ) ); + mbedtls_platform_zeroize( ctx->initial_state, sizeof( ctx->initial_state ) ); + mbedtls_platform_zeroize( ctx->working_state, sizeof( ctx->working_state ) ); + mbedtls_platform_zeroize( ctx->keystream8, sizeof( ctx->keystream8 ) ); /* Initially, there's no keystream bytes available */ ctx->keystream_bytes_used = CHACHA20_BLOCK_SIZE_BYTES; @@ -195,7 +192,7 @@ void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx ) { if ( ctx != NULL ) { - mbedtls_zeroize( ctx, sizeof( mbedtls_chacha20_context ) ); + mbedtls_platform_zeroize( ctx, sizeof( mbedtls_chacha20_context ) ); } } @@ -243,8 +240,8 @@ int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx, ctx->initial_state[14] = BYTES_TO_U32_LE( nonce, 4 ); ctx->initial_state[15] = BYTES_TO_U32_LE( nonce, 8 ); - mbedtls_zeroize( ctx->working_state, sizeof( ctx->working_state ) ); - mbedtls_zeroize( ctx->keystream8, sizeof( ctx->keystream8 ) ); + mbedtls_platform_zeroize( ctx->working_state, sizeof( ctx->working_state ) ); + mbedtls_platform_zeroize( ctx->keystream8, sizeof( ctx->keystream8 ) ); /* Initially, there's no keystream bytes available */ ctx->keystream_bytes_used = CHACHA20_BLOCK_SIZE_BYTES; diff --git a/library/chachapoly.c b/library/chachapoly.c index de9e66ccc..5ce27f210 100644 --- a/library/chachapoly.c +++ b/library/chachapoly.c @@ -29,6 +29,8 @@ #if defined(MBEDTLS_CHACHAPOLY_C) #include "mbedtls/chachapoly.h" +#include "mbedtls/platform_util.h" + #include #if defined(MBEDTLS_SELF_TEST) @@ -47,11 +49,6 @@ #define CHACHAPOLY_STATE_CIPHERTEXT ( 2 ) /* Encrypting or decrypting */ #define CHACHAPOLY_STATE_FINISHED ( 3 ) -/* 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; -} - /** * \brief Adds padding bytes (zeroes) to pad the AAD for Poly1305. * @@ -170,7 +167,7 @@ int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx, } cleanup: - mbedtls_zeroize( poly1305_key, 64U ); + mbedtls_platform_zeroize( poly1305_key, 64U ); return( result ); } @@ -355,7 +352,7 @@ int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx, if( diff != 0 ) { - mbedtls_zeroize( output, length ); + mbedtls_platform_zeroize( output, length ); return( MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED ); } diff --git a/library/poly1305.c b/library/poly1305.c index a9fff4757..bdd674475 100644 --- a/library/poly1305.c +++ b/library/poly1305.c @@ -28,9 +28,8 @@ #if defined(MBEDTLS_POLY1305_C) -#if !defined(MBEDTLS_POLY1305_ALT) - #include "mbedtls/poly1305.h" +#include "mbedtls/platform_util.h" #include @@ -43,6 +42,8 @@ #endif /* MBEDTLS_PLATFORM_C */ #endif /* MBEDTLS_SELF_TEST */ +#if !defined(MBEDTLS_POLY1305_ALT) + #define POLY1305_BLOCK_SIZE_BYTES ( 16U ) #define BYTES_TO_U32_LE( data, offset ) \ @@ -52,11 +53,6 @@ | (uint32_t) ( (uint32_t) data[( offset ) + 3] << 24 ) \ ) -/* 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; -} - /** * \brief Process blocks with Poly1305. * @@ -244,7 +240,7 @@ void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx ) { if ( ctx != NULL ) { - mbedtls_zeroize( ctx, sizeof( mbedtls_poly1305_context ) ); + mbedtls_platform_zeroize( ctx, sizeof( mbedtls_poly1305_context ) ); } } @@ -252,7 +248,7 @@ void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx ) { if ( ctx != NULL ) { - mbedtls_zeroize( ctx, sizeof( mbedtls_poly1305_context ) ); + mbedtls_platform_zeroize( ctx, sizeof( mbedtls_poly1305_context ) ); } } @@ -283,7 +279,7 @@ int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx, ctx->acc[4] = 0U; /* Queue initially empty */ - mbedtls_zeroize( ctx->queue, sizeof( ctx->queue ) ); + mbedtls_platform_zeroize( ctx->queue, sizeof( ctx->queue ) ); ctx->queue_len = 0U; return( 0 );