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. |
David Benjamin | 2556f8b | 2018-08-24 14:58:18 -0500 | [diff] [blame] | 87 | // |
| 88 | // An |EC_KEY| object represents a public or private EC key. A given object may |
| 89 | // be used concurrently on multiple threads by non-mutating functions, provided |
| 90 | // no other thread is concurrently calling a mutating function. Unless otherwise |
| 91 | // documented, functions which take a |const| pointer are non-mutating and |
| 92 | // functions which take a non-|const| pointer are mutating. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 93 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 94 | // 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] | 95 | OPENSSL_EXPORT EC_KEY *EC_KEY_new(void); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 96 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 97 | // EC_KEY_new_method acts the same as |EC_KEY_new|, but takes an explicit |
| 98 | // |ENGINE|. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 99 | OPENSSL_EXPORT EC_KEY *EC_KEY_new_method(const ENGINE *engine); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 100 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 101 | // EC_KEY_new_by_curve_name returns a fresh EC_KEY for group specified by |nid| |
| 102 | // or NULL on error. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 103 | OPENSSL_EXPORT EC_KEY *EC_KEY_new_by_curve_name(int nid); |
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_free frees all the data owned by |key| and |key| itself. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 106 | OPENSSL_EXPORT void EC_KEY_free(EC_KEY *key); |
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_dup returns a fresh copy of |src| or NULL on error. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 109 | OPENSSL_EXPORT EC_KEY *EC_KEY_dup(const EC_KEY *src); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 110 | |
David Benjamin | 2556f8b | 2018-08-24 14:58:18 -0500 | [diff] [blame] | 111 | // EC_KEY_up_ref increases the reference count of |key| and returns one. It does |
| 112 | // not mutate |key| for thread-safety purposes and may be used concurrently. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 113 | OPENSSL_EXPORT int EC_KEY_up_ref(EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 114 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 115 | // EC_KEY_is_opaque returns one if |key| is opaque and doesn't expose its key |
| 116 | // material. Otherwise it return zero. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 117 | OPENSSL_EXPORT int EC_KEY_is_opaque(const EC_KEY *key); |
David Benjamin | ecc0ce7 | 2014-07-18 18:39:42 -0400 | [diff] [blame] | 118 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 119 | // 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] | 120 | 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] | 121 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 122 | // EC_KEY_set_group sets the |EC_GROUP| object that |key| will use to |group|. |
Joshua Liebow-Feeser | 9781699 | 2018-08-13 16:02:05 -0700 | [diff] [blame] | 123 | // It returns one on success and zero if |key| is already configured with a |
| 124 | // different group. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 125 | 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] | 126 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 127 | // 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] | 128 | 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] | 129 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 130 | // EC_KEY_set_private_key sets the private key of |key| to |priv|. It returns |
David Benjamin | 5bcaa11 | 2017-12-16 15:34:00 -0500 | [diff] [blame] | 131 | // one on success and zero otherwise. |key| must already have had a group |
| 132 | // configured (see |EC_KEY_set_group| and |EC_KEY_new_by_curve_name|). |
David Benjamin | bc4c09d | 2019-10-11 11:54:37 -0400 | [diff] [blame] | 133 | OPENSSL_EXPORT int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 134 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 135 | // EC_KEY_get0_public_key returns a pointer to the public key point inside |
| 136 | // |key|. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 137 | 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] | 138 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 139 | // EC_KEY_set_public_key sets the public key of |key| to |pub|, by copying it. |
David Benjamin | 5bcaa11 | 2017-12-16 15:34:00 -0500 | [diff] [blame] | 140 | // It returns one on success and zero otherwise. |key| must already have had a |
| 141 | // group configured (see |EC_KEY_set_group| and |EC_KEY_new_by_curve_name|), and |
| 142 | // |pub| must also belong to that group. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 143 | 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] | 144 | |
| 145 | #define EC_PKEY_NO_PARAMETERS 0x001 |
| 146 | #define EC_PKEY_NO_PUBKEY 0x002 |
| 147 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 148 | // EC_KEY_get_enc_flags returns the encoding flags for |key|, which is a |
| 149 | // bitwise-OR of |EC_PKEY_*| values. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 150 | OPENSSL_EXPORT unsigned EC_KEY_get_enc_flags(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_enc_flags sets the encoding flags for |key|, which is a |
| 153 | // bitwise-OR of |EC_PKEY_*| values. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 154 | 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] | 155 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 156 | // EC_KEY_get_conv_form returns the conversation form that will be used by |
| 157 | // |key|. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 158 | 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] | 159 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 160 | // 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] | 161 | OPENSSL_EXPORT void EC_KEY_set_conv_form(EC_KEY *key, |
| 162 | point_conversion_form_t cform); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 163 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 164 | // EC_KEY_check_key performs several checks on |key| (possibly including an |
| 165 | // expensive check that the public key is in the primary subgroup). It returns |
| 166 | // one if all checks pass and zero otherwise. If it returns zero then detail |
| 167 | // about the problem can be found on the error stack. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 168 | OPENSSL_EXPORT int EC_KEY_check_key(const EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 169 | |
Adam Langley | 8bbefbf | 2022-03-18 13:00:09 -0700 | [diff] [blame] | 170 | // EC_KEY_check_fips performs both a signing pairwise consistency test |
| 171 | // (FIPS 140-2 4.9.2) and the consistency test from SP 800-56Ar3 section |
| 172 | // 5.6.2.1.4. It returns one if it passes and zero otherwise. |
Steven Valdez | 400d0b7 | 2017-04-06 14:55:18 -0400 | [diff] [blame] | 173 | OPENSSL_EXPORT int EC_KEY_check_fips(const EC_KEY *key); |
| 174 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 175 | // EC_KEY_set_public_key_affine_coordinates sets the public key in |key| to |
David Benjamin | 0deb91a | 2020-01-07 11:33:18 -0500 | [diff] [blame] | 176 | // (|x|, |y|). It returns one on success and zero on error. It's considered an |
| 177 | // error if |x| and |y| do not represent a point on |key|'s curve. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 178 | OPENSSL_EXPORT int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, |
David Benjamin | 5ce7022 | 2019-09-10 12:31:02 -0700 | [diff] [blame] | 179 | const BIGNUM *x, |
| 180 | const BIGNUM *y); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 181 | |
David Benjamin | 671ccb1 | 2022-10-20 16:16:50 -0400 | [diff] [blame] | 182 | // EC_KEY_oct2key decodes |len| bytes from |in| as an EC public key in X9.62 |
| 183 | // form. |key| must already have a group configured. On success, it sets the |
| 184 | // public key in |key| to the result and returns one. Otherwise, it returns |
| 185 | // zero. |
| 186 | OPENSSL_EXPORT int EC_KEY_oct2key(EC_KEY *key, const uint8_t *in, size_t len, |
| 187 | BN_CTX *ctx); |
| 188 | |
David Benjamin | 38f621a | 2022-11-04 17:16:49 -0400 | [diff] [blame] | 189 | // EC_KEY_key2buf behaves like |EC_POINT_point2buf|, except it encodes the |
| 190 | // public key in |key|. |
David Benjamin | 0deb91a | 2020-01-07 11:33:18 -0500 | [diff] [blame] | 191 | OPENSSL_EXPORT size_t EC_KEY_key2buf(const EC_KEY *key, |
| 192 | point_conversion_form_t form, |
David Benjamin | 38f621a | 2022-11-04 17:16:49 -0400 | [diff] [blame] | 193 | uint8_t **out_buf, BN_CTX *ctx); |
| 194 | |
| 195 | // EC_KEY_oct2priv decodes a big-endian, zero-padded integer from |len| bytes |
| 196 | // from |in| and sets |key|'s private key to the result. It returns one on |
| 197 | // success and zero on error. The input must be padded to the size of |key|'s |
| 198 | // group order. |
| 199 | OPENSSL_EXPORT int EC_KEY_oct2priv(EC_KEY *key, const uint8_t *in, size_t len); |
| 200 | |
| 201 | // EC_KEY_priv2oct serializes |key|'s private key as a big-endian integer, |
| 202 | // zero-padded to the size of |key|'s group order and writes the result to at |
| 203 | // most |max_out| bytes of |out|. It returns the number of bytes written on |
| 204 | // success and zero on error. If |out| is NULL, it returns the number of bytes |
| 205 | // needed without writing anything. |
| 206 | OPENSSL_EXPORT size_t EC_KEY_priv2oct(const EC_KEY *key, uint8_t *out, |
| 207 | size_t max_out); |
| 208 | |
| 209 | // EC_KEY_priv2buf behaves like |EC_KEY_priv2oct| but sets |*out_buf| to a |
| 210 | // newly-allocated buffer containing the result. It returns the size of the |
| 211 | // result on success and zero on error. The caller must release |*out_buf| with |
| 212 | // |OPENSSL_free| when done. |
| 213 | OPENSSL_EXPORT size_t EC_KEY_priv2buf(const EC_KEY *key, uint8_t **out_buf); |
Jeremy Apthorp | 7177c1d | 2018-12-19 14:46:14 -0800 | [diff] [blame] | 214 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 215 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 216 | // Key generation. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 217 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 218 | // EC_KEY_generate_key generates a random, private key, calculates the |
| 219 | // corresponding public key and stores both in |key|. It returns one on success |
| 220 | // or zero otherwise. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 221 | OPENSSL_EXPORT int EC_KEY_generate_key(EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 222 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 223 | // EC_KEY_generate_key_fips behaves like |EC_KEY_generate_key| but performs |
Adam Langley | 8bbefbf | 2022-03-18 13:00:09 -0700 | [diff] [blame] | 224 | // additional checks for FIPS compliance. This function is applicable when |
| 225 | // generating keys for either signing/verification or key agreement because |
| 226 | // both types of consistency check (PCT) are performed. |
Steven Valdez | 467d322 | 2017-05-16 14:35:22 -0400 | [diff] [blame] | 227 | OPENSSL_EXPORT int EC_KEY_generate_key_fips(EC_KEY *key); |
| 228 | |
David Benjamin | bc4c09d | 2019-10-11 11:54:37 -0400 | [diff] [blame] | 229 | // EC_KEY_derive_from_secret deterministically derives a private key for |group| |
| 230 | // from an input secret using HKDF-SHA256. It returns a newly-allocated |EC_KEY| |
| 231 | // on success or NULL on error. |secret| must not be used in any other |
| 232 | // algorithm. If using a base secret for multiple operations, derive separate |
| 233 | // values with a KDF such as HKDF first. |
| 234 | // |
| 235 | // Note this function implements an arbitrary derivation scheme, rather than any |
| 236 | // particular standard one. New protocols are recommended to use X25519 and |
| 237 | // Ed25519, which have standard byte import functions. See |
| 238 | // |X25519_public_from_private| and |ED25519_keypair_from_seed|. |
| 239 | OPENSSL_EXPORT EC_KEY *EC_KEY_derive_from_secret(const EC_GROUP *group, |
| 240 | const uint8_t *secret, |
| 241 | size_t secret_len); |
| 242 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 243 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 244 | // Serialisation. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 245 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 246 | // EC_KEY_parse_private_key parses a DER-encoded ECPrivateKey structure (RFC |
| 247 | // 5915) from |cbs| and advances |cbs|. It returns a newly-allocated |EC_KEY| or |
| 248 | // NULL on error. If |group| is non-null, the parameters field of the |
| 249 | // ECPrivateKey may be omitted (but must match |group| if present). Otherwise, |
| 250 | // the parameters field is required. |
David Benjamin | 2f6410b | 2016-01-03 00:57:37 -0800 | [diff] [blame] | 251 | OPENSSL_EXPORT EC_KEY *EC_KEY_parse_private_key(CBS *cbs, |
| 252 | const EC_GROUP *group); |
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 | // EC_KEY_marshal_private_key marshals |key| as a DER-encoded ECPrivateKey |
| 255 | // structure (RFC 5915) and appends the result to |cbb|. It returns one on |
| 256 | // success and zero on failure. |enc_flags| is a combination of |EC_PKEY_*| |
| 257 | // values and controls whether corresponding fields are omitted. |
David Benjamin | 2f6410b | 2016-01-03 00:57:37 -0800 | [diff] [blame] | 258 | OPENSSL_EXPORT int EC_KEY_marshal_private_key(CBB *cbb, const EC_KEY *key, |
| 259 | unsigned enc_flags); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 260 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 261 | // EC_KEY_parse_curve_name parses a DER-encoded OBJECT IDENTIFIER as a curve |
David Benjamin | 417069f | 2023-02-13 18:33:02 -0500 | [diff] [blame] | 262 | // name from |cbs| and advances |cbs|. It returns the decoded |EC_GROUP| or NULL |
| 263 | // on error. |
| 264 | // |
| 265 | // This function returns a non-const pointer which may be passed to |
| 266 | // |EC_GROUP_free|. However, the resulting object is actually static and calling |
| 267 | // |EC_GROUP_free| is optional. |
| 268 | // |
| 269 | // TODO(davidben): Make this return a const pointer, if it does not break too |
| 270 | // many callers. |
David Benjamin | 0d76c40 | 2016-03-25 18:07:15 -0400 | [diff] [blame] | 271 | OPENSSL_EXPORT EC_GROUP *EC_KEY_parse_curve_name(CBS *cbs); |
| 272 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 273 | // EC_KEY_marshal_curve_name marshals |group| as a DER-encoded OBJECT IDENTIFIER |
| 274 | // and appends the result to |cbb|. It returns one on success and zero on |
| 275 | // failure. |
David Benjamin | 0d76c40 | 2016-03-25 18:07:15 -0400 | [diff] [blame] | 276 | OPENSSL_EXPORT int EC_KEY_marshal_curve_name(CBB *cbb, const EC_GROUP *group); |
| 277 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 278 | // EC_KEY_parse_parameters parses a DER-encoded ECParameters structure (RFC |
David Benjamin | 417069f | 2023-02-13 18:33:02 -0500 | [diff] [blame] | 279 | // 5480) from |cbs| and advances |cbs|. It returns the resulting |EC_GROUP| or |
| 280 | // NULL on error. It supports the namedCurve and specifiedCurve options, but use |
| 281 | // of specifiedCurve is deprecated. Use |EC_KEY_parse_curve_name| instead. |
| 282 | // |
| 283 | // This function returns a non-const pointer which may be passed to |
| 284 | // |EC_GROUP_free|. However, the resulting object is actually static and calling |
| 285 | // |EC_GROUP_free| is optional. |
| 286 | // |
| 287 | // TODO(davidben): Make this return a const pointer, if it does not break too |
| 288 | // many callers. |
David Benjamin | 2f6410b | 2016-01-03 00:57:37 -0800 | [diff] [blame] | 289 | OPENSSL_EXPORT EC_GROUP *EC_KEY_parse_parameters(CBS *cbs); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 290 | |
| 291 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 292 | // ex_data functions. |
| 293 | // |
| 294 | // These functions are wrappers. See |ex_data.h| for details. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 295 | |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 296 | 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] | 297 | CRYPTO_EX_unused *unused, |
David Benjamin | d94682d | 2017-05-14 17:10:18 -0400 | [diff] [blame] | 298 | CRYPTO_EX_dup *dup_unused, |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 299 | CRYPTO_EX_free *free_func); |
| 300 | OPENSSL_EXPORT int EC_KEY_set_ex_data(EC_KEY *r, int idx, void *arg); |
| 301 | 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] | 302 | |
| 303 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 304 | // ECDSA method. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 305 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 306 | // ECDSA_FLAG_OPAQUE specifies that this ECDSA_METHOD does not expose its key |
| 307 | // material. This may be set if, for instance, it is wrapping some other crypto |
| 308 | // API, like a platform key store. |
David Benjamin | ecc0ce7 | 2014-07-18 18:39:42 -0400 | [diff] [blame] | 309 | #define ECDSA_FLAG_OPAQUE 1 |
| 310 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 311 | // ecdsa_method_st is a structure of function pointers for implementing ECDSA. |
| 312 | // See engine.h. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 313 | struct ecdsa_method_st { |
| 314 | struct openssl_method_common_st common; |
| 315 | |
| 316 | void *app_data; |
| 317 | |
| 318 | int (*init)(EC_KEY *key); |
| 319 | int (*finish)(EC_KEY *key); |
| 320 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 321 | // group_order_size returns the number of bytes needed to represent the order |
| 322 | // of the group. This is used to calculate the maximum size of an ECDSA |
| 323 | // signature in |ECDSA_size|. |
Adam Langley | 449f16b | 2014-07-15 15:15:09 -0700 | [diff] [blame] | 324 | size_t (*group_order_size)(const EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 325 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 326 | // sign matches the arguments and behaviour of |ECDSA_sign|. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 327 | int (*sign)(const uint8_t *digest, size_t digest_len, uint8_t *sig, |
| 328 | unsigned int *sig_len, EC_KEY *eckey); |
| 329 | |
David Benjamin | ecc0ce7 | 2014-07-18 18:39:42 -0400 | [diff] [blame] | 330 | int flags; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 331 | }; |
| 332 | |
| 333 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 334 | // Deprecated functions. |
Adam Langley | c3ef76f | 2015-04-13 14:34:17 -0700 | [diff] [blame] | 335 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 336 | // EC_KEY_set_asn1_flag does nothing. |
Adam Langley | c3ef76f | 2015-04-13 14:34:17 -0700 | [diff] [blame] | 337 | OPENSSL_EXPORT void EC_KEY_set_asn1_flag(EC_KEY *key, int flag); |
| 338 | |
David Benjamin | cfafcd4 | 2021-10-14 17:22:23 -0400 | [diff] [blame] | 339 | // d2i_ECPrivateKey parses a DER-encoded ECPrivateKey structure (RFC 5915) from |
| 340 | // |len| bytes at |*inp|, as described in |d2i_SAMPLE|. On input, if |*out_key| |
| 341 | // is non-NULL and has a group configured, the parameters field may be omitted |
| 342 | // but must match that group if present. |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 343 | // |
| 344 | // Use |EC_KEY_parse_private_key| instead. |
David Benjamin | 2f6410b | 2016-01-03 00:57:37 -0800 | [diff] [blame] | 345 | OPENSSL_EXPORT EC_KEY *d2i_ECPrivateKey(EC_KEY **out_key, const uint8_t **inp, |
| 346 | long len); |
| 347 | |
David Benjamin | cfafcd4 | 2021-10-14 17:22:23 -0400 | [diff] [blame] | 348 | // i2d_ECPrivateKey marshals |key| as a DER-encoded ECPrivateKey structure (RFC |
| 349 | // 5915), as described in |i2d_SAMPLE|. |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 350 | // |
| 351 | // Use |EC_KEY_marshal_private_key| instead. |
David Benjamin | 2f6410b | 2016-01-03 00:57:37 -0800 | [diff] [blame] | 352 | OPENSSL_EXPORT int i2d_ECPrivateKey(const EC_KEY *key, uint8_t **outp); |
| 353 | |
David Benjamin | cfafcd4 | 2021-10-14 17:22:23 -0400 | [diff] [blame] | 354 | // d2i_ECParameters parses a DER-encoded ECParameters structure (RFC 5480) from |
| 355 | // |len| bytes at |*inp|, as described in |d2i_SAMPLE|. |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 356 | // |
| 357 | // Use |EC_KEY_parse_parameters| or |EC_KEY_parse_curve_name| instead. |
David Benjamin | 2f6410b | 2016-01-03 00:57:37 -0800 | [diff] [blame] | 358 | OPENSSL_EXPORT EC_KEY *d2i_ECParameters(EC_KEY **out_key, const uint8_t **inp, |
| 359 | long len); |
| 360 | |
David Benjamin | cfafcd4 | 2021-10-14 17:22:23 -0400 | [diff] [blame] | 361 | // i2d_ECParameters marshals |key|'s parameters as a DER-encoded OBJECT |
| 362 | // IDENTIFIER, as described in |i2d_SAMPLE|. |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 363 | // |
| 364 | // Use |EC_KEY_marshal_curve_name| instead. |
David Benjamin | 2f6410b | 2016-01-03 00:57:37 -0800 | [diff] [blame] | 365 | OPENSSL_EXPORT int i2d_ECParameters(const EC_KEY *key, uint8_t **outp); |
| 366 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 367 | // o2i_ECPublicKey parses an EC point from |len| bytes at |*inp| into |
| 368 | // |*out_key|. Note that this differs from the d2i format in that |*out_key| |
| 369 | // must be non-NULL with a group set. On successful exit, |*inp| is advanced by |
| 370 | // |len| bytes. It returns |*out_key| or NULL on error. |
| 371 | // |
| 372 | // Use |EC_POINT_oct2point| instead. |
David Benjamin | 2f6410b | 2016-01-03 00:57:37 -0800 | [diff] [blame] | 373 | OPENSSL_EXPORT EC_KEY *o2i_ECPublicKey(EC_KEY **out_key, const uint8_t **inp, |
| 374 | long len); |
| 375 | |
David Benjamin | cfafcd4 | 2021-10-14 17:22:23 -0400 | [diff] [blame] | 376 | // i2o_ECPublicKey marshals an EC point from |key|, as described in |
David Benjamin | 2972382 | 2022-11-23 12:30:36 -0500 | [diff] [blame] | 377 | // |i2d_SAMPLE|, except it returns zero on error instead of a negative value. |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 378 | // |
| 379 | // Use |EC_POINT_point2cbb| instead. |
David Benjamin | 2f6410b | 2016-01-03 00:57:37 -0800 | [diff] [blame] | 380 | OPENSSL_EXPORT int i2o_ECPublicKey(const EC_KEY *key, unsigned char **outp); |
| 381 | |
Adam Langley | c3ef76f | 2015-04-13 14:34:17 -0700 | [diff] [blame] | 382 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 383 | #if defined(__cplusplus) |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 384 | } // extern C |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 385 | |
| 386 | extern "C++" { |
| 387 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 388 | BSSL_NAMESPACE_BEGIN |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 389 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 390 | BORINGSSL_MAKE_DELETER(EC_KEY, EC_KEY_free) |
David Benjamin | 5cf05ad | 2018-09-14 09:34:17 -0700 | [diff] [blame] | 391 | BORINGSSL_MAKE_UP_REF(EC_KEY, EC_KEY_up_ref) |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 392 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 393 | BSSL_NAMESPACE_END |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 394 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 395 | } // extern C++ |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 396 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 397 | #endif |
| 398 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 399 | #endif // OPENSSL_HEADER_EC_KEY_H |