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