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));
91 ret->propq = OPENSSL_strdup(propq);
92 if (ret->propq == NULL)
97 ret->lock = CRYPTO_THREAD_lock_new();
98 if (ret->lock == NULL) {
99 ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
103 ret->meth = EC_KEY_get_default_method();
104 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
105 if (engine != NULL) {
106 if (!ENGINE_init(engine)) {
107 ERR_raise(ERR_LIB_EC, ERR_R_ENGINE_LIB);
110 ret->engine = engine;
112 ret->engine = ENGINE_get_default_EC();
113 if (ret->engine != NULL) {
114 ret->meth = ENGINE_get_EC(ret->engine);
115 if (ret->meth == NULL) {
116 ERR_raise(ERR_LIB_EC, ERR_R_ENGINE_LIB);
123 ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
125 /* No ex_data inside the FIPS provider */
127 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) {
128 ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
133 if (ret->meth->init != NULL && ret->meth->init(ret) == 0) {
134 ERR_raise(ERR_LIB_EC, ERR_R_INIT_FAIL);
145 EC_KEY *EC_KEY_new_method(ENGINE *engine)
147 return ossl_ec_key_new_method_int(NULL, NULL, engine);
151 int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
153 void *(*KDF) (const void *in, size_t inlen, void *out,
156 unsigned char *sec = NULL;
158 if (eckey->meth->compute_key == NULL) {
159 ERR_raise(ERR_LIB_EC, EC_R_OPERATION_NOT_SUPPORTED);
162 if (outlen > INT_MAX) {
163 ERR_raise(ERR_LIB_EC, EC_R_INVALID_OUTPUT_LENGTH);
166 if (!eckey->meth->compute_key(&sec, &seclen, pub_key, eckey))
169 KDF(sec, seclen, out, &outlen);
173 memcpy(out, sec, outlen);
175 OPENSSL_clear_free(sec, seclen);
179 EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth)
181 EC_KEY_METHOD *ret = OPENSSL_zalloc(sizeof(*meth));
187 ret->flags |= EC_KEY_METHOD_DYNAMIC;
191 void EC_KEY_METHOD_free(EC_KEY_METHOD *meth)
193 if (meth->flags & EC_KEY_METHOD_DYNAMIC)
197 void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth,
198 int (*init)(EC_KEY *key),
199 void (*finish)(EC_KEY *key),
200 int (*copy)(EC_KEY *dest, const EC_KEY *src),
201 int (*set_group)(EC_KEY *key, const EC_GROUP *grp),
202 int (*set_private)(EC_KEY *key,
203 const BIGNUM *priv_key),
204 int (*set_public)(EC_KEY *key,
205 const EC_POINT *pub_key))
208 meth->finish = finish;
210 meth->set_group = set_group;
211 meth->set_private = set_private;
212 meth->set_public = set_public;
215 void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
216 int (*keygen)(EC_KEY *key))
218 meth->keygen = keygen;
221 void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
222 int (*ckey)(unsigned char **psec,
224 const EC_POINT *pub_key,
227 meth->compute_key = ckey;
230 void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth,
231 int (*sign)(int type, const unsigned char *dgst,
232 int dlen, unsigned char *sig,
233 unsigned int *siglen,
234 const BIGNUM *kinv, const BIGNUM *r,
236 int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
237 BIGNUM **kinvp, BIGNUM **rp),
238 ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
240 const BIGNUM *in_kinv,
245 meth->sign_setup = sign_setup;
246 meth->sign_sig = sign_sig;
249 void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth,
250 int (*verify)(int type, const unsigned
251 char *dgst, int dgst_len,
252 const unsigned char *sigbuf,
253 int sig_len, EC_KEY *eckey),
254 int (*verify_sig)(const unsigned char *dgst,
256 const ECDSA_SIG *sig,
259 meth->verify = verify;
260 meth->verify_sig = verify_sig;
263 void EC_KEY_METHOD_get_init(const EC_KEY_METHOD *meth,
264 int (**pinit)(EC_KEY *key),
265 void (**pfinish)(EC_KEY *key),
266 int (**pcopy)(EC_KEY *dest, const EC_KEY *src),
267 int (**pset_group)(EC_KEY *key,
268 const EC_GROUP *grp),
269 int (**pset_private)(EC_KEY *key,
270 const BIGNUM *priv_key),
271 int (**pset_public)(EC_KEY *key,
272 const EC_POINT *pub_key))
277 *pfinish = meth->finish;
280 if (pset_group != NULL)
281 *pset_group = meth->set_group;
282 if (pset_private != NULL)
283 *pset_private = meth->set_private;
284 if (pset_public != NULL)
285 *pset_public = meth->set_public;
288 void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth,
289 int (**pkeygen)(EC_KEY *key))
292 *pkeygen = meth->keygen;
295 void EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD *meth,
296 int (**pck)(unsigned char **pout,
298 const EC_POINT *pub_key,
302 *pck = meth->compute_key;
305 void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth,
306 int (**psign)(int type, const unsigned char *dgst,
307 int dlen, unsigned char *sig,
308 unsigned int *siglen,
309 const BIGNUM *kinv, const BIGNUM *r,
311 int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
312 BIGNUM **kinvp, BIGNUM **rp),
313 ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
315 const BIGNUM *in_kinv,
321 if (psign_setup != NULL)
322 *psign_setup = meth->sign_setup;
323 if (psign_sig != NULL)
324 *psign_sig = meth->sign_sig;
327 void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth,
328 int (**pverify)(int type, const unsigned
329 char *dgst, int dgst_len,
330 const unsigned char *sigbuf,
331 int sig_len, EC_KEY *eckey),
332 int (**pverify_sig)(const unsigned char *dgst,
334 const ECDSA_SIG *sig,
338 *pverify = meth->verify;
339 if (pverify_sig != NULL)
340 *pverify_sig = meth->verify_sig;