Debian Codegraph @

crt/aws-c-cal/source/unix/opensslcrypto_ecc.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 #include <aws/cal/private/ecc.h>
6
7 #include <aws/cal/cal.h>
8 #include <aws/cal/private/der.h>
9
10 #define OPENSSL_SUPPRESS_DEPRECATED
11 #include <openssl/bn.h>
12 #include <openssl/ec.h>
13 #include <openssl/ecdsa.h>
14 #include <openssl/obj_mac.h>
15
16 struct libcrypto_ecc_key {
17 struct aws_ecc_key_pair key_pair;
18 EC_KEY *ec_key;
19 };
20
21 static int s_curve_name_to_nid(enum aws_ecc_curve_name curve_name) {
22 switch (curve_name) {
23 case AWS_CAL_ECDSA_P256:
24 return NID_X9_62_prime256v1;
25 case AWS_CAL_ECDSA_P384:
26 return NID_secp384r1;
27 }
28
29 AWS_FATAL_ASSERT(!"Unsupported elliptic curve name");
30 return -1;
31 }
32
33 static void s_key_pair_destroy(struct aws_ecc_key_pair *key_pair) {
34
35 if (key_pair) {
36 aws_byte_buf_clean_up(&key_pair->pub_x);
37 aws_byte_buf_clean_up(&key_pair->pub_y);
38 aws_byte_buf_clean_up_secure(&key_pair->priv_d);
39
40 struct libcrypto_ecc_key *key_impl = key_pair->impl;
41
42 if (key_impl->ec_key) {
43 EC_KEY_free(key_impl->ec_key);
44 }
45 aws_mem_release(key_pair->allocator, key_pair);
46 }
47 }
48
49 static int s_sign_payload(
50 const struct aws_ecc_key_pair *key_pair,
51 const struct aws_byte_cursor *hash,
52 struct aws_byte_buf *signature_output) {
53 struct libcrypto_ecc_key *libcrypto_key_pair = key_pair->impl;
54
55 unsigned int signature_size = signature_output->capacity - signature_output->len;
56 int ret_val = ECDSA_sign(
57 0,
58 hash->ptr,
59 hash->len,
60 signature_output->buffer + signature_output->len,
61 &signature_size,
62 libcrypto_key_pair->ec_key);
63 signature_output->len += signature_size;
64
65 return ret_val == 1 ? AWS_OP_SUCCESS : aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
66 }
67
68 static int s_verify_payload(
69 const struct aws_ecc_key_pair *key_pair,
70 const struct aws_byte_cursor *hash,
71 const struct aws_byte_cursor *signature) {
72 struct libcrypto_ecc_key *libcrypto_key_pair = key_pair->impl;
73
74 return ECDSA_verify(0, hash->ptr, hash->len, signature->ptr, signature->len, libcrypto_key_pair->ec_key) == 1
75 ? AWS_OP_SUCCESS
76 : aws_raise_error(AWS_ERROR_CAL_SIGNATURE_VALIDATION_FAILED);
77 }
78
79 static size_t s_signature_length(const struct aws_ecc_key_pair *key_pair) {
80 struct libcrypto_ecc_key *libcrypto_key_pair = key_pair->impl;
81
82 return ECDSA_size(libcrypto_key_pair->ec_key);
83 }
84
85 static int s_fill_in_public_key_info(
86 struct libcrypto_ecc_key *libcrypto_key_pair,
87 const EC_GROUP *group,
88 const EC_POINT *pub_key_point) {
89 BIGNUM *big_num_x = BN_new();
90 BIGNUM *big_num_y = BN_new();
91
92 int ret_val = AWS_OP_ERR;
93
94 if (EC_POINT_get_affine_coordinates_GFp(group, pub_key_point, big_num_x, big_num_y, NULL) != 1) {
95 aws_raise_error(AWS_ERROR_INVALID_STATE);
96 goto clean_up;
97 }
98
99 size_t x_coor_size = BN_num_bytes(big_num_x);
100 size_t y_coor_size = BN_num_bytes(big_num_y);
101
102 if (aws_byte_buf_init(&libcrypto_key_pair->key_pair.pub_x, libcrypto_key_pair->key_pair.allocator, x_coor_size)) {
103 goto clean_up;
104 }
105
106 if (aws_byte_buf_init(&libcrypto_key_pair->key_pair.pub_y, libcrypto_key_pair->key_pair.allocator, y_coor_size)) {
107 goto clean_up;
108 }
109
110 BN_bn2bin(big_num_x, libcrypto_key_pair->key_pair.pub_x.buffer);
111 BN_bn2bin(big_num_y, libcrypto_key_pair->key_pair.pub_y.buffer);
112
113 libcrypto_key_pair->key_pair.pub_x.len = x_coor_size;
114 libcrypto_key_pair->key_pair.pub_y.len = y_coor_size;
115
116 ret_val = AWS_OP_SUCCESS;
117
118 clean_up:
119 BN_free(big_num_x);
120 BN_free(big_num_y);
121
122 return ret_val;
123 }
124
125 static int s_derive_public_key(struct aws_ecc_key_pair *key_pair) {
126 struct libcrypto_ecc_key *libcrypto_key_pair = key_pair->impl;
127
128 if (!libcrypto_key_pair->key_pair.priv_d.buffer) {
129 return aws_raise_error(AWS_ERROR_INVALID_STATE);
130 }
131
132 /* we already have a public key. */
133 if (libcrypto_key_pair->key_pair.pub_x.len) {
134 return AWS_OP_SUCCESS;
135 }
136
137 BIGNUM *priv_key_num =
138 BN_bin2bn(libcrypto_key_pair->key_pair.priv_d.buffer, libcrypto_key_pair->key_pair.priv_d.len, NULL);
139
140 const EC_GROUP *group = EC_KEY_get0_group(libcrypto_key_pair->ec_key);
141 EC_POINT *point = EC_POINT_new(group);
142
143 EC_POINT_mul(group, point, priv_key_num, NULL, NULL, NULL);
144 BN_free(priv_key_num);
145
146 EC_KEY_set_public_key(libcrypto_key_pair->ec_key, point);
147 int ret_val = s_fill_in_public_key_info(libcrypto_key_pair, group, point);
148 EC_POINT_free(point);
149 return ret_val;
150 }
151
152 static struct aws_ecc_key_pair_vtable vtable = {
153 .sign_message = s_sign_payload,
154 .verify_signature = s_verify_payload,
155 .derive_pub_key = s_derive_public_key,
156 .signature_length = s_signature_length,
157 .destroy = s_key_pair_destroy,
158 };
159
160 struct aws_ecc_key_pair *aws_ecc_key_pair_new_from_private_key_impl(
161 struct aws_allocator *allocator,
162 enum aws_ecc_curve_name curve_name,
163 const struct aws_byte_cursor *priv_key) {
164
165 size_t key_length = aws_ecc_key_coordinate_byte_size_from_curve_name(curve_name);
166 if (priv_key->len != key_length) {
167 AWS_LOGF_ERROR(AWS_LS_CAL_ECC, "Private key length does not match curve's expected length");
168 aws_raise_error(AWS_ERROR_CAL_INVALID_KEY_LENGTH_FOR_ALGORITHM);
169 return NULL;
170 }
171
172 struct libcrypto_ecc_key *key_impl = aws_mem_calloc(allocator, 1, sizeof(struct libcrypto_ecc_key));
173
174 key_impl->ec_key = EC_KEY_new_by_curve_name(s_curve_name_to_nid(curve_name));
175 key_impl->key_pair.curve_name = curve_name;
176 key_impl->key_pair.allocator = allocator;
177 key_impl->key_pair.vtable = &vtable;
178 key_impl->key_pair.impl = key_impl;
179 aws_atomic_init_int(&key_impl->key_pair.ref_count, 1);
180 aws_byte_buf_init_copy_from_cursor(&key_impl->key_pair.priv_d, allocator, *priv_key);
181
182 BIGNUM *priv_key_num = BN_bin2bn(key_impl->key_pair.priv_d.buffer, key_impl->key_pair.priv_d.len, NULL);
183 if (!EC_KEY_set_private_key(key_impl->ec_key, priv_key_num)) {
184 AWS_LOGF_ERROR(AWS_LS_CAL_ECC, "Failed to set openssl private key");
185 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
186 BN_free(priv_key_num);
187 s_key_pair_destroy(&key_impl->key_pair);
188 return NULL;
189 }
190 BN_free(priv_key_num);
191 return &key_impl->key_pair;
192 }
193
194 struct aws_ecc_key_pair *aws_ecc_key_pair_new_generate_random_impl(
195 struct aws_allocator *allocator,
196 enum aws_ecc_curve_name curve_name) {
197 struct libcrypto_ecc_key *key_impl = aws_mem_calloc(allocator, 1, sizeof(struct libcrypto_ecc_key));
198
199 key_impl->ec_key = EC_KEY_new_by_curve_name(s_curve_name_to_nid(curve_name));
200 key_impl->key_pair.curve_name = curve_name;
201 key_impl->key_pair.allocator = allocator;
202 key_impl->key_pair.vtable = &vtable;
203 key_impl->key_pair.impl = key_impl;
204 aws_atomic_init_int(&key_impl->key_pair.ref_count, 1);
205
206 if (EC_KEY_generate_key(key_impl->ec_key) != 1) {
207 goto error;
208 }
209
210 const EC_POINT *pub_key_point = EC_KEY_get0_public_key(key_impl->ec_key);
211 const EC_GROUP *group = EC_KEY_get0_group(key_impl->ec_key);
212
213 const BIGNUM *private_key_num = EC_KEY_get0_private_key(key_impl->ec_key);
214 size_t priv_key_size = BN_num_bytes(private_key_num);
215 if (aws_byte_buf_init(&key_impl->key_pair.priv_d, allocator, priv_key_size)) {
216 goto error;
217 }
218
219 BN_bn2bin(private_key_num, key_impl->key_pair.priv_d.buffer);
220 key_impl->key_pair.priv_d.len = priv_key_size;
221
222 if (!s_fill_in_public_key_info(key_impl, group, pub_key_point)) {
223 return &key_impl->key_pair;
224 }
225
226 error:
227 s_key_pair_destroy(&key_impl->key_pair);
228 return NULL;
229 }
230
231 static int s_ec_key_set_private_key(EC_KEY *key, struct aws_byte_cursor priv) {
232 BIGNUM *priv_n = BN_bin2bn(priv.ptr, priv.len, NULL);
233 if (!priv_n) {
234 return aws_raise_error(AWS_ERROR_CAL_CRYPTO_OPERATION_FAILED);
235 }
236
237 if (!EC_KEY_set_private_key(key, priv_n)) {
238 BN_free(priv_n);
239 return AWS_ERROR_CAL_CRYPTO_OPERATION_FAILED;
240 }
241
242 BN_free(priv_n);
243 return AWS_OP_SUCCESS;
244 }
245
246 static int s_ec_key_set_public_key(EC_KEY *key, struct aws_byte_cursor pub_x, struct aws_byte_cursor pub_y) {
247 BIGNUM *pub_x_num = BN_bin2bn(pub_x.ptr, pub_x.len, NULL);
248 BIGNUM *pub_y_num = BN_bin2bn(pub_y.ptr, pub_y.len, NULL);
249
250 const EC_GROUP *group = EC_KEY_get0_group(key);
251 EC_POINT *point = EC_POINT_new(group);
252
253 if (EC_POINT_set_affine_coordinates_GFp(group, point, pub_x_num, pub_y_num, NULL) != 1) {
254 goto on_error;
255 }
256
257 if (EC_KEY_set_public_key(key, point) != 1) {
258 goto on_error;
259 }
260
261 EC_POINT_free(point);
262 BN_free(pub_x_num);
263 BN_free(pub_y_num);
264
265 return AWS_OP_SUCCESS;
266
267 on_error:
268 if (point) {
269 EC_POINT_free(point);
270 }
271
272 if (pub_x_num) {
273 BN_free(pub_x_num);
274 }
275
276 if (pub_x_num) {
277 BN_free(pub_y_num);
278 }
279
280 return aws_raise_error(AWS_ERROR_CAL_CRYPTO_OPERATION_FAILED);
281 }
282
283 struct aws_ecc_key_pair *aws_ecc_key_pair_new_from_public_key_impl(
284 struct aws_allocator *allocator,
285 enum aws_ecc_curve_name curve_name,
286 const struct aws_byte_cursor *public_key_x,
287 const struct aws_byte_cursor *public_key_y) {
288 struct libcrypto_ecc_key *key_impl = aws_mem_calloc(allocator, 1, sizeof(struct libcrypto_ecc_key));
289
290 if (!key_impl) {
291 return NULL;
292 }
293
294 key_impl->ec_key = EC_KEY_new_by_curve_name(s_curve_name_to_nid(curve_name));
295 key_impl->key_pair.curve_name = curve_name;
296 key_impl->key_pair.allocator = allocator;
297 key_impl->key_pair.vtable = &vtable;
298 key_impl->key_pair.impl = key_impl;
299 aws_atomic_init_int(&key_impl->key_pair.ref_count, 1);
300
301 aws_byte_buf_init_copy_from_cursor(&key_impl->key_pair.pub_x, allocator, *public_key_x);
302 aws_byte_buf_init_copy_from_cursor(&key_impl->key_pair.pub_y, allocator, *public_key_y);
303
304 if (s_ec_key_set_public_key(key_impl->ec_key, *public_key_x, *public_key_y)) {
305 goto on_error;
306 }
307
308 return &key_impl->key_pair;
309
310 on_error:
311 s_key_pair_destroy(&key_impl->key_pair);
312
313 return NULL;
314 }
315
316 struct aws_ecc_key_pair *aws_ecc_key_pair_new_from_asn1_impl(
317 struct aws_allocator *allocator,
318 const struct aws_byte_cursor *encoded_keys) {
319
320 struct aws_ecc_key_pair *key = NULL;
321 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, *encoded_keys);
322
323 if (!decoder) {
324 return NULL;
325 }
326
327 struct aws_byte_cursor pub_x;
328 struct aws_byte_cursor pub_y;
329 struct aws_byte_cursor priv_d;
330
331 enum aws_ecc_curve_name curve_name;
332 if (aws_der_decoder_load_ecc_key_pair(decoder, &pub_x, &pub_y, &priv_d, &curve_name)) {
333 goto error;
334 }
335
336 if (priv_d.ptr) {
337 struct libcrypto_ecc_key *key_impl = aws_mem_calloc(allocator, 1, sizeof(struct libcrypto_ecc_key));
338 key_impl->key_pair.curve_name = curve_name;
339
340 key_impl->ec_key = EC_KEY_new_by_curve_name(s_curve_name_to_nid(curve_name));
341
342 if (s_ec_key_set_private_key(key_impl->ec_key, priv_d)) {
343 aws_mem_release(allocator, key_impl);
344 aws_raise_error(AWS_ERROR_CAL_CRYPTO_OPERATION_FAILED);
345 goto error;
346 }
347
348 /* if pub is missing, generate it */
349 if (!pub_x.ptr || !pub_y.ptr) {
350 if (!EC_KEY_generate_key(key_impl->ec_key)) {
351 aws_mem_release(allocator, key_impl);
352 aws_raise_error(AWS_ERROR_CAL_CRYPTO_OPERATION_FAILED);
353 goto error;
354 }
355 } else {
356 if (s_ec_key_set_public_key(key_impl->ec_key, pub_x, pub_y)) {
357 aws_mem_release(allocator, key_impl);
358 aws_raise_error(AWS_ERROR_CAL_CRYPTO_OPERATION_FAILED);
359 goto error;
360 }
361 }
362
363 key_impl->key_pair.allocator = allocator;
364 key_impl->key_pair.vtable = &vtable;
365 key_impl->key_pair.impl = key_impl;
366 aws_atomic_init_int(&key_impl->key_pair.ref_count, 1);
367 key = &key_impl->key_pair;
368
369 if (pub_x.ptr) {
370 aws_byte_buf_init_copy_from_cursor(&key->pub_x, allocator, pub_x);
371 }
372
373 if (pub_y.ptr) {
374 aws_byte_buf_init_copy_from_cursor(&key->pub_y, allocator, pub_y);
375 }
376
377 aws_byte_buf_init_copy_from_cursor(&key->priv_d, allocator, priv_d);
378
379 } else {
380 key = aws_ecc_key_pair_new_from_public_key(allocator, curve_name, &pub_x, &pub_y);
381
382 if (!key) {
383 goto error;
384 }
385 }
386
387 aws_der_decoder_destroy(decoder);
388 return key;
389
390 error:
391 aws_der_decoder_destroy(decoder);
392 s_key_pair_destroy(key);
393
394 return NULL;
395 }