Debian Codegraph @

crt/aws-c-cal/include/aws/cal/ecc.h

raw symbols SCIP
1 #ifndef AWS_CAL_ECC_H
2 #define AWS_CAL_ECC_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7 #include <aws/cal/exports.h>
8
9 #include <aws/common/atomics.h>
10 #include <aws/common/byte_buf.h>
11 #include <aws/common/common.h>
12
13 AWS_PUSH_SANE_WARNING_LEVEL
14
15 enum aws_ecc_curve_name {
16 AWS_CAL_ECDSA_P256,
17 AWS_CAL_ECDSA_P384,
18 };
19
20 struct aws_ecc_key_pair;
21
22 typedef void aws_ecc_key_pair_destroy_fn(struct aws_ecc_key_pair *key_pair);
23 typedef int aws_ecc_key_pair_sign_message_fn(
24 const struct aws_ecc_key_pair *key_pair,
25 const struct aws_byte_cursor *message,
26 struct aws_byte_buf *signature_output);
27 typedef int aws_ecc_key_pair_derive_public_key_fn(struct aws_ecc_key_pair *key_pair);
28 typedef int aws_ecc_key_pair_verify_signature_fn(
29 const struct aws_ecc_key_pair *signer,
30 const struct aws_byte_cursor *message,
31 const struct aws_byte_cursor *signature);
32 typedef size_t aws_ecc_key_pair_signature_length_fn(const struct aws_ecc_key_pair *signer);
33
34 struct aws_ecc_key_pair_vtable {
35 aws_ecc_key_pair_destroy_fn *destroy;
36 aws_ecc_key_pair_derive_public_key_fn *derive_pub_key;
37 aws_ecc_key_pair_sign_message_fn *sign_message;
38 aws_ecc_key_pair_verify_signature_fn *verify_signature;
39 aws_ecc_key_pair_signature_length_fn *signature_length;
40 };
41
42 struct aws_ecc_key_pair {
43 struct aws_allocator *allocator;
44 struct aws_atomic_var ref_count;
45 enum aws_ecc_curve_name curve_name;
46 struct aws_byte_buf key_buf;
47 struct aws_byte_buf pub_x;
48 struct aws_byte_buf pub_y;
49 struct aws_byte_buf priv_d;
50 struct aws_ecc_key_pair_vtable *vtable;
51 void *impl;
52 };
53
54 AWS_EXTERN_C_BEGIN
55
56 /**
57 * Adds one to an ecc key pair's ref count.
58 */
59 AWS_CAL_API void aws_ecc_key_pair_acquire(struct aws_ecc_key_pair *key_pair);
60
61 /**
62 * Subtracts one from an ecc key pair's ref count. If ref count reaches zero, the key pair is destroyed.
63 */
64 AWS_CAL_API void aws_ecc_key_pair_release(struct aws_ecc_key_pair *key_pair);
65
66 /**
67 * Creates an Elliptic Curve private key that can be used for signing.
68 * Returns a new instance of aws_ecc_key_pair if the key was successfully built.
69 * Otherwise returns NULL. Note: priv_key::len must match the appropriate length
70 * for the selected curve_name.
71 */
72 AWS_CAL_API struct aws_ecc_key_pair *aws_ecc_key_pair_new_from_private_key(
73 struct aws_allocator *allocator,
74 enum aws_ecc_curve_name curve_name,
75 const struct aws_byte_cursor *priv_key);
76
77 /**
78 * Creates an Elliptic Curve public/private key pair that can be used for signing and verifying.
79 * Returns a new instance of aws_ecc_key_pair if the key was successfully built.
80 * Otherwise returns NULL.
81 */
82 AWS_CAL_API struct aws_ecc_key_pair *aws_ecc_key_pair_new_generate_random(
83 struct aws_allocator *allocator,
84 enum aws_ecc_curve_name curve_name);
85
86 /**
87 * Creates an Elliptic Curve public key that can be used for verifying.
88 * Returns a new instance of aws_ecc_key_pair if the key was successfully built.
89 * Otherwise returns NULL. Note: public_key_x::len and public_key_y::len must
90 * match the appropriate length for the selected curve_name.
91 */
92 AWS_CAL_API struct aws_ecc_key_pair *aws_ecc_key_pair_new_from_public_key(
93 struct aws_allocator *allocator,
94 enum aws_ecc_curve_name curve_name,
95 const struct aws_byte_cursor *public_key_x,
96 const struct aws_byte_cursor *public_key_y);
97
98 /**
99 * Creates an Elliptic Curve public/private key pair from a DER encoded key pair.
100 * Returns a new instance of aws_ecc_key_pair if the key was successfully built.
101 * Otherwise returns NULL. Whether or not signing or verification can be perform depends
102 * on if encoded_keys is a public/private pair or a public key.
103 */
104 AWS_CAL_API struct aws_ecc_key_pair *aws_ecc_key_pair_new_from_asn1(
105 struct aws_allocator *allocator,
106 const struct aws_byte_cursor *encoded_keys);
107
108 /**
109 * Creates an Elliptic curve public key from x and y coordinates encoded as hex strings
110 * Returns a new instance of aws_ecc_key_pair if the key was successfully built.
111 * Otherwise returns NULL.
112 */
113 AWS_CAL_API struct aws_ecc_key_pair *aws_ecc_key_new_from_hex_coordinates(
114 struct aws_allocator *allocator,
115 enum aws_ecc_curve_name curve_name,
116 struct aws_byte_cursor pub_x_hex_cursor,
117 struct aws_byte_cursor pub_y_hex_cursor);
118
119 /**
120 * Derives a public key from the private key if supported by this operating system (not supported on OSX).
121 * key_pair::pub_x and key_pair::pub_y will be set with the raw key buffers.
122 */
123 AWS_CAL_API int aws_ecc_key_pair_derive_public_key(struct aws_ecc_key_pair *key_pair);
124
125 /**
126 * Get the curve name from the oid. OID here is the payload of the DER encoded ASN.1 part (doesn't include
127 * type specifier or length. On success, the value of curve_name will be set.
128 */
129 AWS_CAL_API int aws_ecc_curve_name_from_oid(struct aws_byte_cursor *oid, enum aws_ecc_curve_name *curve_name);
130
131 /**
132 * Get the DER encoded OID from the curve_name. The OID in this case will not contain the type or the length specifier.
133 */
134 AWS_CAL_API int aws_ecc_oid_from_curve_name(enum aws_ecc_curve_name curve_name, struct aws_byte_cursor *oid);
135
136 /**
137 * Uses the key_pair's private key to sign message. The output will be in signature. Signature must be large enough
138 * to hold the signature. Check aws_ecc_key_pair_signature_length() for the appropriate size. Signature will be DER
139 * encoded.
140 *
141 * It is the callers job to make sure message is the appropriate cryptographic digest for this operation. It's usually
142 * something like a SHA256.
143 */
144 AWS_CAL_API int aws_ecc_key_pair_sign_message(
145 const struct aws_ecc_key_pair *key_pair,
146 const struct aws_byte_cursor *message,
147 struct aws_byte_buf *signature);
148
149 /**
150 * Uses the key_pair's public key to verify signature of message. Signature should be DER
151 * encoded.
152 *
153 * It is the callers job to make sure message is the appropriate cryptographic digest for this operation. It's usually
154 * something like a SHA256.
155 *
156 * returns AWS_OP_SUCCESS if the signature is valid.
157 */
158 AWS_CAL_API int aws_ecc_key_pair_verify_signature(
159 const struct aws_ecc_key_pair *key_pair,
160 const struct aws_byte_cursor *message,
161 const struct aws_byte_cursor *signature);
162 AWS_CAL_API size_t aws_ecc_key_pair_signature_length(const struct aws_ecc_key_pair *key_pair);
163
164 AWS_CAL_API void aws_ecc_key_pair_get_public_key(
165 const struct aws_ecc_key_pair *key_pair,
166 struct aws_byte_cursor *pub_x,
167 struct aws_byte_cursor *pub_y);
168
169 AWS_CAL_API void aws_ecc_key_pair_get_private_key(
170 const struct aws_ecc_key_pair *key_pair,
171 struct aws_byte_cursor *private_d);
172
173 AWS_CAL_API size_t aws_ecc_key_coordinate_byte_size_from_curve_name(enum aws_ecc_curve_name curve_name);
174
175 enum aws_ecc_key_export_format {
176 AWS_CAL_ECC_KEY_EXPORT_PRIVATE_SEC1,
177 AWS_CAL_ECC_KEY_EXPORT_PRIVATE_PKCS8,
178 AWS_CAL_ECC_KEY_EXPORT_PUBLIC_SPKI
179 };
180
181 /**
182 * Export key to a specified format.
183 * out should be initialized and have enough space for the key.
184 * returns error if export is not possible.
185 */
186 AWS_CAL_API int aws_ecc_key_pair_export(
187 const struct aws_ecc_key_pair *key_pair,
188 enum aws_ecc_key_export_format format,
189 struct aws_byte_buf *out);
190
191 /*
192 * Helper to decode ECDSA signature from DER format to base components R and S.
193 * Does not pad and returns values as they are in DER. r and s might be smaller than coord size
194 */
195 AWS_CAL_API int aws_ecc_decode_signature_der_to_raw(
196 struct aws_allocator *allocator,
197 struct aws_byte_cursor signature,
198 struct aws_byte_cursor *out_r,
199 struct aws_byte_cursor *out_s);
200
201 /*
202 * Helper to decode ECDSA signature from DER format to padded R || S.
203 * Common jwt format.
204 * Pads each R and S to pad_to.
205 * Returns error if pad_to is less than len of r or s.
206 * use aws_ecc_decode_signature_der_to_raw if you need unpadded values
207 */
208 AWS_CAL_API int aws_ecc_decode_signature_der_to_raw_padded(
209 struct aws_allocator *allocator,
210 struct aws_byte_cursor signature,
211 struct aws_byte_buf *out,
212 size_t pad_to);
213
214 /*
215 * Helper to encode ECDSA signature from raw format (R and S) to DER.
216 * out_signature must be initialized and must be able to fit signature
217 */
218 AWS_CAL_API int aws_ecc_encode_signature_raw_to_der(
219 struct aws_allocator *allocator,
220 struct aws_byte_cursor r,
221 struct aws_byte_cursor s,
222 struct aws_byte_buf *out_signature);
223
224 AWS_EXTERN_C_END
225 AWS_POP_SANE_WARNING_LEVEL
226
227 #endif /* AWS_CAL_ECC_H */