1 /*
2  * virsecureerase.c: Secure clearing of memory
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library.  If not, see
16  * <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #include <config.h>
21 
22 #include "virsecureerase.h"
23 
24 /**
25  * virSecureErase:
26  * @ptr: pointer to memory to clear
27  * @size: size of memory to clear
28  *
29  * Clear @size bytes of memory at @ptr.
30  *
31  * Note that for now this is implemented using memset which is not secure as
32  * it can be optimized out.
33  *
34  * Also note that there are possible leftover direct uses of memset.
35  */
36 void
virSecureErase(void * ptr,size_t size)37 virSecureErase(void *ptr,
38                size_t size)
39 {
40     if (!ptr || size == 0)
41         return;
42 
43     memset(ptr, 0, size);
44 }
45 
46 /**
47  * virSecureEraseString:
48  * @str: String to securely erase
49  */
50 void
virSecureEraseString(char * str)51 virSecureEraseString(char *str)
52 {
53     if (!str)
54         return;
55 
56     virSecureErase(str, strlen(str));
57 }
58