| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/testing/aws_test_harness.h> |
| 7 |
|
| 8 |
#include <aws/auth/credentials.h> |
| 9 |
#include <aws/auth/private/key_derivation.h> |
| 10 |
#include <aws/cal/ecc.h> |
| 11 |
#include <aws/common/encoding.h> |
| 12 |
#include <aws/common/string.h> |
| 13 |
|
| 14 |
struct aws_be_add_one_test { |
| 15 |
uint8_t *input; |
| 16 |
size_t input_length; |
| 17 |
uint8_t *expected_output; |
| 18 |
size_t expected_output_length; |
| 19 |
}; |
| 20 |
|
| 21 |
static uint8_t add_one_input_1[] = {0x00, 0x00, 0x00}; |
| 22 |
static uint8_t add_one_expected_output_1[] = {0x00, 0x00, 0x01}; |
| 23 |
static uint8_t add_one_input_2[] = {0x00, 0x00, 0xFF}; |
| 24 |
static uint8_t add_one_expected_output_2[] = {0x00, 0x01, 0x00}; |
| 25 |
static uint8_t add_one_input_3[] = {0x00, 0xFF, 0xFF}; |
| 26 |
static uint8_t add_one_expected_output_3[] = {0x01, 0x00, 0x00}; |
| 27 |
static uint8_t add_one_input_4[] = {0xFF, 0xFF, 0xFF, 0xFF}; |
| 28 |
static uint8_t add_one_expected_output_4[] = {0x00, 0x00, 0x00, 0x00}; |
| 29 |
|
| 30 |
static struct aws_be_add_one_test s_be_add_one_test_cases[] = { |
| 31 |
{ |
| 32 |
.input = add_one_input_1, |
| 33 |
.input_length = AWS_ARRAY_SIZE(add_one_input_1), |
| 34 |
.expected_output = add_one_expected_output_1, |
| 35 |
.expected_output_length = AWS_ARRAY_SIZE(add_one_expected_output_1), |
| 36 |
}, |
| 37 |
{ |
| 38 |
.input = add_one_input_2, |
| 39 |
.input_length = AWS_ARRAY_SIZE(add_one_input_2), |
| 40 |
.expected_output = add_one_expected_output_2, |
| 41 |
.expected_output_length = AWS_ARRAY_SIZE(add_one_expected_output_2), |
| 42 |
}, |
| 43 |
{ |
| 44 |
.input = add_one_input_3, |
| 45 |
.input_length = AWS_ARRAY_SIZE(add_one_input_3), |
| 46 |
.expected_output = add_one_expected_output_3, |
| 47 |
.expected_output_length = AWS_ARRAY_SIZE(add_one_expected_output_3), |
| 48 |
}, |
| 49 |
{ |
| 50 |
.input = add_one_input_4, |
| 51 |
.input_length = AWS_ARRAY_SIZE(add_one_input_4), |
| 52 |
.expected_output = add_one_expected_output_4, |
| 53 |
.expected_output_length = AWS_ARRAY_SIZE(add_one_expected_output_4), |
| 54 |
}, |
| 55 |
}; |
| 56 |
|
| 57 |
static int s_be_sequence_add_one(struct aws_allocator *allocator, void *ctx) { |
| 58 |
(void)ctx; |
| 59 |
(void)allocator; |
| 60 |
|
| 61 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_be_add_one_test_cases); ++i) { |
| 62 |
struct aws_be_add_one_test *test_case = &s_be_add_one_test_cases[i]; |
| 63 |
|
| 64 |
struct aws_byte_buf input = { |
| 65 |
.len = test_case->input_length, |
| 66 |
.buffer = test_case->input, |
| 67 |
.capacity = test_case->input_length, |
| 68 |
.allocator = NULL, |
| 69 |
}; |
| 70 |
|
| 71 |
aws_be_bytes_add_one_constant_time(&input); |
| 72 |
|
| 73 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 74 |
test_case->expected_output, test_case->expected_output_length, input.buffer, input.len); |
| 75 |
} |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
AWS_TEST_CASE(be_sequence_add_one, s_be_sequence_add_one); |
| 81 |
|
| 82 |
struct aws_be_compare_test { |
| 83 |
uint8_t *lhs; |
| 84 |
size_t lhs_length; |
| 85 |
uint8_t *rhs; |
| 86 |
size_t rhs_length; |
| 87 |
int expected_return_value; |
| 88 |
int expected_result; |
| 89 |
}; |
| 90 |
|
| 91 |
static uint8_t compare_lhs_bad[] = {0x00, 0x00, 0x00}; |
| 92 |
static uint8_t compare_rhs_bad[] = {0x00, 0x00, 0x01, 0xFF}; |
| 93 |
|
| 94 |
static uint8_t compare_lhs_1[] = {0x00, 0x00, 0x00}; |
| 95 |
static uint8_t compare_rhs_1[] = {0x00, 0x00, 0x01}; |
| 96 |
static uint8_t compare_lhs_2[] = {0xAB, 0xCD, 0x80, 0xFF, 0x01, 0x0A}; |
| 97 |
static uint8_t compare_rhs_2[] = {0xAB, 0xCD, 0x80, 0xFF, 0x01, 0x0A}; |
| 98 |
static uint8_t compare_lhs_3[] = {0xFF, 0xCD, 0x80, 0xFF, 0x01, 0x0A}; |
| 99 |
static uint8_t compare_rhs_3[] = {0xFE, 0xCD, 0x80, 0xFF, 0x01, 0x0A}; |
| 100 |
|
| 101 |
static struct aws_be_compare_test s_be_compare_test_cases[] = { |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
{ |
| 106 |
.lhs = compare_lhs_bad, |
| 107 |
.lhs_length = AWS_ARRAY_SIZE(compare_lhs_bad), |
| 108 |
.rhs = compare_rhs_bad, |
| 109 |
.rhs_length = AWS_ARRAY_SIZE(compare_rhs_bad), |
| 110 |
.expected_return_value = AWS_OP_ERR, |
| 111 |
.expected_result = 0, |
| 112 |
}, |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
{ |
| 118 |
.lhs = compare_lhs_1, |
| 119 |
.lhs_length = AWS_ARRAY_SIZE(compare_lhs_1), |
| 120 |
.rhs = compare_rhs_1, |
| 121 |
.rhs_length = AWS_ARRAY_SIZE(compare_rhs_1), |
| 122 |
.expected_return_value = AWS_OP_SUCCESS, |
| 123 |
.expected_result = -1, |
| 124 |
}, |
| 125 |
{ |
| 126 |
.lhs = compare_lhs_2, |
| 127 |
.lhs_length = AWS_ARRAY_SIZE(compare_lhs_2), |
| 128 |
.rhs = compare_rhs_2, |
| 129 |
.rhs_length = AWS_ARRAY_SIZE(compare_rhs_2), |
| 130 |
.expected_return_value = AWS_OP_SUCCESS, |
| 131 |
.expected_result = 0, |
| 132 |
}, |
| 133 |
{ |
| 134 |
.lhs = compare_lhs_3, |
| 135 |
.lhs_length = AWS_ARRAY_SIZE(compare_lhs_3), |
| 136 |
.rhs = compare_rhs_3, |
| 137 |
.rhs_length = AWS_ARRAY_SIZE(compare_rhs_3), |
| 138 |
.expected_return_value = AWS_OP_SUCCESS, |
| 139 |
.expected_result = 1, |
| 140 |
}, |
| 141 |
}; |
| 142 |
|
| 143 |
static int s_be_sequence_compare(struct aws_allocator *allocator, void *ctx) { |
| 144 |
(void)ctx; |
| 145 |
(void)allocator; |
| 146 |
|
| 147 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_be_compare_test_cases); ++i) { |
| 148 |
struct aws_be_compare_test *test_case = &s_be_compare_test_cases[i]; |
| 149 |
|
| 150 |
struct aws_byte_buf lhs = { |
| 151 |
.len = test_case->lhs_length, |
| 152 |
.buffer = test_case->lhs, |
| 153 |
.capacity = test_case->lhs_length, |
| 154 |
.allocator = NULL, |
| 155 |
}; |
| 156 |
|
| 157 |
struct aws_byte_buf rhs = { |
| 158 |
.len = test_case->rhs_length, |
| 159 |
.buffer = test_case->rhs, |
| 160 |
.capacity = test_case->rhs_length, |
| 161 |
.allocator = NULL, |
| 162 |
}; |
| 163 |
|
| 164 |
int comparison_result = 0; |
| 165 |
int result = aws_be_bytes_compare_constant_time(&lhs, &rhs, &comparison_result); |
| 166 |
|
| 167 |
ASSERT_INT_EQUALS(test_case->expected_return_value, result); |
| 168 |
if (result == AWS_OP_SUCCESS) { |
| 169 |
ASSERT_INT_EQUALS(test_case->expected_result, comparison_result); |
| 170 |
} |
| 171 |
|
| 172 |
int swapped_comparison_result = 0; |
| 173 |
int swapped_result = aws_be_bytes_compare_constant_time(&rhs, &lhs, &swapped_comparison_result); |
| 174 |
ASSERT_INT_EQUALS(test_case->expected_return_value, swapped_result); |
| 175 |
if (swapped_result == AWS_OP_SUCCESS) { |
| 176 |
ASSERT_INT_EQUALS(-test_case->expected_result, swapped_comparison_result); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
AWS_TEST_CASE(be_sequence_compare, s_be_sequence_compare); |
| 184 |
|
| 185 |
AWS_STATIC_STRING_FROM_LITERAL(s_ecc_derive_fixed_access_key_id_test_value, "AKISORANDOMAASORANDOM"); |
| 186 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 187 |
s_ecc_derive_fixed_secret_access_key_test_value, |
| 188 |
"q+jcrXGc+0zWN6uzclKVhvMmUsIfRPa4rlRandom"); |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
#ifndef __APPLE__ |
| 194 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 195 |
s_expected_fixed_pub_x, |
| 196 |
"15d242ceebf8d8169fd6a8b5a746c41140414c3b07579038da06af89190fffcb"); |
| 197 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 198 |
s_expected_fixed_pub_y, |
| 199 |
"0515242cedd82e94799482e4c0514b505afccf2c0c98d6a553bf539f424c5ec0"); |
| 200 |
#endif |
| 201 |
|
| 202 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 203 |
s_expected_fixed_private_key, |
| 204 |
"7fd3bd010c0d9c292141c2b77bfbde1042c92e6836fff749d1269ec890fca1bd"); |
| 205 |
|
| 206 |
static int s_verify_fixed_ecc_key_public(struct aws_ecc_key_pair *key, struct aws_allocator *allocator) { |
| 207 |
#ifdef __APPLE__ |
| 208 |
(void)key; |
| 209 |
(void)allocator; |
| 210 |
#else |
| 211 |
aws_ecc_key_pair_derive_public_key(key); |
| 212 |
|
| 213 |
struct aws_byte_cursor pub_x_cursor; |
| 214 |
AWS_ZERO_STRUCT(pub_x_cursor); |
| 215 |
struct aws_byte_cursor pub_y_cursor; |
| 216 |
AWS_ZERO_STRUCT(pub_y_cursor); |
| 217 |
|
| 218 |
aws_ecc_key_pair_get_public_key(key, &pub_x_cursor, &pub_y_cursor); |
| 219 |
|
| 220 |
struct aws_byte_buf pub_coord_x; |
| 221 |
ASSERT_SUCCESS(aws_byte_buf_init(&pub_coord_x, allocator, 128)); |
| 222 |
|
| 223 |
ASSERT_SUCCESS(aws_hex_encode(&pub_x_cursor, &pub_coord_x)); |
| 224 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 225 |
s_expected_fixed_pub_x->bytes, s_expected_fixed_pub_x->len, pub_coord_x.buffer, pub_coord_x.len); |
| 226 |
|
| 227 |
struct aws_byte_buf pub_coord_y; |
| 228 |
ASSERT_SUCCESS(aws_byte_buf_init(&pub_coord_y, allocator, 128)); |
| 229 |
|
| 230 |
ASSERT_SUCCESS(aws_hex_encode(&pub_y_cursor, &pub_coord_y)); |
| 231 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 232 |
s_expected_fixed_pub_y->bytes, s_expected_fixed_pub_y->len, pub_coord_y.buffer, pub_coord_y.len); |
| 233 |
|
| 234 |
aws_byte_buf_clean_up(&pub_coord_x); |
| 235 |
aws_byte_buf_clean_up(&pub_coord_y); |
| 236 |
#endif |
| 237 |
|
| 238 |
return AWS_OP_SUCCESS; |
| 239 |
} |
| 240 |
|
| 241 |
static int s_verify_fixed_ecc_key_private(struct aws_ecc_key_pair *key, struct aws_allocator *allocator) { |
| 242 |
struct aws_byte_cursor private_key_cursor; |
| 243 |
AWS_ZERO_STRUCT(private_key_cursor); |
| 244 |
|
| 245 |
aws_ecc_key_pair_get_private_key(key, &private_key_cursor); |
| 246 |
|
| 247 |
struct aws_byte_buf private_buf; |
| 248 |
ASSERT_SUCCESS(aws_byte_buf_init(&private_buf, allocator, 128)); |
| 249 |
|
| 250 |
ASSERT_SUCCESS(aws_hex_encode(&private_key_cursor, &private_buf)); |
| 251 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 252 |
s_expected_fixed_private_key->bytes, s_expected_fixed_private_key->len, private_buf.buffer, private_buf.len); |
| 253 |
|
| 254 |
aws_byte_buf_clean_up(&private_buf); |
| 255 |
|
| 256 |
return AWS_OP_SUCCESS; |
| 257 |
} |
| 258 |
|
| 259 |
static int s_credentials_derive_ecc_key_fixed(struct aws_allocator *allocator, void *ctx) { |
| 260 |
(void)ctx; |
| 261 |
|
| 262 |
aws_auth_library_init(allocator); |
| 263 |
|
| 264 |
struct aws_credentials *creds = aws_credentials_new_from_string( |
| 265 |
allocator, |
| 266 |
s_ecc_derive_fixed_access_key_id_test_value, |
| 267 |
s_ecc_derive_fixed_secret_access_key_test_value, |
| 268 |
NULL, |
| 269 |
UINT64_MAX); |
| 270 |
|
| 271 |
struct aws_ecc_key_pair *derived_key = aws_ecc_key_pair_new_ecdsa_p256_key_from_aws_credentials(allocator, creds); |
| 272 |
ASSERT_TRUE(derived_key != NULL); |
| 273 |
|
| 274 |
ASSERT_SUCCESS(s_verify_fixed_ecc_key_public(derived_key, allocator)); |
| 275 |
ASSERT_SUCCESS(s_verify_fixed_ecc_key_private(derived_key, allocator)); |
| 276 |
|
| 277 |
aws_ecc_key_pair_release(derived_key); |
| 278 |
aws_credentials_release(creds); |
| 279 |
|
| 280 |
aws_auth_library_clean_up(); |
| 281 |
|
| 282 |
return 0; |
| 283 |
} |
| 284 |
|
| 285 |
AWS_TEST_CASE(credentials_derive_ecc_key_fixed, s_credentials_derive_ecc_key_fixed); |
| 286 |
|
| 287 |
static int s_credentials_new_ecc_fixed(struct aws_allocator *allocator, void *ctx) { |
| 288 |
(void)ctx; |
| 289 |
|
| 290 |
aws_auth_library_init(allocator); |
| 291 |
|
| 292 |
struct aws_credentials *creds = aws_credentials_new_from_string( |
| 293 |
allocator, |
| 294 |
s_ecc_derive_fixed_access_key_id_test_value, |
| 295 |
s_ecc_derive_fixed_secret_access_key_test_value, |
| 296 |
NULL, |
| 297 |
UINT64_MAX); |
| 298 |
|
| 299 |
struct aws_credentials *derived_credentials = aws_credentials_new_ecc_from_aws_credentials(allocator, creds); |
| 300 |
ASSERT_TRUE(derived_credentials != NULL); |
| 301 |
|
| 302 |
struct aws_ecc_key_pair *derived_key = aws_credentials_get_ecc_key_pair(derived_credentials); |
| 303 |
|
| 304 |
ASSERT_SUCCESS(s_verify_fixed_ecc_key_public(derived_key, allocator)); |
| 305 |
ASSERT_SUCCESS(s_verify_fixed_ecc_key_private(derived_key, allocator)); |
| 306 |
|
| 307 |
aws_credentials_release(derived_credentials); |
| 308 |
aws_credentials_release(creds); |
| 309 |
|
| 310 |
aws_auth_library_clean_up(); |
| 311 |
|
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
AWS_TEST_CASE(credentials_new_ecc_fixed, s_credentials_new_ecc_fixed); |
| 316 |
|
| 317 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 318 |
s_ecc_derive_long_access_key_id_test_value, |
| 319 |
"AKISORANDOMAASORANDOMFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" |
| 320 |
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" |
| 321 |
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFf"); |
| 322 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 323 |
s_ecc_derive_long_secret_access_key_test_value, |
| 324 |
"q+jcrXGc+0zWN6uzclKVhvMmUsIfRPa4rlRandom"); |
| 325 |
|
| 326 |
static int s_credentials_derive_ecc_key_long_access_key(struct aws_allocator *allocator, void *ctx) { |
| 327 |
(void)ctx; |
| 328 |
|
| 329 |
aws_auth_library_init(allocator); |
| 330 |
|
| 331 |
struct aws_credentials *creds = aws_credentials_new_from_string( |
| 332 |
allocator, |
| 333 |
s_ecc_derive_long_access_key_id_test_value, |
| 334 |
s_ecc_derive_long_secret_access_key_test_value, |
| 335 |
NULL, |
| 336 |
UINT64_MAX); |
| 337 |
|
| 338 |
struct aws_ecc_key_pair *derived_key = aws_ecc_key_pair_new_ecdsa_p256_key_from_aws_credentials(allocator, creds); |
| 339 |
ASSERT_TRUE(derived_key != NULL); |
| 340 |
|
| 341 |
aws_ecc_key_pair_release(derived_key); |
| 342 |
aws_credentials_release(creds); |
| 343 |
|
| 344 |
aws_auth_library_clean_up(); |
| 345 |
|
| 346 |
return 0; |
| 347 |
} |
| 348 |
|
| 349 |
AWS_TEST_CASE(credentials_derive_ecc_key_long_access_key, s_credentials_derive_ecc_key_long_access_key); |