ASCON Suite
ascon-clean.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #if defined(HAVE_CONFIG_H)
24 #include <config.h>
25 #endif
26 #define __STDC_WANT_LIB_EXT1__ 1 /* Detect if the C library has memset_s */
27 #include <ascon/utility.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #if defined(HAVE_STRINGS_H)
31 #include <strings.h>
32 #endif
33 #if defined(_WIN32) || defined(_WIN64)
34 #include <windows.h>
35 #include <wincrypt.h>
36 #endif
37 
38 void ascon_clean(void *buf, unsigned size)
39 {
40  /* The safest way to do this is using SecureZeroMemory(), memset_s(), or
41  * explicit_bzero() so that the compiler will not optimise away the
42  * call to memset() by accident. If that doesn't work, then we fall
43  * back to using volatile pointers which usually works to trick the
44  * compiler, but may not. */
45 #if defined(_WIN32) || defined(_WIN64)
46  SecureZeroMemory(buf, size);
47 #elif defined(__STDC_LIB_EXT1__) || defined(HAVE_MEMSET_S)
48  memset_s(buf, (rsize_t)size, 0, (rsize_t)size);
49 #elif defined(HAVE_EXPLICIT_BZERO)
50  explicit_bzero(buf, size);
51 #else
52  volatile unsigned char *d = (volatile unsigned char *)buf;
53  while (size > 0) {
54  *d++ = 0;
55  --size;
56  }
57 #endif
58 }
void ascon_clean(void *buf, unsigned size)
Cleans a buffer that contains sensitive material.
Definition: ascon-clean.c:38
System utilities of use to applications that use ASCON.