1 #ifndef AWS_COMMON_ZERO_H
2 #define AWS_COMMON_ZERO_H
3 
4 /**
5  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6  * SPDX-License-Identifier: Apache-2.0.
7  */
8 
9 #include <aws/common/stdbool.h>
10 #include <aws/common/stdint.h>
11 
12 #include <string.h>
13 
14 AWS_EXTERN_C_BEGIN
15 
16 /**
17  * Set each byte in the struct to zero.
18  */
19 #define AWS_ZERO_STRUCT(object)                                                                                        \
20     do {                                                                                                               \
21         memset(&(object), 0, sizeof(object));                                                                          \
22     } while (0)
23 
24 /**
25  * Set each byte in the array to zero.
26  * Does not work with arrays of unknown bound.
27  */
28 #define AWS_ZERO_ARRAY(array) memset((void *)(array), 0, sizeof(array))
29 
30 /**
31  * Returns whether each byte in the object is zero.
32  */
33 #ifdef CBMC
34 /* clang-format off */
35 #    define AWS_IS_ZEROED(object)                                                                                      \
36         __CPROVER_forall {                                                                                             \
37             int i;                                                                                                     \
38             (i >= 0 && i < sizeof(object)) ==> ((const uint8_t *)&object)[i] == 0                                      \
39         }
40 /* clang-format on */
41 #else
42 #    define AWS_IS_ZEROED(object) aws_is_mem_zeroed(&(object), sizeof(object))
43 #endif
44 
45 /**
46  * Returns whether each byte is zero.
47  */
48 AWS_STATIC_IMPL
49 bool aws_is_mem_zeroed(const void *buf, size_t bufsize);
50 
51 /**
52  * Securely zeroes a memory buffer. This function will attempt to ensure that
53  * the compiler will not optimize away this zeroing operation.
54  */
55 AWS_COMMON_API
56 void aws_secure_zero(void *pBuf, size_t bufsize);
57 
58 #ifndef AWS_NO_STATIC_IMPL
59 #    include <aws/common/zero.inl>
60 #endif /* AWS_NO_STATIC_IMPL */
61 
62 AWS_EXTERN_C_END
63 
64 #endif /* AWS_COMMON_ZERO_H */
65