summaryrefslogtreecommitdiff
path: root/support/dktools/dk4memrs.ctr
blob: 735df8db5a511fab1a57def26ca53bfff302ebd2 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2015-xxxx
license		=	bsd


%%	header

/**	@file
	Secure memory reset (reset byte buffer
	to 0x00 bytes and read the buffer to prevent compilers
	from optimizing the resetting out).
*/

#ifdef __cplusplus
extern "C" {
#endif

/**	Secure memory reset.
	This function does not just reset the bytes to 0,
	it also reads each byte to check it.
	Reading each byte should prevent the compiler from optimizing
	out.
	This function has significantly lower performance than
	dk4mem_reset(), so use it to clear sensitive memory
	(i.e. used for passwords, keys...) only.

	CRT on Windows: Not used.
	@param	ptr	Start address of memory range.
	@param	sz	Size of memory range in bytes.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if ptr is NULL or sz is 0,
	- DK4_E_BUG<br>
	  if one of the bytes in the buffer is not 0 during check.
*/
int
dk4mem_reset_secure(void *ptr, size_t sz, dk4_er_t *erp);

/**	Secure memory release.
	This function does not just reset the bytes to 0 before releasing
	memory, it also reads each byte to check it.
	Reading each byte should prevent the compiler from optimizing
	out.
	This function has significantly lower performance than
	dk4mem_free(), so use it to clear sensitive memory
	(i.e. used for passwords, keys...) only.

	CRT on Windows: Not used.
	@param	ptr	Start address of memory range.
	@param	sz	Size of memory range in bytes.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if ptr is NULL or sz is 0,
	- DK4_E_BUG<br>
	  if one of the bytes in the buffer is not 0 during check.
*/
int
dk4mem_free_secure(void *ptr, size_t sz, dk4_er_t *erp);

#ifdef __cplusplus
}
#endif

/**	Release memory securely (sanitize memory before releasing it).
*/
#define	dk4mem_release_secure(ptr,sz,erp) \
do { if (NULL != ptr) { (void)dk4mem_free_secure(ptr,sz,erp); } ptr = NULL; } while (0)


%%	module

#include "dk4conf.h"

#ifndef STDIO_H_INCLUDED
#include <stdio.h>
#define STDIO_H_INCLUDED 1
#endif
#ifndef STDLIB_H_INCLUDED
#include <stdlib.h>
#define STDLIB_H_INCLUDED 1
#endif
#if DK4_ON_WINDOWS
#ifndef WINDOWS_H_INCLUDED
#include <windows.h>
#define	WINDOWS_H_INCLUDED 1
#endif
#endif

#include "dk4mem.h"

int
dk4mem_reset_secure(void *ptr, size_t sz, dk4_er_t *erp)
{
#if !DK4_ON_WINDOWS
  unsigned char	*p1;
#endif
  int		 back = 0;

  if ((NULL != ptr) && (0 < sz)) {
#if DK4_ON_WINDOWS
    SecureZeroMemory(ptr, sz);
    back = 1;
#else
    DK4_MEMRES(ptr, sz);
    p1 = (unsigned char *)ptr;
    back = 1;
    while(sz--) {
      if ('\0' != *(p1++)) {
        back = 0;
	dk4error_set_simple_error_code(erp, DK4_E_BUG);
      }
    }
#endif
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
  return back;
}



int
dk4mem_free_secure(void *ptr, size_t sz, dk4_er_t *erp)
{
  int	 back	= 0;

  if (NULL != ptr) {
    if (0 < sz) {
      back = dk4mem_reset_secure(ptr, sz, erp);
    } else {
      back = 1;
    }
    dk4mem_free(ptr);
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
  return back;
}