| 1 |
/* Originally written by Bodo Moeller for the OpenSSL project. |
| 2 |
* ==================================================================== |
| 3 |
* Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* |
| 9 |
* 1. Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in |
| 14 |
* the documentation and/or other materials provided with the |
| 15 |
* distribution. |
| 16 |
* |
| 17 |
* 3. All advertising materials mentioning features or use of this |
| 18 |
* software must display the following acknowledgment: |
| 19 |
* "This product includes software developed by the OpenSSL Project |
| 20 |
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 21 |
* |
| 22 |
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 23 |
* endorse or promote products derived from this software without |
| 24 |
* prior written permission. For written permission, please contact |
| 25 |
* [email protected]. |
| 26 |
* |
| 27 |
* 5. Products derived from this software may not be called "OpenSSL" |
| 28 |
* nor may "OpenSSL" appear in their names without prior written |
| 29 |
* permission of the OpenSSL Project. |
| 30 |
* |
| 31 |
* 6. Redistributions of any form whatsoever must retain the following |
| 32 |
* acknowledgment: |
| 33 |
* "This product includes software developed by the OpenSSL Project |
| 34 |
* for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 35 |
* |
| 36 |
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 37 |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 38 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 39 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 40 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 41 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 42 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 43 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 44 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 45 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 46 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 47 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 48 |
* ==================================================================== |
| 49 |
* |
| 50 |
* This product includes cryptographic software written by Eric Young |
| 51 |
* ([email protected]). This product includes software written by Tim |
| 52 |
* Hudson ([email protected]). |
| 53 |
* |
| 54 |
*/ |
| 55 |
/* ==================================================================== |
| 56 |
* Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 57 |
* |
| 58 |
* Portions of the attached software ("Contribution") are developed by |
| 59 |
* SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
| 60 |
* |
| 61 |
* The Contribution is licensed pursuant to the OpenSSL open source |
| 62 |
* license provided above. |
| 63 |
* |
| 64 |
* The elliptic curve binary polynomial software is originally written by |
| 65 |
* Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems |
| 66 |
* Laboratories. */ |
| 67 |
|
| 68 |
#ifndef OPENSSL_HEADER_EC_KEY_H |
| 69 |
#define OPENSSL_HEADER_EC_KEY_H |
| 70 |
|
| 71 |
#include <openssl/base.h> |
| 72 |
|
| 73 |
#include <openssl/ec.h> |
| 74 |
#include <openssl/engine.h> |
| 75 |
#include <openssl/ex_data.h> |
| 76 |
|
| 77 |
#if defined(__cplusplus) |
| 78 |
extern "C" { |
| 79 |
#endif |
| 80 |
|
| 81 |
|
| 82 |
/* ec_key.h contains functions that handle elliptic-curve points that are |
| 83 |
* public/private keys. */ |
| 84 |
|
| 85 |
|
| 86 |
/* EC key objects. */ |
| 87 |
|
| 88 |
/* EC_KEY_new returns a fresh |EC_KEY| object or NULL on error. */ |
| 89 |
OPENSSL_EXPORT EC_KEY *EC_KEY_new(void); |
| 90 |
|
| 91 |
/* EC_KEY_new_method acts the same as |EC_KEY_new|, but takes an explicit |
| 92 |
* |ENGINE|. */ |
| 93 |
OPENSSL_EXPORT EC_KEY *EC_KEY_new_method(const ENGINE *engine); |
| 94 |
|
| 95 |
/* EC_KEY_new_by_curve_name returns a fresh EC_KEY for group specified by |nid| |
| 96 |
* or NULL on error. */ |
| 97 |
OPENSSL_EXPORT EC_KEY *EC_KEY_new_by_curve_name(int nid); |
| 98 |
|
| 99 |
/* EC_KEY_free frees all the data owned by |key| and |key| itself. */ |
| 100 |
OPENSSL_EXPORT void EC_KEY_free(EC_KEY *key); |
| 101 |
|
| 102 |
/* EC_KEY_copy sets |dst| equal to |src| and returns |dst| or NULL on error. */ |
| 103 |
OPENSSL_EXPORT EC_KEY *EC_KEY_copy(EC_KEY *dst, const EC_KEY *src); |
| 104 |
|
| 105 |
/* EC_KEY_dup returns a fresh copy of |src| or NULL on error. */ |
| 106 |
OPENSSL_EXPORT EC_KEY *EC_KEY_dup(const EC_KEY *src); |
| 107 |
|
| 108 |
/* EC_KEY_up_ref increases the reference count of |key|. It returns one on |
| 109 |
* success and zero otherwise. */ |
| 110 |
OPENSSL_EXPORT int EC_KEY_up_ref(EC_KEY *key); |
| 111 |
|
| 112 |
/* EC_KEY_is_opaque returns one if |key| is opaque and doesn't expose its key |
| 113 |
* material. Otherwise it return zero. */ |
| 114 |
OPENSSL_EXPORT int EC_KEY_is_opaque(const EC_KEY *key); |
| 115 |
|
| 116 |
/* EC_KEY_get0_group returns a pointer to the |EC_GROUP| object inside |key|. */ |
| 117 |
OPENSSL_EXPORT const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key); |
| 118 |
|
| 119 |
/* EC_KEY_set_group sets the |EC_GROUP| object that |key| will use to |group|. |
| 120 |
* It returns one on success and zero otherwise. */ |
| 121 |
OPENSSL_EXPORT int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group); |
| 122 |
|
| 123 |
/* EC_KEY_get0_private_key returns a pointer to the private key inside |key|. */ |
| 124 |
OPENSSL_EXPORT const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key); |
| 125 |
|
| 126 |
/* EC_KEY_set_private_key sets the private key of |key| to |priv|. It returns |
| 127 |
* one on success and zero otherwise. */ |
| 128 |
OPENSSL_EXPORT int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *prv); |
| 129 |
|
| 130 |
/* EC_KEY_get0_public_key returns a pointer to the public key point inside |
| 131 |
* |key|. */ |
| 132 |
OPENSSL_EXPORT const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key); |
| 133 |
|
| 134 |
/* EC_KEY_set_public_key sets the public key of |key| to |pub|, by copying it. |
| 135 |
* It returns one on success and zero otherwise. */ |
| 136 |
OPENSSL_EXPORT int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub); |
| 137 |
|
| 138 |
#define EC_PKEY_NO_PARAMETERS 0x001 |
| 139 |
#define EC_PKEY_NO_PUBKEY 0x002 |
| 140 |
|
| 141 |
/* EC_KEY_get_enc_flags returns the encoding flags for |key|, which is a |
| 142 |
* bitwise-OR of |EC_PKEY_*| values. */ |
| 143 |
OPENSSL_EXPORT unsigned EC_KEY_get_enc_flags(const EC_KEY *key); |
| 144 |
|
| 145 |
/* EC_KEY_set_enc_flags sets the encoding flags for |key|, which is a |
| 146 |
* bitwise-OR of |EC_PKEY_*| values. */ |
| 147 |
OPENSSL_EXPORT void EC_KEY_set_enc_flags(EC_KEY *key, unsigned flags); |
| 148 |
|
| 149 |
/* EC_KEY_get_conv_form returns the conversation form that will be used by |
| 150 |
* |key|. */ |
| 151 |
OPENSSL_EXPORT point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key); |
| 152 |
|
| 153 |
/* EC_KEY_set_conv_form sets the conversion form to be used by |key|. */ |
| 154 |
OPENSSL_EXPORT void EC_KEY_set_conv_form(EC_KEY *key, |
| 155 |
point_conversion_form_t cform); |
| 156 |
|
| 157 |
/* EC_KEY_check_key performs several checks on |key| (possibly including an |
| 158 |
* expensive check that the public key is in the primary subgroup). It returns |
| 159 |
* one if all checks pass and zero otherwise. If it returns zero then detail |
| 160 |
* about the problem can be found on the error stack. */ |
| 161 |
OPENSSL_EXPORT int EC_KEY_check_key(const EC_KEY *key); |
| 162 |
|
| 163 |
/* EC_KEY_set_public_key_affine_coordinates sets the public key in |key| to |
| 164 |
* (|x|, |y|). It returns one on success and zero otherwise. */ |
| 165 |
OPENSSL_EXPORT int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, |
| 166 |
BIGNUM *x, |
| 167 |
BIGNUM *y); |
| 168 |
|
| 169 |
|
| 170 |
/* Key generation. */ |
| 171 |
|
| 172 |
/* EC_KEY_generate_key generates a random, private key, calculates the |
| 173 |
* corresponding public key and stores both in |key|. It returns one on success |
| 174 |
* or zero otherwise. */ |
| 175 |
OPENSSL_EXPORT int EC_KEY_generate_key(EC_KEY *key); |
| 176 |
|
| 177 |
|
| 178 |
/* Serialisation. */ |
| 179 |
|
| 180 |
/* EC_KEY_parse_private_key parses a DER-encoded ECPrivateKey structure (RFC |
| 181 |
* 5915) from |cbs| and advances |cbs|. It returns a newly-allocated |EC_KEY| or |
| 182 |
* NULL on error. If |group| is non-null, the parameters field of the |
| 183 |
* ECPrivateKey may be omitted (but must match |group| if present). Otherwise, |
| 184 |
* the parameters field is required. */ |
| 185 |
OPENSSL_EXPORT EC_KEY *EC_KEY_parse_private_key(CBS *cbs, |
| 186 |
const EC_GROUP *group); |
| 187 |
|
| 188 |
/* EC_KEY_marshal_private_key marshals |key| as a DER-encoded ECPrivateKey |
| 189 |
* structure (RFC 5915) and appends the result to |cbb|. It returns one on |
| 190 |
* success and zero on failure. |enc_flags| is a combination of |EC_PKEY_*| |
| 191 |
* values and controls whether corresponding fields are omitted. */ |
| 192 |
OPENSSL_EXPORT int EC_KEY_marshal_private_key(CBB *cbb, const EC_KEY *key, |
| 193 |
unsigned enc_flags); |
| 194 |
|
| 195 |
/* EC_KEY_parse_curve_name parses a DER-encoded OBJECT IDENTIFIER as a curve |
| 196 |
* name from |cbs| and advances |cbs|. It returns a newly-allocated |EC_GROUP| |
| 197 |
* or NULL on error. */ |
| 198 |
OPENSSL_EXPORT EC_GROUP *EC_KEY_parse_curve_name(CBS *cbs); |
| 199 |
|
| 200 |
/* EC_KEY_marshal_curve_name marshals |group| as a DER-encoded OBJECT IDENTIFIER |
| 201 |
* and appends the result to |cbb|. It returns one on success and zero on |
| 202 |
* failure. */ |
| 203 |
OPENSSL_EXPORT int EC_KEY_marshal_curve_name(CBB *cbb, const EC_GROUP *group); |
| 204 |
|
| 205 |
/* EC_KEY_parse_parameters parses a DER-encoded ECParameters structure (RFC |
| 206 |
* 5480) from |cbs| and advances |cbs|. It returns a newly-allocated |EC_GROUP| |
| 207 |
* or NULL on error. It supports the namedCurve and specifiedCurve options, but |
| 208 |
* use of specifiedCurve is deprecated. Use |EC_KEY_parse_curve_name| |
| 209 |
* instead. */ |
| 210 |
OPENSSL_EXPORT EC_GROUP *EC_KEY_parse_parameters(CBS *cbs); |
| 211 |
|
| 212 |
|
| 213 |
/* ex_data functions. |
| 214 |
* |
| 215 |
* These functions are wrappers. See |ex_data.h| for details. */ |
| 216 |
|
| 217 |
OPENSSL_EXPORT int EC_KEY_get_ex_new_index(long argl, void *argp, |
| 218 |
CRYPTO_EX_unused *unused, |
| 219 |
CRYPTO_EX_dup *dup_func, |
| 220 |
CRYPTO_EX_free *free_func); |
| 221 |
OPENSSL_EXPORT int EC_KEY_set_ex_data(EC_KEY *r, int idx, void *arg); |
| 222 |
OPENSSL_EXPORT void *EC_KEY_get_ex_data(const EC_KEY *r, int idx); |
| 223 |
|
| 224 |
|
| 225 |
/* ECDSA method. */ |
| 226 |
|
| 227 |
/* ECDSA_FLAG_OPAQUE specifies that this ECDSA_METHOD does not expose its key |
| 228 |
* material. This may be set if, for instance, it is wrapping some other crypto |
| 229 |
* API, like a platform key store. */ |
| 230 |
#define ECDSA_FLAG_OPAQUE 1 |
| 231 |
|
| 232 |
/* ecdsa_method_st is a structure of function pointers for implementing ECDSA. |
| 233 |
* See engine.h. */ |
| 234 |
struct ecdsa_method_st { |
| 235 |
struct openssl_method_common_st common; |
| 236 |
|
| 237 |
void *app_data; |
| 238 |
|
| 239 |
int (*init)(EC_KEY *key); |
| 240 |
int (*finish)(EC_KEY *key); |
| 241 |
|
| 242 |
/* group_order_size returns the number of bytes needed to represent the order |
| 243 |
* of the group. This is used to calculate the maximum size of an ECDSA |
| 244 |
* signature in |ECDSA_size|. */ |
| 245 |
size_t (*group_order_size)(const EC_KEY *key); |
| 246 |
|
| 247 |
/* sign matches the arguments and behaviour of |ECDSA_sign|. */ |
| 248 |
int (*sign)(const uint8_t *digest, size_t digest_len, uint8_t *sig, |
| 249 |
unsigned int *sig_len, EC_KEY *eckey); |
| 250 |
|
| 251 |
/* Ignored. Set this to NULL. */ |
| 252 |
int (*verify)(const uint8_t *digest, size_t digest_len, const uint8_t *sig, |
| 253 |
size_t sig_len, EC_KEY *eckey); |
| 254 |
|
| 255 |
int flags; |
| 256 |
}; |
| 257 |
|
| 258 |
|
| 259 |
/* Deprecated functions. */ |
| 260 |
|
| 261 |
/* EC_KEY_set_asn1_flag does nothing. */ |
| 262 |
OPENSSL_EXPORT void EC_KEY_set_asn1_flag(EC_KEY *key, int flag); |
| 263 |
|
| 264 |
/* d2i_ECPrivateKey parses an ASN.1, DER-encoded, private key from |len| bytes |
| 265 |
* at |*inp|. If |out_key| is not NULL then, on exit, a pointer to the result |
| 266 |
* is in |*out_key|. Note that, even if |*out_key| is already non-NULL on entry, |
| 267 |
* it * will not be written to. Rather, a fresh |EC_KEY| is allocated and the |
| 268 |
* previous * one is freed. On successful exit, |*inp| is advanced past the DER |
| 269 |
* structure. It returns the result or NULL on error. |
| 270 |
* |
| 271 |
* On input, if |*out_key| is non-NULL and has a group configured, the |
| 272 |
* parameters field may be omitted but must match that group if present. |
| 273 |
* |
| 274 |
* Use |EC_KEY_parse_private_key| instead. */ |
| 275 |
OPENSSL_EXPORT EC_KEY *d2i_ECPrivateKey(EC_KEY **out_key, const uint8_t **inp, |
| 276 |
long len); |
| 277 |
|
| 278 |
/* i2d_ECPrivateKey marshals an EC private key from |key| to an ASN.1, DER |
| 279 |
* structure. If |outp| is not NULL then the result is written to |*outp| and |
| 280 |
* |*outp| is advanced just past the output. It returns the number of bytes in |
| 281 |
* the result, whether written or not, or a negative value on error. |
| 282 |
* |
| 283 |
* Use |EC_KEY_marshal_private_key| instead. */ |
| 284 |
OPENSSL_EXPORT int i2d_ECPrivateKey(const EC_KEY *key, uint8_t **outp); |
| 285 |
|
| 286 |
/* d2i_ECParameters parses an ASN.1, DER-encoded, set of EC parameters from |
| 287 |
* |len| bytes at |*inp|. If |out_key| is not NULL then, on exit, a pointer to |
| 288 |
* the result is in |*out_key|. Note that, even if |*out_key| is already |
| 289 |
* non-NULL on entry, it will not be written to. Rather, a fresh |EC_KEY| is |
| 290 |
* allocated and the previous one is freed. On successful exit, |*inp| is |
| 291 |
* advanced past the DER structure. It returns the result or NULL on error. |
| 292 |
* |
| 293 |
* Use |EC_KEY_parse_parameters| or |EC_KEY_parse_curve_name| instead. */ |
| 294 |
OPENSSL_EXPORT EC_KEY *d2i_ECParameters(EC_KEY **out_key, const uint8_t **inp, |
| 295 |
long len); |
| 296 |
|
| 297 |
/* i2d_ECParameters marshals EC parameters from |key| to an ASN.1, DER |
| 298 |
* structure. If |outp| is not NULL then the result is written to |*outp| and |
| 299 |
* |*outp| is advanced just past the output. It returns the number of bytes in |
| 300 |
* the result, whether written or not, or a negative value on error. |
| 301 |
* |
| 302 |
* Use |EC_KEY_marshal_curve_name| instead. */ |
| 303 |
OPENSSL_EXPORT int i2d_ECParameters(const EC_KEY *key, uint8_t **outp); |
| 304 |
|
| 305 |
/* o2i_ECPublicKey parses an EC point from |len| bytes at |*inp| into |
| 306 |
* |*out_key|. Note that this differs from the d2i format in that |*out_key| |
| 307 |
* must be non-NULL with a group set. On successful exit, |*inp| is advanced by |
| 308 |
* |len| bytes. It returns |*out_key| or NULL on error. |
| 309 |
* |
| 310 |
* Use |EC_POINT_oct2point| instead. */ |
| 311 |
OPENSSL_EXPORT EC_KEY *o2i_ECPublicKey(EC_KEY **out_key, const uint8_t **inp, |
| 312 |
long len); |
| 313 |
|
| 314 |
/* i2o_ECPublicKey marshals an EC point from |key|. If |outp| is not NULL then |
| 315 |
* the result is written to |*outp| and |*outp| is advanced just past the |
| 316 |
* output. It returns the number of bytes in the result, whether written or |
| 317 |
* not, or a negative value on error. |
| 318 |
* |
| 319 |
* Use |EC_POINT_point2cbb| instead. */ |
| 320 |
OPENSSL_EXPORT int i2o_ECPublicKey(const EC_KEY *key, unsigned char **outp); |
| 321 |
|
| 322 |
|
| 323 |
#if defined(__cplusplus) |
| 324 |
} /* extern C */ |
| 325 |
#endif |
| 326 |
|
| 327 |
#endif /* OPENSSL_HEADER_EC_KEY_H */ |