Debian Codegraph @

crt/aws-c-cal/source/shared/ed25519.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/cal/ed25519.h>
7 #include <aws/cal/private/opensslcrypto_common.h>
8
9 #include <aws/common/device_random.h>
10 #include <aws/common/encoding.h>
11
12 #include <openssl/evp.h>
13 #include <openssl/objects.h>
14
15 #if defined(OPENSSL_IS_OPENSSL) && OPENSSL_VERSION_NUMBER < 0x10101000L
16 /* ed25519 support does not exist prior to 1.1.1 */
17 # define LIBCRYPTO_DOES_NOT_SUPPORT_ED25519
18 #endif
19
20 struct aws_ed25519_key_pair_impl {
21 struct aws_allocator *allocator;
22 EVP_PKEY *key;
23 };
24
25 int aws_ed25519_key_pair_get_private_key_impl(
26 const struct aws_ed25519_key_pair_impl *key_pair,
27 enum aws_ed25519_key_export_format format,
28 struct aws_byte_buf *out);
29
30 int aws_ed25519_key_pair_get_public_key_impl(
31 const struct aws_ed25519_key_pair_impl *key_pair,
32 enum aws_ed25519_key_export_format format,
33 struct aws_byte_buf *out);
34
35 static const size_t s_private_key_size = 32;
36 static const size_t s_public_key_size = 32;
37
38 int s_byte_buf_write_be32_with_err(struct aws_byte_buf *buf, uint32_t x) {
39 return aws_byte_buf_write_be32(buf, x) ? AWS_OP_SUCCESS : AWS_ERROR_SHORT_BUFFER;
40 }
41
42 void aws_ed25519_key_pair_destroy_impl(struct aws_ed25519_key_pair_impl *key_pair) {
43 if (key_pair == NULL) {
44 return;
45 }
46
47 if (key_pair->key != NULL) {
48 EVP_PKEY_free(key_pair->key);
49 }
50
51 aws_mem_release(key_pair->allocator, key_pair);
52 }
53
54 struct aws_ed25519_key_pair_impl *aws_ed25519_key_pair_new_generate_impl(struct aws_allocator *allocator) {
55 #if defined(LIBCRYPTO_DOES_NOT_SUPPORT_ED25519)
56 /* Compile time check on whether we compiled against libcrypto that supported ed25519
57 * Note: skipping explicit runtime check here because EVP_PKEY_CTX_new_id existed before ed25519 support was added,
58 * but algo was not defined, so ctx init will fail on old versions at runtime.
59 */
60 aws_raise_error(AWS_ERROR_CAL_UNSUPPORTED_ALGORITHM);
61 return NULL;
62 #else
63 EVP_PKEY *pkey = NULL;
64
65 /* Note: nids are not consistent between versions, so we need to do runtime retrieval
66 * to avoid weird issues when building against one version and running against different version. */
67 int nid = OBJ_sn2nid("ED25519");
68 if (nid == NID_undef) {
69 aws_raise_error(AWS_ERROR_CAL_UNSUPPORTED_ALGORITHM);
70 return NULL;
71 }
72
73 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(nid, NULL);
74
75 if (ctx == NULL) {
76 aws_raise_error(AWS_ERROR_CAL_UNSUPPORTED_ALGORITHM);
77 return NULL;
78 }
79
80 if (aws_reinterpret_lc_evp_error_as_crt(EVP_PKEY_keygen_init(ctx), "EVP_PKEY_keygen_init", AWS_LS_CAL_ED25519)) {
81 goto on_error;
82 }
83
84 if (aws_reinterpret_lc_evp_error_as_crt(EVP_PKEY_keygen(ctx, &pkey), "EVP_PKEY_keygen", AWS_LS_CAL_ED25519)) {
85 goto on_error;
86 }
87
88 struct aws_ed25519_key_pair_impl *key_pair = aws_mem_calloc(allocator, 1, sizeof(struct aws_ed25519_key_pair_impl));
89 key_pair->key = pkey;
90 key_pair->allocator = allocator;
91
92 EVP_PKEY_CTX_free(ctx);
93 return key_pair;
94
95 on_error:
96 EVP_PKEY_CTX_free(ctx);
97 return NULL;
98 #endif
99 }
100
101 static struct aws_byte_cursor s_key_type_literal = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("ssh-ed25519");
102
103 int s_ed25519_openssh_encode_public_key(const struct aws_ed25519_key_pair_impl *key_pair, struct aws_byte_buf *out) {
104 if (s_byte_buf_write_be32_with_err(out, (uint32_t)s_key_type_literal.len) != AWS_OP_SUCCESS ||
105 aws_byte_buf_append(out, &s_key_type_literal) != AWS_OP_SUCCESS) {
106 return AWS_OP_ERR;
107 }
108
109 if (s_byte_buf_write_be32_with_err(out, 32) != AWS_OP_SUCCESS ||
110 aws_ed25519_key_pair_get_public_key_impl(key_pair, AWS_CAL_ED25519_KEY_EXPORT_RAW, out) != AWS_OP_SUCCESS) {
111 return AWS_OP_ERR;
112 }
113
114 return AWS_OP_SUCCESS;
115 }
116
117 /**
118 * format here is b64 of the following structure
119 * string "ssh-ed25519" #literal
120 * string key
121 * Note: string is always u32 size followed by the data. all multibyte ints are in big-endian
122 */
123 int s_ed25519_export_public_openssh(const struct aws_ed25519_key_pair_impl *key_pair, struct aws_byte_buf *out) {
124 uint8_t key_data[4 /*id len*/ + 11 /* ssh-ed25519 literal */ + 4 /*key len*/ + 32 /* key */] = {0};
125 struct aws_byte_buf key_buf = aws_byte_buf_from_empty_array(key_data, AWS_ARRAY_SIZE(key_data));
126
127 if (s_ed25519_openssh_encode_public_key(key_pair, &key_buf)) {
128 return AWS_OP_ERR;
129 }
130
131 struct aws_byte_cursor key_cur = aws_byte_cursor_from_buf(&key_buf);
132
133 if (aws_base64_encode(&key_cur, out) != AWS_OP_SUCCESS) {
134 return AWS_OP_ERR;
135 }
136
137 return AWS_OP_SUCCESS;
138 }
139
140 int s_ed25519_export_public_raw(const struct aws_ed25519_key_pair_impl *key_pair, struct aws_byte_buf *out) {
141 #if defined(LIBCRYPTO_DOES_NOT_SUPPORT_ED25519)
142 return aws_raise_error(AWS_ERROR_CAL_UNSUPPORTED_ALGORITHM);
143 #else
144 size_t remaining = out->capacity - out->len;
145 if (remaining < s_public_key_size) {
146 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
147 }
148
149 if (aws_reinterpret_lc_evp_error_as_crt(
150 EVP_PKEY_get_raw_public_key(key_pair->key, out->buffer + out->len, &remaining),
151 "EVP_PKEY_get_raw_public_key",
152 AWS_LS_CAL_ED25519)) {
153 return aws_raise_error(AWS_ERROR_CAL_CRYPTO_OPERATION_FAILED);
154 }
155 if (remaining != s_public_key_size) {
156 return aws_raise_error(AWS_ERROR_CAL_CRYPTO_OPERATION_FAILED);
157 }
158 out->len += s_public_key_size;
159
160 return AWS_OP_SUCCESS;
161 #endif
162 }
163
164 int aws_ed25519_key_pair_get_public_key_impl(
165 const struct aws_ed25519_key_pair_impl *key_pair,
166 enum aws_ed25519_key_export_format format,
167 struct aws_byte_buf *out) {
168 AWS_PRECONDITION(key_pair);
169 AWS_PRECONDITION(aws_byte_buf_is_valid(out));
170
171 switch (format) {
172 case AWS_CAL_ED25519_KEY_EXPORT_RAW:
173 return s_ed25519_export_public_raw(key_pair, out);
174 case AWS_CAL_ED25519_KEY_EXPORT_OPENSSH_B64:
175 return s_ed25519_export_public_openssh(key_pair, out);
176 default:
177 return aws_raise_error(AWS_ERROR_CAL_UNSUPPORTED_KEY_FORMAT);
178 }
179 }
180
181 size_t aws_ed25519_key_pair_get_public_key_size_impl(enum aws_ed25519_key_export_format format) {
182 switch (format) {
183 case AWS_CAL_ED25519_KEY_EXPORT_RAW:
184 return s_public_key_size;
185 case AWS_CAL_ED25519_KEY_EXPORT_OPENSSH_B64:
186 return 68;
187 default:
188 aws_raise_error(AWS_ERROR_CAL_UNSUPPORTED_KEY_FORMAT);
189 return 0;
190 }
191 }
192
193 static struct aws_byte_cursor s_private_magic = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("openssh-key-v1");
194 static struct aws_byte_cursor s_private_none_literal = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("none");
195
196 /**
197 * Openssl only added helpers for this format in 3.0 so we are out of luck with lc and boringssl.
198 * Hence lets just implement export ourselves.
199 * Some of the spec features (encryption, comments) are not supported.
200 * High level format is:
201 * string "openssh-key-v1\0" #literal
202 * string cipher #literal none since we dont support enc
203 * string kdf #literal none since we dont support enc
204 * string kdf options #empty since we dont support enc
205 * u32 num keys
206 * string public key #openssh encoded version
207 * string private key blob
208 * - u32 check #random num
209 * - u32 check #same check repeated
210 * - string "ssh-ed25519" #literal
211 * - string raw pub key
212 * - string raw priv key
213 * - string comment # no comment for now
214 * - padding to 8 bytes # just add bytes 1, 2, 3, ... until priv block is divisible by 8
215 * Note: string is always u32 size followed by the data. all multibyte ints are in big-endian
216 */
217 int s_ed25519_export_private_openssh(const struct aws_ed25519_key_pair_impl *key_pair, struct aws_byte_buf *out) {
218
219 struct aws_byte_buf key_buf;
220 aws_byte_buf_init(&key_buf, key_pair->allocator, 312);
221
222 /* magic */
223 if (aws_byte_buf_append(&key_buf, &s_private_magic) != AWS_OP_SUCCESS) {
224 goto on_error;
225 }
226 if (!aws_byte_buf_write_u8(&key_buf, 0)) {
227 aws_raise_error(AWS_ERROR_SHORT_BUFFER);
228 goto on_error;
229 }
230
231 /* cipher name (we dont support it now, but still need to write out 0) */
232 if (s_byte_buf_write_be32_with_err(&key_buf, 4) != AWS_OP_SUCCESS ||
233 aws_byte_buf_append(&key_buf, &s_private_none_literal) != AWS_OP_SUCCESS) {
234 goto on_error;
235 }
236
237 /* kdf name (we dont support it now, but still need to write out 0) */
238 if (s_byte_buf_write_be32_with_err(&key_buf, 4) != AWS_OP_SUCCESS ||
239 aws_byte_buf_append(&key_buf, &s_private_none_literal) != AWS_OP_SUCCESS) {
240 goto on_error;
241 }
242
243 /* kdf options (we dont support it now, but still need to write out 0) */
244 if (s_byte_buf_write_be32_with_err(&key_buf, 0) != AWS_OP_SUCCESS) {
245 goto on_error;
246 }
247
248 /* number of keys */
249 if (s_byte_buf_write_be32_with_err(&key_buf, 1) != AWS_OP_SUCCESS) {
250 goto on_error;
251 }
252
253 /* encoded public key */
254 const size_t pub_encoded_len = 4 /*id len*/ + 11 /* ssh-ed25519 literal */ + 4 /*key len*/ + 32 /* key */;
255 if (s_byte_buf_write_be32_with_err(&key_buf, (uint32_t)pub_encoded_len) != AWS_OP_SUCCESS ||
256 s_ed25519_openssh_encode_public_key(key_pair, &key_buf) != AWS_OP_SUCCESS) {
257 goto on_error;
258 }
259
260 size_t priv_block_len = 4 + /* check1 */
261 4 + /* check2 */
262 4 + s_key_type_literal.len + /* key type string */
263 4 + s_public_key_size + /* public key */
264 4 + s_private_key_size + s_public_key_size + /* private key (includes public) */
265 4 + 0; /* comment (0, since comment not currently supported) */
266
267 /* pad block to the next multiple of 8 */
268 size_t priv_block_padded_len = (priv_block_len + 7) & ~7;
269
270 if (s_byte_buf_write_be32_with_err(&key_buf, (uint32_t)priv_block_padded_len) != AWS_OP_SUCCESS) {
271 goto on_error;
272 }
273
274 uint32_t check = 0;
275 if (aws_device_random_u32(&check) != AWS_OP_SUCCESS) {
276 aws_raise_error(AWS_ERROR_RANDOM_GEN_FAILED);
277 goto on_error;
278 }
279
280 /* check (and yeah its written twice on purpose) */
281 if (s_byte_buf_write_be32_with_err(&key_buf, check) != AWS_OP_SUCCESS ||
282 s_byte_buf_write_be32_with_err(&key_buf, check) != AWS_OP_SUCCESS) {
283 goto on_error;
284 }
285
286 /* key type */
287 if (s_byte_buf_write_be32_with_err(&key_buf, (uint32_t)s_key_type_literal.len) != AWS_OP_SUCCESS ||
288 aws_byte_buf_append(&key_buf, &s_key_type_literal) != AWS_OP_SUCCESS) {
289 goto on_error;
290 }
291
292 /* public key (raw) */
293 if (s_byte_buf_write_be32_with_err(&key_buf, (uint32_t)s_public_key_size) != AWS_OP_SUCCESS ||
294 aws_ed25519_key_pair_get_public_key_impl(key_pair, AWS_CAL_ED25519_KEY_EXPORT_RAW, &key_buf) !=
295 AWS_OP_SUCCESS) {
296 goto on_error;
297 }
298
299 /* private key - seed + pub (raw) */
300 if (s_byte_buf_write_be32_with_err(&key_buf, (uint32_t)(s_private_key_size + s_public_key_size)) !=
301 AWS_OP_SUCCESS ||
302 aws_ed25519_key_pair_get_private_key_impl(key_pair, AWS_CAL_ED25519_KEY_EXPORT_RAW, &key_buf) !=
303 AWS_OP_SUCCESS ||
304 aws_ed25519_key_pair_get_public_key_impl(key_pair, AWS_CAL_ED25519_KEY_EXPORT_RAW, &key_buf) !=
305 AWS_OP_SUCCESS) {
306 goto on_error;
307 }
308
309 /* comment */
310 if (s_byte_buf_write_be32_with_err(&key_buf, 0) != AWS_OP_SUCCESS) {
311 aws_raise_error(AWS_ERROR_SHORT_BUFFER);
312 goto on_error;
313 }
314
315 /* padding */
316 for (uint8_t i = 1; i < (priv_block_padded_len - priv_block_len + 1); ++i) {
317 if (!aws_byte_buf_write_u8(&key_buf, i)) {
318 aws_raise_error(AWS_ERROR_SHORT_BUFFER);
319 goto on_error;
320 }
321 }
322
323 struct aws_byte_cursor key_cur = aws_byte_cursor_from_buf(&key_buf);
324
325 if (aws_base64_encode(&key_cur, out) != AWS_OP_SUCCESS) {
326 aws_byte_buf_clean_up(&key_buf);
327 return AWS_OP_ERR;
328 }
329
330 aws_byte_buf_clean_up(&key_buf);
331 return AWS_OP_SUCCESS;
332
333 on_error:
334 aws_byte_buf_clean_up(&key_buf);
335 return AWS_OP_ERR;
336 }
337
338 int s_ed25519_export_private_raw(const struct aws_ed25519_key_pair_impl *key_pair, struct aws_byte_buf *out) {
339 #if defined(LIBCRYPTO_DOES_NOT_SUPPORT_ED25519)
340 return aws_raise_error(AWS_ERROR_CAL_UNSUPPORTED_ALGORITHM);
341 #else
342 size_t remaining = out->capacity - out->len;
343 if (remaining < s_private_key_size) {
344 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
345 }
346
347 /**
348 * RFC defines private key to be 64 bytes (seed + pub).
349 * Old versions of openssl did return it that way, but at some point (seems to be around 1.1.1l) they switched to
350 * just returning seed. So for consistency lets also return just the seed.
351 * Which on older versions of openssl just means reading first 32 bytes.
352 */
353 remaining = s_private_key_size;
354
355 if (aws_reinterpret_lc_evp_error_as_crt(
356 EVP_PKEY_get_raw_private_key(key_pair->key, out->buffer + out->len, &remaining),
357 "EVP_PKEY_get_raw_private_key",
358 AWS_LS_CAL_ED25519)) {
359 return aws_raise_error(AWS_ERROR_CAL_CRYPTO_OPERATION_FAILED);
360 }
361
362 if (remaining != s_private_key_size) {
363 return aws_raise_error(AWS_ERROR_CAL_CRYPTO_OPERATION_FAILED);
364 }
365 out->len += s_private_key_size;
366
367 return AWS_OP_SUCCESS;
368 #endif
369 }
370
371 int aws_ed25519_key_pair_get_private_key_impl(
372 const struct aws_ed25519_key_pair_impl *key_pair,
373 enum aws_ed25519_key_export_format format,
374 struct aws_byte_buf *out) {
375 AWS_PRECONDITION(key_pair);
376 AWS_PRECONDITION(aws_byte_buf_is_valid(out));
377
378 switch (format) {
379 case AWS_CAL_ED25519_KEY_EXPORT_RAW:
380 return s_ed25519_export_private_raw(key_pair, out);
381 case AWS_CAL_ED25519_KEY_EXPORT_OPENSSH_B64:
382 return s_ed25519_export_private_openssh(key_pair, out);
383 default:
384 return aws_raise_error(AWS_ERROR_CAL_UNSUPPORTED_KEY_FORMAT);
385 }
386 }
387
388 size_t aws_ed25519_key_pair_get_private_key_size_impl(enum aws_ed25519_key_export_format format) {
389 switch (format) {
390 case AWS_CAL_ED25519_KEY_EXPORT_RAW:
391 return s_private_key_size;
392 case AWS_CAL_ED25519_KEY_EXPORT_OPENSSH_B64:
393 return 312;
394 default:
395 aws_raise_error(AWS_ERROR_CAL_UNSUPPORTED_KEY_FORMAT);
396 return 0;
397 }
398 }