2019-02-16 15:03:46 +00:00
|
|
|
#ifndef __STDC_WANT_LIB_EXT1__
|
2019-03-29 14:44:55 +00:00
|
|
|
#define __STDC_WANT_LIB_EXT1__ 1 // C11's bounds-checking interface.
|
2019-02-16 15:03:46 +00:00
|
|
|
#endif
|
2018-01-18 14:18:09 +00:00
|
|
|
#include <string.h>
|
2019-02-16 15:03:46 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2021-04-08 12:16:23 +00:00
|
|
|
#include <windows.h>
|
2019-02-16 15:03:46 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __unix__
|
2019-01-23 18:39:17 +00:00
|
|
|
#include <strings.h>
|
2019-02-16 15:03:46 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// C11's bounds-checking interface.
|
|
|
|
#if defined(__STDC_LIB_EXT1__)
|
|
|
|
#define HAVE_MEMSET_S 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// GNU C Library version 2.25 or later.
|
2019-03-29 14:44:55 +00:00
|
|
|
#if defined(__GLIBC__) && \
|
|
|
|
(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25))
|
2019-02-16 15:14:12 +00:00
|
|
|
#define HAVE_EXPLICIT_BZERO 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Newlib
|
2019-03-29 14:44:55 +00:00
|
|
|
#if defined(__NEWLIB__)
|
2019-02-16 15:03:46 +00:00
|
|
|
#define HAVE_EXPLICIT_BZERO 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// FreeBSD version 11.0 or later.
|
|
|
|
#if defined(__FreeBSD__) && __FreeBSD_version >= 1100037
|
|
|
|
#define HAVE_EXPLICIT_BZERO 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// OpenBSD version 5.5 or later.
|
|
|
|
#if defined(__OpenBSD__) && OpenBSD >= 201405
|
|
|
|
#define HAVE_EXPLICIT_BZERO 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// NetBSD version 7.2 or later.
|
|
|
|
#if defined(__NetBSD__) && __NetBSD_Version__ >= 702000000
|
|
|
|
#define HAVE_EXPLICIT_MEMSET 1
|
|
|
|
#endif
|
2018-01-18 14:18:09 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
// Adapted from
|
|
|
|
// https://github.com/jedisct1/libsodium/blob/1647f0d53ae0e370378a9195477e3df0a792408f/src/libsodium/sodium/utils.c#L102-L130
|
2019-01-23 18:39:17 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
void memzero(void *const pnt, const size_t len) {
|
2019-01-23 18:39:17 +00:00
|
|
|
#ifdef _WIN32
|
2019-03-29 14:44:55 +00:00
|
|
|
SecureZeroMemory(pnt, len);
|
2019-01-23 18:39:17 +00:00
|
|
|
#elif defined(HAVE_MEMSET_S)
|
2019-03-29 14:44:55 +00:00
|
|
|
memset_s(pnt, (rsize_t)len, 0, (rsize_t)len);
|
2019-01-23 18:39:17 +00:00
|
|
|
#elif defined(HAVE_EXPLICIT_BZERO)
|
2019-03-29 14:44:55 +00:00
|
|
|
explicit_bzero(pnt, len);
|
2019-01-23 18:39:17 +00:00
|
|
|
#elif defined(HAVE_EXPLICIT_MEMSET)
|
2019-03-29 14:44:55 +00:00
|
|
|
explicit_memset(pnt, 0, len);
|
2019-01-23 18:39:17 +00:00
|
|
|
#else
|
2019-03-29 14:44:55 +00:00
|
|
|
volatile unsigned char *volatile pnt_ = (volatile unsigned char *volatile)pnt;
|
|
|
|
size_t i = (size_t)0U;
|
2019-01-23 18:39:17 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
while (i < len) {
|
|
|
|
pnt_[i++] = 0U;
|
|
|
|
}
|
2020-07-01 12:04:02 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// explicitly mark the memory as overwritten for the Clang MemorySanitizer
|
|
|
|
// this is only included at compile time if MemorySanitizer is enabled and
|
|
|
|
// should not come with any downsides during regular builds
|
|
|
|
#if defined(__has_feature)
|
|
|
|
#if __has_feature(memory_sanitizer)
|
|
|
|
memset(pnt, 0, len);
|
|
|
|
#endif
|
2019-01-23 18:39:17 +00:00
|
|
|
#endif
|
2018-01-18 14:18:09 +00:00
|
|
|
}
|