From 3d53f416389103fc2c1d507906a4f6c04c19a247 Mon Sep 17 00:00:00 2001 From: Alexey Skalozub Date: Wed, 13 Jan 2016 16:53:40 +0200 Subject: [PATCH] Faster mbedtls_zeroize for MPI Writes in `sizeof(mbedtls_mpi_uint)` units perform faster than plain chars, also eliminates multiplication by `ciL` --- library/bignum.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/library/bignum.c b/library/bignum.c index 7841bea43..07e099afb 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -59,8 +59,8 @@ #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; +static void mbedtls_zeroize( mbedtls_mpi_uint *v, size_t n ) { + volatile mbedtls_mpi_uint *p = v; while( n-- ) *p++ = 0; } #define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */ @@ -99,7 +99,7 @@ void mbedtls_mpi_free( mbedtls_mpi *X ) if( X->p != NULL ) { - mbedtls_zeroize( X->p, X->n * ciL ); + mbedtls_zeroize( X->p, X->n ); mbedtls_free( X->p ); } @@ -126,7 +126,7 @@ int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs ) if( X->p != NULL ) { memcpy( p, X->p, X->n * ciL ); - mbedtls_zeroize( X->p, X->n * ciL ); + mbedtls_zeroize( X->p, X->n ); mbedtls_free( X->p ); } @@ -164,7 +164,7 @@ int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs ) if( X->p != NULL ) { memcpy( p, X->p, i * ciL ); - mbedtls_zeroize( X->p, X->n * ciL ); + mbedtls_zeroize( X->p, X->n ); mbedtls_free( X->p ); }