Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | * openssl-core@openssl.org. |
| 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 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 52 | * Hudson (tjh@cryptsoft.com). |
| 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 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 82 | // ec_key.h contains functions that handle elliptic-curve points that are |
| 83 | // public/private keys. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 84 | |
| 85 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 86 | // EC key objects. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 87 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 88 | // EC_KEY_new returns a fresh |EC_KEY| object or NULL on error. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 89 | OPENSSL_EXPORT EC_KEY *EC_KEY_new(void); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 90 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 91 | // EC_KEY_new_method acts the same as |EC_KEY_new|, but takes an explicit |
| 92 | // |ENGINE|. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 93 | OPENSSL_EXPORT EC_KEY *EC_KEY_new_method(const ENGINE *engine); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 94 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 95 | // EC_KEY_new_by_curve_name returns a fresh EC_KEY for group specified by |nid| |
| 96 | // or NULL on error. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 97 | OPENSSL_EXPORT EC_KEY *EC_KEY_new_by_curve_name(int nid); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 98 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 99 | // EC_KEY_free frees all the data owned by |key| and |key| itself. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 100 | OPENSSL_EXPORT void EC_KEY_free(EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 101 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 102 | // EC_KEY_copy sets |dst| equal to |src| and returns |dst| or NULL on error. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 103 | OPENSSL_EXPORT EC_KEY *EC_KEY_copy(EC_KEY *dst, const EC_KEY *src); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 104 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 105 | // EC_KEY_dup returns a fresh copy of |src| or NULL on error. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 106 | OPENSSL_EXPORT EC_KEY *EC_KEY_dup(const EC_KEY *src); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 107 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 108 | // EC_KEY_up_ref increases the reference count of |key| and returns one. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 109 | OPENSSL_EXPORT int EC_KEY_up_ref(EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 110 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 111 | // EC_KEY_is_opaque returns one if |key| is opaque and doesn't expose its key |
| 112 | // material. Otherwise it return zero. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 113 | OPENSSL_EXPORT int EC_KEY_is_opaque(const EC_KEY *key); |
David Benjamin | ecc0ce7 | 2014-07-18 18:39:42 -0400 | [diff] [blame] | 114 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 115 | // EC_KEY_get0_group returns a pointer to the |EC_GROUP| object inside |key|. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 116 | OPENSSL_EXPORT const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 117 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 118 | // EC_KEY_set_group sets the |EC_GROUP| object that |key| will use to |group|. |
| 119 | // It returns one on success and zero otherwise. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 120 | OPENSSL_EXPORT int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 121 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 122 | // EC_KEY_get0_private_key returns a pointer to the private key inside |key|. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 123 | OPENSSL_EXPORT const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 124 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 125 | // EC_KEY_set_private_key sets the private key of |key| to |priv|. It returns |
| 126 | // one on success and zero otherwise. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 127 | OPENSSL_EXPORT int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *prv); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 128 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 129 | // EC_KEY_get0_public_key returns a pointer to the public key point inside |
| 130 | // |key|. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 131 | OPENSSL_EXPORT const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 132 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 133 | // EC_KEY_set_public_key sets the public key of |key| to |pub|, by copying it. |
| 134 | // It returns one on success and zero otherwise. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 135 | OPENSSL_EXPORT int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 136 | |
| 137 | #define EC_PKEY_NO_PARAMETERS 0x001 |
| 138 | #define EC_PKEY_NO_PUBKEY 0x002 |
| 139 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 140 | // EC_KEY_get_enc_flags returns the encoding flags for |key|, which is a |
| 141 | // bitwise-OR of |EC_PKEY_*| values. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 142 | OPENSSL_EXPORT unsigned EC_KEY_get_enc_flags(const EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 143 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 144 | // EC_KEY_set_enc_flags sets the encoding flags for |key|, which is a |
| 145 | // bitwise-OR of |EC_PKEY_*| values. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 146 | OPENSSL_EXPORT void EC_KEY_set_enc_flags(EC_KEY *key, unsigned flags); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 147 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 148 | // EC_KEY_get_conv_form returns the conversation form that will be used by |
| 149 | // |key|. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 150 | OPENSSL_EXPORT point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 151 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 152 | // EC_KEY_set_conv_form sets the conversion form to be used by |key|. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 153 | OPENSSL_EXPORT void EC_KEY_set_conv_form(EC_KEY *key, |
| 154 | point_conversion_form_t cform); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 155 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 156 | // EC_KEY_check_key performs several checks on |key| (possibly including an |
| 157 | // expensive check that the public key is in the primary subgroup). It returns |
| 158 | // one if all checks pass and zero otherwise. If it returns zero then detail |
| 159 | // about the problem can be found on the error stack. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 160 | OPENSSL_EXPORT int EC_KEY_check_key(const EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 161 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 162 | // EC_KEY_check_fips performs a signing pairwise consistency test (FIPS 140-2 |
| 163 | // 4.9.2). It returns one if it passes and zero otherwise. |
Steven Valdez | 400d0b7 | 2017-04-06 14:55:18 -0400 | [diff] [blame] | 164 | OPENSSL_EXPORT int EC_KEY_check_fips(const EC_KEY *key); |
| 165 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 166 | // EC_KEY_set_public_key_affine_coordinates sets the public key in |key| to |
| 167 | // (|x|, |y|). It returns one on success and zero otherwise. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 168 | OPENSSL_EXPORT int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, |
| 169 | BIGNUM *x, |
| 170 | BIGNUM *y); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 171 | |
| 172 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 173 | // Key generation. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 174 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 175 | // EC_KEY_generate_key generates a random, private key, calculates the |
| 176 | // corresponding public key and stores both in |key|. It returns one on success |
| 177 | // or zero otherwise. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 178 | OPENSSL_EXPORT int EC_KEY_generate_key(EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 179 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 180 | // EC_KEY_generate_key_fips behaves like |EC_KEY_generate_key| but performs |
| 181 | // additional checks for FIPS compliance. |
Steven Valdez | 467d322 | 2017-05-16 14:35:22 -0400 | [diff] [blame] | 182 | OPENSSL_EXPORT int EC_KEY_generate_key_fips(EC_KEY *key); |
| 183 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 184 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 185 | // Serialisation. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 186 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 187 | // EC_KEY_parse_private_key parses a DER-encoded ECPrivateKey structure (RFC |
| 188 | // 5915) from |cbs| and advances |cbs|. It returns a newly-allocated |EC_KEY| or |
| 189 | // NULL on error. If |group| is non-null, the parameters field of the |
| 190 | // ECPrivateKey may be omitted (but must match |group| if present). Otherwise, |
| 191 | // the parameters field is required. |
David Benjamin | 2f6410b | 2016-01-03 00:57:37 -0800 | [diff] [blame] | 192 | OPENSSL_EXPORT EC_KEY *EC_KEY_parse_private_key(CBS *cbs, |
| 193 | const EC_GROUP *group); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 194 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 195 | // EC_KEY_marshal_private_key marshals |key| as a DER-encoded ECPrivateKey |
| 196 | // structure (RFC 5915) and appends the result to |cbb|. It returns one on |
| 197 | // success and zero on failure. |enc_flags| is a combination of |EC_PKEY_*| |
| 198 | // values and controls whether corresponding fields are omitted. |
David Benjamin | 2f6410b | 2016-01-03 00:57:37 -0800 | [diff] [blame] | 199 | OPENSSL_EXPORT int EC_KEY_marshal_private_key(CBB *cbb, const EC_KEY *key, |
| 200 | unsigned enc_flags); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 201 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 202 | // EC_KEY_parse_curve_name parses a DER-encoded OBJECT IDENTIFIER as a curve |
| 203 | // name from |cbs| and advances |cbs|. It returns a newly-allocated |EC_GROUP| |
| 204 | // or NULL on error. |
David Benjamin | 0d76c40 | 2016-03-25 18:07:15 -0400 | [diff] [blame] | 205 | OPENSSL_EXPORT EC_GROUP *EC_KEY_parse_curve_name(CBS *cbs); |
| 206 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 207 | // EC_KEY_marshal_curve_name marshals |group| as a DER-encoded OBJECT IDENTIFIER |
| 208 | // and appends the result to |cbb|. It returns one on success and zero on |
| 209 | // failure. |
David Benjamin | 0d76c40 | 2016-03-25 18:07:15 -0400 | [diff] [blame] | 210 | OPENSSL_EXPORT int EC_KEY_marshal_curve_name(CBB *cbb, const EC_GROUP *group); |
| 211 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 212 | // EC_KEY_parse_parameters parses a DER-encoded ECParameters structure (RFC |
| 213 | // 5480) from |cbs| and advances |cbs|. It returns a newly-allocated |EC_GROUP| |
| 214 | // or NULL on error. It supports the namedCurve and specifiedCurve options, but |
| 215 | // use of specifiedCurve is deprecated. Use |EC_KEY_parse_curve_name| |
| 216 | // instead. |
David Benjamin | 2f6410b | 2016-01-03 00:57:37 -0800 | [diff] [blame] | 217 | OPENSSL_EXPORT EC_GROUP *EC_KEY_parse_parameters(CBS *cbs); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 218 | |
| 219 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 220 | // ex_data functions. |
| 221 | // |
| 222 | // These functions are wrappers. See |ex_data.h| for details. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 223 | |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 224 | OPENSSL_EXPORT int EC_KEY_get_ex_new_index(long argl, void *argp, |
David Benjamin | 8a58933 | 2015-12-04 23:14:35 -0500 | [diff] [blame] | 225 | CRYPTO_EX_unused *unused, |
David Benjamin | d94682d | 2017-05-14 17:10:18 -0400 | [diff] [blame] | 226 | CRYPTO_EX_dup *dup_unused, |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 227 | CRYPTO_EX_free *free_func); |
| 228 | OPENSSL_EXPORT int EC_KEY_set_ex_data(EC_KEY *r, int idx, void *arg); |
| 229 | OPENSSL_EXPORT void *EC_KEY_get_ex_data(const EC_KEY *r, int idx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 230 | |
| 231 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 232 | // ECDSA method. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 233 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 234 | // ECDSA_FLAG_OPAQUE specifies that this ECDSA_METHOD does not expose its key |
| 235 | // material. This may be set if, for instance, it is wrapping some other crypto |
| 236 | // API, like a platform key store. |
David Benjamin | ecc0ce7 | 2014-07-18 18:39:42 -0400 | [diff] [blame] | 237 | #define ECDSA_FLAG_OPAQUE 1 |
| 238 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 239 | // ecdsa_method_st is a structure of function pointers for implementing ECDSA. |
| 240 | // See engine.h. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 241 | struct ecdsa_method_st { |
| 242 | struct openssl_method_common_st common; |
| 243 | |
| 244 | void *app_data; |
| 245 | |
| 246 | int (*init)(EC_KEY *key); |
| 247 | int (*finish)(EC_KEY *key); |
| 248 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 249 | // group_order_size returns the number of bytes needed to represent the order |
| 250 | // of the group. This is used to calculate the maximum size of an ECDSA |
| 251 | // signature in |ECDSA_size|. |
Adam Langley | 449f16b | 2014-07-15 15:15:09 -0700 | [diff] [blame] | 252 | size_t (*group_order_size)(const EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 253 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 254 | // sign matches the arguments and behaviour of |ECDSA_sign|. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 255 | int (*sign)(const uint8_t *digest, size_t digest_len, uint8_t *sig, |
| 256 | unsigned int *sig_len, EC_KEY *eckey); |
| 257 | |
David Benjamin | ecc0ce7 | 2014-07-18 18:39:42 -0400 | [diff] [blame] | 258 | int flags; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 262 | // Deprecated functions. |
Adam Langley | c3ef76f | 2015-04-13 14:34:17 -0700 | [diff] [blame] | 263 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 264 | // EC_KEY_set_asn1_flag does nothing. |
Adam Langley | c3ef76f | 2015-04-13 14:34:17 -0700 | [diff] [blame] | 265 | OPENSSL_EXPORT void EC_KEY_set_asn1_flag(EC_KEY *key, int flag); |
| 266 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 267 | // d2i_ECPrivateKey parses an ASN.1, DER-encoded, private key from |len| bytes |
| 268 | // at |*inp|. If |out_key| is not NULL then, on exit, a pointer to the result |
| 269 | // is in |*out_key|. Note that, even if |*out_key| is already non-NULL on entry, |
| 270 | // it * will not be written to. Rather, a fresh |EC_KEY| is allocated and the |
| 271 | // previous * one is freed. On successful exit, |*inp| is advanced past the DER |
| 272 | // structure. It returns the result or NULL on error. |
| 273 | // |
| 274 | // On input, if |*out_key| is non-NULL and has a group configured, the |
| 275 | // parameters field may be omitted but must match that group if present. |
| 276 | // |
| 277 | // Use |EC_KEY_parse_private_key| instead. |
David Benjamin | 2f6410b | 2016-01-03 00:57:37 -0800 | [diff] [blame] | 278 | OPENSSL_EXPORT EC_KEY *d2i_ECPrivateKey(EC_KEY **out_key, const uint8_t **inp, |
| 279 | long len); |
| 280 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 281 | // i2d_ECPrivateKey marshals an EC private key from |key| to an ASN.1, DER |
| 282 | // structure. If |outp| is not NULL then the result is written to |*outp| and |
| 283 | // |*outp| is advanced just past the output. It returns the number of bytes in |
| 284 | // the result, whether written or not, or a negative value on error. |
| 285 | // |
| 286 | // Use |EC_KEY_marshal_private_key| instead. |
David Benjamin | 2f6410b | 2016-01-03 00:57:37 -0800 | [diff] [blame] | 287 | OPENSSL_EXPORT int i2d_ECPrivateKey(const EC_KEY *key, uint8_t **outp); |
| 288 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 289 | // d2i_ECParameters parses an ASN.1, DER-encoded, set of EC parameters from |
| 290 | // |len| bytes at |*inp|. If |out_key| is not NULL then, on exit, a pointer to |
| 291 | // the result is in |*out_key|. Note that, even if |*out_key| is already |
| 292 | // non-NULL on entry, it will not be written to. Rather, a fresh |EC_KEY| is |
| 293 | // allocated and the previous one is freed. On successful exit, |*inp| is |
| 294 | // advanced past the DER structure. It returns the result or NULL on error. |
| 295 | // |
| 296 | // Use |EC_KEY_parse_parameters| or |EC_KEY_parse_curve_name| instead. |
David Benjamin | 2f6410b | 2016-01-03 00:57:37 -0800 | [diff] [blame] | 297 | OPENSSL_EXPORT EC_KEY *d2i_ECParameters(EC_KEY **out_key, const uint8_t **inp, |
| 298 | long len); |
| 299 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 300 | // i2d_ECParameters marshals EC parameters from |key| to an ASN.1, DER |
| 301 | // structure. If |outp| is not NULL then the result is written to |*outp| and |
| 302 | // |*outp| is advanced just past the output. It returns the number of bytes in |
| 303 | // the result, whether written or not, or a negative value on error. |
| 304 | // |
| 305 | // Use |EC_KEY_marshal_curve_name| instead. |
David Benjamin | 2f6410b | 2016-01-03 00:57:37 -0800 | [diff] [blame] | 306 | OPENSSL_EXPORT int i2d_ECParameters(const EC_KEY *key, uint8_t **outp); |
| 307 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 308 | // o2i_ECPublicKey parses an EC point from |len| bytes at |*inp| into |
| 309 | // |*out_key|. Note that this differs from the d2i format in that |*out_key| |
| 310 | // must be non-NULL with a group set. On successful exit, |*inp| is advanced by |
| 311 | // |len| bytes. It returns |*out_key| or NULL on error. |
| 312 | // |
| 313 | // Use |EC_POINT_oct2point| instead. |
David Benjamin | 2f6410b | 2016-01-03 00:57:37 -0800 | [diff] [blame] | 314 | OPENSSL_EXPORT EC_KEY *o2i_ECPublicKey(EC_KEY **out_key, const uint8_t **inp, |
| 315 | long len); |
| 316 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 317 | // i2o_ECPublicKey marshals an EC point from |key|. If |outp| is not NULL then |
| 318 | // the result is written to |*outp| and |*outp| is advanced just past the |
| 319 | // output. It returns the number of bytes in the result, whether written or |
| 320 | // not, or a negative value on error. |
| 321 | // |
| 322 | // Use |EC_POINT_point2cbb| instead. |
David Benjamin | 2f6410b | 2016-01-03 00:57:37 -0800 | [diff] [blame] | 323 | OPENSSL_EXPORT int i2o_ECPublicKey(const EC_KEY *key, unsigned char **outp); |
| 324 | |
Adam Langley | c3ef76f | 2015-04-13 14:34:17 -0700 | [diff] [blame] | 325 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 326 | #if defined(__cplusplus) |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 327 | } // extern C |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 328 | |
| 329 | extern "C++" { |
| 330 | |
| 331 | namespace bssl { |
| 332 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 333 | BORINGSSL_MAKE_DELETER(EC_KEY, EC_KEY_free) |
| 334 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 335 | } // namespace bssl |
| 336 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 337 | } // extern C++ |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 338 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 339 | #endif |
| 340 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 341 | #endif // OPENSSL_HEADER_EC_KEY_H |