Debian Codegraph @

crt/aws-c-common/tests/zero_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/byte_buf.h>
7 #include <aws/common/common.h>
8 #include <aws/testing/aws_test_harness.h>
9
10 AWS_TEST_CASE(test_secure_zero, s_test_secure_zero_fn)
11 static int s_test_secure_zero_fn(struct aws_allocator *allocator, void *ctx) {
12 (void)allocator;
13 (void)ctx;
14
15 /* We can't actually test the secure part of the zero operation - anything
16 * we do to observe the buffer will teach the compiler that it needs to
17 * actually zero the buffer (or provide a convincing-enough simulation of
18 * the same). So we'll just test that it behaves like memset.
19 */
20
21 unsigned char buf[16];
22
23 for (size_t i = 0; i < sizeof(buf); i++) {
24 volatile unsigned char *ptr = buf;
25 ptr += i;
26
27 *ptr = (unsigned char)0xDD;
28 }
29
30 aws_secure_zero(buf, sizeof(buf) / 2);
31
32 for (size_t i = 0; i < sizeof(buf); i++) {
33 if (i < sizeof(buf) / 2) {
34 ASSERT_INT_EQUALS(0, buf[i]);
35 } else {
36 ASSERT_INT_EQUALS((unsigned char)0xDD, (unsigned char)buf[i]);
37 }
38 }
39
40 /* check that it's safe to pass NULL */
41 aws_secure_zero(NULL, 0);
42
43 return SUCCESS;
44 }
45
46 AWS_TEST_CASE(test_buffer_secure_zero, s_test_buffer_secure_zero_fn)
47 static int s_test_buffer_secure_zero_fn(struct aws_allocator *allocator, void *ctx) {
48 (void)ctx;
49
50 struct aws_byte_buf buf;
51 size_t len = 27;
52 ASSERT_SUCCESS(aws_byte_buf_init(&buf, allocator, len));
53 buf.len = buf.capacity;
54 for (size_t i = 0; i < len; ++i) {
55 buf.buffer[i] = 0xDD;
56 }
57
58 aws_byte_buf_secure_zero(&buf);
59 for (size_t i = 0; i < len; ++i) {
60 ASSERT_INT_EQUALS(0, buf.buffer[i]);
61 }
62 ASSERT_INT_EQUALS(0, buf.len);
63
64 aws_byte_buf_clean_up(&buf);
65 return SUCCESS;
66 }
67
68 AWS_TEST_CASE(test_buffer_clean_up_secure, s_test_buffer_clean_up_secure_fn)
69 static int s_test_buffer_clean_up_secure_fn(struct aws_allocator *allocator, void *ctx) {
70 (void)ctx;
71
72 /* We cannot test the zeroing of data here, because that would require reading
73 * memory that has already been freed. Simply verifies that there is no memory leak.
74 */
75 struct aws_byte_buf buf;
76 ASSERT_SUCCESS(aws_byte_buf_init(&buf, allocator, 37));
77 aws_byte_buf_clean_up_secure(&buf);
78 ASSERT_INT_EQUALS(buf.len, 0);
79 ASSERT_INT_EQUALS(buf.capacity, 0);
80 ASSERT_NULL(buf.buffer);
81 ASSERT_NULL(buf.allocator);
82 return SUCCESS;
83 }
84
85 AWS_TEST_CASE(is_zeroed, s_test_is_zeroed_fn)
86 static int s_test_is_zeroed_fn(struct aws_allocator *allocator, void *ctx) {
87 (void)allocator;
88 (void)ctx;
89
90 /* Using a value that's 2X the largest amount we check in a single CPU instruction */
91 enum { max_size = 64 * 2 };
92 uint8_t buf[max_size];
93
94 for (size_t size = 1; size <= max_size; ++size) {
95 /* Zero out buffer and check */
96 memset(buf, 0, size);
97 ASSERT_TRUE(aws_is_mem_zeroed(buf, size));
98
99 /* Set 1 byte to be non-zero and check */
100 for (size_t non_zero_byte = 0; non_zero_byte < size; ++non_zero_byte) {
101 buf[non_zero_byte] = 1;
102 ASSERT_FALSE(aws_is_mem_zeroed(buf, size));
103 buf[non_zero_byte] = 0;
104 }
105 }
106
107 return SUCCESS;
108 }