wolfcrypt/src/memory.c, wolfcrypt/src/misc.c, and wolfssl/wolfcrypt/misc.h: move

the new implementation of wc_ForceZero from wolfcrypt/src/memory.c to inline in
  wolfcrypt/src/misc.c replacing old ForceZero() implementation, and add a wrapper
  wc_ForceZero() to wolfcrypt/src/memory.c.
This commit is contained in:
Daniel Pouzzner
2025-08-11 16:02:34 -05:00
parent e36daf41a4
commit 6617f2edf8
3 changed files with 37 additions and 52 deletions

View File

@@ -26,6 +26,13 @@
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
/*
Possible memory options:
* NO_WOLFSSL_MEMORY: Disables wolf memory callback support. When not defined settings.h defines USE_WOLFSSL_MEMORY.
@@ -1661,36 +1668,10 @@ void __attribute__((no_instrument_function))
#endif
#ifndef WOLFSSL_NO_FORCE_ZERO
/* Exported version of ForceZero() that takes a size_t. */
/* Exported version of ForceZero(). */
void wc_ForceZero(void *mem, size_t len)
{
byte *zb = (byte *)mem;
unsigned long *zl;
XFENCE();
while ((wc_ptr_t)zb & (wc_ptr_t)(sizeof(unsigned long) - 1U)) {
if (len == 0)
return;
*zb++ = 0;
--len;
}
zl = (unsigned long *)zb;
while (len > sizeof(unsigned long)) {
*zl++ = 0;
len -= sizeof(unsigned long);
}
zb = (byte *)zl;
while (len) {
*zb++ = 0;
--len;
}
XFENCE();
ForceZero(mem, len);
}
#endif

View File

@@ -587,32 +587,36 @@ WC_MISC_STATIC WC_INLINE void xorbuf(void* buf, const void* mask, word32 count)
#ifndef WOLFSSL_NO_FORCE_ZERO
/* This routine fills the first len bytes of the memory area pointed by mem
with zeros. It ensures compiler optimization doesn't skip it */
WC_MISC_STATIC WC_INLINE void ForceZero(void* mem, word32 len)
with zeros. It ensures compiler optimization doesn't skip it. */
WC_MISC_STATIC WC_INLINE void ForceZero(void* mem, size_t len)
{
volatile byte* z = (volatile byte*)mem;
byte *zb = (byte *)mem;
unsigned long *zl;
#if (defined(WOLFSSL_X86_64_BUILD) || defined(WOLFSSL_AARCH64_BUILD)) \
&& defined(WORD64_AVAILABLE)
volatile word64* w;
#ifndef WOLFSSL_UNALIGNED_64BIT_ACCESS
word32 l = (sizeof(word64) - ((size_t)z & (sizeof(word64)-1))) &
(sizeof(word64)-1);
XFENCE();
if (len < l) l = len;
len -= l;
while (l--) *z++ = 0;
#endif
for (w = (volatile word64*)z;
len >= sizeof(*w);
len -= (word32)sizeof(*w))
{
*w++ = 0;
}
z = (volatile byte*)w;
#endif
while ((wc_ptr_t)zb & (wc_ptr_t)(sizeof(unsigned long) - 1U)) {
if (len == 0)
return;
*zb++ = 0;
--len;
}
while (len--) *z++ = 0;
zl = (unsigned long *)zb;
while (len >= sizeof(unsigned long)) {
*zl++ = 0;
len -= sizeof(unsigned long);
}
zb = (byte *)zl;
while (len) {
*zb++ = 0;
--len;
}
XFENCE();
}
#endif

View File

@@ -67,7 +67,7 @@ WOLFSSL_LOCAL
void xorbuf(void* buf, const void* mask, word32 count);
WOLFSSL_LOCAL
void ForceZero(void* mem, word32 len);
void ForceZero(void* mem, size_t len);
WOLFSSL_LOCAL
int ConstantCompare(const byte* a, const byte* b, int length);
@@ -184,7 +184,7 @@ WOLFSSL_LOCAL w64wrapper w64Mul(word32 a, word32 b);
/* Declarations for user defined functions */
#ifdef WOLFSSL_NO_FORCE_ZERO
void ForceZero(void* mem, word32 len);
void ForceZero(void* mem, size_t len);
#endif
#ifdef WOLFSSL_NO_CONST_CMP
int ConstantCompare(const byte* a, const byte* b, int length);