Debian Codegraph @

crt/aws-c-auth/source/key_derivation.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
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 * The maximum number of iterations we will attempt to derive a valid ecc key for. The probability that this counter
17 * value ever gets reached is vanishingly low -- with reasonable uniformity/independence assumptions, it's
18 * approximately
19 *
20 * 2 ^ (-32 * 254)
21 */
22 #define MAX_KEY_DERIVATION_COUNTER_VALUE 254
23
24 /*
25 * The encoding (32-bit, big-endian) of the prefix to the FixedInputString when fed to the hmac function, per
26 * the sigv4a key derivation specification.
27 */
28 AWS_STATIC_STRING_FROM_LITERAL(s_1_as_four_bytes_be, "\x00\x00\x00\x01");
29
30 /*
31 * The encoding (32-bit, big-endian) of the "Length" component of the sigv4a key derivation specification
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 * This constructs the fixed input byte sequence of the Sigv4a key derivation specification. It also includes the
41 * value (0x01 as a 32-bit big endian value) that is pre-pended to the fixed input before invoking the hmac to
42 * generate the candidate key value.
43 *
44 * The final output looks like
45 *
46 * 0x00000001 || "AWS4-ECDSA-P256-SHA256" || 0x00 || AccessKeyId || CounterValue as uint8_t || 0x00000100 (Length)
47 *
48 * From this, we can determine the necessary buffer capacity when setting up the fixed input buffer:
49 *
50 * 4 + 22 + 1 + len(AccessKeyId) + 1 + 4 = 32 + len(AccessKeyId)
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 * A placeholder value that's not actually part of the fixed input string in the spec, but is always this value
68 * and is always the first byte of the hmac-ed string.
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 * aws_be_bytes_compare_constant_time() and aws_be_bytes_add_one_constant_time() are constant-time arithmetic functions
103 * that operate on raw bytes as if they were unbounded integers in a big-endian base 255 format.
104 */
105
106 /*
107 * In the following function gt and eq are updated together. After each update, the variables will be
108 * in one of the following states:
109 *
110 * (1) gt is 0, eq is 1, and from an ordering perspective, lhs == rhs, as checked "so far"
111 * (2) gt is 1, eq is 0, (lhs > rhs)
112 * (3) gt is 0, eq is 0, (lhs < rhs)
113 *
114 * States (2) and (3) are terminal states that cannot be exited since eq is 0 and is the and-wise mask of all
115 * subsequent gt updates. Similarly, once eq is zero it cannot ever become non-zero.
116 *
117 * Intuitively these ideas match the standard way of comparing magnitude equality by considering digit count and
118 * digits from most significant to least significant.
119 *
120 * Let l and r be the the two digits that we are
121 * comparing between lhs and rhs. Assume 0 <= l, r <= 255 seated in 32-bit integers
122 *
123 * gt is maintained by the following bit trick:
124 *
125 * l > r <=>
126 * (r - l) < 0 <=>
127 * (r - l) as an int32 has the high bit set <=>
128 * ((r - l) >> 31) & 0x01 == 1
129 *
130 * eq is maintained by the following bit trick:
131 *
132 * l == r <=>
133 * l ^ r == 0 <=>
134 * (l ^ r) - 1 == -1 <=>
135 * (((l ^ r) - 1) >> 31) & 0x01 == 1
136 *
137 * We apply to the volatile type modifier to attempt to prevent all early-out optimizations that a compiler might
138 * apply if it performed constraint-based reasoning on the logic. This is based on treating volatile
139 * semantically as "this value can change underneath you at any time so you always have to re-read it and cannot
140 * reason statically about program behavior when it reaches a certain value (like 0)"
141 */
142
143 /**
144 * Compares two large unsigned integers in a raw byte format.
145 * The two operands *must* be the same size (simplifies the problem significantly).
146 *
147 * The output parameter comparison_result is set to:
148 * -1 if lhs_raw_be_bigint < rhs_raw_be_bigint
149 * 0 if lhs_raw_be_bigint == rhs_raw_be_bigint
150 * 1 if lhs_raw_be_bigint > rhs_raw_be_bigint
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 * We only need to support comparing byte sequences of the same length here
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 * For each digit, check for a state (1) => (2) ie lhs > rhs, or (1) => (3) ie lhs < rhs transition
179 * based on comparing the two digits in constant time using the ideas explained in the giant comment
180 * block above this function.
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 * Adds one to a large unsigned integer represented by a sequence of bytes.
193 *
194 * A maximal value will roll over to zero. This does not affect the correctness of the users
195 * of this function.
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 /* clang-format off */
218
219 /* In the spec, this is N-2 */
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 /* clang-format on */
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 * This value is calculated based on the format of the fixed input string as described above at
318 * the definition of s_aws_build_fixed_input_buffer()
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 }