| 1 |
#ifndef AWS_CAL_ECC_H |
| 2 |
#define AWS_CAL_ECC_H |
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 58 |
|
| 59 |
AWS_CAL_API void aws_ecc_key_pair_acquire(struct aws_ecc_key_pair *key_pair); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
AWS_CAL_API void aws_ecc_key_pair_release(struct aws_ecc_key_pair *key_pair); |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 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 |
|
| 79 |
|
| 80 |
|
| 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 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 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 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 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 |
|
| 110 |
|
| 111 |
|
| 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 |
|
| 121 |
|
| 122 |
|
| 123 |
AWS_CAL_API int aws_ecc_key_pair_derive_public_key(struct aws_ecc_key_pair *key_pair); |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 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 |
|
| 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 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 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 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 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 |
|
| 183 |
|
| 184 |
|
| 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 |
|
| 193 |
|
| 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 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 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 |
|
| 216 |
|
| 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 |