summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorManuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>2018-05-24 13:46:15 +0200
committerManuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>2018-05-24 16:54:57 +0200
commitfb78c901389043d37a3076a745835084c63b8d7b (patch)
tree7f750caa2ad03fda98198f8f523f53b327fe65a3
parent3798b6be6be7a8a9b3ecd1f7e43df3a72382f365 (diff)
Use recently-introduced platform_util module
-rw-r--r--library/chacha20.c23
-rw-r--r--library/chachapoly.c11
-rw-r--r--library/poly1305.c16
3 files changed, 20 insertions, 30 deletions
diff --git a/library/chacha20.c b/library/chacha20.c
index 5a753eba..7f760354 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 <stddef.h>
#include <string.h>
@@ -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 de9e66cc..5ce27f21 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 <string.h>
#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 a9fff475..bdd67447 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 <string.h>
@@ -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 );