mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-08-01 03:34:39 +02:00
Normal math speed-up to not allocate on mp_int and defer until mp_grow. Added memory tracker support to ./tests/unit.test. Fix memory leak with curve cache enabled, by adding to wolfSSL_Cleanup.
This commit is contained in:
@@ -8311,8 +8311,13 @@ int wolfSSL_Cleanup(void)
|
||||
if (wc_FreeMutex(&count_mutex) != 0)
|
||||
ret = BAD_MUTEX_E;
|
||||
|
||||
#if defined(HAVE_ECC) && defined(FP_ECC)
|
||||
wc_ecc_fp_free();
|
||||
#ifdef HAVE_ECC
|
||||
#ifdef FP_ECC
|
||||
wc_ecc_fp_free();
|
||||
#endif
|
||||
#ifdef ECC_CACHE_CURVE
|
||||
wc_ecc_curve_cache_free();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if (wolfCrypt_Cleanup() != 0) {
|
||||
|
@@ -51,6 +51,10 @@ int unit_test(int argc, char** argv)
|
||||
(void)argv;
|
||||
printf("starting unit tests...\n");
|
||||
|
||||
#if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY)
|
||||
InitMemoryTracker();
|
||||
#endif
|
||||
|
||||
#if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
|
||||
wolfSSL_Debugging_ON();
|
||||
#endif
|
||||
@@ -85,6 +89,10 @@ int unit_test(int argc, char** argv)
|
||||
err_sys("Failed to free netRandom context");
|
||||
#endif /* HAVE_WNR */
|
||||
|
||||
#if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY)
|
||||
ShowMemoryTracker();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@@ -2323,12 +2323,6 @@ ecc_point* wc_ecc_new_point_h(void* heap)
|
||||
}
|
||||
XMEMSET(p, 0, sizeof(ecc_point));
|
||||
|
||||
#ifndef USE_FAST_MATH
|
||||
p->x->dp = NULL;
|
||||
p->y->dp = NULL;
|
||||
p->z->dp = NULL;
|
||||
#endif
|
||||
|
||||
#ifndef ALT_ECC_SIZE
|
||||
if (mp_init_multi(p->x, p->y, p->z, NULL, NULL, NULL) != MP_OKAY) {
|
||||
XFREE(p, heap, DYNAMIC_TYPE_ECC);
|
||||
@@ -3018,17 +3012,20 @@ int wc_ecc_init_ex(ecc_key* key, void* heap, int devId)
|
||||
}
|
||||
#else
|
||||
#ifdef ALT_ECC_SIZE
|
||||
if (mp_init(&key->k) != MP_OKAY) {
|
||||
return MEMORY_E;
|
||||
}
|
||||
|
||||
key->pubkey.x = (mp_int*)&key->pubkey.xyz[0];
|
||||
key->pubkey.y = (mp_int*)&key->pubkey.xyz[1];
|
||||
key->pubkey.z = (mp_int*)&key->pubkey.xyz[2];
|
||||
alt_fp_init(key->pubkey.x);
|
||||
alt_fp_init(key->pubkey.y);
|
||||
alt_fp_init(key->pubkey.z);
|
||||
ret = mp_init(&key->k);
|
||||
#else
|
||||
ret = mp_init_multi(&key->k, key->pubkey.x, key->pubkey.y, key->pubkey.z,
|
||||
NULL, NULL);
|
||||
#endif /* ALT_ECC_SIZE */
|
||||
if (ret != MP_OKAY) {
|
||||
return MEMORY_E;
|
||||
}
|
||||
#endif /* WOLFSSL_ATECC508A */
|
||||
|
||||
#ifdef WOLFSSL_HEAP_TEST
|
||||
|
@@ -142,28 +142,17 @@ int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e,
|
||||
/* init a new mp_int */
|
||||
int mp_init (mp_int * a)
|
||||
{
|
||||
int i;
|
||||
|
||||
/* Safeguard against passing in a null pointer */
|
||||
if (a == NULL)
|
||||
return MP_VAL;
|
||||
|
||||
/* allocate memory required and clear it */
|
||||
a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * MP_PREC, 0,
|
||||
DYNAMIC_TYPE_BIGINT);
|
||||
if (a->dp == NULL) {
|
||||
return MP_MEM;
|
||||
}
|
||||
|
||||
/* set the digits to zero */
|
||||
for (i = 0; i < MP_PREC; i++) {
|
||||
a->dp[i] = 0;
|
||||
}
|
||||
/* defer memory allocation */
|
||||
a->dp = NULL;
|
||||
|
||||
/* set the used to zero, allocated digits to the default precision
|
||||
* and sign to positive */
|
||||
a->used = 0;
|
||||
a->alloc = MP_PREC;
|
||||
a->alloc = 0;
|
||||
a->sign = MP_ZPOS;
|
||||
|
||||
return MP_OKAY;
|
||||
@@ -328,7 +317,7 @@ int mp_copy (mp_int * a, mp_int * b)
|
||||
}
|
||||
|
||||
/* grow dest */
|
||||
if (b->alloc < a->used) {
|
||||
if (b->alloc < a->used || b->alloc == 0) {
|
||||
if ((res = mp_grow (b, a->used)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
@@ -371,7 +360,7 @@ int mp_grow (mp_int * a, int size)
|
||||
mp_digit *tmp;
|
||||
|
||||
/* if the alloc size is smaller alloc more ram */
|
||||
if (a->alloc < size) {
|
||||
if (a->alloc < size || size == 0) {
|
||||
/* ensure there are always at least MP_PREC digits extra on top */
|
||||
size += (MP_PREC * 2) - (size % MP_PREC);
|
||||
|
||||
@@ -381,7 +370,7 @@ int mp_grow (mp_int * a, int size)
|
||||
* in case the operation failed we don't want
|
||||
* to overwrite the dp member of a.
|
||||
*/
|
||||
tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * size, 0,
|
||||
tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * size, NULL,
|
||||
DYNAMIC_TYPE_BIGINT);
|
||||
if (tmp == NULL) {
|
||||
/* reallocation failed but "a" is still valid [can be freed] */
|
||||
@@ -1313,6 +1302,7 @@ int mp_cmp_d(mp_int * a, mp_digit b)
|
||||
void mp_set (mp_int * a, mp_digit b)
|
||||
{
|
||||
mp_zero (a);
|
||||
mp_grow(a, 1);
|
||||
a->dp[0] = (mp_digit)(b & MP_MASK);
|
||||
a->used = (a->dp[0] != 0) ? 1 : 0;
|
||||
}
|
||||
|
@@ -214,13 +214,6 @@ int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId)
|
||||
else
|
||||
#endif
|
||||
{
|
||||
/* For normal math defer the memory allocations */
|
||||
#ifndef USE_FAST_MATH
|
||||
key->n.dp = key->e.dp = 0; /* public alloc parts */
|
||||
key->d.dp = key->p.dp = 0; /* private alloc parts */
|
||||
key->q.dp = key->dP.dp = 0;
|
||||
key->u.dp = key->dQ.dp = 0;
|
||||
#else
|
||||
mp_init(&key->n);
|
||||
mp_init(&key->e);
|
||||
mp_init(&key->d);
|
||||
@@ -229,7 +222,6 @@ int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId)
|
||||
mp_init(&key->dP);
|
||||
mp_init(&key->dQ);
|
||||
mp_init(&key->u);
|
||||
#endif /* USE_FAST_MATH */
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -266,6 +258,12 @@ int wc_FreeRsaKey(RsaKey* key)
|
||||
mp_forcezero(&key->p);
|
||||
mp_forcezero(&key->d);
|
||||
}
|
||||
mp_clear(&key->u);
|
||||
mp_clear(&key->dQ);
|
||||
mp_clear(&key->dP);
|
||||
mp_clear(&key->q);
|
||||
mp_clear(&key->p);
|
||||
mp_clear(&key->d);
|
||||
mp_clear(&key->e);
|
||||
mp_clear(&key->n);
|
||||
}
|
||||
@@ -907,7 +905,7 @@ static int wc_RsaFunctionSync(const byte* in, word32 inLen, byte* out,
|
||||
*/
|
||||
#define RET_ERR(ret, r, e) \
|
||||
((ret) | (COND_N((ret) == 0, COND_N((r) != MP_OKAY, (e)))))
|
||||
|
||||
|
||||
{ /* tmpa/b scope */
|
||||
mp_int tmpa, tmpb;
|
||||
int r;
|
||||
|
Reference in New Issue
Block a user