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>
diff --git a/lib/libmbedtls/mbedtls/library/bignum.c b/lib/libmbedtls/mbedtls/library/bignum.c
index 674026b..2e45498 100644
--- a/lib/libmbedtls/mbedtls/library/bignum.c
+++ b/lib/libmbedtls/mbedtls/library/bignum.c
@@ -59,6 +59,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 )                                           \
@@ -77,6 +79,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 )
 {
@@ -86,15 +90,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
  */
@@ -106,7 +121,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;
@@ -127,14 +145,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;
@@ -169,14 +201,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, 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, 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;
@@ -467,7 +513,7 @@
     if( radix < 2 || radix > 16 )
         return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
 
-    mbedtls_mpi_init( &T );
+    mbedtls_mpi_init_mempool( &T );
 
     slen = strlen( s );
 
@@ -587,7 +633,7 @@
     }
 
     p = buf;
-    mbedtls_mpi_init( &T );
+    mbedtls_mpi_init_mempool( &T );
 
     if( X->s == -1 )
         *p++ = '-';
@@ -1065,7 +1111,7 @@
     if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
         return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
 
-    mbedtls_mpi_init( &TB );
+    mbedtls_mpi_init_mempool( &TB );
 
     if( X == B )
     {
@@ -1286,7 +1332,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; }
@@ -1443,8 +1489,9 @@
     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( &T2 );
+    mbedtls_mpi_init_mempool( &X ); mbedtls_mpi_init_mempool( &Y );
+    mbedtls_mpi_init_mempool( &Z ); mbedtls_mpi_init_mempool( &T1 );
+    mbedtls_mpi_init_mempool( &T2 );
 
     if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
     {
@@ -1756,8 +1803,8 @@
      * Init temps and window size
      */
     mbedtls_mpi_montg_init( &mm, N );
-    mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
-    mbedtls_mpi_init( &Apos );
+    mbedtls_mpi_init_mempool( &RR ); mbedtls_mpi_init_mempool( &T );
+    mbedtls_mpi_init_mempool( &Apos );
     memset( W, 0, sizeof( W ) );
 
     i = mbedtls_mpi_bitlen( E );
@@ -1954,7 +2001,8 @@
     MPI_VALIDATE_RET( A != NULL );
     MPI_VALIDATE_RET( B != NULL );
 
-    mbedtls_mpi_init( &TG ); mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
+    mbedtls_mpi_init_mempool( &TG ); 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 ) );
@@ -2038,9 +2086,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 ) );
 
@@ -2196,9 +2246,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
@@ -2360,7 +2410,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 );
 
@@ -2478,8 +2528,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" \