| 1 |
#if defined(__has_include) |
| 2 |
#if __has_include("config.h") |
| 3 |
#include "config.h" |
| 4 |
#endif |
| 5 |
#endif |
| 6 |
|
| 7 |
#ifdef _WIN32 |
| 8 |
#include <windows.h> |
| 9 |
#endif |
| 10 |
|
| 11 |
#if defined(__APPLE__) && defined(__MACH__) |
| 12 |
#include <AvailabilityMacros.h> |
| 13 |
// memset_s is available from macOS 10.9, iOS 7, watchOS 2, and on all tvOS and visionOS versions. |
| 14 |
# if (defined(MAC_OS_X_VERSION_MIN_REQUIRED) && defined(MAC_OS_X_VERSION_10_9) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_9)) |
| 15 |
# define APPLE_HAS_MEMSET_S |
| 16 |
# elif (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && defined(__IPHONE_7_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_7_0)) |
| 17 |
# define APPLE_HAS_MEMSET_S |
| 18 |
# elif (defined(TARGET_OS_TV) && TARGET_OS_TV) |
| 19 |
# define APPLE_HAS_MEMSET_S |
| 20 |
# elif (defined(__WATCH_OS_VERSION_MIN_REQUIRED) && defined(__WATCHOS_2_0) && (__WATCH_OS_VERSION_MIN_REQUIRED >= __WATCHOS_2_0)) |
| 21 |
# define APPLE_HAS_MEMSET_S |
| 22 |
# elif (defined(TARGET_OS_VISION) && TARGET_OS_VISION) |
| 23 |
# define APPLE_HAS_MEMSET_S |
| 24 |
# else |
| 25 |
# undef APPLE_HAS_MEMSET_S |
| 26 |
# endif |
| 27 |
#endif |
| 28 |
|
| 29 |
#if (defined(__APPLE__) && defined(__MACH__)) || defined(__linux__) || defined(__OpenBSD__) |
| 30 |
#define __STDC_WANT_LIB_EXT1__ 1 |
| 31 |
#include <string.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#if defined(__FreeBSD__) || defined(__NetBSD__) |
| 35 |
#include <strings.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#include <stdlib.h> |
| 39 |
#include <stdio.h> |
| 40 |
#include <limits.h> |
| 41 |
#include <inttypes.h> |
| 42 |
|
| 43 |
/* This is now a hand-written header */ |
| 44 |
#include "lib_memzero0.h" |
| 45 |
#include "krml/internal/target.h" |
| 46 |
|
| 47 |
/* The F* formalization talks about the number of elements in the array. The C |
| 48 |
implementation wants a number of bytes in the array. KaRaMeL is aware of this |
| 49 |
and inserts a sizeof multiplication. */ |
| 50 |
void Lib_Memzero0_memzero0(void *dst, uint64_t len) { |
| 51 |
/* This is safe: karamel checks at run-time (if needed) that all object sizes |
| 52 |
fit within a size_t, so the size we receive has been checked at |
| 53 |
allocation-time, possibly via KRML_CHECK_SIZE, to fit in a size_t. */ |
| 54 |
size_t len_ = (size_t) len; |
| 55 |
|
| 56 |
#ifdef _WIN32 |
| 57 |
SecureZeroMemory(dst, len_); |
| 58 |
#elif defined(__APPLE__) && defined(__MACH__) && defined(APPLE_HAS_MEMSET_S) |
| 59 |
memset_s(dst, len_, 0, len_); |
| 60 |
#elif (defined(__linux__) && !defined(LINUX_NO_EXPLICIT_BZERO)) || defined(__FreeBSD__) || defined(__OpenBSD__) |
| 61 |
explicit_bzero(dst, len_); |
| 62 |
#elif defined(__NetBSD__) |
| 63 |
explicit_memset(dst, 0, len_); |
| 64 |
#else |
| 65 |
/* Default implementation for platforms with no particular support. */ |
| 66 |
#warning "Your platform does not support any safe implementation of memzero -- consider a pull request!" |
| 67 |
volatile unsigned char *volatile dst_ = (volatile unsigned char *volatile) dst; |
| 68 |
size_t i = 0U; |
| 69 |
while (i < len_) |
| 70 |
dst_[i++] = 0U; |
| 71 |
#endif |
| 72 |
} |