@@ -373,6 +373,14 @@ ECC Curve Sizes:
373373#define MAX_ECC_BITS_USE (MAX_ECC_BITS + MAX_ECC_BITS_EXTRA)
374374#endif
375375
376+ /* Size sp_int curve parameters for the largest curve, up to SP_INT_DIGITS */
377+ #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
378+ #define ECC_SPEC_INTS_SIZED
379+ #define ECC_SPEC_INT_CNT \
380+ ((MP_BITS_CNT(MAX_ECC_BITS_USE) < SP_INT_DIGITS) ? \
381+ MP_BITS_CNT(MAX_ECC_BITS_USE) : (unsigned int)SP_INT_DIGITS)
382+ #endif
383+
376384/* MAX_ECC_BITS_EXTRA (ecc.h) is the one bit the builds whose order can be a bit
377385 * greater than the prime need, and the ceiling is sized from the same macro. */
378386#define ECC_KEY_MAX_BITS(key) \
@@ -1617,7 +1625,7 @@ typedef struct ecc_curve_spec {
16171625 mp_int Gx_lcl;
16181626 mp_int Gy_lcl;
16191627#else
1620- #ifdef WOLFSSL_SP_MATH_ALL
1628+ #ifdef ECC_SPEC_INTS_SIZED
16211629 unsigned char* spec_ints;
16221630#else
16231631 mp_int* spec_ints;
@@ -1704,7 +1712,7 @@ static int xil_mpi_import(mp_int *mpi,
17041712 #define ALLOC_CURVE_SPECS(intcount, err) (err) = MP_OKAY
17051713 #define FREE_CURVE_SPECS() WC_DO_NOTHING
17061714#elif defined(WOLFSSL_SMALL_STACK)
1707- #ifdef WOLFSSL_SP_MATH_ALL
1715+ #ifdef ECC_SPEC_INTS_SIZED
17081716 #define DECLARE_CURVE_SPECS(intcount) \
17091717 unsigned char* spec_ints = NULL; \
17101718 ecc_curve_spec curve_lcl; \
@@ -1714,8 +1722,8 @@ static int xil_mpi_import(mp_int *mpi,
17141722
17151723 #define ALLOC_CURVE_SPECS(intcount, err) \
17161724 do { \
1717- spec_ints = (unsigned char*)XMALLOC(MP_INT_SIZEOF(MP_BITS_CNT( \
1718- MAX_ECC_BITS_USE)) * (intcount), NULL, \
1725+ spec_ints = (unsigned char*)XMALLOC(MP_INT_SIZEOF( \
1726+ ECC_SPEC_INT_CNT) * (intcount), NULL, \
17191727 DYNAMIC_TYPE_ECC); \
17201728 if (spec_ints == NULL) \
17211729 (err) = MEMORY_E; \
@@ -1747,14 +1755,14 @@ static int xil_mpi_import(mp_int *mpi,
17471755 #define FREE_CURVE_SPECS() \
17481756 XFREE(spec_ints, NULL, DYNAMIC_TYPE_ECC)
17491757#else
1750- #ifdef WOLFSSL_SP_MATH_ALL
1758+ #ifdef ECC_SPEC_INTS_SIZED
17511759 #define DECLARE_CURVE_SPECS(intcount) \
1752- unsigned char spec_ints[MP_INT_SIZEOF(MP_BITS_CNT( \
1753- MAX_ECC_BITS_USE)) * (intcount)]; \
1760+ sp_int_digit spec_ints[MP_INT_SIZEOF_DIGITS(ECC_SPEC_INT_CNT) * \
1761+ (intcount)]; \
17541762 ecc_curve_spec curve_lcl; \
17551763 ecc_curve_spec* curve = &curve_lcl; \
17561764 XMEMSET(curve, 0, sizeof(ecc_curve_spec)); \
1757- curve->spec_ints = spec_ints; \
1765+ curve->spec_ints = (unsigned char*) spec_ints; \
17581766 curve->spec_count = (intcount)
17591767#else
17601768 #define DECLARE_CURVE_SPECS(intcount) \
@@ -1830,16 +1838,16 @@ static int wc_ecc_curve_cache_load_item(ecc_curve_spec* curve, const char* src,
18301838 WOLFSSL_MSG("Invalid DECLARE_CURVE_SPECS count");
18311839 return ECC_BAD_ARG_E;
18321840 }
1833- #ifdef WOLFSSL_SP_MATH_ALL
1834- *dst = (mp_int*)(curve->spec_ints + MP_INT_SIZEOF(MP_BITS_CNT(
1835- MAX_ECC_BITS_USE)) * curve->spec_use++);
1841+ #ifdef ECC_SPEC_INTS_SIZED
1842+ *dst = (mp_int*)(curve->spec_ints + MP_INT_SIZEOF(ECC_SPEC_INT_CNT) *
1843+ curve->spec_use++);
18361844#else
18371845 *dst = &curve->spec_ints[curve->spec_use++];
18381846#endif
18391847#endif
18401848
1841- #ifdef WOLFSSL_SP_MATH_ALL
1842- err = mp_init_size(*dst, MP_BITS_CNT(MAX_ECC_BITS_USE) );
1849+ #ifdef ECC_SPEC_INTS_SIZED
1850+ err = mp_init_size(*dst, ECC_SPEC_INT_CNT );
18431851#else
18441852 err = mp_init(*dst);
18451853#endif
0 commit comments