| 
									
										
										
										
											2024-08-13 14:42:19 -07:00
										 |  |  | #if defined(__has_include)
 | 
					
						
							|  |  |  | #if __has_include("config.h")
 | 
					
						
							|  |  |  | #include "config.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | #include <windows.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-18 09:14:16 +03:00
										 |  |  | #if defined(__APPLE__) && defined(__MACH__)
 | 
					
						
							|  |  |  | #include <AvailabilityMacros.h>
 | 
					
						
							| 
									
										
										
										
											2025-05-01 10:57:49 +08:00
										 |  |  | // memset_s is available from macOS 10.9, iOS 7, watchOS 2, and on all tvOS and visionOS versions.
 | 
					
						
							| 
									
										
										
										
											2025-05-16 13:23:11 +03:00
										 |  |  | #  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))
 | 
					
						
							|  |  |  | #    define APPLE_HAS_MEMSET_S
 | 
					
						
							|  |  |  | #  elif (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && defined(__IPHONE_7_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_7_0))
 | 
					
						
							|  |  |  | #    define APPLE_HAS_MEMSET_S
 | 
					
						
							| 
									
										
										
										
											2025-05-01 10:57:49 +08:00
										 |  |  | #  elif (defined(TARGET_OS_TV) && TARGET_OS_TV)
 | 
					
						
							| 
									
										
										
										
											2025-05-16 13:23:11 +03:00
										 |  |  | #    define APPLE_HAS_MEMSET_S
 | 
					
						
							|  |  |  | #  elif (defined(__WATCH_OS_VERSION_MIN_REQUIRED) && defined(__WATCHOS_2_0) && (__WATCH_OS_VERSION_MIN_REQUIRED >= __WATCHOS_2_0))
 | 
					
						
							|  |  |  | #    define APPLE_HAS_MEMSET_S
 | 
					
						
							| 
									
										
										
										
											2025-05-01 10:57:49 +08:00
										 |  |  | #  elif (defined(TARGET_OS_VISION) && TARGET_OS_VISION)
 | 
					
						
							| 
									
										
										
										
											2025-05-16 13:23:11 +03:00
										 |  |  | #    define APPLE_HAS_MEMSET_S
 | 
					
						
							| 
									
										
										
										
											2025-05-01 10:57:49 +08:00
										 |  |  | #  else
 | 
					
						
							| 
									
										
										
										
											2025-05-16 13:23:11 +03:00
										 |  |  | #    undef APPLE_HAS_MEMSET_S
 | 
					
						
							| 
									
										
										
										
											2025-05-01 10:57:49 +08:00
										 |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2024-12-18 09:14:16 +03:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-15 18:42:27 +01:00
										 |  |  | #if (defined(__APPLE__) && defined(__MACH__)) || defined(__linux__) || defined(__OpenBSD__)
 | 
					
						
							| 
									
										
										
										
											2024-08-13 14:42:19 -07:00
										 |  |  | #define __STDC_WANT_LIB_EXT1__ 1
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-06 03:09:04 +03:00
										 |  |  | #if defined(__FreeBSD__) || defined(__NetBSD__)
 | 
					
						
							| 
									
										
										
										
											2024-08-13 14:42:19 -07:00
										 |  |  | #include <strings.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <limits.h>
 | 
					
						
							|  |  |  | #include <inttypes.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* This is now a hand-written header */ | 
					
						
							|  |  |  | #include "lib_memzero0.h"
 | 
					
						
							|  |  |  | #include "krml/internal/target.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* The F* formalization talks about the number of elements in the array. The C
 | 
					
						
							|  |  |  |    implementation wants a number of bytes in the array. KaRaMeL is aware of this | 
					
						
							|  |  |  |    and inserts a sizeof multiplication. */ | 
					
						
							|  |  |  | void Lib_Memzero0_memzero0(void *dst, uint64_t len) { | 
					
						
							|  |  |  |   /* This is safe: karamel checks at run-time (if needed) that all object sizes
 | 
					
						
							|  |  |  |      fit within a size_t, so the size we receive has been checked at | 
					
						
							|  |  |  |      allocation-time, possibly via KRML_CHECK_SIZE, to fit in a size_t. */ | 
					
						
							|  |  |  |   size_t len_ = (size_t) len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2024-11-15 01:22:50 +01:00
										 |  |  |     SecureZeroMemory(dst, len_); | 
					
						
							| 
									
										
										
										
											2025-05-16 13:23:11 +03:00
										 |  |  |   #elif defined(__APPLE__) && defined(__MACH__) && defined(APPLE_HAS_MEMSET_S)
 | 
					
						
							| 
									
										
										
										
											2024-08-13 14:42:19 -07:00
										 |  |  |     memset_s(dst, len_, 0, len_); | 
					
						
							| 
									
										
										
										
											2025-03-15 18:42:27 +01:00
										 |  |  |   #elif (defined(__linux__) && !defined(LINUX_NO_EXPLICIT_BZERO)) || defined(__FreeBSD__) || defined(__OpenBSD__)
 | 
					
						
							| 
									
										
										
										
											2024-08-13 14:42:19 -07:00
										 |  |  |     explicit_bzero(dst, len_); | 
					
						
							|  |  |  |   #elif defined(__NetBSD__)
 | 
					
						
							|  |  |  |     explicit_memset(dst, 0, len_); | 
					
						
							|  |  |  |   #else
 | 
					
						
							|  |  |  |     /* Default implementation for platforms with no particular support. */ | 
					
						
							|  |  |  |     #warning "Your platform does not support any safe implementation of memzero -- consider a pull request!"
 | 
					
						
							|  |  |  |     volatile unsigned char *volatile dst_ = (volatile unsigned char *volatile) dst; | 
					
						
							|  |  |  |     size_t i = 0U; | 
					
						
							| 
									
										
										
										
											2025-05-01 10:57:49 +08:00
										 |  |  |     while (i < len_) | 
					
						
							| 
									
										
										
										
											2024-08-13 14:42:19 -07:00
										 |  |  |       dst_[i++] = 0U; | 
					
						
							|  |  |  |   #endif
 | 
					
						
							|  |  |  | } |