1#ifndef AWS_COMMON_ZERO_H
2#define AWS_COMMON_ZERO_H
3
4/*
5 * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License").
8 * You may not use this file except in compliance with the License.
9 * A copy of the License is located at
10 *
11 * http://aws.amazon.com/apache2.0
12 *
13 * or in the "license" file accompanying this file. This file is distributed
14 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
15 * express or implied. See the License for the specific language governing
16 * permissions and limitations under the License.
17 */
18
19#include <aws/common/stdbool.h>
20#include <aws/common/stdint.h>
21
22#include <string.h>
23
24AWS_EXTERN_C_BEGIN
25
26/**
27 * Set each byte in the struct to zero.
28 */
29#define AWS_ZERO_STRUCT(object) \
30 do { \
31 memset(&(object), 0, sizeof(object)); \
32 } while (0)
33
34/**
35 * Set each byte in the array to zero.
36 * Does not work with arrays of unknown bound.
37 */
38#define AWS_ZERO_ARRAY(array) memset((void *)(array), 0, sizeof(array))
39
40/**
41 * Returns whether each byte in the object is zero.
42 */
43#ifdef CBMC
44/* clang-format off */
45# define AWS_IS_ZEROED(object) \
46 __CPROVER_forall { \
47 int i; \
48 (i >= 0 && i < sizeof(object)) ==> ((const uint8_t *)&object)[i] == 0 \
49 }
50/* clang-format on */
51#else
52# define AWS_IS_ZEROED(object) aws_is_mem_zeroed(&(object), sizeof(object))
53#endif
54
55/**
56 * Returns whether each byte is zero.
57 */
58AWS_STATIC_IMPL
59bool aws_is_mem_zeroed(const void *buf, size_t bufsize);
60
61/**
62 * Securely zeroes a memory buffer. This function will attempt to ensure that
63 * the compiler will not optimize away this zeroing operation.
64 */
65AWS_COMMON_API
66void aws_secure_zero(void *pBuf, size_t bufsize);
67
68#ifndef AWS_NO_STATIC_IMPL
69# include <aws/common/zero.inl>
70#endif /* AWS_NO_STATIC_IMPL */
71
72AWS_EXTERN_C_END
73
74#endif /* AWS_COMMON_ZERO_H */
75