| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include "aws/s3/private/s3_checksums.h" |
| 6 |
#include <aws/cal/hash.h> |
| 7 |
#include <aws/common/byte_buf.h> |
| 8 |
#include <aws/testing/aws_test_harness.h> |
| 9 |
|
| 10 |
#include <s3_checksums_test_case_helper.h> |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
static int s_sha256_nist_test_case_1_fn(struct aws_allocator *allocator, void *ctx) { |
| 17 |
(void)ctx; |
| 18 |
|
| 19 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abc"); |
| 20 |
uint8_t expected[] = { |
| 21 |
0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, |
| 22 |
0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad, |
| 23 |
}; |
| 24 |
struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 25 |
|
| 26 |
return s_verify_checksum_test_case(allocator, &input, &expected_buf, aws_checksum_new, AWS_SCA_SHA256); |
| 27 |
} |
| 28 |
|
| 29 |
AWS_TEST_CASE(sha256_nist_test_case_1, s_sha256_nist_test_case_1_fn) |
| 30 |
|
| 31 |
static int s_sha256_nist_test_case_2_fn(struct aws_allocator *allocator, void *ctx) { |
| 32 |
(void)ctx; |
| 33 |
|
| 34 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str(""); |
| 35 |
uint8_t expected[] = { |
| 36 |
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, |
| 37 |
0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, |
| 38 |
}; |
| 39 |
struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 40 |
|
| 41 |
return s_verify_checksum_test_case(allocator, &input, &expected_buf, aws_checksum_new, AWS_SCA_SHA256); |
| 42 |
} |
| 43 |
|
| 44 |
AWS_TEST_CASE(sha256_nist_test_case_2, s_sha256_nist_test_case_2_fn) |
| 45 |
|
| 46 |
static int s_sha256_nist_test_case_3_fn(struct aws_allocator *allocator, void *ctx) { |
| 47 |
(void)ctx; |
| 48 |
|
| 49 |
struct aws_byte_cursor input = |
| 50 |
aws_byte_cursor_from_c_str("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"); |
| 51 |
uint8_t expected[] = { |
| 52 |
0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39, |
| 53 |
0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1, |
| 54 |
}; |
| 55 |
struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 56 |
|
| 57 |
return s_verify_checksum_test_case(allocator, &input, &expected_buf, aws_checksum_new, AWS_SCA_SHA256); |
| 58 |
} |
| 59 |
|
| 60 |
AWS_TEST_CASE(sha256_nist_test_case_3, s_sha256_nist_test_case_3_fn) |
| 61 |
|
| 62 |
static int s_sha256_nist_test_case_4_fn(struct aws_allocator *allocator, void *ctx) { |
| 63 |
(void)ctx; |
| 64 |
|
| 65 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij" |
| 66 |
"klmghijklmnhijklmnoijklmnopjklmnopqklm" |
| 67 |
"nopqrlmnopqrsmnopqrstnopqrstu"); |
| 68 |
uint8_t expected[] = { |
| 69 |
0xcf, 0x5b, 0x16, 0xa7, 0x78, 0xaf, 0x83, 0x80, 0x03, 0x6c, 0xe5, 0x9e, 0x7b, 0x04, 0x92, 0x37, |
| 70 |
0x0b, 0x24, 0x9b, 0x11, 0xe8, 0xf0, 0x7a, 0x51, 0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1, |
| 71 |
}; |
| 72 |
struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 73 |
|
| 74 |
return s_verify_checksum_test_case(allocator, &input, &expected_buf, aws_checksum_new, AWS_SCA_SHA256); |
| 75 |
} |
| 76 |
|
| 77 |
AWS_TEST_CASE(sha256_nist_test_case_4, s_sha256_nist_test_case_4_fn) |
| 78 |
|
| 79 |
static int s_sha256_nist_test_case_5_fn(struct aws_allocator *allocator, void *ctx) { |
| 80 |
(void)ctx; |
| 81 |
|
| 82 |
aws_s3_library_init(allocator); |
| 83 |
|
| 84 |
struct aws_s3_checksum *checksum = aws_checksum_new(allocator, AWS_SCA_SHA256); |
| 85 |
ASSERT_NOT_NULL(checksum); |
| 86 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str("a"); |
| 87 |
|
| 88 |
for (size_t i = 0; i < 1000000; ++i) { |
| 89 |
ASSERT_SUCCESS(aws_checksum_update(checksum, &input)); |
| 90 |
} |
| 91 |
|
| 92 |
uint8_t output[AWS_SHA256_LEN] = {0}; |
| 93 |
struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output)); |
| 94 |
output_buf.len = 0; |
| 95 |
ASSERT_SUCCESS(aws_checksum_finalize(checksum, &output_buf)); |
| 96 |
|
| 97 |
uint8_t expected[] = { |
| 98 |
0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92, 0x81, 0xa1, 0xc7, 0xe2, 0x84, 0xd7, 0x3e, 0x67, |
| 99 |
0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e, 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0, |
| 100 |
}; |
| 101 |
struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 102 |
ASSERT_BIN_ARRAYS_EQUALS(expected_buf.ptr, expected_buf.len, output_buf.buffer, output_buf.len); |
| 103 |
|
| 104 |
aws_checksum_destroy(checksum); |
| 105 |
|
| 106 |
aws_s3_library_clean_up(); |
| 107 |
|
| 108 |
return AWS_OP_SUCCESS; |
| 109 |
} |
| 110 |
|
| 111 |
AWS_TEST_CASE(sha256_nist_test_case_5, s_sha256_nist_test_case_5_fn) |
| 112 |
|
| 113 |
static int s_sha256_nist_test_case_6_fn(struct aws_allocator *allocator, void *ctx) { |
| 114 |
(void)ctx; |
| 115 |
|
| 116 |
aws_s3_library_init(allocator); |
| 117 |
|
| 118 |
struct aws_s3_checksum *checksum = aws_checksum_new(allocator, AWS_SCA_SHA256); |
| 119 |
ASSERT_NOT_NULL(checksum); |
| 120 |
struct aws_byte_cursor input = |
| 121 |
aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"); |
| 122 |
|
| 123 |
for (size_t i = 0; i < 16777216; ++i) { |
| 124 |
ASSERT_SUCCESS(aws_checksum_update(checksum, &input)); |
| 125 |
} |
| 126 |
|
| 127 |
uint8_t output[AWS_SHA256_LEN] = {0}; |
| 128 |
struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output)); |
| 129 |
output_buf.len = 0; |
| 130 |
ASSERT_SUCCESS(aws_checksum_finalize(checksum, &output_buf)); |
| 131 |
|
| 132 |
uint8_t expected[] = { |
| 133 |
0x50, 0xe7, 0x2a, 0x0e, 0x26, 0x44, 0x2f, 0xe2, 0x55, 0x2d, 0xc3, 0x93, 0x8a, 0xc5, 0x86, 0x58, |
| 134 |
0x22, 0x8c, 0x0c, 0xbf, 0xb1, 0xd2, 0xca, 0x87, 0x2a, 0xe4, 0x35, 0x26, 0x6f, 0xcd, 0x05, 0x5e, |
| 135 |
}; |
| 136 |
|
| 137 |
struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 138 |
ASSERT_BIN_ARRAYS_EQUALS(expected_buf.ptr, expected_buf.len, output_buf.buffer, output_buf.len); |
| 139 |
|
| 140 |
aws_checksum_destroy(checksum); |
| 141 |
|
| 142 |
aws_s3_library_clean_up(); |
| 143 |
|
| 144 |
return AWS_OP_SUCCESS; |
| 145 |
} |
| 146 |
|
| 147 |
AWS_TEST_CASE(sha256_nist_test_case_6, s_sha256_nist_test_case_6_fn) |
| 148 |
|
| 149 |
static int s_sha256_test_invalid_buffer_fn(struct aws_allocator *allocator, void *ctx) { |
| 150 |
(void)ctx; |
| 151 |
|
| 152 |
aws_s3_library_init(allocator); |
| 153 |
|
| 154 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij" |
| 155 |
"klmghijklmnhijklmnoijklmnopjklmnopqklm" |
| 156 |
"nopqrlmnopqrsmnopqrstnopqrstu"); |
| 157 |
uint8_t output[AWS_SHA256_LEN] = {0}; |
| 158 |
struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output)); |
| 159 |
output_buf.len = 1; |
| 160 |
|
| 161 |
ASSERT_ERROR(AWS_ERROR_SHORT_BUFFER, aws_checksum_compute(allocator, AWS_SCA_SHA256, &input, &output_buf)); |
| 162 |
|
| 163 |
aws_s3_library_clean_up(); |
| 164 |
|
| 165 |
return AWS_OP_SUCCESS; |
| 166 |
} |
| 167 |
|
| 168 |
AWS_TEST_CASE(sha256_test_invalid_buffer, s_sha256_test_invalid_buffer_fn) |
| 169 |
|
| 170 |
static int s_sha256_test_oneshot_fn(struct aws_allocator *allocator, void *ctx) { |
| 171 |
(void)ctx; |
| 172 |
|
| 173 |
aws_s3_library_init(allocator); |
| 174 |
|
| 175 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij" |
| 176 |
"klmghijklmnhijklmnoijklmnopjklmnopqklm" |
| 177 |
"nopqrlmnopqrsmnopqrstnopqrstu"); |
| 178 |
uint8_t expected[] = { |
| 179 |
0xcf, 0x5b, 0x16, 0xa7, 0x78, 0xaf, 0x83, 0x80, 0x03, 0x6c, 0xe5, 0x9e, 0x7b, 0x04, 0x92, 0x37, |
| 180 |
0x0b, 0x24, 0x9b, 0x11, 0xe8, 0xf0, 0x7a, 0x51, 0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1, |
| 181 |
}; |
| 182 |
|
| 183 |
uint8_t output[AWS_SHA256_LEN] = {0}; |
| 184 |
struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output)); |
| 185 |
output_buf.len = 0; |
| 186 |
|
| 187 |
ASSERT_SUCCESS(aws_checksum_compute(allocator, AWS_SCA_SHA256, &input, &output_buf)); |
| 188 |
ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected), output_buf.buffer, output_buf.len); |
| 189 |
|
| 190 |
aws_s3_library_clean_up(); |
| 191 |
|
| 192 |
return AWS_OP_SUCCESS; |
| 193 |
} |
| 194 |
|
| 195 |
AWS_TEST_CASE(sha256_test_oneshot, s_sha256_test_oneshot_fn) |
| 196 |
|
| 197 |
static int s_sha256_test_invalid_state_fn(struct aws_allocator *allocator, void *ctx) { |
| 198 |
(void)ctx; |
| 199 |
|
| 200 |
aws_s3_library_init(allocator); |
| 201 |
|
| 202 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij" |
| 203 |
"klmghijklmnhijklmnoijklmnopjklmnopqklm" |
| 204 |
"nopqrlmnopqrsmnopqrstnopqrstu"); |
| 205 |
|
| 206 |
struct aws_s3_checksum *checksum = aws_checksum_new(allocator, AWS_SCA_SHA256); |
| 207 |
ASSERT_NOT_NULL(checksum); |
| 208 |
|
| 209 |
uint8_t output[AWS_SHA256_LEN] = {0}; |
| 210 |
struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output)); |
| 211 |
output_buf.len = 0; |
| 212 |
|
| 213 |
ASSERT_SUCCESS(aws_checksum_update(checksum, &input)); |
| 214 |
ASSERT_SUCCESS(aws_checksum_finalize(checksum, &output_buf)); |
| 215 |
ASSERT_ERROR(AWS_ERROR_INVALID_STATE, aws_checksum_update(checksum, &input)); |
| 216 |
ASSERT_ERROR(AWS_ERROR_INVALID_STATE, aws_checksum_finalize(checksum, &output_buf)); |
| 217 |
|
| 218 |
aws_checksum_destroy(checksum); |
| 219 |
|
| 220 |
aws_s3_library_clean_up(); |
| 221 |
|
| 222 |
return AWS_OP_SUCCESS; |
| 223 |
} |
| 224 |
|
| 225 |
AWS_TEST_CASE(sha256_test_invalid_state, s_sha256_test_invalid_state_fn) |