Use recently-introduced platform_util module

This commit is contained in:
Manuel Pégourié-Gonnard 2018-05-24 13:46:15 +02:00
parent 3798b6be6b
commit fb78c90138
3 changed files with 20 additions and 30 deletions

View File

@ -22,7 +22,6 @@
* *
* This file is part of mbed TLS (https://tls.mbed.org) * This file is part of mbed TLS (https://tls.mbed.org)
*/ */
#include "mbedtls/chacha20.h"
#if !defined(MBEDTLS_CONFIG_FILE) #if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h" #include "mbedtls/config.h"
@ -32,7 +31,8 @@
#if defined(MBEDTLS_CHACHA20_C) #if defined(MBEDTLS_CHACHA20_C)
#if !defined(MBEDTLS_CHACHA20_ALT) #include "mbedtls/chacha20.h"
#include "mbedtls/platform_util.h"
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>
@ -46,6 +46,8 @@
#endif /* MBEDTLS_PLATFORM_C */ #endif /* MBEDTLS_PLATFORM_C */
#endif /* MBEDTLS_SELF_TEST */ #endif /* MBEDTLS_SELF_TEST */
#if !defined(MBEDTLS_CHACHA20_ALT)
#define BYTES_TO_U32_LE( data, offset ) \ #define BYTES_TO_U32_LE( data, offset ) \
( (uint32_t) data[offset] \ ( (uint32_t) data[offset] \
| (uint32_t) ( (uint32_t) data[( offset ) + 1] << 8 ) \ | (uint32_t) ( (uint32_t) data[( offset ) + 1] << 8 ) \
@ -59,11 +61,6 @@
#define CHACHA20_BLOCK_SIZE_BYTES ( 4U * 16U ) #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. * \brief ChaCha20 quarter round operation.
* *
@ -182,9 +179,9 @@ void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx )
{ {
if ( ctx != NULL ) if ( ctx != NULL )
{ {
mbedtls_zeroize( ctx->initial_state, sizeof( ctx->initial_state ) ); mbedtls_platform_zeroize( ctx->initial_state, sizeof( ctx->initial_state ) );
mbedtls_zeroize( ctx->working_state, sizeof( ctx->working_state ) ); mbedtls_platform_zeroize( ctx->working_state, sizeof( ctx->working_state ) );
mbedtls_zeroize( ctx->keystream8, sizeof( ctx->keystream8 ) ); mbedtls_platform_zeroize( ctx->keystream8, sizeof( ctx->keystream8 ) );
/* Initially, there's no keystream bytes available */ /* Initially, there's no keystream bytes available */
ctx->keystream_bytes_used = CHACHA20_BLOCK_SIZE_BYTES; ctx->keystream_bytes_used = CHACHA20_BLOCK_SIZE_BYTES;
@ -195,7 +192,7 @@ void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx )
{ {
if ( ctx != NULL ) 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[14] = BYTES_TO_U32_LE( nonce, 4 );
ctx->initial_state[15] = BYTES_TO_U32_LE( nonce, 8 ); ctx->initial_state[15] = BYTES_TO_U32_LE( nonce, 8 );
mbedtls_zeroize( ctx->working_state, sizeof( ctx->working_state ) ); mbedtls_platform_zeroize( ctx->working_state, sizeof( ctx->working_state ) );
mbedtls_zeroize( ctx->keystream8, sizeof( ctx->keystream8 ) ); mbedtls_platform_zeroize( ctx->keystream8, sizeof( ctx->keystream8 ) );
/* Initially, there's no keystream bytes available */ /* Initially, there's no keystream bytes available */
ctx->keystream_bytes_used = CHACHA20_BLOCK_SIZE_BYTES; ctx->keystream_bytes_used = CHACHA20_BLOCK_SIZE_BYTES;

View File

@ -29,6 +29,8 @@
#if defined(MBEDTLS_CHACHAPOLY_C) #if defined(MBEDTLS_CHACHAPOLY_C)
#include "mbedtls/chachapoly.h" #include "mbedtls/chachapoly.h"
#include "mbedtls/platform_util.h"
#include <string.h> #include <string.h>
#if defined(MBEDTLS_SELF_TEST) #if defined(MBEDTLS_SELF_TEST)
@ -47,11 +49,6 @@
#define CHACHAPOLY_STATE_CIPHERTEXT ( 2 ) /* Encrypting or decrypting */ #define CHACHAPOLY_STATE_CIPHERTEXT ( 2 ) /* Encrypting or decrypting */
#define CHACHAPOLY_STATE_FINISHED ( 3 ) #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. * \brief Adds padding bytes (zeroes) to pad the AAD for Poly1305.
* *
@ -170,7 +167,7 @@ int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx,
} }
cleanup: cleanup:
mbedtls_zeroize( poly1305_key, 64U ); mbedtls_platform_zeroize( poly1305_key, 64U );
return( result ); return( result );
} }
@ -355,7 +352,7 @@ int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx,
if( diff != 0 ) if( diff != 0 )
{ {
mbedtls_zeroize( output, length ); mbedtls_platform_zeroize( output, length );
return( MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED ); return( MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED );
} }

View File

@ -28,9 +28,8 @@
#if defined(MBEDTLS_POLY1305_C) #if defined(MBEDTLS_POLY1305_C)
#if !defined(MBEDTLS_POLY1305_ALT)
#include "mbedtls/poly1305.h" #include "mbedtls/poly1305.h"
#include "mbedtls/platform_util.h"
#include <string.h> #include <string.h>
@ -43,6 +42,8 @@
#endif /* MBEDTLS_PLATFORM_C */ #endif /* MBEDTLS_PLATFORM_C */
#endif /* MBEDTLS_SELF_TEST */ #endif /* MBEDTLS_SELF_TEST */
#if !defined(MBEDTLS_POLY1305_ALT)
#define POLY1305_BLOCK_SIZE_BYTES ( 16U ) #define POLY1305_BLOCK_SIZE_BYTES ( 16U )
#define BYTES_TO_U32_LE( data, offset ) \ #define BYTES_TO_U32_LE( data, offset ) \
@ -52,11 +53,6 @@
| (uint32_t) ( (uint32_t) data[( offset ) + 3] << 24 ) \ | (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. * \brief Process blocks with Poly1305.
* *
@ -244,7 +240,7 @@ void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx )
{ {
if ( ctx != NULL ) 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 ) 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; ctx->acc[4] = 0U;
/* Queue initially empty */ /* Queue initially empty */
mbedtls_zeroize( ctx->queue, sizeof( ctx->queue ) ); mbedtls_platform_zeroize( ctx->queue, sizeof( ctx->queue ) );
ctx->queue_len = 0U; ctx->queue_len = 0U;
return( 0 ); return( 0 );