Last active
April 8, 2024 21:17
-
-
Save mimoo/4d7a898da2f0e05d1a6b2afe0e9289e1 to your computer and use it in GitHub Desktop.
Include this file to get the `erase_from_memory` function that zeros memory. See https://www.cryptologie.net/article/419/zeroing-memory-compiler-optimizations-and-memset_s/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __ERASE_FROM_MEMORY_H__ | |
#define __ERASE_FROM_MEMORY_H__ 1 | |
#define __STDC_WANT_LIB_EXT1__ 1 | |
#include <stdlib.h> | |
#include <string.h> | |
void *erase_from_memory(void *pointer, size_t size_data, size_t size_to_remove) { | |
#ifdef __STDC_LIB_EXT1__ | |
memset_s(pointer, size_data, 0, size_to_remove); | |
#else | |
if(size_to_remove > size_data) size_to_remove = size_data; | |
volatile unsigned char *p = pointer; | |
while (size_to_remove--){ | |
*p++ = 0; | |
} | |
#endif | |
return pointer; | |
} | |
#endif // __ERASE_FROM_MEMORY_H__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
gcc test_erase.c erase_from_memory.h | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "erase_from_memory.h" | |
int main(){ | |
char a[6] = "hello"; | |
printf("%s\n", a); | |
erase_from_memory(a, 6, 6); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment