diff --git a/wolfcrypt/src/misc.c b/wolfcrypt/src/misc.c index 484ffd501..ffb03a8d1 100644 --- a/wolfcrypt/src/misc.c +++ b/wolfcrypt/src/misc.c @@ -201,7 +201,13 @@ STATIC INLINE void ForceZero(const void* mem, word32 len) volatile byte* z = (volatile byte*)mem; #if defined(WOLFSSL_X86_64_BUILD) && defined(WORD64_AVAILABLE) volatile word64* w; + #ifndef WOLFSSL_UNALIGNED_64BIT_ACCESS + word32 l = (8 - ((size_t)z & 0x7)) & 0x7; + if (len < l) l = len; + len -= l; + while (l--) *z++ = 0; + #endif for (w = (volatile word64*)z; len >= sizeof(*w); len -= sizeof(*w)) *w++ = 0; z = (volatile byte*)w; diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index bd1b90ffd..314285f0c 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -120,6 +120,10 @@ #include #endif +#define WOLFSSL_MISC_INCLUDED +#include + + /* only for stack size check */ #ifdef HAVE_STACK_SIZE #include @@ -335,6 +339,7 @@ int memcb_test(void); #ifdef WOLFSSL_IMX6_CAAM_BLOB int blob_test(void); #endif +int misc_test(void); /* General big buffer size for many tests. */ @@ -950,6 +955,11 @@ initDefaultName(); printf( "blob test passed!\n"); #endif + if ( (ret = misc_test()) != 0) + return err_sys("misc test failed!\n", ret); + else + printf( "misc test passed!\n"); + #ifdef WOLFSSL_ASYNC_CRYPT wolfAsync_DevClose(&devId); #endif @@ -18524,6 +18534,33 @@ int blob_test(void) } #endif /* WOLFSSL_IMX6_CAAM_BLOB */ +int misc_test(void) +{ + unsigned char data[32]; + unsigned int i, j, len; + + /* Test ForceZero */ + for (i = 0; i < sizeof(data); i++) { + for (len = 1; len < sizeof(data) - i; len++) { + for (j = 0; j < sizeof(data); j++) + data[j] = j + 1; + + ForceZero(data + i, len); + + for (j = 0; j < sizeof(data); j++) { + if (j < i || j >= i + len) { + if (data[j] == 0x00) + return -9000; + } + else if (data[j] != 0x00) + return -9001; + } + } + } + + return 0; +} + #undef ERROR_OUT #else