blob: cc075e544bf6b0cc6e14baec0ccd00d917016622 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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)
78extern "C" {
79#endif
80
81
Robert Sloan8f860b12017-08-28 07:37:06 -070082// ec_key.h contains functions that handle elliptic-curve points that are
83// public/private keys.
Adam Langleyd9e397b2015-01-22 14:27:53 -080084
85
Robert Sloan8f860b12017-08-28 07:37:06 -070086// EC key objects.
Adam Langleyd9e397b2015-01-22 14:27:53 -080087
Robert Sloan8f860b12017-08-28 07:37:06 -070088// EC_KEY_new returns a fresh |EC_KEY| object or NULL on error.
Adam Langleyd9e397b2015-01-22 14:27:53 -080089OPENSSL_EXPORT EC_KEY *EC_KEY_new(void);
90
Robert Sloan8f860b12017-08-28 07:37:06 -070091// EC_KEY_new_method acts the same as |EC_KEY_new|, but takes an explicit
92// |ENGINE|.
Adam Langleyd9e397b2015-01-22 14:27:53 -080093OPENSSL_EXPORT EC_KEY *EC_KEY_new_method(const ENGINE *engine);
94
Robert Sloan8f860b12017-08-28 07:37:06 -070095// EC_KEY_new_by_curve_name returns a fresh EC_KEY for group specified by |nid|
96// or NULL on error.
Adam Langleyd9e397b2015-01-22 14:27:53 -080097OPENSSL_EXPORT EC_KEY *EC_KEY_new_by_curve_name(int nid);
98
Robert Sloan8f860b12017-08-28 07:37:06 -070099// EC_KEY_free frees all the data owned by |key| and |key| itself.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800100OPENSSL_EXPORT void EC_KEY_free(EC_KEY *key);
101
Robert Sloan8f860b12017-08-28 07:37:06 -0700102// EC_KEY_dup returns a fresh copy of |src| or NULL on error.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800103OPENSSL_EXPORT EC_KEY *EC_KEY_dup(const EC_KEY *src);
104
Robert Sloan8f860b12017-08-28 07:37:06 -0700105// EC_KEY_up_ref increases the reference count of |key| and returns one.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800106OPENSSL_EXPORT int EC_KEY_up_ref(EC_KEY *key);
107
Robert Sloan8f860b12017-08-28 07:37:06 -0700108// EC_KEY_is_opaque returns one if |key| is opaque and doesn't expose its key
109// material. Otherwise it return zero.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800110OPENSSL_EXPORT int EC_KEY_is_opaque(const EC_KEY *key);
111
Robert Sloan8f860b12017-08-28 07:37:06 -0700112// EC_KEY_get0_group returns a pointer to the |EC_GROUP| object inside |key|.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800113OPENSSL_EXPORT const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key);
114
Robert Sloan8f860b12017-08-28 07:37:06 -0700115// EC_KEY_set_group sets the |EC_GROUP| object that |key| will use to |group|.
Robert Sloan0da43952018-01-03 15:13:14 -0800116// It returns one on success and zero otherwise. If |key| already has a group,
117// it is an error to change to a different one.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800118OPENSSL_EXPORT int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group);
119
Robert Sloan8f860b12017-08-28 07:37:06 -0700120// EC_KEY_get0_private_key returns a pointer to the private key inside |key|.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800121OPENSSL_EXPORT const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key);
122
Robert Sloan8f860b12017-08-28 07:37:06 -0700123// EC_KEY_set_private_key sets the private key of |key| to |priv|. It returns
Robert Sloan0da43952018-01-03 15:13:14 -0800124// one on success and zero otherwise. |key| must already have had a group
125// configured (see |EC_KEY_set_group| and |EC_KEY_new_by_curve_name|).
Adam Langleyd9e397b2015-01-22 14:27:53 -0800126OPENSSL_EXPORT int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *prv);
127
Robert Sloan8f860b12017-08-28 07:37:06 -0700128// EC_KEY_get0_public_key returns a pointer to the public key point inside
129// |key|.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800130OPENSSL_EXPORT const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key);
131
Robert Sloan8f860b12017-08-28 07:37:06 -0700132// EC_KEY_set_public_key sets the public key of |key| to |pub|, by copying it.
Robert Sloan0da43952018-01-03 15:13:14 -0800133// It returns one on success and zero otherwise. |key| must already have had a
134// group configured (see |EC_KEY_set_group| and |EC_KEY_new_by_curve_name|), and
135// |pub| must also belong to that group.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800136OPENSSL_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
Robert Sloan8f860b12017-08-28 07:37:06 -0700141// EC_KEY_get_enc_flags returns the encoding flags for |key|, which is a
142// bitwise-OR of |EC_PKEY_*| values.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800143OPENSSL_EXPORT unsigned EC_KEY_get_enc_flags(const EC_KEY *key);
144
Robert Sloan8f860b12017-08-28 07:37:06 -0700145// EC_KEY_set_enc_flags sets the encoding flags for |key|, which is a
146// bitwise-OR of |EC_PKEY_*| values.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800147OPENSSL_EXPORT void EC_KEY_set_enc_flags(EC_KEY *key, unsigned flags);
148
Robert Sloan8f860b12017-08-28 07:37:06 -0700149// EC_KEY_get_conv_form returns the conversation form that will be used by
150// |key|.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800151OPENSSL_EXPORT point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key);
152
Robert Sloan8f860b12017-08-28 07:37:06 -0700153// EC_KEY_set_conv_form sets the conversion form to be used by |key|.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800154OPENSSL_EXPORT void EC_KEY_set_conv_form(EC_KEY *key,
155 point_conversion_form_t cform);
156
Robert Sloan8f860b12017-08-28 07:37:06 -0700157// 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.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800161OPENSSL_EXPORT int EC_KEY_check_key(const EC_KEY *key);
162
Robert Sloan8f860b12017-08-28 07:37:06 -0700163// EC_KEY_check_fips performs a signing pairwise consistency test (FIPS 140-2
164// 4.9.2). It returns one if it passes and zero otherwise.
Robert Sloan572a4e22017-04-17 10:52:19 -0700165OPENSSL_EXPORT int EC_KEY_check_fips(const EC_KEY *key);
166
Robert Sloan8f860b12017-08-28 07:37:06 -0700167// EC_KEY_set_public_key_affine_coordinates sets the public key in |key| to
168// (|x|, |y|). It returns one on success and zero otherwise.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800169OPENSSL_EXPORT int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key,
170 BIGNUM *x,
171 BIGNUM *y);
172
173
Robert Sloan8f860b12017-08-28 07:37:06 -0700174// Key generation.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800175
Robert Sloan8f860b12017-08-28 07:37:06 -0700176// EC_KEY_generate_key generates a random, private key, calculates the
177// corresponding public key and stores both in |key|. It returns one on success
178// or zero otherwise.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800179OPENSSL_EXPORT int EC_KEY_generate_key(EC_KEY *key);
180
Robert Sloan8f860b12017-08-28 07:37:06 -0700181// EC_KEY_generate_key_fips behaves like |EC_KEY_generate_key| but performs
182// additional checks for FIPS compliance.
Robert Sloan8ff03552017-06-14 12:40:58 -0700183OPENSSL_EXPORT int EC_KEY_generate_key_fips(EC_KEY *key);
184
Adam Langleyd9e397b2015-01-22 14:27:53 -0800185
Robert Sloan8f860b12017-08-28 07:37:06 -0700186// Serialisation.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800187
Robert Sloan8f860b12017-08-28 07:37:06 -0700188// EC_KEY_parse_private_key parses a DER-encoded ECPrivateKey structure (RFC
189// 5915) from |cbs| and advances |cbs|. It returns a newly-allocated |EC_KEY| or
190// NULL on error. If |group| is non-null, the parameters field of the
191// ECPrivateKey may be omitted (but must match |group| if present). Otherwise,
192// the parameters field is required.
David Benjamin4969cc92016-04-22 15:02:23 -0400193OPENSSL_EXPORT EC_KEY *EC_KEY_parse_private_key(CBS *cbs,
194 const EC_GROUP *group);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800195
Robert Sloan8f860b12017-08-28 07:37:06 -0700196// EC_KEY_marshal_private_key marshals |key| as a DER-encoded ECPrivateKey
197// structure (RFC 5915) and appends the result to |cbb|. It returns one on
198// success and zero on failure. |enc_flags| is a combination of |EC_PKEY_*|
199// values and controls whether corresponding fields are omitted.
David Benjamin4969cc92016-04-22 15:02:23 -0400200OPENSSL_EXPORT int EC_KEY_marshal_private_key(CBB *cbb, const EC_KEY *key,
201 unsigned enc_flags);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800202
Robert Sloan8f860b12017-08-28 07:37:06 -0700203// EC_KEY_parse_curve_name parses a DER-encoded OBJECT IDENTIFIER as a curve
204// name from |cbs| and advances |cbs|. It returns a newly-allocated |EC_GROUP|
205// or NULL on error.
David Benjamin4969cc92016-04-22 15:02:23 -0400206OPENSSL_EXPORT EC_GROUP *EC_KEY_parse_curve_name(CBS *cbs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800207
Robert Sloan8f860b12017-08-28 07:37:06 -0700208// EC_KEY_marshal_curve_name marshals |group| as a DER-encoded OBJECT IDENTIFIER
209// and appends the result to |cbb|. It returns one on success and zero on
210// failure.
David Benjamin4969cc92016-04-22 15:02:23 -0400211OPENSSL_EXPORT int EC_KEY_marshal_curve_name(CBB *cbb, const EC_GROUP *group);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800212
Robert Sloan8f860b12017-08-28 07:37:06 -0700213// EC_KEY_parse_parameters parses a DER-encoded ECParameters structure (RFC
214// 5480) from |cbs| and advances |cbs|. It returns a newly-allocated |EC_GROUP|
215// or NULL on error. It supports the namedCurve and specifiedCurve options, but
216// use of specifiedCurve is deprecated. Use |EC_KEY_parse_curve_name|
217// instead.
David Benjamin4969cc92016-04-22 15:02:23 -0400218OPENSSL_EXPORT EC_GROUP *EC_KEY_parse_parameters(CBS *cbs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800219
220
Robert Sloan8f860b12017-08-28 07:37:06 -0700221// ex_data functions.
222//
223// These functions are wrappers. See |ex_data.h| for details.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800224
225OPENSSL_EXPORT int EC_KEY_get_ex_new_index(long argl, void *argp,
Adam Langley4139edb2016-01-13 15:00:54 -0800226 CRYPTO_EX_unused *unused,
Robert Sloan8ff03552017-06-14 12:40:58 -0700227 CRYPTO_EX_dup *dup_unused,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800228 CRYPTO_EX_free *free_func);
229OPENSSL_EXPORT int EC_KEY_set_ex_data(EC_KEY *r, int idx, void *arg);
230OPENSSL_EXPORT void *EC_KEY_get_ex_data(const EC_KEY *r, int idx);
231
232
Robert Sloan8f860b12017-08-28 07:37:06 -0700233// ECDSA method.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800234
Robert Sloan8f860b12017-08-28 07:37:06 -0700235// ECDSA_FLAG_OPAQUE specifies that this ECDSA_METHOD does not expose its key
236// material. This may be set if, for instance, it is wrapping some other crypto
237// API, like a platform key store.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800238#define ECDSA_FLAG_OPAQUE 1
239
Robert Sloan8f860b12017-08-28 07:37:06 -0700240// ecdsa_method_st is a structure of function pointers for implementing ECDSA.
241// See engine.h.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800242struct ecdsa_method_st {
243 struct openssl_method_common_st common;
244
245 void *app_data;
246
247 int (*init)(EC_KEY *key);
248 int (*finish)(EC_KEY *key);
249
Robert Sloan8f860b12017-08-28 07:37:06 -0700250 // group_order_size returns the number of bytes needed to represent the order
251 // of the group. This is used to calculate the maximum size of an ECDSA
252 // signature in |ECDSA_size|.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800253 size_t (*group_order_size)(const EC_KEY *key);
254
Robert Sloan8f860b12017-08-28 07:37:06 -0700255 // sign matches the arguments and behaviour of |ECDSA_sign|.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800256 int (*sign)(const uint8_t *digest, size_t digest_len, uint8_t *sig,
257 unsigned int *sig_len, EC_KEY *eckey);
258
Adam Langleyd9e397b2015-01-22 14:27:53 -0800259 int flags;
260};
261
262
Robert Sloan8f860b12017-08-28 07:37:06 -0700263// Deprecated functions.
Adam Langleyf7e890d2015-03-31 18:58:05 -0700264
Robert Sloan8f860b12017-08-28 07:37:06 -0700265// EC_KEY_set_asn1_flag does nothing.
Adam Langleyf7e890d2015-03-31 18:58:05 -0700266OPENSSL_EXPORT void EC_KEY_set_asn1_flag(EC_KEY *key, int flag);
267
Robert Sloan8f860b12017-08-28 07:37:06 -0700268// d2i_ECPrivateKey parses an ASN.1, DER-encoded, private key from |len| bytes
269// at |*inp|. If |out_key| is not NULL then, on exit, a pointer to the result
270// is in |*out_key|. Note that, even if |*out_key| is already non-NULL on entry,
271// it * will not be written to. Rather, a fresh |EC_KEY| is allocated and the
272// previous * one is freed. On successful exit, |*inp| is advanced past the DER
273// structure. It returns the result or NULL on error.
274//
275// On input, if |*out_key| is non-NULL and has a group configured, the
276// parameters field may be omitted but must match that group if present.
277//
278// Use |EC_KEY_parse_private_key| instead.
David Benjamin4969cc92016-04-22 15:02:23 -0400279OPENSSL_EXPORT EC_KEY *d2i_ECPrivateKey(EC_KEY **out_key, const uint8_t **inp,
280 long len);
281
Robert Sloan8f860b12017-08-28 07:37:06 -0700282// i2d_ECPrivateKey marshals an EC private key from |key| to an ASN.1, DER
283// structure. If |outp| is not NULL then the result is written to |*outp| and
284// |*outp| is advanced just past the output. It returns the number of bytes in
285// the result, whether written or not, or a negative value on error.
286//
287// Use |EC_KEY_marshal_private_key| instead.
David Benjamin4969cc92016-04-22 15:02:23 -0400288OPENSSL_EXPORT int i2d_ECPrivateKey(const EC_KEY *key, uint8_t **outp);
289
Robert Sloan8f860b12017-08-28 07:37:06 -0700290// d2i_ECParameters parses an ASN.1, DER-encoded, set of EC parameters from
291// |len| bytes at |*inp|. If |out_key| is not NULL then, on exit, a pointer to
292// the result is in |*out_key|. Note that, even if |*out_key| is already
293// non-NULL on entry, it will not be written to. Rather, a fresh |EC_KEY| is
294// allocated and the previous one is freed. On successful exit, |*inp| is
295// advanced past the DER structure. It returns the result or NULL on error.
296//
297// Use |EC_KEY_parse_parameters| or |EC_KEY_parse_curve_name| instead.
David Benjamin4969cc92016-04-22 15:02:23 -0400298OPENSSL_EXPORT EC_KEY *d2i_ECParameters(EC_KEY **out_key, const uint8_t **inp,
299 long len);
300
Robert Sloan8f860b12017-08-28 07:37:06 -0700301// i2d_ECParameters marshals EC parameters from |key| to an ASN.1, DER
302// structure. If |outp| is not NULL then the result is written to |*outp| and
303// |*outp| is advanced just past the output. It returns the number of bytes in
304// the result, whether written or not, or a negative value on error.
305//
306// Use |EC_KEY_marshal_curve_name| instead.
David Benjamin4969cc92016-04-22 15:02:23 -0400307OPENSSL_EXPORT int i2d_ECParameters(const EC_KEY *key, uint8_t **outp);
308
Robert Sloan8f860b12017-08-28 07:37:06 -0700309// o2i_ECPublicKey parses an EC point from |len| bytes at |*inp| into
310// |*out_key|. Note that this differs from the d2i format in that |*out_key|
311// must be non-NULL with a group set. On successful exit, |*inp| is advanced by
312// |len| bytes. It returns |*out_key| or NULL on error.
313//
314// Use |EC_POINT_oct2point| instead.
David Benjamin4969cc92016-04-22 15:02:23 -0400315OPENSSL_EXPORT EC_KEY *o2i_ECPublicKey(EC_KEY **out_key, const uint8_t **inp,
316 long len);
317
Robert Sloan8f860b12017-08-28 07:37:06 -0700318// i2o_ECPublicKey marshals an EC point from |key|. If |outp| is not NULL then
319// the result is written to |*outp| and |*outp| is advanced just past the
320// output. It returns the number of bytes in the result, whether written or
321// not, or a negative value on error.
322//
323// Use |EC_POINT_point2cbb| instead.
David Benjamin4969cc92016-04-22 15:02:23 -0400324OPENSSL_EXPORT int i2o_ECPublicKey(const EC_KEY *key, unsigned char **outp);
325
Adam Langleyf7e890d2015-03-31 18:58:05 -0700326
Adam Langleyd9e397b2015-01-22 14:27:53 -0800327#if defined(__cplusplus)
Robert Sloan8f860b12017-08-28 07:37:06 -0700328} // extern C
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400329
330extern "C++" {
331
332namespace bssl {
333
334BORINGSSL_MAKE_DELETER(EC_KEY, EC_KEY_free)
335
336} // namespace bssl
337
Robert Sloan8f860b12017-08-28 07:37:06 -0700338} // extern C++
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400339
Adam Langleyd9e397b2015-01-22 14:27:53 -0800340#endif
341
Robert Sloan8f860b12017-08-28 07:37:06 -0700342#endif // OPENSSL_HEADER_EC_KEY_H