Faster mbedtls_zeroize for MPI

Writes in `sizeof(mbedtls_mpi_uint)` units perform faster than plain chars, also eliminates multiplication by `ciL`
This commit is contained in:
Alexey Skalozub 2016-01-13 16:53:40 +02:00 committed by Simon Butcher
parent 8a3170571e
commit 3d53f41638

View file

@ -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 );
}