mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-08-02 12:14:38 +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)
|
if (wc_FreeMutex(&count_mutex) != 0)
|
||||||
ret = BAD_MUTEX_E;
|
ret = BAD_MUTEX_E;
|
||||||
|
|
||||||
#if defined(HAVE_ECC) && defined(FP_ECC)
|
#ifdef HAVE_ECC
|
||||||
|
#ifdef FP_ECC
|
||||||
wc_ecc_fp_free();
|
wc_ecc_fp_free();
|
||||||
|
#endif
|
||||||
|
#ifdef ECC_CACHE_CURVE
|
||||||
|
wc_ecc_curve_cache_free();
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (wolfCrypt_Cleanup() != 0) {
|
if (wolfCrypt_Cleanup() != 0) {
|
||||||
|
@@ -51,6 +51,10 @@ int unit_test(int argc, char** argv)
|
|||||||
(void)argv;
|
(void)argv;
|
||||||
printf("starting unit tests...\n");
|
printf("starting unit tests...\n");
|
||||||
|
|
||||||
|
#if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY)
|
||||||
|
InitMemoryTracker();
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
|
#if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
|
||||||
wolfSSL_Debugging_ON();
|
wolfSSL_Debugging_ON();
|
||||||
#endif
|
#endif
|
||||||
@@ -85,6 +89,10 @@ int unit_test(int argc, char** argv)
|
|||||||
err_sys("Failed to free netRandom context");
|
err_sys("Failed to free netRandom context");
|
||||||
#endif /* HAVE_WNR */
|
#endif /* HAVE_WNR */
|
||||||
|
|
||||||
|
#if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY)
|
||||||
|
ShowMemoryTracker();
|
||||||
|
#endif
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -2323,12 +2323,6 @@ ecc_point* wc_ecc_new_point_h(void* heap)
|
|||||||
}
|
}
|
||||||
XMEMSET(p, 0, sizeof(ecc_point));
|
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
|
#ifndef ALT_ECC_SIZE
|
||||||
if (mp_init_multi(p->x, p->y, p->z, NULL, NULL, NULL) != MP_OKAY) {
|
if (mp_init_multi(p->x, p->y, p->z, NULL, NULL, NULL) != MP_OKAY) {
|
||||||
XFREE(p, heap, DYNAMIC_TYPE_ECC);
|
XFREE(p, heap, DYNAMIC_TYPE_ECC);
|
||||||
@@ -3018,17 +3012,20 @@ int wc_ecc_init_ex(ecc_key* key, void* heap, int devId)
|
|||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
#ifdef ALT_ECC_SIZE
|
#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.x = (mp_int*)&key->pubkey.xyz[0];
|
||||||
key->pubkey.y = (mp_int*)&key->pubkey.xyz[1];
|
key->pubkey.y = (mp_int*)&key->pubkey.xyz[1];
|
||||||
key->pubkey.z = (mp_int*)&key->pubkey.xyz[2];
|
key->pubkey.z = (mp_int*)&key->pubkey.xyz[2];
|
||||||
alt_fp_init(key->pubkey.x);
|
alt_fp_init(key->pubkey.x);
|
||||||
alt_fp_init(key->pubkey.y);
|
alt_fp_init(key->pubkey.y);
|
||||||
alt_fp_init(key->pubkey.z);
|
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 */
|
#endif /* ALT_ECC_SIZE */
|
||||||
|
if (ret != MP_OKAY) {
|
||||||
|
return MEMORY_E;
|
||||||
|
}
|
||||||
#endif /* WOLFSSL_ATECC508A */
|
#endif /* WOLFSSL_ATECC508A */
|
||||||
|
|
||||||
#ifdef WOLFSSL_HEAP_TEST
|
#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 */
|
/* init a new mp_int */
|
||||||
int mp_init (mp_int * a)
|
int mp_init (mp_int * a)
|
||||||
{
|
{
|
||||||
int i;
|
|
||||||
|
|
||||||
/* Safeguard against passing in a null pointer */
|
/* Safeguard against passing in a null pointer */
|
||||||
if (a == NULL)
|
if (a == NULL)
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
|
|
||||||
/* allocate memory required and clear it */
|
/* defer memory allocation */
|
||||||
a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * MP_PREC, 0,
|
a->dp = NULL;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* set the used to zero, allocated digits to the default precision
|
/* set the used to zero, allocated digits to the default precision
|
||||||
* and sign to positive */
|
* and sign to positive */
|
||||||
a->used = 0;
|
a->used = 0;
|
||||||
a->alloc = MP_PREC;
|
a->alloc = 0;
|
||||||
a->sign = MP_ZPOS;
|
a->sign = MP_ZPOS;
|
||||||
|
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
@@ -328,7 +317,7 @@ int mp_copy (mp_int * a, mp_int * b)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* grow dest */
|
/* grow dest */
|
||||||
if (b->alloc < a->used) {
|
if (b->alloc < a->used || b->alloc == 0) {
|
||||||
if ((res = mp_grow (b, a->used)) != MP_OKAY) {
|
if ((res = mp_grow (b, a->used)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -371,7 +360,7 @@ int mp_grow (mp_int * a, int size)
|
|||||||
mp_digit *tmp;
|
mp_digit *tmp;
|
||||||
|
|
||||||
/* if the alloc size is smaller alloc more ram */
|
/* 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 */
|
/* ensure there are always at least MP_PREC digits extra on top */
|
||||||
size += (MP_PREC * 2) - (size % MP_PREC);
|
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
|
* in case the operation failed we don't want
|
||||||
* to overwrite the dp member of a.
|
* 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);
|
DYNAMIC_TYPE_BIGINT);
|
||||||
if (tmp == NULL) {
|
if (tmp == NULL) {
|
||||||
/* reallocation failed but "a" is still valid [can be freed] */
|
/* 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)
|
void mp_set (mp_int * a, mp_digit b)
|
||||||
{
|
{
|
||||||
mp_zero (a);
|
mp_zero (a);
|
||||||
|
mp_grow(a, 1);
|
||||||
a->dp[0] = (mp_digit)(b & MP_MASK);
|
a->dp[0] = (mp_digit)(b & MP_MASK);
|
||||||
a->used = (a->dp[0] != 0) ? 1 : 0;
|
a->used = (a->dp[0] != 0) ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
@@ -214,13 +214,6 @@ int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId)
|
|||||||
else
|
else
|
||||||
#endif
|
#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->n);
|
||||||
mp_init(&key->e);
|
mp_init(&key->e);
|
||||||
mp_init(&key->d);
|
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->dP);
|
||||||
mp_init(&key->dQ);
|
mp_init(&key->dQ);
|
||||||
mp_init(&key->u);
|
mp_init(&key->u);
|
||||||
#endif /* USE_FAST_MATH */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@@ -266,6 +258,12 @@ int wc_FreeRsaKey(RsaKey* key)
|
|||||||
mp_forcezero(&key->p);
|
mp_forcezero(&key->p);
|
||||||
mp_forcezero(&key->d);
|
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->e);
|
||||||
mp_clear(&key->n);
|
mp_clear(&key->n);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user