2 * Copyright 2015-2020 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>
18 #include <openssl/engine.h>
19 #include <openssl/err.h>
23 static const EC_KEY_METHOD openssl_ec_key_method = {
24 "OpenSSL EC_KEY method",
28 ossl_ecdh_compute_key,
30 ossl_ecdsa_sign_setup,
36 static const EC_KEY_METHOD *default_ec_key_meth = &openssl_ec_key_method;
38 const EC_KEY_METHOD *EC_KEY_OpenSSL(void)
40 return &openssl_ec_key_method;
43 const EC_KEY_METHOD *EC_KEY_get_default_method(void)
45 return default_ec_key_meth;
48 void EC_KEY_set_default_method(const EC_KEY_METHOD *meth)
51 default_ec_key_meth = &openssl_ec_key_method;
53 default_ec_key_meth = meth;
56 const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key)
61 int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth)
63 void (*finish)(EC_KEY *key) = key->meth->finish;
68 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
69 ENGINE_finish(key->engine);
74 if (meth->init != NULL)
75 return meth->init(key);
79 EC_KEY *ec_key_new_method_int(OPENSSL_CTX *libctx, ENGINE *engine)
81 EC_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
84 ECerr(EC_F_EC_KEY_NEW_METHOD_INT, ERR_R_MALLOC_FAILURE);
91 ret->lock = CRYPTO_THREAD_lock_new();
92 if (ret->lock == NULL) {
93 ECerr(EC_F_EC_KEY_NEW_METHOD_INT, ERR_R_MALLOC_FAILURE);
98 ret->meth = EC_KEY_get_default_method();
99 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
100 if (engine != NULL) {
101 if (!ENGINE_init(engine)) {
102 ECerr(EC_F_EC_KEY_NEW_METHOD_INT, ERR_R_ENGINE_LIB);
105 ret->engine = engine;
107 ret->engine = ENGINE_get_default_EC();
108 if (ret->engine != NULL) {
109 ret->meth = ENGINE_get_EC(ret->engine);
110 if (ret->meth == NULL) {
111 ECerr(EC_F_EC_KEY_NEW_METHOD_INT, ERR_R_ENGINE_LIB);
118 ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
120 /* No ex_data inside the FIPS provider */
122 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) {
127 if (ret->meth->init != NULL && ret->meth->init(ret) == 0) {
128 ECerr(EC_F_EC_KEY_NEW_METHOD_INT, ERR_R_INIT_FAIL);
139 EC_KEY *EC_KEY_new_method(ENGINE *engine)
141 return ec_key_new_method_int(NULL, engine);
145 int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
147 void *(*KDF) (const void *in, size_t inlen, void *out,
150 unsigned char *sec = NULL;
152 if (eckey->meth->compute_key == NULL) {
153 ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
156 if (outlen > INT_MAX) {
157 ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_INVALID_OUTPUT_LENGTH);
160 if (!eckey->meth->compute_key(&sec, &seclen, pub_key, eckey))
163 KDF(sec, seclen, out, &outlen);
167 memcpy(out, sec, outlen);
169 OPENSSL_clear_free(sec, seclen);
173 EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth)
175 EC_KEY_METHOD *ret = OPENSSL_zalloc(sizeof(*meth));
181 ret->flags |= EC_KEY_METHOD_DYNAMIC;
185 void EC_KEY_METHOD_free(EC_KEY_METHOD *meth)
187 if (meth->flags & EC_KEY_METHOD_DYNAMIC)
191 void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth,
192 int (*init)(EC_KEY *key),
193 void (*finish)(EC_KEY *key),
194 int (*copy)(EC_KEY *dest, const EC_KEY *src),
195 int (*set_group)(EC_KEY *key, const EC_GROUP *grp),
196 int (*set_private)(EC_KEY *key,
197 const BIGNUM *priv_key),
198 int (*set_public)(EC_KEY *key,
199 const EC_POINT *pub_key))
202 meth->finish = finish;
204 meth->set_group = set_group;
205 meth->set_private = set_private;
206 meth->set_public = set_public;
209 void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
210 int (*keygen)(EC_KEY *key))
212 meth->keygen = keygen;
215 void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
216 int (*ckey)(unsigned char **psec,
218 const EC_POINT *pub_key,
221 meth->compute_key = ckey;
224 void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth,
225 int (*sign)(int type, const unsigned char *dgst,
226 int dlen, unsigned char *sig,
227 unsigned int *siglen,
228 const BIGNUM *kinv, const BIGNUM *r,
230 int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
231 BIGNUM **kinvp, BIGNUM **rp),
232 ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
234 const BIGNUM *in_kinv,
239 meth->sign_setup = sign_setup;
240 meth->sign_sig = sign_sig;
243 void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth,
244 int (*verify)(int type, const unsigned
245 char *dgst, int dgst_len,
246 const unsigned char *sigbuf,
247 int sig_len, EC_KEY *eckey),
248 int (*verify_sig)(const unsigned char *dgst,
250 const ECDSA_SIG *sig,
253 meth->verify = verify;
254 meth->verify_sig = verify_sig;
257 void EC_KEY_METHOD_get_init(const EC_KEY_METHOD *meth,
258 int (**pinit)(EC_KEY *key),
259 void (**pfinish)(EC_KEY *key),
260 int (**pcopy)(EC_KEY *dest, const EC_KEY *src),
261 int (**pset_group)(EC_KEY *key,
262 const EC_GROUP *grp),
263 int (**pset_private)(EC_KEY *key,
264 const BIGNUM *priv_key),
265 int (**pset_public)(EC_KEY *key,
266 const EC_POINT *pub_key))
271 *pfinish = meth->finish;
274 if (pset_group != NULL)
275 *pset_group = meth->set_group;
276 if (pset_private != NULL)
277 *pset_private = meth->set_private;
278 if (pset_public != NULL)
279 *pset_public = meth->set_public;
282 void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth,
283 int (**pkeygen)(EC_KEY *key))
286 *pkeygen = meth->keygen;
289 void EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD *meth,
290 int (**pck)(unsigned char **pout,
292 const EC_POINT *pub_key,
296 *pck = meth->compute_key;
299 void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth,
300 int (**psign)(int type, const unsigned char *dgst,
301 int dlen, unsigned char *sig,
302 unsigned int *siglen,
303 const BIGNUM *kinv, const BIGNUM *r,
305 int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
306 BIGNUM **kinvp, BIGNUM **rp),
307 ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
309 const BIGNUM *in_kinv,
315 if (psign_setup != NULL)
316 *psign_setup = meth->sign_setup;
317 if (psign_sig != NULL)
318 *psign_sig = meth->sign_sig;
321 void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth,
322 int (**pverify)(int type, const unsigned
323 char *dgst, int dgst_len,
324 const unsigned char *sigbuf,
325 int sig_len, EC_KEY *eckey),
326 int (**pverify_sig)(const unsigned char *dgst,
328 const ECDSA_SIG *sig,
332 *pverify = meth->verify;
333 if (pverify_sig != NULL)
334 *pverify_sig = meth->verify_sig;