@@ -64,6 +64,8 @@ struct xmalloc_slot {
6464
6565#if defined(WOLFBOOT_SIGN_ECC256 ) || defined(WOLFBOOT_SIGN_ECC384 ) || defined(WOLFBOOT_SIGN_ECC521 )
6666
67+ #include <wolfssl/wolfcrypt/ecc.h>
68+
6769#ifndef USE_FAST_MATH
6870 /* SP MATH */
6971 #ifdef WOLFBOOT_SIGN_ECC256
@@ -113,6 +115,15 @@ struct xmalloc_slot {
113115 #define MP_MONTGOMERY_SIZE (sizeof(int64_t) * 2 * 12)
114116 #endif
115117 #endif /* WOLFBOOT_SIGN_ECC521 */
118+ #ifdef WOLFSSL_SP_MATH_ALL
119+ #define MP_CURVE_SPECS_ALL_SIZE (MP_CURVE_SPECS_SIZE * 5)
120+ static uint8_t mp_curve_specs_all [MP_CURVE_SPECS_ALL_SIZE ];
121+ static uint8_t ecc_point_dyn_0 [sizeof (ecc_point )];
122+ static uint8_t ecc_point_dyn_1 [sizeof (ecc_point )];
123+ static uint8_t ecc_point_dyn_2 [sizeof (ecc_point )];
124+ static uint8_t ecc_point_dyn_3 [sizeof (ecc_point )];
125+ static uint8_t ecc_point_dyn_4 [sizeof (ecc_point )];
126+ #endif
116127 #ifndef WC_NO_CACHE_RESISTANT
117128 static uint8_t mp_points_3 [MP_POINT_SIZE ];
118129 #endif
@@ -203,6 +214,14 @@ static struct xmalloc_slot xmalloc_pool[] = {
203214 { (uint8_t * )sha_block , HASH_BLOCK_SIZE * sizeof (uint32_t ), 0 },
204215#endif
205216 { (uint8_t * )mp_curve_specs , MP_CURVE_SPECS_SIZE , 0 },
217+ #ifdef WOLFSSL_SP_MATH_ALL
218+ { (uint8_t * )mp_curve_specs_all , MP_CURVE_SPECS_ALL_SIZE , 0 },
219+ { (uint8_t * )ecc_point_dyn_0 , sizeof (ecc_point ), 0 },
220+ { (uint8_t * )ecc_point_dyn_1 , sizeof (ecc_point ), 0 },
221+ { (uint8_t * )ecc_point_dyn_2 , sizeof (ecc_point ), 0 },
222+ { (uint8_t * )ecc_point_dyn_3 , sizeof (ecc_point ), 0 },
223+ { (uint8_t * )ecc_point_dyn_4 , sizeof (ecc_point ), 0 },
224+ #endif
206225#ifndef USE_FAST_MATH
207226 { (uint8_t * )mp_points_0 , MP_POINT_SIZE * 2 , 0 },
208227 #ifdef WOLFSSL_SP_ARM_CORTEX_M_ASM
@@ -304,6 +323,10 @@ static uint8_t asncheck_buf[ASNCHECK_BUF_SIZE];
304323#ifndef USE_FAST_MATH
305324 #ifdef WOLFBOOT_SIGN_RSA2048
306325 #define MP_SCHEME "SP RSA2048"
326+ #define MP_INT_DYNAMIC_SIZE MP_INT_SIZEOF(MP_BITS_CNT(2048))
327+ #define MP_BIGINT_MODEXP_SIZE (MP_INT_DYNAMIC_SIZE * 4)
328+ #define MP_BIGINT_DIV_SIZE (MP_INT_DYNAMIC_SIZE * 3)
329+ #define MP_BIGINT_MUL_SIZE (MP_INT_DYNAMIC_SIZE - sizeof(sp_int_digit))
307330 #ifdef WOLFSSL_SP_ARM_CORTEX_M_ASM
308331 #define MPDIGIT_BUF0_SIZE (MP_DIGIT_SIZE * 64 * 5)
309332 #else
@@ -313,6 +336,10 @@ static uint8_t asncheck_buf[ASNCHECK_BUF_SIZE];
313336 #endif
314337 #elif defined WOLFBOOT_SIGN_RSA3072
315338 #define MP_SCHEME "SP RSA3072"
339+ #define MP_INT_DYNAMIC_SIZE MP_INT_SIZEOF(MP_BITS_CNT(3072))
340+ #define MP_BIGINT_MODEXP_SIZE (MP_INT_DYNAMIC_SIZE * 4)
341+ #define MP_BIGINT_DIV_SIZE (MP_INT_DYNAMIC_SIZE * 3)
342+ #define MP_BIGINT_MUL_SIZE (MP_INT_DYNAMIC_SIZE - sizeof(sp_int_digit))
316343 #ifdef WOLFSSL_SP_ARM_CORTEX_M_ASM
317344 #define MPDIGIT_BUF0_SIZE (MP_DIGIT_SIZE * 96 * 5)
318345 #else
@@ -322,6 +349,10 @@ static uint8_t asncheck_buf[ASNCHECK_BUF_SIZE];
322349 #endif
323350 #else
324351 #define MP_SCHEME "SP RSA4096"
352+ #define MP_INT_DYNAMIC_SIZE MP_INT_SIZEOF(MP_BITS_CNT(4096))
353+ #define MP_BIGINT_MODEXP_SIZE (MP_INT_DYNAMIC_SIZE * 4)
354+ #define MP_BIGINT_DIV_SIZE (MP_INT_DYNAMIC_SIZE * 3)
355+ #define MP_BIGINT_MUL_SIZE (MP_INT_DYNAMIC_SIZE - sizeof(sp_int_digit))
325356 #ifdef WOLFSSL_SP_ARM_CORTEX_M_ASM
326357 #define MPDIGIT_BUF0_SIZE (MP_DIGIT_SIZE * 128 * 5)
327358 #else
@@ -330,12 +361,20 @@ static uint8_t asncheck_buf[ASNCHECK_BUF_SIZE];
330361 static uint8_t mp_digit_buf1 [MPDIGIT_BUF1_SIZE ];
331362 #endif
332363 #endif
364+ static uint8_t mp_int_dynamic [MP_INT_DYNAMIC_SIZE ];
365+ static uint8_t mp_bigint_modexp [MP_BIGINT_MODEXP_SIZE ];
366+ static uint8_t mp_bigint_div [MP_BIGINT_DIV_SIZE ];
367+ static uint8_t mp_bigint_mul [MP_BIGINT_MUL_SIZE ];
333368 static uint8_t mp_digit_buf0 [MPDIGIT_BUF0_SIZE ];
334369 static struct xmalloc_slot xmalloc_pool [] = {
335370 #if defined(WOLFBOOT_HASH_SHA256 ) || defined(WOLFBOOT_HASH_SHA384 )
336371 { (uint8_t * )sha_block , HASH_BLOCK_SIZE * sizeof (uint32_t ), 0 },
337372 #endif
338373 { asncheck_buf , ASNCHECK_BUF_SIZE , 0 },
374+ { mp_int_dynamic , MP_INT_DYNAMIC_SIZE , 0 },
375+ { mp_bigint_modexp , MP_BIGINT_MODEXP_SIZE , 0 },
376+ { mp_bigint_div , MP_BIGINT_DIV_SIZE , 0 },
377+ { mp_bigint_mul , MP_BIGINT_MUL_SIZE , 0 },
339378 { mp_digit_buf0 , MPDIGIT_BUF0_SIZE , 0 },
340379 #ifndef WOLFSSL_SP_ARM_CORTEX_M_ASM
341380 { mp_digit_buf1 , MPDIGIT_BUF1_SIZE , 0 },
@@ -400,6 +439,7 @@ static struct xmalloc_slot xmalloc_pool[] = {
400439void * XMALLOC (size_t n , void * heap , int type )
401440{
402441 int i = 0 ;
442+ int best = -1 ;
403443
404444#ifdef WOLFBOOT_DEBUG_MALLOC
405445 static int detect_init = 0 ;
@@ -412,16 +452,22 @@ void* XMALLOC(size_t n, void* heap, int type)
412452#endif
413453
414454 while (xmalloc_pool [i ].addr ) {
415- if ((n == xmalloc_pool [i ].size ) &&
416- (xmalloc_pool [i ].in_use == 0 )) {
417- xmalloc_pool [i ].in_use ++ ;
418- #ifdef WOLFBOOT_DEBUG_MALLOC
419- printf (" Index %d, Ptr %p\n" , i , xmalloc_pool [i ].addr );
420- #endif
421- return xmalloc_pool [i ].addr ;
455+ if ((xmalloc_pool [i ].in_use == 0 ) && (n <= xmalloc_pool [i ].size )) {
456+ if ((best < 0 ) || (xmalloc_pool [i ].size < xmalloc_pool [best ].size )) {
457+ best = i ;
458+ }
422459 }
423460 i ++ ;
424461 }
462+
463+ if (best >= 0 ) {
464+ xmalloc_pool [best ].in_use ++ ;
465+ #ifdef WOLFBOOT_DEBUG_MALLOC
466+ printf (" Index %d, Ptr %p\n" , best , xmalloc_pool [best ].addr );
467+ #endif
468+ return xmalloc_pool [best ].addr ;
469+ }
470+
425471 (void )heap ;
426472 (void )type ;
427473#ifdef WOLFBOOT_DEBUG_MALLOC
0 commit comments