Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions IDE/ARDUINO/wolfssl-arduino.sh
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ if [ "$THIS_DIR" = "ARDUINO" ]; then
# cp ../../wolfcrypt/src/*.c ./wolfssl/src/wolfcrypt/src
echo "Step 09: cp ${WOLFCRYPT_SRC_TOP}/*.c .${WOLFCRYPT_SRC}"
$CP_CMD -r "${WOLFCRYPT_SRC_TOP}"/*.c ."${WOLFCRYPT_SRC}" || exit 1
# private header included by the wolfcrypt sources (aligned allocation)
$CP_CMD "${WOLFCRYPT_SRC_TOP}"/alloc_align.h ".${WOLFCRYPT_SRC}" || exit 1
$CP_CMD -r "${WOLFCRYPT_SRC_TOP}"/port/atmel/*.c ."${WOLFCRYPT_SRC}"/port/atmel || exit 1
$CP_CMD -r "${WOLFCRYPT_SRC_TOP}"/port/Espressif/*.c ."${WOLFCRYPT_SRC}"/port/Espressif || exit 1

Expand Down
11 changes: 6 additions & 5 deletions src/ssl_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#include <wolfcrypt/src/alloc_align.h>

#ifndef WOLFSSL_SSL_CRYPTO_INCLUDED
#ifndef WOLFSSL_IGNORE_FILE_WARN
Expand Down Expand Up @@ -2254,7 +2255,7 @@ WOLFSSL_CMAC_CTX* wolfSSL_CMAC_CTX_new(void)
/* Allocate memory for EVP cipher context object. */
ctx->cctx = wolfSSL_EVP_CIPHER_CTX_new();
if (ctx->cctx == NULL) {
XFREE(ctx->internal, NULL, DYNAMIC_TYPE_CMAC);
WC_FREE_CMAC((Cmac*)ctx->internal, NULL);
XFREE(ctx, NULL, DYNAMIC_TYPE_OPENSSL);
ctx = NULL;
}
Expand All @@ -2277,7 +2278,7 @@ void wolfSSL_CMAC_CTX_free(WOLFSSL_CMAC_CTX *ctx)
#if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
wc_CmacFree((Cmac*)ctx->internal);
#endif
XFREE(ctx->internal, NULL, DYNAMIC_TYPE_CMAC);
WC_FREE_CMAC((Cmac*)ctx->internal, NULL);
}
if (ctx->cctx != NULL) {
wolfSSL_EVP_CIPHER_CTX_cleanup(ctx->cctx);
Expand Down Expand Up @@ -2363,7 +2364,7 @@ int wolfSSL_CMAC_Init(WOLFSSL_CMAC_CTX* ctx, const void *key, size_t keySz,
}
else if (ret == 1) {
/* Allocate memory for wolfSSL CMAC object. */
ctx->internal = (Cmac*)XMALLOC(sizeof(Cmac), NULL, DYNAMIC_TYPE_CMAC);
ctx->internal = WC_ALLOC_CMAC(NULL);
if (ctx->internal == NULL)
ret = 0;
}
Expand All @@ -2372,7 +2373,7 @@ int wolfSSL_CMAC_Init(WOLFSSL_CMAC_CTX* ctx, const void *key, size_t keySz,
if ((ret == 1) && (wc_InitCmac((Cmac*)ctx->internal, (const byte*)key,
(word32)keySz, WC_CMAC_AES, NULL) != 0)) {
WOLFSSL_MSG("wolfSSL_CMAC_Init: wc_InitCmac() failed");
XFREE(ctx->internal, NULL, DYNAMIC_TYPE_CMAC);
WC_FREE_CMAC((Cmac*)ctx->internal, NULL);
ctx->internal = NULL;
ret = 0;
}
Expand Down Expand Up @@ -2469,7 +2470,7 @@ int wolfSSL_CMAC_Final(WOLFSSL_CMAC_CTX* ctx, unsigned char* out, size_t* len)
*len = (size_t)len32;
}

XFREE(ctx->internal, NULL, DYNAMIC_TYPE_CMAC);
WC_FREE_CMAC((Cmac*)ctx->internal, NULL);
ctx->internal = NULL;
}

