Merge pull request #7490 from dgarske/ecc_curvecache_nomalloc

Support for ECC_CACHE_CURVE with no malloc
This commit is contained in:
Sean Parkinson
2024-05-02 07:17:01 +10:00
committed by GitHub
2 changed files with 17 additions and 1 deletions

View File

@ -1495,7 +1495,11 @@ static int xil_mpi_import(mp_int *mpi,
#ifdef ECC_CACHE_CURVE #ifdef ECC_CACHE_CURVE
/* cache (mp_int) of the curve parameters */ /* cache (mp_int) of the curve parameters */
#ifdef WOLFSSL_NO_MALLOC
static ecc_curve_spec ecc_curve_spec_cache[ECC_SET_COUNT];
#else
static ecc_curve_spec* ecc_curve_spec_cache[ECC_SET_COUNT]; static ecc_curve_spec* ecc_curve_spec_cache[ECC_SET_COUNT];
#endif
#ifndef SINGLE_THREADED #ifndef SINGLE_THREADED
static wolfSSL_Mutex ecc_curve_cache_mutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(ecc_curve_cache_mutex); static wolfSSL_Mutex ecc_curve_cache_mutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(ecc_curve_cache_mutex);
#endif #endif
@ -1675,6 +1679,9 @@ static int wc_ecc_curve_load(const ecc_set_type* dp, ecc_curve_spec** pCurve,
} }
#endif #endif
#ifdef WOLFSSL_NO_MALLOC
curve = &ecc_curve_spec_cache[x];
#else
/* make sure cache has been allocated */ /* make sure cache has been allocated */
if (ecc_curve_spec_cache[x] == NULL if (ecc_curve_spec_cache[x] == NULL
#ifdef WOLFSSL_CUSTOM_CURVES #ifdef WOLFSSL_CUSTOM_CURVES
@ -1701,6 +1708,8 @@ static int wc_ecc_curve_load(const ecc_set_type* dp, ecc_curve_spec** pCurve,
else { else {
curve = ecc_curve_spec_cache[x]; curve = ecc_curve_spec_cache[x];
} }
#endif /* WOLFSSL_NO_MALLOC */
/* return new or cached curve */ /* return new or cached curve */
*pCurve = curve; *pCurve = curve;
#else #else
@ -1780,11 +1789,16 @@ void wc_ecc_curve_cache_free(void)
/* free all ECC curve caches */ /* free all ECC curve caches */
for (x = 0; x < (int)ECC_SET_COUNT; x++) { for (x = 0; x < (int)ECC_SET_COUNT; x++) {
#ifdef WOLFSSL_NO_MALLOC
wc_ecc_curve_cache_free_spec(&ecc_curve_spec_cache[x]);
XMEMSET(&ecc_curve_spec_cache[x], 0, sizeof(ecc_curve_spec_cache[x]));
#else
if (ecc_curve_spec_cache[x]) { if (ecc_curve_spec_cache[x]) {
wc_ecc_curve_cache_free_spec(ecc_curve_spec_cache[x]); wc_ecc_curve_cache_free_spec(ecc_curve_spec_cache[x]);
XFREE(ecc_curve_spec_cache[x], NULL, DYNAMIC_TYPE_ECC); XFREE(ecc_curve_spec_cache[x], NULL, DYNAMIC_TYPE_ECC);
ecc_curve_spec_cache[x] = NULL; ecc_curve_spec_cache[x] = NULL;
} }
#endif /* WOLFSSL_NO_MALLOC */
} }
#if defined(ECC_CACHE_CURVE) && !defined(SINGLE_THREADED) && \ #if defined(ECC_CACHE_CURVE) && !defined(SINGLE_THREADED) && \

View File

@ -2755,7 +2755,9 @@ extern void uITRON4_free(void *p) ;
#endif #endif
/* Enable ECC_CACHE_CURVE for ASYNC */ /* Enable ECC_CACHE_CURVE for ASYNC */
#if !defined(ECC_CACHE_CURVE) #if !defined(ECC_CACHE_CURVE) && !defined(NO_ECC_CACHE_CURVE)
/* Enabled by default for increased async performance,
* but not required */
#define ECC_CACHE_CURVE #define ECC_CACHE_CURVE
#endif #endif
#endif /* WOLFSSL_ASYNC_CRYPT */ #endif /* WOLFSSL_ASYNC_CRYPT */