| 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_sha1_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 |
0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e, |
| 21 |
0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d, |
| 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_sha1_new); |
| 26 |
} |
| 27 |
|
| 28 |
AWS_TEST_CASE(sha1_nist_test_case_1, s_sha1_nist_test_case_1_fn) |
| 29 |
|
| 30 |
static int s_sha1_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 |
0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, |
| 36 |
0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09, |
| 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_sha1_new); |
| 41 |
} |
| 42 |
|
| 43 |
AWS_TEST_CASE(sha1_nist_test_case_2, s_sha1_nist_test_case_2_fn) |
| 44 |
|
| 45 |
static int s_sha1_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 |
0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba, 0xae, |
| 52 |
0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46, 0x70, 0xf1, |
| 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_sha1_new); |
| 57 |
} |
| 58 |
|
| 59 |
AWS_TEST_CASE(sha1_nist_test_case_3, s_sha1_nist_test_case_3_fn) |
| 60 |
|
| 61 |
static int s_sha1_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 |
0xa4, 0x9b, 0x24, 0x46, 0xa0, 0x2c, 0x64, 0x5b, 0xf4, 0x19, |
| 69 |
0xf9, 0x95, 0xb6, 0x70, 0x91, 0x25, 0x3a, 0x04, 0xa2, 0x59, |
| 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_sha1_new); |
| 74 |
} |
| 75 |
|
| 76 |
AWS_TEST_CASE(sha1_nist_test_case_4, s_sha1_nist_test_case_4_fn) |
| 77 |
|
| 78 |
static int s_sha1_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_sha1_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_SHA1_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 |
0x34, 0xaa, 0x97, 0x3c, 0xd4, 0xc4, 0xda, 0xa4, 0xf6, 0x1e, |
| 98 |
0xeb, 0x2b, 0xdb, 0xad, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6f, |
| 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(sha1_nist_test_case_5, s_sha1_nist_test_case_5_fn) |
| 111 |
|
| 112 |
static int s_sha1_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_sha1_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 |
0x34, 0xaa, 0x97, 0x3c, 0xd4, 0xc4, 0xda, 0xa4, 0xf6, 0x1e, 0xeb, 0x2b, 0xdb, 0xad, 0x27, 0x31}; |
| 127 |
struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 128 |
uint8_t output[AWS_SHA1_LEN] = {0}; |
| 129 |
struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, expected_buf.len); |
| 130 |
output_buf.len = 0; |
| 131 |
ASSERT_SUCCESS(aws_hash_finalize(hash, &output_buf, 16)); |
| 132 |
|
| 133 |
ASSERT_BIN_ARRAYS_EQUALS(expected_buf.ptr, expected_buf.len, output_buf.buffer, output_buf.len); |
| 134 |
|
| 135 |
aws_hash_destroy(hash); |
| 136 |
|
| 137 |
aws_cal_library_clean_up(); |
| 138 |
|
| 139 |
return AWS_OP_SUCCESS; |
| 140 |
} |
| 141 |
|
| 142 |
AWS_TEST_CASE(sha1_nist_test_case_5_truncated, s_sha1_nist_test_case_5_truncated_fn) |
| 143 |
|
| 144 |
static int s_sha1_nist_test_case_6_fn(struct aws_allocator *allocator, void *ctx) { |
| 145 |
(void)ctx; |
| 146 |
|
| 147 |
aws_cal_library_test_init(allocator); |
| 148 |
|
| 149 |
struct aws_hash *hash = aws_sha1_new(allocator); |
| 150 |
ASSERT_NOT_NULL(hash); |
| 151 |
struct aws_byte_cursor input = |
| 152 |
aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"); |
| 153 |
|
| 154 |
for (size_t i = 0; i < 16777216; ++i) { |
| 155 |
ASSERT_SUCCESS(aws_hash_update(hash, &input)); |
| 156 |
} |
| 157 |
|
| 158 |
uint8_t output[AWS_SHA1_LEN] = {0}; |
| 159 |
struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output)); |
| 160 |
output_buf.len = 0; |
| 161 |
ASSERT_SUCCESS(aws_hash_finalize(hash, &output_buf, 0)); |
| 162 |
|
| 163 |
uint8_t expected[] = { |
| 164 |
0x77, 0x89, 0xf0, 0xc9, 0xef, 0x7b, 0xfc, 0x40, 0xd9, 0x33, |
| 165 |
0x11, 0x14, 0x3d, 0xfb, 0xe6, 0x9e, 0x20, 0x17, 0xf5, 0x92, |
| 166 |
}; |
| 167 |
|
| 168 |
struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 169 |
ASSERT_BIN_ARRAYS_EQUALS(expected_buf.ptr, expected_buf.len, output_buf.buffer, output_buf.len); |
| 170 |
|
| 171 |
aws_hash_destroy(hash); |
| 172 |
|
| 173 |
aws_cal_library_clean_up(); |
| 174 |
|
| 175 |
return AWS_OP_SUCCESS; |
| 176 |
} |
| 177 |
|
| 178 |
AWS_TEST_CASE(sha1_nist_test_case_6, s_sha1_nist_test_case_6_fn) |
| 179 |
|
| 180 |
static int s_sha1_test_invalid_buffer_fn(struct aws_allocator *allocator, void *ctx) { |
| 181 |
(void)ctx; |
| 182 |
|
| 183 |
aws_cal_library_test_init(allocator); |
| 184 |
|
| 185 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij" |
| 186 |
"klmghijklmnhijklmnoijklmnopjklmnopqklm" |
| 187 |
"nopqrlmnopqrsmnopqrstnopqrstu"); |
| 188 |
uint8_t output[AWS_SHA1_LEN] = {0}; |
| 189 |
struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output)); |
| 190 |
output_buf.len = 1; |
| 191 |
|
| 192 |
ASSERT_ERROR(AWS_ERROR_SHORT_BUFFER, aws_sha1_compute(allocator, &input, &output_buf, 0)); |
| 193 |
|
| 194 |
aws_cal_library_clean_up(); |
| 195 |
|
| 196 |
return AWS_OP_SUCCESS; |
| 197 |
} |
| 198 |
|
| 199 |
AWS_TEST_CASE(sha1_test_invalid_buffer, s_sha1_test_invalid_buffer_fn) |
| 200 |
|
| 201 |
static int s_sha1_test_oneshot_fn(struct aws_allocator *allocator, void *ctx) { |
| 202 |
(void)ctx; |
| 203 |
|
| 204 |
aws_cal_library_test_init(allocator); |
| 205 |
|
| 206 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij" |
| 207 |
"klmghijklmnhijklmnoijklmnopjklmnopqklm" |
| 208 |
"nopqrlmnopqrsmnopqrstnopqrstu"); |
| 209 |
uint8_t expected[] = { |
| 210 |
0xa4, 0x9b, 0x24, 0x46, 0xa0, 0x2c, 0x64, 0x5b, 0xf4, 0x19, |
| 211 |
0xf9, 0x95, 0xb6, 0x70, 0x91, 0x25, 0x3a, 0x04, 0xa2, 0x59, |
| 212 |
}; |
| 213 |
|
| 214 |
uint8_t output[AWS_SHA1_LEN] = {0}; |
| 215 |
struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output)); |
| 216 |
output_buf.len = 0; |
| 217 |
|
| 218 |
ASSERT_SUCCESS(aws_sha1_compute(allocator, &input, &output_buf, 0)); |
| 219 |
ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected), output_buf.buffer, output_buf.len); |
| 220 |
|
| 221 |
aws_cal_library_clean_up(); |
| 222 |
|
| 223 |
return AWS_OP_SUCCESS; |
| 224 |
} |
| 225 |
|
| 226 |
AWS_TEST_CASE(sha1_test_oneshot, s_sha1_test_oneshot_fn) |
| 227 |
|
| 228 |
static int s_sha1_test_invalid_state_fn(struct aws_allocator *allocator, void *ctx) { |
| 229 |
(void)ctx; |
| 230 |
|
| 231 |
aws_cal_library_test_init(allocator); |
| 232 |
|
| 233 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij" |
| 234 |
"klmghijklmnhijklmnoijklmnopjklmnopqklm" |
| 235 |
"nopqrlmnopqrsmnopqrstnopqrstu"); |
| 236 |
|
| 237 |
struct aws_hash *hash = aws_sha1_new(allocator); |
| 238 |
ASSERT_NOT_NULL(hash); |
| 239 |
|
| 240 |
uint8_t output[AWS_SHA1_LEN] = {0}; |
| 241 |
struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output)); |
| 242 |
output_buf.len = 0; |
| 243 |
|
| 244 |
ASSERT_SUCCESS(aws_hash_update(hash, &input)); |
| 245 |
ASSERT_SUCCESS(aws_hash_finalize(hash, &output_buf, 0)); |
| 246 |
ASSERT_ERROR(AWS_ERROR_INVALID_STATE, aws_hash_update(hash, &input)); |
| 247 |
ASSERT_ERROR(AWS_ERROR_INVALID_STATE, aws_hash_finalize(hash, &output_buf, 0)); |
| 248 |
|
| 249 |
aws_hash_destroy(hash); |
| 250 |
|
| 251 |
aws_cal_library_clean_up(); |
| 252 |
|
| 253 |
return AWS_OP_SUCCESS; |
| 254 |
} |
| 255 |
AWS_TEST_CASE(sha1_test_invalid_state, s_sha1_test_invalid_state_fn) |
| 256 |
|
| 257 |
static int s_sha1_test_extra_buffer_space_fn(struct aws_allocator *allocator, void *ctx) { |
| 258 |
(void)ctx; |
| 259 |
|
| 260 |
aws_cal_library_test_init(allocator); |
| 261 |
|
| 262 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str("123456789012345678901234567890123456789012345" |
| 263 |
"67890123456789012345678901234567890"); |
| 264 |
|
| 265 |
struct aws_byte_buf digest_size_buf; |
| 266 |
struct aws_byte_buf super_size_buf; |
| 267 |
|
| 268 |
aws_byte_buf_init(&digest_size_buf, allocator, AWS_SHA1_LEN); |
| 269 |
aws_byte_buf_init(&super_size_buf, allocator, AWS_SHA1_LEN + 100); |
| 270 |
|
| 271 |
aws_sha1_compute(allocator, &input, &digest_size_buf, 0); |
| 272 |
aws_sha1_compute(allocator, &input, &super_size_buf, 0); |
| 273 |
|
| 274 |
ASSERT_TRUE(aws_byte_buf_eq(&digest_size_buf, &super_size_buf)); |
| 275 |
ASSERT_TRUE(super_size_buf.len == AWS_SHA1_LEN); |
| 276 |
|
| 277 |
aws_byte_buf_clean_up(&digest_size_buf); |
| 278 |
aws_byte_buf_clean_up(&super_size_buf); |
| 279 |
|
| 280 |
aws_cal_library_clean_up(); |
| 281 |
|
| 282 |
return AWS_OP_SUCCESS; |
| 283 |
} |
| 284 |
AWS_TEST_CASE(sha1_test_extra_buffer_space, s_sha1_test_extra_buffer_space_fn) |