Skip to content

Commit

Permalink
Fix possible leak in ED25519 because the isAllocated bit was being …
Browse files Browse the repository at this point in the history
…cleared before check.
  • Loading branch information
dgarske committed Aug 23, 2024
1 parent 315cc9c commit 67f7452
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
10 changes: 8 additions & 2 deletions wolfcrypt/src/curve25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -707,9 +707,15 @@ int wc_curve25519_init(curve25519_key* key)
/* Clean the memory of a key */
void wc_curve25519_free(curve25519_key* key)
{
int isAllocated = 0;
void* heap;

if (key == NULL)
return;

isAllocated = key->isAllocated;
heap = key->heap;

#ifdef WOLFSSL_SE050
se050_curve25519_free_key(key);
#endif
Expand All @@ -719,12 +725,12 @@ void wc_curve25519_free(curve25519_key* key)
XMEMSET(&key->p, 0, sizeof(key->p));
key->pubSet = 0;
key->privSet = 0;

#ifdef WOLFSSL_CHECK_MEM_ZERO
wc_MemZero_Check(key, sizeof(curve25519_key));
#endif

if (key->isAllocated) {
void* heap = key->heap;
if (isAllocated) {
XFREE(key, heap, DYNAMIC_TYPE_CURVE25519);
(void)heap;
}
Expand Down
9 changes: 7 additions & 2 deletions wolfcrypt/src/ed25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -1023,9 +1023,15 @@ int wc_ed25519_init(ed25519_key* key)
/* clear memory of key */
void wc_ed25519_free(ed25519_key* key)
{
int isAllocated = 0;
void* heap;

if (key == NULL)
return;

isAllocated = key->isAllocated;
heap = key->heap;

#ifdef WOLFSSL_ED25519_PERSISTENT_SHA
ed25519_hash_free(key, &key->sha);
#endif
Expand All @@ -1039,8 +1045,7 @@ void wc_ed25519_free(ed25519_key* key)
wc_MemZero_Check(key, sizeof(ed25519_key));
#endif

if (key->isAllocated) {
void* heap = key->heap;
if (isAllocated) {
XFREE(key, heap, DYNAMIC_TYPE_ED25519);
(void)heap;
}
Expand Down
8 changes: 6 additions & 2 deletions wolfcrypt/src/rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,16 @@ int wc_RsaGetKeyId(RsaKey* key, word32* keyId)
int wc_FreeRsaKey(RsaKey* key)
{
int ret = 0;
int isAllocated = 0;
void* heap;

if (key == NULL) {
return BAD_FUNC_ARG;
}

isAllocated = key->isAllocated;
heap = key->heap;

wc_RsaCleanup(key);

#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA)
Expand Down Expand Up @@ -610,8 +615,7 @@ int wc_FreeRsaKey(RsaKey* key)
wc_fspsm_RsaKeyFree(key);
#endif

if (key->isAllocated) {
void* heap = key->heap;
if (isAllocated) {
XFREE(key, heap, DYNAMIC_TYPE_RSA);
(void)heap;
}
Expand Down
4 changes: 2 additions & 2 deletions wolfssl/wolfcrypt/ed25519.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ struct ed25519_key {
void *heap;
#ifdef WOLFSSL_ED25519_PERSISTENT_SHA
wc_Sha512 sha;
int sha_clean_flag;
unsigned int sha_clean_flag : 1;
#endif
unsigned int isAllocated:1; /* flag indicates if structure was allocated */
unsigned int isAllocated : 1; /* flag indicates if structure was allocated */
};

#ifndef WC_ED25519KEY_TYPE_DEFINED
Expand Down
2 changes: 1 addition & 1 deletion wolfssl/wolfcrypt/ed448.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ struct ed448_key {
void *heap;
#ifdef WOLFSSL_ED448_PERSISTENT_SHA
wc_Shake sha;
int sha_clean_flag;
unsigned int sha_clean_flag : 1;
#endif
};

Expand Down

0 comments on commit 67f7452

Please sign in to comment.