Skip to content

Commit

Permalink
Merge pull request wolfSSL#7695 from dgarske/compat_realloc
Browse files Browse the repository at this point in the history
Fixes for building the compatibility layer with no realloc
  • Loading branch information
JacobBarthelmeh authored Jul 1, 2024
2 parents bbf3bb4 + 2fd7a2e commit 3206637
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 18 deletions.
9 changes: 6 additions & 3 deletions wolfcrypt/benchmark/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -14182,10 +14182,13 @@ void bench_sphincsKeySign(byte level, byte optim)
* Use care when repeatedly calling calling. See implementation. */
double current_time(int reset)
{
portTickType tickCount;
/* tick count == ms, if configTICK_RATE_HZ is set to 1000 */
tickCount = xTaskGetTickCount();
portTickType tickCount = xTaskGetTickCount();
/* if configTICK_RATE_HZ is available use if (default is 1000) */
#ifdef configTICK_RATE_HZ
return (double)tickCount / configTICK_RATE_HZ;
#else
return (double)tickCount / 1000;
#endif
}
#endif

Expand Down
95 changes: 88 additions & 7 deletions wolfcrypt/src/evp.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,19 @@ static int evpCipherBlock(WOLFSSL_EVP_CIPHER_CTX *ctx,
static int wolfSSL_EVP_CipherUpdate_GCM_AAD(WOLFSSL_EVP_CIPHER_CTX *ctx,
const unsigned char *in, int inl) {
if (in && inl > 0) {
byte* tmp = (byte*)XREALLOC(ctx->authIn,
byte* tmp;
#ifdef WOLFSSL_NO_REALLOC
tmp = (byte*)XMALLOC((size_t)(ctx->authInSz + inl), NULL,
DYNAMIC_TYPE_OPENSSL);
if (tmp != NULL) {
XMEMCPY(tmp, ctx->authIn, (size_t)ctx->authInSz);
XFREE(ctx->authIn, NULL, DYNAMIC_TYPE_OPENSSL);
ctx->authIn = NULL;
}
#else
tmp = (byte*)XREALLOC(ctx->authIn,
(size_t)(ctx->authInSz + inl), NULL, DYNAMIC_TYPE_OPENSSL);
#endif
if (tmp) {
ctx->authIn = tmp;
XMEMCPY(ctx->authIn + ctx->authInSz, in, (size_t)inl);
Expand Down Expand Up @@ -745,9 +756,19 @@ static int wolfSSL_EVP_CipherUpdate_GCM(WOLFSSL_EVP_CIPHER_CTX *ctx,
/* Buffer input for one-shot API */
if (inl > 0) {
byte* tmp;
#ifdef WOLFSSL_NO_REALLOC
tmp = (byte*)XMALLOC((size_t)(ctx->authBufferLen + inl), NULL,
DYNAMIC_TYPE_OPENSSL);
if (tmp != NULL) {
XMEMCPY(tmp, ctx->authBuffer, (size_t)ctx->authBufferLen);
XFREE(ctx->authBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
ctx->authBuffer = NULL;
}
#else
tmp = (byte*)XREALLOC(ctx->authBuffer,
(size_t)(ctx->authBufferLen + inl), NULL,
DYNAMIC_TYPE_OPENSSL);
#endif
if (tmp) {
XMEMCPY(tmp + ctx->authBufferLen, in, (size_t)inl);
ctx->authBufferLen += inl;
Expand Down Expand Up @@ -817,8 +838,19 @@ static int wolfSSL_EVP_CipherUpdate_GCM(WOLFSSL_EVP_CIPHER_CTX *ctx,
static int wolfSSL_EVP_CipherUpdate_CCM_AAD(WOLFSSL_EVP_CIPHER_CTX *ctx,
const unsigned char *in, int inl) {
if (in && inl > 0) {
byte* tmp = (byte*)XREALLOC(ctx->authIn,
byte* tmp;
#ifdef WOLFSSL_NO_REALLOC
tmp = (byte*)XMALLOC((size_t)(ctx->authInSz + inl), NULL,
DYNAMIC_TYPE_OPENSSL);
if (tmp != NULL) {
XMEMCPY(tmp, ctx->authIn, (size_t)ctx->authInSz);
XFREE(ctx->authIn, NULL, DYNAMIC_TYPE_OPENSSL);
ctx->authIn = NULL;
}
#else
tmp = (byte*)XREALLOC(ctx->authIn,
(size_t)(ctx->authInSz + inl), NULL, DYNAMIC_TYPE_OPENSSL);
#endif
if (tmp) {
ctx->authIn = tmp;
XMEMCPY(ctx->authIn + ctx->authInSz, in, (size_t)inl);
Expand All @@ -843,9 +875,19 @@ static int wolfSSL_EVP_CipherUpdate_CCM(WOLFSSL_EVP_CIPHER_CTX *ctx,
/* Buffer input for one-shot API */
if (inl > 0) {
byte* tmp;
#ifdef WOLFSSL_NO_REALLOC
tmp = (byte*)XMALLOC((size_t)(ctx->authBufferLen + inl), NULL,
DYNAMIC_TYPE_OPENSSL);
if (tmp != NULL) {
XMEMCPY(tmp, ctx->authBuffer, (size_t)ctx->authBufferLen);
XFREE(ctx->authBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
ctx->authBuffer = NULL;
}
#else
tmp = (byte*)XREALLOC(ctx->authBuffer,
(size_t)(ctx->authBufferLen + inl), NULL,
DYNAMIC_TYPE_OPENSSL);
#endif
if (tmp) {
XMEMCPY(tmp + ctx->authBufferLen, in, (size_t)inl);
ctx->authBufferLen += inl;
Expand Down Expand Up @@ -875,8 +917,19 @@ static int wolfSSL_EVP_CipherUpdate_AriaGCM_AAD(WOLFSSL_EVP_CIPHER_CTX *ctx,
const unsigned char *in, int inl)
{
if (in && inl > 0) {
byte* tmp = (byte*)XREALLOC(ctx->authIn,
byte* tmp;
#ifdef WOLFSSL_NO_REALLOC
tmp = (byte*)XMALLOC((size_t)ctx->authInSz + inl, NULL,
DYNAMIC_TYPE_OPENSSL);
if (tmp != NULL) {
XMEMCPY(tmp, ctx->authIn, (size_t)ctx->authInSz);
XFREE(ctx->authIn, NULL, DYNAMIC_TYPE_OPENSSL);
ctx->authIn = NULL;
}
#else
tmp = (byte*)XREALLOC(ctx->authIn,
(size_t)ctx->authInSz + inl, NULL, DYNAMIC_TYPE_OPENSSL);
#endif
if (tmp) {
ctx->authIn = tmp;
XMEMCPY(ctx->authIn + ctx->authInSz, in, (size_t)inl);
Expand Down Expand Up @@ -905,9 +958,18 @@ static int wolfSSL_EVP_CipherUpdate_AriaGCM(WOLFSSL_EVP_CIPHER_CTX *ctx,
if (ctx->enc == 0) { /* Append extra space for the tag */
size = WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(size);
}
tmp = (byte*)XREALLOC(ctx->authBuffer,
(size_t)size, NULL,
DYNAMIC_TYPE_OPENSSL);
#ifdef WOLFSSL_NO_REALLOC
tmp = (byte*)XMALLOC((size_t)size, NULL,
DYNAMIC_TYPE_OPENSSL);
if (tmp != NULL) {
XMEMCPY(tmp, ctx->authBuffer, (size_t)ctx->authBufferLen);
XFREE(ctx->authBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
ctx->authBuffer = NULL;
}
#else
tmp = (byte*)XREALLOC(ctx->authBuffer, (size_t)size, NULL,
DYNAMIC_TYPE_OPENSSL);
#endif
if (tmp) {
XMEMCPY(tmp + ctx->authBufferLen, in, (size_t)inl);
ctx->authBufferLen += inl;
Expand Down Expand Up @@ -2693,9 +2755,19 @@ int wolfSSL_EVP_PKEY_CTX_add1_hkdf_info(WOLFSSL_EVP_PKEY_CTX* ctx,
if (ret == WOLFSSL_SUCCESS && info != NULL && infoSz > 0) {
unsigned char* p;
/* If there's already info in the buffer, append. */
#ifdef WOLFSSL_NO_REALLOC
p = (byte*)XMALLOC((size_t)(ctx->pkey->hkdfInfoSz + (word32)infoSz), NULL,
DYNAMIC_TYPE_INFO);
if (p != NULL) {
XMEMCPY(p, ctx->pkey->hkdfInfo, (size_t)ctx->pkey->hkdfInfoSz);
XFREE(ctx->pkey->hkdfInfo, NULL, DYNAMIC_TYPE_INFO);
ctx->pkey->hkdfInfo = NULL;
}
#else
p = (byte*)XREALLOC(ctx->pkey->hkdfInfo,
(size_t)(ctx->pkey->hkdfInfoSz + (word32)infoSz), NULL,
DYNAMIC_TYPE_INFO);
#endif
if (p == NULL) {
WOLFSSL_MSG("Failed to reallocate larger HKDF info buffer.");
ret = WOLFSSL_FAILURE;
Expand Down Expand Up @@ -9128,8 +9200,17 @@ static int ECC_populate_EVP_PKEY(EVP_PKEY* pkey, WOLFSSL_EC_KEY *key)
}
else if (ecc->type == ECC_PUBLICKEY) {
if ((derSz = wc_EccPublicKeyDerSize(ecc, 1)) > 0) {
derBuf = (byte*)XREALLOC(pkey->pkey.ptr, (size_t)derSz, NULL,
#ifdef WOLFSSL_NO_REALLOC
derBuf = (byte*)XMALLOC((size_t)derSz, pkey->heap, DYNAMIC_TYPE_OPENSSL);
if (derBuf != NULL) {
XMEMCPY(derBuf, pkey->pkey.ptr, (size_t)pkey->pkey_sz);
XFREE(pkey->pkey.ptr, pkey->heap, DYNAMIC_TYPE_OPENSSL);
pkey->pkey.ptr = NULL;
}
#else
derBuf = (byte*)XREALLOC(pkey->pkey.ptr, (size_t)derSz, pkey->heap,
DYNAMIC_TYPE_OPENSSL);
#endif
if (derBuf != NULL) {
pkey->pkey.ptr = (char*)derBuf;
if ((derSz = wc_EccPublicKeyToDer(ecc, derBuf, (word32)derSz,
Expand Down
16 changes: 8 additions & 8 deletions wolfcrypt/test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -17917,9 +17917,9 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void)
#endif
if (b)
XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
if ((b == NULL)
if (b == NULL
#ifndef WOLFSSL_NO_REALLOC
|| (c == NULL)
|| c == NULL
#endif
) {
return WC_TEST_RET_ENC_ERRNO;
Expand Down Expand Up @@ -53600,8 +53600,8 @@ static void *my_Realloc_cb(void *ptr, size_t size)
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memcb_test(void)
{
wc_test_ret_t ret = 0;
#if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
!defined(WOLFSSL_STATIC_MEMORY)
#if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_NO_REALLOC) && \
!defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_STATIC_MEMORY)
byte* b = NULL;
#endif
wolfSSL_Malloc_cb mc;
Expand All @@ -53614,8 +53614,8 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memcb_test(void)
if (ret != 0)
return WC_TEST_RET_ENC_EC(ret);

#if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
!defined(WOLFSSL_STATIC_MEMORY)
#if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_NO_REALLOC) && \
!defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_STATIC_MEMORY)

/* test realloc */
b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
Expand Down Expand Up @@ -53650,8 +53650,8 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memcb_test(void)
ret = WC_TEST_RET_ENC_NC;
#endif /* !WOLFSSL_NO_MALLOC */

#if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
!defined(WOLFSSL_STATIC_MEMORY)
#if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_NO_REALLOC) && \
!defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_STATIC_MEMORY)
exit_memcb:

/* reset malloc/free/realloc counts */
Expand Down

0 comments on commit 3206637

Please sign in to comment.