libmbedtls: add mbedtls_mpi_init_mempool()
Adds mbedtls_mpi_init_mempool() which initializes a mbedtls_mpi struct
to use the mempool mbedtls_mpi_mempool if configured for memory
allocation. All local memory allocation are changed to use
mbedtls_mpi_init_mempool() instead of mbedtls_mpi_init(). This will give
a stack like alloc/free pattern for which the mempool is optimized.
Acked-by: Jerome Forissier <jerome.forissier@linaro.org>
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
[jf: rebased onto mbedtls-2.22.0]
Signed-off-by: Jerome Forissier <jerome@forissier.org>
[jf: rebased onto mbedtls-2.27.0, fold fixup commit:
2df910b2df8b ("libmbedtls: mbedtls_mpi_shrink(): fix possible unwanted truncation"),
adjust macro ECP_MPI_INIT]
Signed-off-by: Jerome Forissier <jerome@forissier.org>
diff --git a/lib/libmbedtls/mbedtls/library/bignum.c b/lib/libmbedtls/mbedtls/library/bignum.c
index 9e84da5..86df192 100644
--- a/lib/libmbedtls/mbedtls/library/bignum.c
+++ b/lib/libmbedtls/mbedtls/library/bignum.c
@@ -54,6 +54,8 @@
#define mbedtls_free free
#endif
+#include <mempool.h>
+
#define MPI_VALIDATE_RET( cond ) \
MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
#define MPI_VALIDATE( cond ) \
@@ -72,6 +74,8 @@
#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
+void *mbedtls_mpi_mempool;
+
/* Implementation that should never be optimized out by the compiler */
static void mbedtls_mpi_zeroize( mbedtls_mpi_uint *v, size_t n )
{
@@ -81,15 +85,26 @@
/*
* Initialize one MPI
*/
-void mbedtls_mpi_init( mbedtls_mpi *X )
+static void mpi_init( mbedtls_mpi *X, short use_mempool )
{
MPI_VALIDATE( X != NULL );
X->s = 1;
+ X->use_mempool = use_mempool;
X->n = 0;
X->p = NULL;
}
+void mbedtls_mpi_init( mbedtls_mpi *X )
+{
+ mpi_init( X, 0 /*use_mempool*/ );
+}
+
+void mbedtls_mpi_init_mempool( mbedtls_mpi *X )
+{
+ mpi_init( X, !!mbedtls_mpi_mempool /*use_mempool*/ );
+}
+
/*
* Unallocate one MPI
*/
@@ -101,7 +116,10 @@
if( X->p != NULL )
{
mbedtls_mpi_zeroize( X->p, X->n );
- mbedtls_free( X->p );
+ if( X->use_mempool )
+ mempool_free( mbedtls_mpi_mempool, X->p );
+ else
+ mbedtls_free( X->p );
}
X->s = 1;
@@ -122,14 +140,28 @@
if( X->n < nblimbs )
{
- if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( nblimbs, ciL ) ) == NULL )
- return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
+ if( X->use_mempool )
+ {
+ p = mempool_alloc( mbedtls_mpi_mempool, nblimbs * ciL );
+ if( p == NULL )
+ return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
+ memset( p, 0, nblimbs * ciL );
+ }
+ else
+ {
+ p = (mbedtls_mpi_uint*)mbedtls_calloc( nblimbs, ciL );
+ if( p == NULL )
+ return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
+ }
if( X->p != NULL )
{
memcpy( p, X->p, X->n * ciL );
mbedtls_mpi_zeroize( X->p, X->n );
- mbedtls_free( X->p );
+ if( X->use_mempool )
+ mempool_free( mbedtls_mpi_mempool, X->p);
+ else
+ mbedtls_free( X->p );
}
X->n = nblimbs;
@@ -165,14 +197,28 @@
if( i < nblimbs )
i = nblimbs;
- if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( i, ciL ) ) == NULL )
- return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
+ if( X->use_mempool )
+ {
+ p = mempool_alloc( mbedtls_mpi_mempool, i * ciL );
+ if( p == NULL )
+ return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
+ memset( p, 0, i * ciL );
+ }
+ else
+ {
+ p = (mbedtls_mpi_uint*)mbedtls_calloc( i, ciL );
+ if( p == NULL )
+ return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
+ }
if( X->p != NULL )
{
memcpy( p, X->p, i * ciL );
mbedtls_mpi_zeroize( X->p, X->n );
- mbedtls_free( X->p );
+ if( X->use_mempool )
+ mempool_free( mbedtls_mpi_mempool, X->p );
+ else
+ mbedtls_free( X->p );
}
X->n = i;
@@ -583,7 +629,7 @@
if( radix < 2 || radix > 16 )
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
- mbedtls_mpi_init( &T );
+ mbedtls_mpi_init_mempool( &T );
if( s[0] == 0 )
{
@@ -715,7 +761,7 @@
}
p = buf;
- mbedtls_mpi_init( &T );
+ mbedtls_mpi_init_mempool( &T );
if( X->s == -1 )
{
@@ -1719,7 +1765,7 @@
MPI_VALIDATE_RET( A != NULL );
MPI_VALIDATE_RET( B != NULL );
- mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
+ mbedtls_mpi_init_mempool( &TA ); mbedtls_mpi_init_mempool( &TB );
if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
@@ -1908,8 +1954,8 @@
if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
- mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
- mbedtls_mpi_init( &T1 );
+ mbedtls_mpi_init_mempool( &X ); mbedtls_mpi_init_mempool( &Y );
+ mbedtls_mpi_init_mempool( &Z ); mbedtls_mpi_init_mempool( &T1 );
/*
* Avoid dynamic memory allocations for constant-size T2.
*
@@ -2343,9 +2389,9 @@
* Init temps and window size
*/
mpi_montg_init( &mm, N );
- mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
- mbedtls_mpi_init( &Apos );
- mbedtls_mpi_init( &WW );
+ mbedtls_mpi_init_mempool( &RR ); mbedtls_mpi_init( &T );
+ mbedtls_mpi_init_mempool( &Apos );
+ mbedtls_mpi_init_mempool( &WW );
memset( W, 0, sizeof( W ) );
i = mbedtls_mpi_bitlen( E );
@@ -2559,7 +2605,7 @@
MPI_VALIDATE_RET( A != NULL );
MPI_VALIDATE_RET( B != NULL );
- mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
+ mbedtls_mpi_init_mempool( &TA ); mbedtls_mpi_init_mempool( &TB );
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
@@ -2804,9 +2850,11 @@
if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 )
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
- mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
- mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
- mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
+ mbedtls_mpi_init_mempool( &TA ); mbedtls_mpi_init_mempool( &TU );
+ mbedtls_mpi_init_mempool( &U1 ); mbedtls_mpi_init_mempool( &U2 );
+ mbedtls_mpi_init_mempool( &G ); mbedtls_mpi_init_mempool( &TB );
+ mbedtls_mpi_init_mempool( &TV ); mbedtls_mpi_init_mempool( &V1 );
+ mbedtls_mpi_init_mempool( &V2 );
MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
@@ -2962,9 +3010,9 @@
MPI_VALIDATE_RET( X != NULL );
MPI_VALIDATE_RET( f_rng != NULL );
- mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R );
- mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
- mbedtls_mpi_init( &RR );
+ mbedtls_mpi_init_mempool( &W ); mbedtls_mpi_init_mempool( &R );
+ mbedtls_mpi_init_mempool( &T ); mbedtls_mpi_init_mempool( &A );
+ mbedtls_mpi_init_mempool( &RR );
/*
* W = |X| - 1
@@ -3125,7 +3173,7 @@
if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
- mbedtls_mpi_init( &Y );
+ mbedtls_mpi_init_mempool( &Y );
n = BITS_TO_LIMBS( nbits );
@@ -3243,8 +3291,10 @@
int ret, i;
mbedtls_mpi A, E, N, X, Y, U, V;
- mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
- mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
+ mbedtls_mpi_init_mempool( &A ); mbedtls_mpi_init_mempool( &E );
+ mbedtls_mpi_init_mempool( &N ); mbedtls_mpi_init_mempool( &X );
+ mbedtls_mpi_init_mempool( &Y ); mbedtls_mpi_init_mempool( &U );
+ mbedtls_mpi_init_mempool( &V );
MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
"EFE021C2645FD1DC586E69184AF4A31E" \
diff --git a/lib/libmbedtls/mbedtls/library/ecp.c b/lib/libmbedtls/mbedtls/library/ecp.c
index ca49f99..9baafc3 100644
--- a/lib/libmbedtls/mbedtls/library/ecp.c
+++ b/lib/libmbedtls/mbedtls/library/ecp.c
@@ -2937,7 +2937,7 @@
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
-#define ECP_MPI_INIT(s, n, p) {s, (n), (mbedtls_mpi_uint *)(p)}
+#define ECP_MPI_INIT(s, n, p) {s, 0, (n), (mbedtls_mpi_uint *)(p)}
#define ECP_MPI_INIT_ARRAY(x) \
ECP_MPI_INIT(1, sizeof(x) / sizeof(mbedtls_mpi_uint), x)
/*