Skip to content

Instantly share code, notes, and snippets.

@mimoo
Last active April 8, 2024 21:17
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mimoo/4d7a898da2f0e05d1a6b2afe0e9289e1 to your computer and use it in GitHub Desktop.
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/
#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__
/*
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