aboutsummaryrefslogtreecommitdiffstats
path: root/librecrypt_wipe.c
blob: a704ef03f6eb270031288964dea0dd96b09e13c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


#if defined(__GNUC__)
# pragma GCC diagnostic ignored "-Wsuggest-attribute=const"
# pragma GCC diagnostic ignored "-Wsuggest-attribute=pure"
#endif


void *(*volatile librecrypt_explicit_memset_____)(void *, int, size_t) = &memset;


#if defined(__clang__) /* before __GNUC__ because that is also set in clang */
# if __has_attribute(optnone)
__attribute__((optnone))
# endif
#elif defined(__GNUC__)
# if __GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ >= 40400
__attribute__((optimize("-O0")))
# endif
#endif
void
librecrypt_wipe(void *buffer, size_t size)
{
	if (buffer && size) {
		buffer = (*librecrypt_explicit_memset_____)(buffer, 0, size);
#if defined(__GNUC__)
		__asm__ volatile ("" :: "g" (buffer) : "memory");
#else
		(void) buffer;
#endif
	}
}


#else


int
main(void)
{
	char *buf;

	SET_UP_ALARM();

#if defined(__GNUC__)
# pragma GCC diagnostic ignored "-Wnonnull"
#endif

	librecrypt_wipe(NULL, 0u);
	librecrypt_wipe(NULL, 4094u);
	librecrypt_wipe((void *)(intptr_t)-1, 0u);

	buf = malloc(256u);
	memset(buf, 99, 256u);
	librecrypt_wipe(buf, 256u);
	free(buf); /* TODO should test memory is wiped */

	return 0;
}


#endif