Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/include/openssl/ec_key.h

raw
1 // Originally written by Bodo Moeller for the OpenSSL project.
2 // Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
3 // Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4 //
5 // The elliptic curve binary polynomial software is originally written by
6 // Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
7 // Laboratories.
8 //
9 // SPDX-License-Identifier: Apache-2.0
10
11 #ifndef OPENSSL_HEADER_EC_KEY_H
12 #define OPENSSL_HEADER_EC_KEY_H
13
14 #include <openssl/base.h>
15
16 #include <openssl/ec.h>
17 #include <openssl/engine.h>
18 #include <openssl/ex_data.h>
19
20 #if defined(__cplusplus)
21 extern "C" {
22 #endif
23
24
25 // ec_key.h contains functions that handle elliptic-curve points that are
26 // public/private keys.
27
28
29 // EC key objects.
30 //
31 // An |EC_KEY| object represents a public or private EC key. A given object may
32 // be used concurrently on multiple threads by non-mutating functions, provided
33 // no other thread is concurrently calling a mutating function. Unless otherwise
34 // documented, functions which take a |const| pointer are non-mutating and
35 // functions which take a non-|const| pointer are mutating.
36
37 // EC_KEY_new returns a fresh |EC_KEY| object or NULL on error.
38 OPENSSL_EXPORT EC_KEY *EC_KEY_new(void);
39
40 // EC_KEY_new_method acts the same as |EC_KEY_new|, but takes an explicit
41 // |ENGINE|.
42 OPENSSL_EXPORT EC_KEY *EC_KEY_new_method(const ENGINE *engine);
43
44 // EC_KEY_new_by_curve_name returns a fresh EC_KEY for group specified by |nid|
45 // or NULL on error.
46 OPENSSL_EXPORT EC_KEY *EC_KEY_new_by_curve_name(int nid);
47
48 // EC_KEY_free frees all the data owned by |key| and |key| itself.
49 OPENSSL_EXPORT void EC_KEY_free(EC_KEY *key);
50
51 // EC_KEY_dup returns a fresh copy of |src| or NULL on error.
52 OPENSSL_EXPORT EC_KEY *EC_KEY_dup(const EC_KEY *src);
53
54 // EC_KEY_up_ref increases the reference count of |key| and returns one. It does
55 // not mutate |key| for thread-safety purposes and may be used concurrently.
56 OPENSSL_EXPORT int EC_KEY_up_ref(EC_KEY *key);
57
58 // EC_KEY_is_opaque returns one if |key| is opaque and doesn't expose its key
59 // material. Otherwise it return zero.
60 OPENSSL_EXPORT int EC_KEY_is_opaque(const EC_KEY *key);
61
62 // EC_KEY_get0_group returns a pointer to the |EC_GROUP| object inside |key|.
63 OPENSSL_EXPORT const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key);
64
65 // EC_KEY_set_group sets the |EC_GROUP| object that |key| will use to |group|.
66 // It returns one on success and zero if |key| is already configured with a
67 // different group.
68 OPENSSL_EXPORT int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group);
69
70 // EC_KEY_get0_private_key returns a pointer to the private key inside |key|.
71 OPENSSL_EXPORT const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key);
72
73 // EC_KEY_set_private_key sets the private key of |key| to |priv|. It returns
74 // one on success and zero otherwise. |key| must already have had a group
75 // configured (see |EC_KEY_set_group| and |EC_KEY_new_by_curve_name|).
76 OPENSSL_EXPORT int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv);
77
78 // EC_KEY_get0_public_key returns a pointer to the public key point inside
79 // |key|.
80 OPENSSL_EXPORT const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key);
81
82 // EC_KEY_set_public_key sets the public key of |key| to |pub|, by copying it.
83 // It returns one on success and zero otherwise. |key| must already have had a
84 // group configured (see |EC_KEY_set_group| and |EC_KEY_new_by_curve_name|), and
85 // |pub| must also belong to that group, and must not be the point at infinity.
86 OPENSSL_EXPORT int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub);
87
88 #define EC_PKEY_NO_PARAMETERS 0x001
89 #define EC_PKEY_NO_PUBKEY 0x002
90
91 // EC_KEY_get_enc_flags returns the encoding flags for |key|, which is a
92 // bitwise-OR of |EC_PKEY_*| values.
93 OPENSSL_EXPORT unsigned EC_KEY_get_enc_flags(const EC_KEY *key);
94
95 // EC_KEY_set_enc_flags sets the encoding flags for |key|, which is a
96 // bitwise-OR of |EC_PKEY_*| values.
97 OPENSSL_EXPORT void EC_KEY_set_enc_flags(EC_KEY *key, unsigned flags);
98
99 // EC_KEY_get_conv_form returns the conversation form that will be used by
100 // |key|.
101 OPENSSL_EXPORT point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key);
102
103 // EC_KEY_set_conv_form sets the conversion form to be used by |key|.
104 OPENSSL_EXPORT void EC_KEY_set_conv_form(EC_KEY *key,
105 point_conversion_form_t cform);
106
107 // EC_KEY_check_key performs several checks on |key| including, if the
108 // private is present, an expensive check that the public key
109 // corresponds to it. It returns one if all checks pass and zero
110 // otherwise. If it returns zero then detail about the problem can be
111 // found on the error stack.
112 OPENSSL_EXPORT int EC_KEY_check_key(const EC_KEY *key);
113
114 // EC_KEY_check_fips performs a signing pairwise consistency test (FIPS 140-2
115 // 4.9.2) and the consistency test from SP 800-56Ar3 section 5.6.2.1.4.
116 // If the public key contains an affine point, it also checks that its
117 // coordinates are in the range [0, p-1]. That is in addition to the checks
118 // performed by EC_KEY_check_key.
119 // It returns one if it passes and zero otherwise.
120 OPENSSL_EXPORT int EC_KEY_check_fips(const EC_KEY *key);
121
122 // EC_KEY_set_public_key_affine_coordinates sets the public key in |key| to
123 // (|x|, |y|). It returns one on success and zero on error. It's considered an
124 // error if |x| and |y| do not represent a point on |key|'s curve.
125 OPENSSL_EXPORT int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key,
126 const BIGNUM *x,
127 const BIGNUM *y);
128
129 // EC_KEY_key2buf encodes the public key in |key| to an allocated octet string
130 // and sets |*out_buf| to point to it. It returns the length of the encoded
131 // octet string or zero if an error occurred.
132 OPENSSL_EXPORT size_t EC_KEY_key2buf(const EC_KEY *key,
133 point_conversion_form_t form,
134 unsigned char **out_buf, BN_CTX *ctx);
135
136
137 // Key generation.
138
139 // EC_KEY_generate_key generates a random, private key, calculates the
140 // corresponding public key and stores both in |key|. It returns one on success
141 // or zero otherwise.
142 OPENSSL_EXPORT int EC_KEY_generate_key(EC_KEY *key);
143
144 // EC_KEY_generate_key_fips behaves like |EC_KEY_generate_key| but performs
145 // additional checks for FIPS compliance. This function is applicable when
146 // generating keys for either signing/verification or key agreement because
147 // both types of consistency check (PCT) are performed.
148 OPENSSL_EXPORT int EC_KEY_generate_key_fips(EC_KEY *key);
149
150 // EC_KEY_derive_from_secret deterministically derives a private key for |group|
151 // from an input secret using HKDF-SHA256. It returns a newly-allocated |EC_KEY|
152 // on success or NULL on error. |secret| must not be used in any other
153 // algorithm. If using a base secret for multiple operations, derive separate
154 // values with a KDF such as HKDF first.
155 //
156 // Note this function implements an arbitrary derivation scheme, rather than any
157 // particular standard one. New protocols are recommended to use X25519 and
158 // Ed25519, which have standard byte import functions. See
159 // |X25519_public_from_private| and |ED25519_keypair_from_seed|.
160 OPENSSL_EXPORT EC_KEY *EC_KEY_derive_from_secret(const EC_GROUP *group,
161 const uint8_t *secret,
162 size_t secret_len);
163
164
165 // Serialisation.
166
167 // EC_KEY_parse_private_key parses a DER-encoded ECPrivateKey structure (RFC
168 // 5915) from |cbs| and advances |cbs|. It returns a newly-allocated |EC_KEY| or
169 // NULL on error. If |group| is non-null, the parameters field of the
170 // ECPrivateKey may be omitted (but must match |group| if present). Otherwise,
171 // the parameters field is required.
172 OPENSSL_EXPORT EC_KEY *EC_KEY_parse_private_key(CBS *cbs,
173 const EC_GROUP *group);
174
175 // EC_KEY_marshal_private_key marshals |key| as a DER-encoded ECPrivateKey
176 // structure (RFC 5915) and appends the result to |cbb|. It returns one on
177 // success and zero on failure. |enc_flags| is a combination of |EC_PKEY_*|
178 // values and controls whether corresponding fields are omitted.
179 OPENSSL_EXPORT int EC_KEY_marshal_private_key(CBB *cbb, const EC_KEY *key,
180 unsigned enc_flags);
181
182 // EC_KEY_parse_curve_name parses a DER-encoded OBJECT IDENTIFIER as a curve
183 // name from |cbs| and advances |cbs|. It returns the decoded |EC_GROUP| or NULL
184 // on error.
185 //
186 // This function returns a non-const pointer which may be passed to
187 // |EC_GROUP_free|. However, the resulting object is actually static and calling
188 // |EC_GROUP_free| is optional.
189 //
190 // TODO(davidben): Make this return a const pointer, if it does not break too
191 // many callers.
192 OPENSSL_EXPORT EC_GROUP *EC_KEY_parse_curve_name(CBS *cbs);
193
194 // EC_KEY_marshal_curve_name marshals |group| as a DER-encoded OBJECT IDENTIFIER
195 // and appends the result to |cbb|. It returns one on success and zero on
196 // failure.
197 OPENSSL_EXPORT int EC_KEY_marshal_curve_name(CBB *cbb, const EC_GROUP *group);
198
199 // EC_KEY_parse_parameters parses a DER-encoded ECParameters structure (RFC
200 // 5480) from |cbs| and advances |cbs|. It returns the resulting |EC_GROUP| or
201 // NULL on error. It supports the namedCurve and specifiedCurve options, but use
202 // of specifiedCurve is deprecated. Use |EC_KEY_parse_curve_name| instead.
203 //
204 // This function returns a non-const pointer which may be passed to
205 // |EC_GROUP_free|. However, the resulting object is actually static and calling
206 // |EC_GROUP_free| is optional.
207 //
208 // TODO(davidben): Make this return a const pointer, if it does not break too
209 // many callers.
210 OPENSSL_EXPORT EC_GROUP *EC_KEY_parse_parameters(CBS *cbs);
211
212
213 // ex_data functions.
214 //
215 // These functions are wrappers. See |ex_data.h| for details.
216
217 OPENSSL_EXPORT int EC_KEY_get_ex_new_index(long argl, void *argp,
218 CRYPTO_EX_unused *unused,
219 CRYPTO_EX_dup *dup_unused,
220 CRYPTO_EX_free *free_func);
221 OPENSSL_EXPORT int EC_KEY_set_ex_data(EC_KEY *r, int idx, void *arg);
222 OPENSSL_EXPORT void *EC_KEY_get_ex_data(const EC_KEY *r, int idx);
223
224
225 // Deprecated functions.
226
227 // d2i_ECPrivateKey parses a DER-encoded ECPrivateKey structure (RFC 5915) from
228 // |len| bytes at |*inp|, as described in |d2i_SAMPLE|. On input, if |*out_key|
229 // is non-NULL and has a group configured, the parameters field may be omitted
230 // but must match that group if present.
231 //
232 // Use |EC_KEY_parse_private_key| instead.
233 OPENSSL_EXPORT EC_KEY *d2i_ECPrivateKey(EC_KEY **out_key, const uint8_t **inp,
234 long len);
235
236 // i2d_ECPrivateKey marshals |key| as a DER-encoded ECPrivateKey structure (RFC
237 // 5915), as described in |i2d_SAMPLE|.
238 //
239 // Use |EC_KEY_marshal_private_key| instead.
240 OPENSSL_EXPORT int i2d_ECPrivateKey(const EC_KEY *key, uint8_t **outp);
241
242 // d2i_ECParameters parses a DER-encoded ECParameters structure (RFC 5480) from
243 // |len| bytes at |*inp|, as described in |d2i_SAMPLE|.
244 //
245 // Use |EC_KEY_parse_parameters| or |EC_KEY_parse_curve_name| instead. Only
246 // deserialization of namedCurves or explicitly-encoded versions of named curves
247 // are supported.
248 OPENSSL_EXPORT EC_KEY *d2i_ECParameters(EC_KEY **out_key, const uint8_t **inp,
249 long len);
250
251 // i2d_ECParameters marshals |key|'s parameters as a DER-encoded OBJECT
252 // IDENTIFIER, as described in |i2d_SAMPLE|.
253 //
254 // Use |EC_KEY_marshal_curve_name| instead. Only serialization of namedCurves
255 // are supported.
256 OPENSSL_EXPORT int i2d_ECParameters(const EC_KEY *key, uint8_t **outp);
257
258 // d2i_ECPKParameters_bio deserializes the |ECPKParameters| specified in RFC
259 // 3279 from |bio| and returns the corresponding |EC_GROUP|. If |*out_group| is
260 // non-null, the original |*out_group| is freed and the returned |EC_GROUP| is
261 // also written to |*out_group|. The user continues to maintain the memory
262 // assigned to |*out_group| if non-null.
263 //
264 // Only deserialization of namedCurves or
265 // explicitly-encoded versions of namedCurves are supported.
266 OPENSSL_EXPORT EC_GROUP *d2i_ECPKParameters_bio(BIO *bio, EC_GROUP **out_group);
267
268 // i2d_ECPKParameters_bio serializes an |EC_GROUP| to |bio| according to the
269 // |ECPKParameters| specified in RFC 3279. It returns 1 on success and 0 on
270 // failure.
271 // Only serialization of namedCurves are supported.
272 OPENSSL_EXPORT int i2d_ECPKParameters_bio(BIO *bio, const EC_GROUP *group);
273
274 // o2i_ECPublicKey parses an EC point from |len| bytes at |*inp| into
275 // |*out_key|. Note that this differs from the d2i format in that |*out_key|
276 // must be non-NULL with a group set. The point must not be the point at
277 // infinity. On successful exit, |*inp| is advanced by |len| bytes. It returns
278 // |*out_key| or NULL on error.
279 //
280 // Use |EC_POINT_oct2point| instead.
281 OPENSSL_EXPORT EC_KEY *o2i_ECPublicKey(EC_KEY **out_key, const uint8_t **inp,
282 long len);
283
284 // i2o_ECPublicKey marshals an EC point from |key|, as described in
285 // |i2d_SAMPLE|, except it returns zero on error instead of a negative value.
286 //
287 // Use |EC_POINT_point2cbb| instead.
288 OPENSSL_EXPORT int i2o_ECPublicKey(const EC_KEY *key, unsigned char **outp);
289
290
291 // EC_KEY_METHOD
292 // This struct replaces the old |ECDSA_METHOD| struct.
293
294 // ECDSA_FLAG_OPAQUE specifies that this EC_KEY_METHOD does not expose its key
295 // material. This may be set if, for instance, it is wrapping some other crypto
296 // API, like a platform key store. Use |EC_KEY_METHOD_set_flag| to set
297 // this flag on an |EC_KEY_METHOD|. It is not set by default.
298 // This was supported in ECDSA_METHOD previously.
299 #define ECDSA_FLAG_OPAQUE 1
300
301 // EC_KEY_get_default_method returns a reference to the default
302 // |EC_KEY| implementation. All |EC_KEY| objects are initialized with the
303 // returned struct. This function currently calls |EC_KEY_OpenSSL| since AWS-LC
304 // does not support changing/setting the default method.
305 OPENSSL_EXPORT const EC_KEY_METHOD *EC_KEY_get_default_method(void);
306
307 // EC_KEY_OpenSSL returns a reference to the default |EC_KEY| implementation.
308 // The returned |EC_KEY_METHOD| object is statically allocated. The application
309 // should not free this struct.
310 //
311 // This struct is also zero-initialized. This is different from OpenSSL which
312 // returns function pointers to the default implementations within the
313 // |EC_KEY_METHOD| struct. We do not do this to make it easier for the
314 // compiler/linker to drop unused functions. The wrapper functions for a given
315 // operation (e.g. |ECDSA_sign| corresponds to the |sign| field in
316 // |EC_KEY_METHOD|) will select the appropriate default implementation.
317 OPENSSL_EXPORT const EC_KEY_METHOD *EC_KEY_OpenSSL(void);
318
319 // EC_KEY_METHOD_new returns a newly allocated |EC_KEY_METHOD| object. If the
320 // input parameter |eckey_meth| is non-NULL, the function pointers within the
321 // returned |EC_KEY_METHOD| object will be initialized to the values from
322 // |eckey_meth|. If |eckey_meth| is NULL, the returned object will be
323 // initialized using the value returned from |EC_KEY_get_default_method|.
324 OPENSSL_EXPORT EC_KEY_METHOD *EC_KEY_METHOD_new(
325 const EC_KEY_METHOD *eckey_meth);
326
327 // EC_KEY_METHOD_free frees the memory associated with |eckey_meth|
328 OPENSSL_EXPORT void EC_KEY_METHOD_free(EC_KEY_METHOD *eckey_meth);
329
330 // EC_KEY_set_method sets |meth| on |ec|. We do not support setting the
331 // |copy|, |set_group|, |set_private|, |set_public|, and |sign_setup|
332 // fields in |ec| and these pointers should be set to NULL. We do not support
333 // the |verify|, |verify_sig|, or |keygen| fields yet.
334 //
335 // Returns zero on failure and one on success.
336 OPENSSL_EXPORT int EC_KEY_set_method(EC_KEY *ec, const EC_KEY_METHOD *meth);
337
338 // EC_KEY_get_method returns the |EC_KEY_METHOD| object associated with |ec|.
339 OPENSSL_EXPORT const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *ec);
340
341 // EC_KEY_METHOD_set_sign_awslc sets the |sign| and |sign_sig| pointers on
342 // |meth|.
343 OPENSSL_EXPORT void EC_KEY_METHOD_set_sign_awslc(
344 EC_KEY_METHOD *meth,
345 int (*sign)(int type, const uint8_t *digest, int digest_len, uint8_t *sig,
346 unsigned int *siglen, const BIGNUM *k_inv, const BIGNUM *r,
347 EC_KEY *eckey),
348 ECDSA_SIG *(*sign_sig)(const uint8_t *digest, int digest_len,
349 const BIGNUM *in_kinv, const BIGNUM *in_r,
350 EC_KEY *eckey));
351
352
353 // EC_KEY_METHOD_set_sign sets function pointers on |meth|. AWS-LC currently
354 // supports setting |sign| and |sign_sig|. |sign_setup| must be set to NULL in
355 // order to compile with AWS-LC.
356 #define EC_KEY_METHOD_set_sign(meth, sign, sign_setup, sign_sig) \
357 OPENSSL_STATIC_ASSERT((sign_setup) == NULL, \
358 EC_KEY_METHOD_sign_setup_field_must_be_NULL); \
359 EC_KEY_METHOD_set_sign_awslc(meth, sign, sign_sig);
360
361 // EC_KEY_METHOD_set_init_awslc sets the |init| and |finish| pointers on |meth|.
362 OPENSSL_EXPORT void EC_KEY_METHOD_set_init_awslc(EC_KEY_METHOD *meth,
363 int (*init)(EC_KEY *key),
364 void (*finish)(EC_KEY *key));
365
366
367 // EC_KEY_METHOD_set_init sets function pointers on |meth|. AWS-LC
368 // currently only supports setting the |init| and |finish| fields. |copy|,
369 // |set_group|, |set_private|, and |set_public| cannot be set yet and must
370 // be NULL.
371 #define EC_KEY_METHOD_set_init(meth, init, finish, copy, set_group, \
372 set_private, set_public) \
373 OPENSSL_STATIC_ASSERT( \
374 (copy) == NULL && (set_group) == NULL && (set_private) == NULL && \
375 (set_public) == NULL, \
376 EC_KEY_METHOD_copy_set_group_set_private_and_set_public_fields_must_be_NULL); \
377 EC_KEY_METHOD_set_init_awslc(meth, init, finish);
378
379 // EC_KEY_METHOD_set_flags sets |flags| on |meth|. Currently, the only supported
380 // flag is |ECDSA_FLAG_OPAQUE|. Returns zero on failure and one on success.
381 OPENSSL_EXPORT int EC_KEY_METHOD_set_flags(EC_KEY_METHOD *meth, int flags);
382
383
384 // General No-op Functions [Deprecated].
385
386 // EC_KEY_set_asn1_flag does nothing. AWS-LC only supports
387 // |OPENSSL_EC_NAMED_CURVE|.
388 OPENSSL_EXPORT OPENSSL_DEPRECATED void EC_KEY_set_asn1_flag(EC_KEY *key,
389 int flag);
390
391
392 #if defined(__cplusplus)
393 } // extern C
394
395 extern "C++" {
396
397 BSSL_NAMESPACE_BEGIN
398
399 BORINGSSL_MAKE_DELETER(EC_KEY, EC_KEY_free)
400 BORINGSSL_MAKE_UP_REF(EC_KEY, EC_KEY_up_ref)
401
402 BSSL_NAMESPACE_END
403
404 } // extern C++
405
406 #endif
407
408 #endif // OPENSSL_HEADER_EC_KEY_H