2 * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * ECDH and ECDSA low level APIs are deprecated for public use, but still ok
14 #include "internal/deprecated.h"
17 #include <openssl/ec.h>
19 # include <openssl/engine.h>
21 #include <openssl/err.h>
25 static const EC_KEY_METHOD openssl_ec_key_method
= {
26 "OpenSSL EC_KEY method",
30 ossl_ecdh_compute_key
,
32 ossl_ecdsa_sign_setup
,
38 static const EC_KEY_METHOD
*default_ec_key_meth
= &openssl_ec_key_method
;
40 const EC_KEY_METHOD
*EC_KEY_OpenSSL(void)
42 return &openssl_ec_key_method
;
45 const EC_KEY_METHOD
*EC_KEY_get_default_method(void)
47 return default_ec_key_meth
;
50 void EC_KEY_set_default_method(const EC_KEY_METHOD
*meth
)
53 default_ec_key_meth
= &openssl_ec_key_method
;
55 default_ec_key_meth
= meth
;
58 const EC_KEY_METHOD
*EC_KEY_get_method(const EC_KEY
*key
)
63 int EC_KEY_set_method(EC_KEY
*key
, const EC_KEY_METHOD
*meth
)
65 void (*finish
)(EC_KEY
*key
) = key
->meth
->finish
;
70 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
71 ENGINE_finish(key
->engine
);
76 if (meth
->init
!= NULL
)
77 return meth
->init(key
);
81 EC_KEY
*ossl_ec_key_new_method_int(OSSL_LIB_CTX
*libctx
, const char *propq
,
84 EC_KEY
*ret
= OPENSSL_zalloc(sizeof(*ret
));
87 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
93 ret
->propq
= OPENSSL_strdup(propq
);
94 if (ret
->propq
== NULL
) {
95 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
101 ret
->lock
= CRYPTO_THREAD_lock_new();
102 if (ret
->lock
== NULL
) {
103 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
107 ret
->meth
= EC_KEY_get_default_method();
108 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
109 if (engine
!= NULL
) {
110 if (!ENGINE_init(engine
)) {
111 ERR_raise(ERR_LIB_EC
, ERR_R_ENGINE_LIB
);
114 ret
->engine
= engine
;
116 ret
->engine
= ENGINE_get_default_EC();
117 if (ret
->engine
!= NULL
) {
118 ret
->meth
= ENGINE_get_EC(ret
->engine
);
119 if (ret
->meth
== NULL
) {
120 ERR_raise(ERR_LIB_EC
, ERR_R_ENGINE_LIB
);
127 ret
->conv_form
= POINT_CONVERSION_UNCOMPRESSED
;
129 /* No ex_data inside the FIPS provider */
131 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY
, ret
, &ret
->ex_data
)) {
136 if (ret
->meth
->init
!= NULL
&& ret
->meth
->init(ret
) == 0) {
137 ERR_raise(ERR_LIB_EC
, ERR_R_INIT_FAIL
);
148 EC_KEY
*EC_KEY_new_method(ENGINE
*engine
)
150 return ossl_ec_key_new_method_int(NULL
, NULL
, engine
);
154 int ECDH_compute_key(void *out
, size_t outlen
, const EC_POINT
*pub_key
,
156 void *(*KDF
) (const void *in
, size_t inlen
, void *out
,
159 unsigned char *sec
= NULL
;
161 if (eckey
->meth
->compute_key
== NULL
) {
162 ERR_raise(ERR_LIB_EC
, EC_R_OPERATION_NOT_SUPPORTED
);
165 if (outlen
> INT_MAX
) {
166 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_OUTPUT_LENGTH
);
169 if (!eckey
->meth
->compute_key(&sec
, &seclen
, pub_key
, eckey
))
172 KDF(sec
, seclen
, out
, &outlen
);
176 memcpy(out
, sec
, outlen
);
178 OPENSSL_clear_free(sec
, seclen
);
182 EC_KEY_METHOD
*EC_KEY_METHOD_new(const EC_KEY_METHOD
*meth
)
184 EC_KEY_METHOD
*ret
= OPENSSL_zalloc(sizeof(*meth
));
190 ret
->flags
|= EC_KEY_METHOD_DYNAMIC
;
194 void EC_KEY_METHOD_free(EC_KEY_METHOD
*meth
)
196 if (meth
->flags
& EC_KEY_METHOD_DYNAMIC
)
200 void EC_KEY_METHOD_set_init(EC_KEY_METHOD
*meth
,
201 int (*init
)(EC_KEY
*key
),
202 void (*finish
)(EC_KEY
*key
),
203 int (*copy
)(EC_KEY
*dest
, const EC_KEY
*src
),
204 int (*set_group
)(EC_KEY
*key
, const EC_GROUP
*grp
),
205 int (*set_private
)(EC_KEY
*key
,
206 const BIGNUM
*priv_key
),
207 int (*set_public
)(EC_KEY
*key
,
208 const EC_POINT
*pub_key
))
211 meth
->finish
= finish
;
213 meth
->set_group
= set_group
;
214 meth
->set_private
= set_private
;
215 meth
->set_public
= set_public
;
218 void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD
*meth
,
219 int (*keygen
)(EC_KEY
*key
))
221 meth
->keygen
= keygen
;
224 void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD
*meth
,
225 int (*ckey
)(unsigned char **psec
,
227 const EC_POINT
*pub_key
,
230 meth
->compute_key
= ckey
;
233 void EC_KEY_METHOD_set_sign(EC_KEY_METHOD
*meth
,
234 int (*sign
)(int type
, const unsigned char *dgst
,
235 int dlen
, unsigned char *sig
,
236 unsigned int *siglen
,
237 const BIGNUM
*kinv
, const BIGNUM
*r
,
239 int (*sign_setup
)(EC_KEY
*eckey
, BN_CTX
*ctx_in
,
240 BIGNUM
**kinvp
, BIGNUM
**rp
),
241 ECDSA_SIG
*(*sign_sig
)(const unsigned char *dgst
,
243 const BIGNUM
*in_kinv
,
248 meth
->sign_setup
= sign_setup
;
249 meth
->sign_sig
= sign_sig
;
252 void EC_KEY_METHOD_set_verify(EC_KEY_METHOD
*meth
,
253 int (*verify
)(int type
, const unsigned
254 char *dgst
, int dgst_len
,
255 const unsigned char *sigbuf
,
256 int sig_len
, EC_KEY
*eckey
),
257 int (*verify_sig
)(const unsigned char *dgst
,
259 const ECDSA_SIG
*sig
,
262 meth
->verify
= verify
;
263 meth
->verify_sig
= verify_sig
;
266 void EC_KEY_METHOD_get_init(const EC_KEY_METHOD
*meth
,
267 int (**pinit
)(EC_KEY
*key
),
268 void (**pfinish
)(EC_KEY
*key
),
269 int (**pcopy
)(EC_KEY
*dest
, const EC_KEY
*src
),
270 int (**pset_group
)(EC_KEY
*key
,
271 const EC_GROUP
*grp
),
272 int (**pset_private
)(EC_KEY
*key
,
273 const BIGNUM
*priv_key
),
274 int (**pset_public
)(EC_KEY
*key
,
275 const EC_POINT
*pub_key
))
280 *pfinish
= meth
->finish
;
283 if (pset_group
!= NULL
)
284 *pset_group
= meth
->set_group
;
285 if (pset_private
!= NULL
)
286 *pset_private
= meth
->set_private
;
287 if (pset_public
!= NULL
)
288 *pset_public
= meth
->set_public
;
291 void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD
*meth
,
292 int (**pkeygen
)(EC_KEY
*key
))
295 *pkeygen
= meth
->keygen
;
298 void EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD
*meth
,
299 int (**pck
)(unsigned char **pout
,
301 const EC_POINT
*pub_key
,
305 *pck
= meth
->compute_key
;
308 void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD
*meth
,
309 int (**psign
)(int type
, const unsigned char *dgst
,
310 int dlen
, unsigned char *sig
,
311 unsigned int *siglen
,
312 const BIGNUM
*kinv
, const BIGNUM
*r
,
314 int (**psign_setup
)(EC_KEY
*eckey
, BN_CTX
*ctx_in
,
315 BIGNUM
**kinvp
, BIGNUM
**rp
),
316 ECDSA_SIG
*(**psign_sig
)(const unsigned char *dgst
,
318 const BIGNUM
*in_kinv
,
324 if (psign_setup
!= NULL
)
325 *psign_setup
= meth
->sign_setup
;
326 if (psign_sig
!= NULL
)
327 *psign_sig
= meth
->sign_sig
;
330 void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD
*meth
,
331 int (**pverify
)(int type
, const unsigned
332 char *dgst
, int dgst_len
,
333 const unsigned char *sigbuf
,
334 int sig_len
, EC_KEY
*eckey
),
335 int (**pverify_sig
)(const unsigned char *dgst
,
337 const ECDSA_SIG
*sig
,
341 *pverify
= meth
->verify
;
342 if (pverify_sig
!= NULL
)
343 *pverify_sig
= meth
->verify_sig
;