Skip to content

Commit 0d5d05d

Browse files
committed
more WOLFSSL_NO_MALLOC fixes:
wolfcrypt/src/dh.c: in wc_DhGenerateParams(), use named constant for buf size, and only XFREE it if !WOLFSSL_NO_MALLOC; wolfcrypt/src/ecc.c and wolfssl/wolfcrypt/ecc.h: in wc_ecc_new_point_ex(), remove !WOLFSSL_NO_MALLOC gate around XMALLOC(), and if XMALLOC()ed, set ecc_point.isAllocated, then in wc_ecc_del_point_ex, XFREE() iff ecc_point.isAllocated; wolfcrypt/src/pkcs7.c: in wc_PKCS7_RsaVerify(), when WOLFSSL_NO_MALLOC, jumbo-size the digest buffer to cope with in-place dynamics in RsaUnPad(); wolfcrypt/test/test.c: add !WOLFSSL_NO_MALLOC gates around various XFREE()s of objects that are on the stack in WOLFSSL_NO_MALLOC builds; wolfssl/wolfcrypt/types.h: add an unconditional include of memory.h (itself guarded against multiple inclusion) to assure availability of WC_DEBUG_CIPHER_LIFECYCLE prototypes/macros.
1 parent 9312f3c commit 0d5d05d

7 files changed

Lines changed: 30 additions & 9 deletions

File tree

wolfcrypt/src/dh.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2980,7 +2980,7 @@ int wc_DhGenerateParams(WC_RNG *rng, int modSz, DhKey *dh)
29802980
int primeCheck = MP_NO,
29812981
ret = 0;
29822982
#ifdef WOLFSSL_NO_MALLOC
2983-
unsigned char buf[4096 / WOLFSSL_BIT_SIZE];
2983+
unsigned char buf[DH_MAX_SIZE / WOLFSSL_BIT_SIZE];
29842984
#else
29852985
unsigned char *buf = NULL;
29862986
#endif
@@ -3181,9 +3181,11 @@ int wc_DhGenerateParams(WC_RNG *rng, int modSz, DhKey *dh)
31813181
#endif
31823182
{
31833183
ForceZero(buf, bufSz);
3184+
#ifndef WOLFSSL_NO_MALLOC
31843185
if (dh != NULL) {
31853186
XFREE(buf, dh->heap, DYNAMIC_TYPE_TMP_BUFFER);
31863187
}
3188+
#endif
31873189
}
31883190

31893191
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)

wolfcrypt/src/ecc.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4092,23 +4092,23 @@ static int wc_ecc_new_point_ex(ecc_point** point, void* heap)
40924092
}
40934093

40944094
p = *point;
4095-
#ifndef WOLFSSL_NO_MALLOC
40964095
if (p == NULL) {
40974096
p = (ecc_point*)XMALLOC(sizeof(ecc_point), heap, DYNAMIC_TYPE_ECC);
40984097
}
4099-
#endif
41004098
if (p == NULL) {
41014099
return MEMORY_E;
41024100
}
41034101
XMEMSET(p, 0, sizeof(ecc_point));
41044102

4103+
if (*point == NULL)
4104+
p->isAllocated = 1;
4105+
41054106
#ifndef ALT_ECC_SIZE
41064107
err = mp_init_multi(p->x, p->y, p->z, NULL, NULL, NULL);
41074108
if (err != MP_OKAY) {
41084109
WOLFSSL_MSG("mp_init_multi failed.");
4109-
#ifndef WOLFSSL_NO_MALLOC
4110-
XFREE(p, heap, DYNAMIC_TYPE_ECC);
4111-
#endif
4110+
if (p->isAllocated)
4111+
XFREE(p, heap, DYNAMIC_TYPE_ECC);
41124112
p = NULL;
41134113
}
41144114
#else
@@ -4148,9 +4148,8 @@ static void wc_ecc_del_point_ex(ecc_point* p, void* heap)
41484148
mp_clear(p->x);
41494149
mp_clear(p->y);
41504150
mp_clear(p->z);
4151-
#ifndef WOLFSSL_NO_MALLOC
4152-
XFREE(p, heap, DYNAMIC_TYPE_ECC);
4153-
#endif
4151+
if (p->isAllocated)
4152+
XFREE(p, heap, DYNAMIC_TYPE_ECC);
41544153
}
41554154
(void)heap;
41564155
}

