| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/auth/private/key_derivation.h> |
| 7 |
|
| 8 |
#include <aws/auth/credentials.h> |
| 9 |
#include <aws/cal/ecc.h> |
| 10 |
#include <aws/cal/hash.h> |
| 11 |
#include <aws/cal/hmac.h> |
| 12 |
#include <aws/common/byte_buf.h> |
| 13 |
#include <aws/common/string.h> |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#define MAX_KEY_DERIVATION_COUNTER_VALUE 254 |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
AWS_STATIC_STRING_FROM_LITERAL(s_1_as_four_bytes_be, "\x00\x00\x00\x01"); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
AWS_STATIC_STRING_FROM_LITERAL(s_256_as_four_bytes_be, "\x00\x00\x01\x00"); |
| 34 |
|
| 35 |
AWS_STRING_FROM_LITERAL(g_signature_type_sigv4a_http_request, "AWS4-ECDSA-P256-SHA256"); |
| 36 |
|
| 37 |
AWS_STATIC_STRING_FROM_LITERAL(s_secret_buffer_prefix, "AWS4A"); |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
static int s_aws_build_fixed_input_buffer( |
| 53 |
struct aws_byte_buf *fixed_input, |
| 54 |
const struct aws_credentials *credentials, |
| 55 |
const uint8_t counter) { |
| 56 |
|
| 57 |
if (counter == 0 || counter > MAX_KEY_DERIVATION_COUNTER_VALUE) { |
| 58 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 59 |
} |
| 60 |
if (!aws_byte_buf_is_valid(fixed_input)) { |
| 61 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 62 |
} |
| 63 |
|
| 64 |
aws_byte_buf_reset(fixed_input, false); |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
struct aws_byte_cursor one_cursor = aws_byte_cursor_from_string(s_1_as_four_bytes_be); |
| 71 |
if (aws_byte_buf_append_dynamic(fixed_input, &one_cursor)) { |
| 72 |
return AWS_OP_ERR; |
| 73 |
} |
| 74 |
|
| 75 |
struct aws_byte_cursor sigv4a_algorithm_cursor = aws_byte_cursor_from_string(g_signature_type_sigv4a_http_request); |
| 76 |
if (aws_byte_buf_append(fixed_input, &sigv4a_algorithm_cursor)) { |
| 77 |
return AWS_OP_ERR; |
| 78 |
} |
| 79 |
|
| 80 |
if (aws_byte_buf_append_byte_dynamic(fixed_input, 0)) { |
| 81 |
return AWS_OP_ERR; |
| 82 |
} |
| 83 |
|
| 84 |
struct aws_byte_cursor access_key_cursor = aws_credentials_get_access_key_id(credentials); |
| 85 |
if (aws_byte_buf_append(fixed_input, &access_key_cursor)) { |
| 86 |
return AWS_OP_ERR; |
| 87 |
} |
| 88 |
|
| 89 |
if (aws_byte_buf_append_byte_dynamic(fixed_input, counter)) { |
| 90 |
return AWS_OP_ERR; |
| 91 |
} |
| 92 |
|
| 93 |
struct aws_byte_cursor encoded_bit_length_cursor = aws_byte_cursor_from_string(s_256_as_four_bytes_be); |
| 94 |
if (aws_byte_buf_append_dynamic(fixed_input, &encoded_bit_length_cursor)) { |
| 95 |
return AWS_OP_ERR; |
| 96 |
} |
| 97 |
|
| 98 |
return AWS_OP_SUCCESS; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
int aws_be_bytes_compare_constant_time( |
| 153 |
const struct aws_byte_buf *lhs_raw_be_bigint, |
| 154 |
const struct aws_byte_buf *rhs_raw_be_bigint, |
| 155 |
int *comparison_result) { |
| 156 |
|
| 157 |
AWS_FATAL_PRECONDITION(aws_byte_buf_is_valid(lhs_raw_be_bigint)); |
| 158 |
AWS_FATAL_PRECONDITION(aws_byte_buf_is_valid(rhs_raw_be_bigint)); |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
const size_t lhs_len = lhs_raw_be_bigint->len; |
| 164 |
if (lhs_len != rhs_raw_be_bigint->len) { |
| 165 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 166 |
} |
| 167 |
|
| 168 |
volatile uint8_t gt = 0; |
| 169 |
volatile uint8_t eq = 1; |
| 170 |
|
| 171 |
const uint8_t *lhs_raw_bytes = lhs_raw_be_bigint->buffer; |
| 172 |
const uint8_t *rhs_raw_bytes = rhs_raw_be_bigint->buffer; |
| 173 |
for (size_t i = 0; i < lhs_len; ++i) { |
| 174 |
volatile int32_t lhs_digit = (int32_t)lhs_raw_bytes[i]; |
| 175 |
volatile int32_t rhs_digit = (int32_t)rhs_raw_bytes[i]; |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
gt |= ((rhs_digit - lhs_digit) >> 31) & eq; |
| 183 |
eq &= (((lhs_digit ^ rhs_digit) - 1) >> 31) & 0x01; |
| 184 |
} |
| 185 |
|
| 186 |
*comparison_result = gt + gt + eq - 1; |
| 187 |
|
| 188 |
return AWS_OP_SUCCESS; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
void aws_be_bytes_add_one_constant_time(struct aws_byte_buf *raw_be_bigint) { |
| 198 |
AWS_FATAL_PRECONDITION(aws_byte_buf_is_valid(raw_be_bigint)); |
| 199 |
|
| 200 |
const size_t byte_count = raw_be_bigint->len; |
| 201 |
|
| 202 |
volatile uint32_t carry = 1; |
| 203 |
uint8_t *raw_bytes = raw_be_bigint->buffer; |
| 204 |
|
| 205 |
for (size_t i = 0; i < byte_count; ++i) { |
| 206 |
const size_t index = byte_count - i - 1; |
| 207 |
|
| 208 |
volatile uint32_t current_digit = raw_bytes[index]; |
| 209 |
current_digit += carry; |
| 210 |
|
| 211 |
carry = (current_digit >> 8) & 0x01; |
| 212 |
|
| 213 |
raw_bytes[index] = (uint8_t)(current_digit & 0xFF); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
static uint8_t s_n_minus_2[32] = { |
| 221 |
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, |
| 222 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 223 |
0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84, |
| 224 |
0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x4F, |
| 225 |
}; |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
enum aws_key_derivation_result { |
| 230 |
AKDR_SUCCESS, |
| 231 |
AKDR_NEXT_COUNTER, |
| 232 |
AKDR_FAILURE, |
| 233 |
}; |
| 234 |
|
| 235 |
static enum aws_key_derivation_result s_aws_derive_ecc_private_key( |
| 236 |
struct aws_byte_buf *private_key_value, |
| 237 |
const struct aws_byte_buf *k0) { |
| 238 |
AWS_FATAL_ASSERT(k0->len == aws_ecc_key_coordinate_byte_size_from_curve_name(AWS_CAL_ECDSA_P256)); |
| 239 |
|
| 240 |
aws_byte_buf_reset(private_key_value, false); |
| 241 |
|
| 242 |
struct aws_byte_buf s_n_minus_2_buf = { |
| 243 |
.allocator = NULL, |
| 244 |
.buffer = s_n_minus_2, |
| 245 |
.capacity = AWS_ARRAY_SIZE(s_n_minus_2), |
| 246 |
.len = AWS_ARRAY_SIZE(s_n_minus_2), |
| 247 |
}; |
| 248 |
|
| 249 |
int comparison_result = 0; |
| 250 |
if (aws_be_bytes_compare_constant_time(k0, &s_n_minus_2_buf, &comparison_result)) { |
| 251 |
return AKDR_FAILURE; |
| 252 |
} |
| 253 |
|
| 254 |
if (comparison_result > 0) { |
| 255 |
return AKDR_NEXT_COUNTER; |
| 256 |
} |
| 257 |
|
| 258 |
struct aws_byte_cursor k0_cursor = aws_byte_cursor_from_buf(k0); |
| 259 |
if (aws_byte_buf_append(private_key_value, &k0_cursor)) { |
| 260 |
return AKDR_FAILURE; |
| 261 |
} |
| 262 |
|
| 263 |
aws_be_bytes_add_one_constant_time(private_key_value); |
| 264 |
|
| 265 |
return AKDR_SUCCESS; |
| 266 |
} |
| 267 |
|
| 268 |
static int s_init_secret_buf( |
| 269 |
struct aws_byte_buf *secret_buf, |
| 270 |
struct aws_allocator *allocator, |
| 271 |
const struct aws_credentials *credentials) { |
| 272 |
|
| 273 |
struct aws_byte_cursor secret_access_key_cursor = aws_credentials_get_secret_access_key(credentials); |
| 274 |
size_t secret_buffer_length = secret_access_key_cursor.len + s_secret_buffer_prefix->len; |
| 275 |
if (aws_byte_buf_init(secret_buf, allocator, secret_buffer_length)) { |
| 276 |
return AWS_OP_ERR; |
| 277 |
} |
| 278 |
|
| 279 |
struct aws_byte_cursor prefix_cursor = aws_byte_cursor_from_string(s_secret_buffer_prefix); |
| 280 |
if (aws_byte_buf_append(secret_buf, &prefix_cursor)) { |
| 281 |
return AWS_OP_ERR; |
| 282 |
} |
| 283 |
|
| 284 |
if (aws_byte_buf_append(secret_buf, &secret_access_key_cursor)) { |
| 285 |
return AWS_OP_ERR; |
| 286 |
} |
| 287 |
|
| 288 |
return AWS_OP_SUCCESS; |
| 289 |
} |
| 290 |
|
| 291 |
struct aws_ecc_key_pair *aws_ecc_key_pair_new_ecdsa_p256_key_from_aws_credentials( |
| 292 |
struct aws_allocator *allocator, |
| 293 |
const struct aws_credentials *credentials) { |
| 294 |
|
| 295 |
if (allocator == NULL || credentials == NULL) { |
| 296 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 297 |
return NULL; |
| 298 |
} |
| 299 |
|
| 300 |
struct aws_ecc_key_pair *ecc_key_pair = NULL; |
| 301 |
|
| 302 |
struct aws_byte_buf fixed_input; |
| 303 |
AWS_ZERO_STRUCT(fixed_input); |
| 304 |
|
| 305 |
struct aws_byte_buf fixed_input_hmac_digest; |
| 306 |
AWS_ZERO_STRUCT(fixed_input_hmac_digest); |
| 307 |
|
| 308 |
struct aws_byte_buf private_key_buf; |
| 309 |
AWS_ZERO_STRUCT(private_key_buf); |
| 310 |
|
| 311 |
struct aws_byte_buf secret_buf; |
| 312 |
AWS_ZERO_STRUCT(secret_buf); |
| 313 |
|
| 314 |
size_t access_key_length = aws_credentials_get_access_key_id(credentials).len; |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
size_t required_fixed_input_capacity = 32 + access_key_length; |
| 321 |
if (aws_byte_buf_init(&fixed_input, allocator, required_fixed_input_capacity)) { |
| 322 |
goto done; |
| 323 |
} |
| 324 |
|
| 325 |
if (aws_byte_buf_init(&fixed_input_hmac_digest, allocator, AWS_SHA256_LEN)) { |
| 326 |
goto done; |
| 327 |
} |
| 328 |
|
| 329 |
size_t key_length = aws_ecc_key_coordinate_byte_size_from_curve_name(AWS_CAL_ECDSA_P256); |
| 330 |
AWS_FATAL_ASSERT(key_length == AWS_SHA256_LEN); |
| 331 |
if (aws_byte_buf_init(&private_key_buf, allocator, key_length)) { |
| 332 |
goto done; |
| 333 |
} |
| 334 |
|
| 335 |
if (s_init_secret_buf(&secret_buf, allocator, credentials)) { |
| 336 |
goto done; |
| 337 |
} |
| 338 |
struct aws_byte_cursor secret_cursor = aws_byte_cursor_from_buf(&secret_buf); |
| 339 |
|
| 340 |
uint8_t counter = 1; |
| 341 |
enum aws_key_derivation_result result = AKDR_NEXT_COUNTER; |
| 342 |
while ((result == AKDR_NEXT_COUNTER) && (counter <= MAX_KEY_DERIVATION_COUNTER_VALUE)) { |
| 343 |
if (s_aws_build_fixed_input_buffer(&fixed_input, credentials, counter++)) { |
| 344 |
break; |
| 345 |
} |
| 346 |
|
| 347 |
aws_byte_buf_reset(&fixed_input_hmac_digest, true); |
| 348 |
|
| 349 |
struct aws_byte_cursor fixed_input_cursor = aws_byte_cursor_from_buf(&fixed_input); |
| 350 |
if (aws_sha256_hmac_compute(allocator, &secret_cursor, &fixed_input_cursor, &fixed_input_hmac_digest, 0)) { |
| 351 |
break; |
| 352 |
} |
| 353 |
|
| 354 |
result = s_aws_derive_ecc_private_key(&private_key_buf, &fixed_input_hmac_digest); |
| 355 |
} |
| 356 |
|
| 357 |
if (result == AKDR_SUCCESS) { |
| 358 |
struct aws_byte_cursor private_key_cursor = aws_byte_cursor_from_buf(&private_key_buf); |
| 359 |
ecc_key_pair = aws_ecc_key_pair_new_from_private_key(allocator, AWS_CAL_ECDSA_P256, &private_key_cursor); |
| 360 |
} |
| 361 |
|
| 362 |
done: |
| 363 |
|
| 364 |
aws_byte_buf_clean_up_secure(&secret_buf); |
| 365 |
aws_byte_buf_clean_up_secure(&private_key_buf); |
| 366 |
aws_byte_buf_clean_up_secure(&fixed_input_hmac_digest); |
| 367 |
aws_byte_buf_clean_up(&fixed_input); |
| 368 |
|
| 369 |
return ecc_key_pair; |
| 370 |
} |