1 // Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
2 // Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef OPENSSL_HEADER_EC_KEY_H
17 #define OPENSSL_HEADER_EC_KEY_H
18 
19 #include <openssl/base.h>   // IWYU pragma: export
20 
21 #include <openssl/ec.h>
22 #include <openssl/engine.h>
23 #include <openssl/ex_data.h>
24 
25 #if defined(__cplusplus)
26 extern "C" {
27 #endif
28 
29 
30 // ec_key.h contains functions that handle elliptic-curve points that are
31 // public/private keys.
32 
33 
34 // EC key objects.
35 //
36 // An |EC_KEY| object represents a public or private EC key. A given object may
37 // be used concurrently on multiple threads by non-mutating functions, provided
38 // no other thread is concurrently calling a mutating function. Unless otherwise
39 // documented, functions which take a |const| pointer are non-mutating and
40 // functions which take a non-|const| pointer are mutating.
41 
42 // EC_KEY_new returns a fresh |EC_KEY| object or NULL on error.
43 OPENSSL_EXPORT EC_KEY *EC_KEY_new(void);
44 
45 // EC_KEY_new_method acts the same as |EC_KEY_new|, but takes an explicit
46 // |ENGINE|.
47 OPENSSL_EXPORT EC_KEY *EC_KEY_new_method(const ENGINE *engine);
48 
49 // EC_KEY_new_by_curve_name returns a fresh EC_KEY for group specified by |nid|
50 // or NULL on error.
51 OPENSSL_EXPORT EC_KEY *EC_KEY_new_by_curve_name(int nid);
52 
53 // EC_KEY_free frees all the data owned by |key| and |key| itself.
54 OPENSSL_EXPORT void EC_KEY_free(EC_KEY *key);
55 
56 // EC_KEY_dup returns a fresh copy of |src| or NULL on error.
57 OPENSSL_EXPORT EC_KEY *EC_KEY_dup(const EC_KEY *src);
58 
59 // EC_KEY_up_ref increases the reference count of |key| and returns one. It does
60 // not mutate |key| for thread-safety purposes and may be used concurrently.
61 OPENSSL_EXPORT int EC_KEY_up_ref(EC_KEY *key);
62 
63 // EC_KEY_is_opaque returns one if |key| is opaque and doesn't expose its key
64 // material. Otherwise it return zero.
65 OPENSSL_EXPORT int EC_KEY_is_opaque(const EC_KEY *key);
66 
67 // EC_KEY_get0_group returns a pointer to the |EC_GROUP| object inside |key|.
68 OPENSSL_EXPORT const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key);
69 
70 // EC_KEY_set_group sets the |EC_GROUP| object that |key| will use to |group|.
71 // It returns one on success and zero if |key| is already configured with a
72 // different group.
73 OPENSSL_EXPORT int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group);
74 
75 // EC_KEY_get0_private_key returns a pointer to the private key inside |key|.
76 OPENSSL_EXPORT const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key);
77 
78 // EC_KEY_set_private_key sets the private key of |key| to |priv|. It returns
79 // one on success and zero otherwise. |key| must already have had a group
80 // configured (see |EC_KEY_set_group| and |EC_KEY_new_by_curve_name|).
81 OPENSSL_EXPORT int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv);
82 
83 // EC_KEY_get0_public_key returns a pointer to the public key point inside
84 // |key|.
85 OPENSSL_EXPORT const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key);
86 
87 // EC_KEY_set_public_key sets the public key of |key| to |pub|, by copying it.
88 // It returns one on success and zero otherwise. |key| must already have had a
89 // group configured (see |EC_KEY_set_group| and |EC_KEY_new_by_curve_name|), and
90 // |pub| must also belong to that group.
91 OPENSSL_EXPORT int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub);
92 
93 #define EC_PKEY_NO_PARAMETERS 0x001
94 #define EC_PKEY_NO_PUBKEY 0x002
95 
96 // EC_KEY_get_enc_flags returns the encoding flags for |key|, which is a
97 // bitwise-OR of |EC_PKEY_*| values.
98 OPENSSL_EXPORT unsigned EC_KEY_get_enc_flags(const EC_KEY *key);
99 
100 // EC_KEY_set_enc_flags sets the encoding flags for |key|, which is a
101 // bitwise-OR of |EC_PKEY_*| values.
102 OPENSSL_EXPORT void EC_KEY_set_enc_flags(EC_KEY *key, unsigned flags);
103 
104 // EC_KEY_get_conv_form returns the conversation form that will be used by
105 // |key|.
106 OPENSSL_EXPORT point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key);
107 
108 // EC_KEY_set_conv_form sets the conversion form to be used by |key|.
109 OPENSSL_EXPORT void EC_KEY_set_conv_form(EC_KEY *key,
110                                          point_conversion_form_t cform);
111 
112 // EC_KEY_check_key performs several checks on |key| (possibly including an
113 // expensive check that the public key is in the primary subgroup). It returns
114 // one if all checks pass and zero otherwise. If it returns zero then detail
115 // about the problem can be found on the error stack.
116 OPENSSL_EXPORT int EC_KEY_check_key(const EC_KEY *key);
117 
118 // EC_KEY_check_fips performs both a signing pairwise consistency test
119 // (FIPS 140-2 4.9.2) and the consistency test from SP 800-56Ar3 section
120 // 5.6.2.1.4. It returns one if it passes and zero otherwise.
121 OPENSSL_EXPORT int EC_KEY_check_fips(const EC_KEY *key);
122 
123 // EC_KEY_set_public_key_affine_coordinates sets the public key in |key| to
124 // (|x|, |y|). It returns one on success and zero on error. It's considered an
125 // error if |x| and |y| do not represent a point on |key|'s curve.
126 OPENSSL_EXPORT int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key,
127                                                             const BIGNUM *x,
128                                                             const BIGNUM *y);
129 
130 // EC_KEY_oct2key decodes |len| bytes from |in| as an EC public key in X9.62
131 // form. |key| must already have a group configured. On success, it sets the
132 // public key in |key| to the result and returns one. Otherwise, it returns
133 // zero. |ctx| may be NULL.
134 OPENSSL_EXPORT int EC_KEY_oct2key(EC_KEY *key, const uint8_t *in, size_t len,
135                                   BN_CTX *ctx);
136 
137 // EC_KEY_key2buf behaves like |EC_POINT_point2buf|, except it encodes the
138 // public key in |key|. |ctx| is ignored and may be NULL.
139 OPENSSL_EXPORT size_t EC_KEY_key2buf(const EC_KEY *key,
140                                      point_conversion_form_t form,
141                                      uint8_t **out_buf, BN_CTX *ctx);
142 
143 // EC_KEY_oct2priv decodes a big-endian, zero-padded integer from |len| bytes
144 // from |in| and sets |key|'s private key to the result. It returns one on
145 // success and zero on error. The input must be padded to the size of |key|'s
146 // group order.
147 OPENSSL_EXPORT int EC_KEY_oct2priv(EC_KEY *key, const uint8_t *in, size_t len);
148 
149 // EC_KEY_priv2oct serializes |key|'s private key as a big-endian integer,
150 // zero-padded to the size of |key|'s group order and writes the result to at
151 // most |max_out| bytes of |out|. It returns the number of bytes written on
152 // success and zero on error. If |out| is NULL, it returns the number of bytes
153 // needed without writing anything.
154 OPENSSL_EXPORT size_t EC_KEY_priv2oct(const EC_KEY *key, uint8_t *out,
155                                       size_t max_out);
156 
157 // EC_KEY_priv2buf behaves like |EC_KEY_priv2oct| but sets |*out_buf| to a
158 // newly-allocated buffer containing the result. It returns the size of the
159 // result on success and zero on error. The caller must release |*out_buf| with
160 // |OPENSSL_free| when done.
161 OPENSSL_EXPORT size_t EC_KEY_priv2buf(const EC_KEY *key, uint8_t **out_buf);
162 
163 
164 // Key generation.
165 
166 // EC_KEY_generate_key generates a random, private key, calculates the
167 // corresponding public key and stores both in |key|. It returns one on success
168 // or zero otherwise.
169 OPENSSL_EXPORT int EC_KEY_generate_key(EC_KEY *key);
170 
171 // EC_KEY_generate_key_fips behaves like |EC_KEY_generate_key| but performs
172 // additional checks for FIPS compliance. This function is applicable when
173 // generating keys for either signing/verification or key agreement because
174 // both types of consistency check (PCT) are performed.
175 OPENSSL_EXPORT int EC_KEY_generate_key_fips(EC_KEY *key);
176 
177 // EC_KEY_derive_from_secret deterministically derives a private key for |group|
178 // from an input secret using HKDF-SHA256. It returns a newly-allocated |EC_KEY|
179 // on success or NULL on error. |secret| must not be used in any other
180 // algorithm. If using a base secret for multiple operations, derive separate
181 // values with a KDF such as HKDF first.
182 //
183 // Note this function implements an arbitrary derivation scheme, rather than any
184 // particular standard one. New protocols are recommended to use X25519 and
185 // Ed25519, which have standard byte import functions. See
186 // |X25519_public_from_private| and |ED25519_keypair_from_seed|.
187 OPENSSL_EXPORT EC_KEY *EC_KEY_derive_from_secret(const EC_GROUP *group,
188                                                  const uint8_t *secret,
189                                                  size_t secret_len);
190 
191 
192 // Serialisation.
193 
194 // EC_KEY_parse_private_key parses a DER-encoded ECPrivateKey structure (RFC
195 // 5915) from |cbs| and advances |cbs|. It returns a newly-allocated |EC_KEY| or
196 // NULL on error. If |group| is non-null, the parameters field of the
197 // ECPrivateKey may be omitted (but must match |group| if present). Otherwise,
198 // the parameters field is required.
199 OPENSSL_EXPORT EC_KEY *EC_KEY_parse_private_key(CBS *cbs,
200                                                 const EC_GROUP *group);
201 
202 // EC_KEY_marshal_private_key marshals |key| as a DER-encoded ECPrivateKey
203 // structure (RFC 5915) and appends the result to |cbb|. It returns one on
204 // success and zero on failure. |enc_flags| is a combination of |EC_PKEY_*|
205 // values and controls whether corresponding fields are omitted.
206 OPENSSL_EXPORT int EC_KEY_marshal_private_key(CBB *cbb, const EC_KEY *key,
207                                               unsigned enc_flags);
208 
209 // EC_KEY_parse_curve_name parses a DER-encoded OBJECT IDENTIFIER as a curve
210 // name from |cbs| and advances |cbs|. It returns the decoded |EC_GROUP| or NULL
211 // on error.
212 //
213 // This function returns a non-const pointer which may be passed to
214 // |EC_GROUP_free|. However, the resulting object is actually static and calling
215 // |EC_GROUP_free| is optional.
216 //
217 // TODO(davidben): Make this return a const pointer, if it does not break too
218 // many callers.
219 OPENSSL_EXPORT EC_GROUP *EC_KEY_parse_curve_name(CBS *cbs);
220 
221 // EC_KEY_marshal_curve_name marshals |group| as a DER-encoded OBJECT IDENTIFIER
222 // and appends the result to |cbb|. It returns one on success and zero on
223 // failure.
224 OPENSSL_EXPORT int EC_KEY_marshal_curve_name(CBB *cbb, const EC_GROUP *group);
225 
226 // EC_KEY_parse_parameters parses a DER-encoded ECParameters structure (RFC
227 // 5480) from |cbs| and advances |cbs|. It returns the resulting |EC_GROUP| or
228 // NULL on error. It supports the namedCurve and specifiedCurve options, but use
229 // of specifiedCurve is deprecated. Use |EC_KEY_parse_curve_name| instead.
230 //
231 // This function returns a non-const pointer which may be passed to
232 // |EC_GROUP_free|. However, the resulting object is actually static and calling
233 // |EC_GROUP_free| is optional.
234 //
235 // TODO(davidben): Make this return a const pointer, if it does not break too
236 // many callers.
237 OPENSSL_EXPORT EC_GROUP *EC_KEY_parse_parameters(CBS *cbs);
238 
239 
240 // ex_data functions.
241 //
242 // These functions are wrappers. See |ex_data.h| for details.
243 
244 OPENSSL_EXPORT int EC_KEY_get_ex_new_index(long argl, void *argp,
245                                            CRYPTO_EX_unused *unused,
246                                            CRYPTO_EX_dup *dup_unused,
247                                            CRYPTO_EX_free *free_func);
248 OPENSSL_EXPORT int EC_KEY_set_ex_data(EC_KEY *r, int idx, void *arg);
249 OPENSSL_EXPORT void *EC_KEY_get_ex_data(const EC_KEY *r, int idx);
250 
251 
252 // ECDSA method.
253 
254 // ECDSA_FLAG_OPAQUE specifies that this ECDSA_METHOD does not expose its key
255 // material. This may be set if, for instance, it is wrapping some other crypto
256 // API, like a platform key store.
257 #define ECDSA_FLAG_OPAQUE 1
258 
259 // ecdsa_method_st is a structure of function pointers for implementing ECDSA.
260 // See engine.h.
261 struct ecdsa_method_st {
262   struct openssl_method_common_st common;
263 
264   void *app_data;
265 
266   int (*init)(EC_KEY *key);
267   int (*finish)(EC_KEY *key);
268 
269   // sign matches the arguments and behaviour of |ECDSA_sign|.
270   int (*sign)(const uint8_t *digest, size_t digest_len, uint8_t *sig,
271               unsigned int *sig_len, EC_KEY *eckey);
272 
273   int flags;
274 };
275 
276 
277 // Deprecated functions.
278 
279 // EC_KEY_set_asn1_flag does nothing.
280 OPENSSL_EXPORT void EC_KEY_set_asn1_flag(EC_KEY *key, int flag);
281 
282 // d2i_ECPrivateKey parses a DER-encoded ECPrivateKey structure (RFC 5915) from
283 // |len| bytes at |*inp|, as described in |d2i_SAMPLE|. On input, if |*out_key|
284 // is non-NULL and has a group configured, the parameters field may be omitted
285 // but must match that group if present.
286 //
287 // Use |EC_KEY_parse_private_key| instead.
288 OPENSSL_EXPORT EC_KEY *d2i_ECPrivateKey(EC_KEY **out_key, const uint8_t **inp,
289                                         long len);
290 
291 // i2d_ECPrivateKey marshals |key| as a DER-encoded ECPrivateKey structure (RFC
292 // 5915), as described in |i2d_SAMPLE|.
293 //
294 // Use |EC_KEY_marshal_private_key| instead.
295 OPENSSL_EXPORT int i2d_ECPrivateKey(const EC_KEY *key, uint8_t **outp);
296 
297 // d2i_ECPKParameters parses a DER-encoded ECParameters structure (RFC 5480)
298 // from |len| bytes at |*inp|, as described in |d2i_SAMPLE|. For legacy reasons,
299 // it recognizes the specifiedCurve form, but only for curves that are already
300 // supported as named curves.
301 //
302 // Use |EC_KEY_parse_parameters| or |EC_KEY_parse_curve_name| instead.
303 OPENSSL_EXPORT EC_GROUP *d2i_ECPKParameters(EC_GROUP **out, const uint8_t **inp,
304                                             long len);
305 
306 // i2d_ECPKParameters marshals |group| as a DER-encoded ECParameters structure
307 // (RFC 5480), as described in |i2d_SAMPLE|.
308 //
309 // Use |EC_KEY_marshal_curve_name| instead.
310 OPENSSL_EXPORT int i2d_ECPKParameters(const EC_GROUP *group, uint8_t **outp);
311 
312 // d2i_ECParameters parses a DER-encoded ECParameters structure (RFC 5480) from
313 // |len| bytes at |*inp|, as described in |d2i_SAMPLE|. It returns the result as
314 // an |EC_KEY| with parameters, but no key, configured.
315 //
316 // Use |EC_KEY_parse_parameters| or |EC_KEY_parse_curve_name| instead.
317 OPENSSL_EXPORT EC_KEY *d2i_ECParameters(EC_KEY **out_key, const uint8_t **inp,
318                                         long len);
319 
320 // i2d_ECParameters marshals |key|'s parameters as a DER-encoded OBJECT
321 // IDENTIFIER, as described in |i2d_SAMPLE|.
322 //
323 // Use |EC_KEY_marshal_curve_name| instead.
324 OPENSSL_EXPORT int i2d_ECParameters(const EC_KEY *key, uint8_t **outp);
325 
326 // o2i_ECPublicKey parses an EC point from |len| bytes at |*inp| into
327 // |*out_key|. Note that this differs from the d2i format in that |*out_key|
328 // must be non-NULL with a group set. On successful exit, |*inp| is advanced by
329 // |len| bytes. It returns |*out_key| or NULL on error.
330 //
331 // Use |EC_POINT_oct2point| instead.
332 OPENSSL_EXPORT EC_KEY *o2i_ECPublicKey(EC_KEY **out_key, const uint8_t **inp,
333                                        long len);
334 
335 // i2o_ECPublicKey marshals an EC point from |key|, as described in
336 // |i2d_SAMPLE|, except it returns zero on error instead of a negative value.
337 //
338 // Use |EC_POINT_point2cbb| instead.
339 OPENSSL_EXPORT int i2o_ECPublicKey(const EC_KEY *key, unsigned char **outp);
340 
341 
342 #if defined(__cplusplus)
343 }  // extern C
344 
345 extern "C++" {
346 
347 BSSL_NAMESPACE_BEGIN
348 
349 BORINGSSL_MAKE_DELETER(EC_KEY, EC_KEY_free)
350 BORINGSSL_MAKE_UP_REF(EC_KEY, EC_KEY_up_ref)
351 
352 BSSL_NAMESPACE_END
353 
354 }  // extern C++
355 
356 #endif
357 
358 #endif  // OPENSSL_HEADER_EC_KEY_H
359