| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 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 |
|
| 73 |
|
| 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 |
|
| 91 |
enum { max_size = 64 * 2 }; |
| 92 |
uint8_t buf[max_size]; |
| 93 |
|
| 94 |
for (size_t size = 1; size <= max_size; ++size) { |
| 95 |
|
| 96 |
memset(buf, 0, size); |
| 97 |
ASSERT_TRUE(aws_is_mem_zeroed(buf, size)); |
| 98 |
|
| 99 |
|
| 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 |
} |