Expand Down
6 changes: 2 additions & 4 deletions tests/unit-mcdc/test_aes_whitebox.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,8 @@ static void wb_ghash_update(void) { WB_NOTE("AESGCM stream off; GHASH_UPDATE ski
* ------------------------------------------------------------------------- */
static void wb_release(Aes* aes)
{
if (aes != NULL) {
wc_AesFree(aes);
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
}
if (aes != NULL)
(void)wc_AesDelete(aes, NULL);
}

static void wb_aesnew_common(void)
Expand Down
47 changes: 31 additions & 16 deletions wolfcrypt/src/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
#endif

#include <wolfssl/wolfcrypt/aes.h>
#include <wolfcrypt/src/alloc_align.h>

#ifdef WOLFSSL_AESNI
#include <wmmintrin.h>
Expand Down Expand Up @@ -17013,7 +17014,7 @@ static Aes* _AesNew_common(void* heap, int devId, int *result_code,
int idLen, const char* label)
{
int ret;
Aes* aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_AES);
Aes* aes = WC_ALLOC_AES(heap);
if (aes == NULL) {
ret = MEMORY_E;
}
Expand Down Expand Up @@ -17047,7 +17048,7 @@ static Aes* _AesNew_common(void* heap, int devId, int *result_code,
break;
}
if (ret != 0) {
XFREE(aes, heap, DYNAMIC_TYPE_AES);
WC_FREE_AES(aes, heap);
aes = NULL;
}
}
Expand Down Expand Up @@ -17092,7 +17093,7 @@ int wc_AesDelete(Aes *aes, Aes** aes_p)
return BAD_FUNC_ARG;
heap = aes->heap;
wc_AesFree(aes);
XFREE(aes, heap, DYNAMIC_TYPE_AES);
WC_FREE_AES(aes, heap);
if (aes_p != NULL)
*aes_p = NULL;
return 0;
Expand Down Expand Up @@ -18680,8 +18681,7 @@ int wc_AesKeyWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
return BAD_FUNC_ARG;

#ifdef WOLFSSL_SMALL_STACK
if ((aes = (Aes *)XMALLOC(sizeof *aes, NULL,
DYNAMIC_TYPE_AES)) == NULL)
if ((aes = WC_ALLOC_AES(NULL)) == NULL)
return MEMORY_E;
Comment thread
danielinux marked this conversation as resolved.
Resolved
#endif

Expand All @@ -18700,7 +18700,9 @@ int wc_AesKeyWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
wc_AesFree(aes);

out:
WC_FREE_VAR_EX(aes, NULL, DYNAMIC_TYPE_AES);
#ifdef WOLFSSL_SMALL_STACK
WC_FREE_AES(aes, NULL);
#endif

return ret;
}
Expand Down Expand Up @@ -18875,8 +18877,7 @@ int wc_AesKeyUnWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
return BAD_FUNC_ARG;

#ifdef WOLFSSL_SMALL_STACK
if ((aes = (Aes *)XMALLOC(sizeof *aes, NULL,
DYNAMIC_TYPE_AES)) == NULL)
if ((aes = WC_ALLOC_AES(NULL)) == NULL)
return MEMORY_E;
#endif

Expand All @@ -18896,7 +18897,9 @@ int wc_AesKeyUnWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
wc_AesFree(aes);

out:
WC_FREE_VAR_EX(aes, NULL, DYNAMIC_TYPE_AES);
#ifdef WOLFSSL_SMALL_STACK
WC_FREE_AES(aes, NULL);
#endif

return ret;
}
Expand Down Expand Up @@ -19007,7 +19010,10 @@ int wc_AesKeyWrap_Pad(const byte* key, word32 keySz, const byte* in,
return BAD_FUNC_ARG;
}

WC_ALLOC_VAR_EX(aes, Aes, 1, NULL, DYNAMIC_TYPE_AES, return MEMORY_E);
#ifdef WOLFSSL_SMALL_STACK
if ((aes = WC_ALLOC_AES(NULL)) == NULL)
return MEMORY_E;
#endif