wolfcrypt/src/memory.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#endif
3333

3434
#include <wolfssl/wolfcrypt/types.h>
35+
#include <wolfssl/wolfcrypt/error-crypt.h>
3536

3637
/*
3738
Possible memory options:

wolfcrypt/src/pkcs7.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4040,8 +4040,14 @@ static int wc_PKCS7_RsaVerify(PKCS7* pkcs7, byte* sig, int sigSz,
40404040
byte* digest;
40414041
RsaKey* key;
40424042
DecodedCert* dCert;
4043+
#else
4044+
#ifdef WOLFSSL_NO_MALLOC
4045+
byte digest[RSA_MAX_SIZE / WOLFSSL_BIT_SIZE]; /* accessed in-place with size
4046+
* key->dataLen
4047+
*/
40434048
#else
40444049
byte digest[MAX_PKCS7_DIGEST_SZ];
4050+
#endif
40454051
RsaKey key[1];
40464052
DecodedCert stack_dCert;
40474053
DecodedCert* dCert = &stack_dCert;

wolfcrypt/test/test.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2673,23 +2673,31 @@ static wc_test_ret_t _SaveDerAndPem(const byte* der, int derSz,
26732673
/* Convert to PEM */
26742674
pemSz = wc_DerToPem(der, (word32)derSz, pem, (word32)pemSz, pemType);
26752675
if (pemSz < 0) {
2676+
#ifndef WOLFSSL_NO_MALLOC
26762677
XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
2678+
#endif
26772679
return WC_TEST_RET_ENC(calling_line, 4, WC_TEST_RET_TAG_I);
26782680
}
26792681
#if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
26802682
pemFile = XFOPEN(filePem, "wb");
26812683
if (!pemFile) {
2684+
#ifndef WOLFSSL_NO_MALLOC
26822685
XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
2686+
#endif
26832687
return WC_TEST_RET_ENC(calling_line, 5, WC_TEST_RET_TAG_I);
26842688
}
26852689
ret = (int)XFWRITE(pem, 1, (size_t)pemSz, pemFile);
26862690
XFCLOSE(pemFile);
26872691
if (ret != pemSz) {
2692+
#ifndef WOLFSSL_NO_MALLOC
26882693
XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
2694+
#endif
26892695
return WC_TEST_RET_ENC(calling_line, 6, WC_TEST_RET_TAG_I);
26902696
}
26912697
#endif
2698+
#ifndef WOLFSSL_NO_MALLOC
26922699
XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
2700+
#endif
26932701
}
26942702
#endif /* WOLFSSL_DER_TO_PEM */
26952703

@@ -37926,8 +37934,10 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed448_test(void)
3792637934
}
3792737935
} while(0);
3792837936

37937+
#ifndef WOLFSSL_NO_MALLOC
3792937938
XFREE(exportPKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
3793037939
XFREE(exportSKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
37940+
#endif
3793137941

3793237942
if (ret != 0)
3793337943
goto out;

wolfssl/wolfcrypt/ecc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ struct ecc_point {
467467
#if defined(WOLFSSL_SMALL_STACK_CACHE) && !defined(WOLFSSL_ECC_NO_SMALL_STACK)
468468
ecc_key* key;
469469
#endif
470+
byte isAllocated:1;
470471
};
471472

472473
/* ECC Flags */

wolfssl/wolfcrypt/types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,8 @@ typedef struct w64wrapper {
602602
#endif /* WOLFSSL_STATIC_MEMORY */
603603
#endif
604604

605+
#include <wolfssl/wolfcrypt/memory.h>
606+
605607
/* declare/free variable handling for async and smallstack */
606608
#ifndef WC_ALLOC_DO_ON_FAILURE
607609
#define WC_ALLOC_DO_ON_FAILURE() WC_DO_NOTHING

0 commit comments

Comments
 (0)