ret = wc_AesInit(aes, NULL, INVALID_DEVID);
if (ret != 0) {
Expand All @@ -19025,7 +19031,9 @@ int wc_AesKeyWrap_Pad(const byte* key, word32 keySz, const byte* in,
wc_AesFree(aes);

out:
WC_FREE_VAR_EX(aes, NULL, DYNAMIC_TYPE_AES);
#ifdef WOLFSSL_SMALL_STACK
WC_FREE_AES(aes, NULL);
#endif

return ret;
}
Expand Down Expand Up @@ -19180,7 +19188,10 @@ int wc_AesKeyUnWrap_Pad(const byte* key, word32 keySz, const byte* in,
return BAD_FUNC_ARG;
}

WC_ALLOC_VAR_EX(aes, Aes, 1, NULL, DYNAMIC_TYPE_AES, return MEMORY_E);
#ifdef WOLFSSL_SMALL_STACK
if ((aes = WC_ALLOC_AES(NULL)) == NULL)
return MEMORY_E;
#endif

ret = wc_AesInit(aes, NULL, INVALID_DEVID);
if (ret != 0) {
Expand All @@ -19198,7 +19209,9 @@ int wc_AesKeyUnWrap_Pad(const byte* key, word32 keySz, const byte* in,
wc_AesFree(aes);

out:
WC_FREE_VAR_EX(aes, NULL, DYNAMIC_TYPE_AES);
#ifdef WOLFSSL_SMALL_STACK
WC_FREE_AES(aes, NULL);
#endif

return ret;
}
Expand Down Expand Up @@ -21156,8 +21169,10 @@ static WARN_UNUSED_RESULT int S2V(
if (ret == 0) {
if (dataSz >= WC_AES_BLOCK_SIZE) {

WC_ALLOC_VAR_EX(cmac, Cmac, 1, NULL, DYNAMIC_TYPE_CMAC,
ret=MEMORY_E);
#ifdef WOLFSSL_SMALL_STACK
if ((cmac = WC_ALLOC_CMAC(NULL)) == NULL)
ret = MEMORY_E;
#endif
if (WC_VAR_OK(cmac))
{
#ifdef WOLFSSL_CHECK_MEM_ZERO
Expand All @@ -21180,7 +21195,7 @@ static WARN_UNUSED_RESULT int S2V(
}
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(cmac, NULL, DYNAMIC_TYPE_CMAC);
WC_FREE_CMAC(cmac, NULL);
#elif defined(WOLFSSL_CHECK_MEM_ZERO)
wc_MemZero_Check(cmac, sizeof(Cmac));
#endif
Expand Down
105 changes: 105 additions & 0 deletions wolfcrypt/src/alloc_align.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/* alloc_align.h
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/

/*
* Private aligned-allocation helpers for heap contexts whose members carry
* alignment attributes (Aes, Cmac). On 32-bit Arm, malloc may only return an
* 8-byte-aligned pointer, so over-allocate, align, and keep the original
* pointer for XFREE. Included only by the files that allocate those contexts
* on the heap; not part of the public API.
*/
#ifndef WOLFSSL_ALLOC_ALIGN_H
#define WOLFSSL_ALLOC_ALIGN_H

#include <wolfssl/wolfcrypt/aes.h>
#include <wolfssl/wolfcrypt/cmac.h>

#if defined(WOLFSSL_USE_ALIGN) && defined(WC_ALIGNOF) && \
!defined(WOLFSSL_TI_CRYPT)
#define WC_HAVE_ALIGNED_ALLOC
static WC_INLINE void* wc_alloc_aligned(void* heap, size_t size,
size_t align, int type)
{
byte* raw = (byte*)XMALLOC(size + align - 1 + sizeof(void*), heap, type);
wc_ptr_t p;

if (raw == NULL)
return NULL;
p = ((wc_ptr_t)(raw + sizeof(void*)) + align - 1) &
~((wc_ptr_t)align - 1);
((void**)p)[-1] = raw;
return (void*)p;
}

static WC_INLINE void wc_free_aligned(void* p, void* heap, int type)
{
(void)heap;
(void)type;
if (p != NULL)
XFREE(((void**)p)[-1], heap, type);
}
#endif

/* Type-specific wrappers: the alignment requirement is the type's, hidden
* here so call sites stay plain. The TI backend and builds without
* WC_ALIGNOF use the plain allocation path, which needs no indirection.
* Each wrapper exists only where its type does. */
#ifndef NO_AES
#ifdef WC_HAVE_ALIGNED_ALLOC
static WC_INLINE Aes* WC_ALLOC_AES(void* heap)
{
return (Aes*)wc_alloc_aligned(heap, sizeof(Aes), WC_ALIGNOF(Aes),
DYNAMIC_TYPE_AES);
}

static WC_INLINE void WC_FREE_AES(Aes* aes, void* heap)
{
wc_free_aligned((void*)aes, heap, DYNAMIC_TYPE_AES);
}
#else
#define WC_ALLOC_AES(heap) \
((Aes*)XMALLOC(sizeof(Aes), (heap), DYNAMIC_TYPE_AES))
#define WC_FREE_AES(aes, heap) \
do {XFREE((aes), (heap), DYNAMIC_TYPE_AES);} while (0)
#endif
#endif /* !NO_AES */

#ifdef WOLFSSL_CMAC
#ifdef WC_HAVE_ALIGNED_ALLOC
static WC_INLINE Cmac* WC_ALLOC_CMAC(void* heap)
{
return (Cmac*)wc_alloc_aligned(heap, sizeof(Cmac), WC_ALIGNOF(Cmac),
DYNAMIC_TYPE_CMAC);
}

static WC_INLINE void WC_FREE_CMAC(Cmac* cmac, void* heap)
{
wc_free_aligned((void*)cmac, heap, DYNAMIC_TYPE_CMAC);
}
#else
#define WC_ALLOC_CMAC(heap) \
((Cmac*)XMALLOC(sizeof(Cmac), (heap), DYNAMIC_TYPE_CMAC))
#define WC_FREE_CMAC(cmac, heap) \
do {XFREE((cmac), (heap), DYNAMIC_TYPE_CMAC);} while (0)
#endif
#endif /* WOLFSSL_CMAC */

#endif /* WOLFSSL_ALLOC_ALIGN_H */
11 changes: 5 additions & 6 deletions wolfcrypt/src/cmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

#include <wolfssl/wolfcrypt/aes.h>
#include <wolfssl/wolfcrypt/cmac.h>
#include <wolfcrypt/src/alloc_align.h>

#ifdef WOLF_CRYPTO_CB
#include <wolfssl/wolfcrypt/cryptocb.h>
Expand Down Expand Up @@ -634,8 +635,7 @@ int wc_AesCmacGenerate(byte* out, word32* outSz,
}

#ifdef WOLFSSL_SMALL_STACK
if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, NULL,
DYNAMIC_TYPE_CMAC)) == NULL) {
if ((cmac = WC_ALLOC_CMAC(NULL)) == NULL) {
return MEMORY_E;
}
#endif
Expand All @@ -658,7 +658,7 @@ int wc_AesCmacGenerate(byte* out, word32* outSz,

#ifdef WOLFSSL_SMALL_STACK
ForceZero(cmac, sizeof(Cmac));
XFREE(cmac, NULL, DYNAMIC_TYPE_CMAC);
WC_FREE_CMAC(cmac, NULL);
#elif defined(WOLFSSL_CHECK_MEM_ZERO)
wc_MemZero_Check(cmac, sizeof(Cmac));
#endif
Expand Down Expand Up @@ -737,8 +737,7 @@ int wc_AesCmacVerify(const byte* check, word32 checkSz,
}

#ifdef WOLFSSL_SMALL_STACK
if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, NULL,
DYNAMIC_TYPE_CMAC)) == NULL) {
if ((cmac = WC_ALLOC_CMAC(NULL)) == NULL) {
return MEMORY_E;
}
#endif
Expand All @@ -760,7 +759,7 @@ int wc_AesCmacVerify(const byte* check, word32 checkSz,

#ifdef WOLFSSL_SMALL_STACK
ForceZero(cmac, sizeof(Cmac));
XFREE(cmac, NULL, DYNAMIC_TYPE_CMAC);
WC_FREE_CMAC(cmac, NULL);
#elif defined(WOLFSSL_CHECK_MEM_ZERO)
wc_MemZero_Check(cmac, sizeof(Cmac));
#endif
Expand Down
3 changes: 2 additions & 1 deletion wolfcrypt/src/include.am
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ EXTRA_DIST += \
wolfcrypt/src/fp_sqr_comba_8.i \
wolfcrypt/src/fp_sqr_comba_9.i \
wolfcrypt/src/fp_sqr_comba_small_set.i \
wolfcrypt/src/fe_x25519_128.h
wolfcrypt/src/fe_x25519_128.h \
wolfcrypt/src/alloc_align.h

EXTRA_DIST += wolfcrypt/src/port/ti/ti-aes.c \
wolfcrypt/src/port/ti/ti-des3.c \
Expand Down
Loading
Loading