2 * Copyright 2015-2023 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));
89 if (!CRYPTO_NEW_REF(&ret->references, 1)) {
96 ret->propq = OPENSSL_strdup(propq);
97 if (ret->propq == NULL)
101 ret->meth = EC_KEY_get_default_method();
102 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
103 if (engine != NULL) {
104 if (!ENGINE_init(engine)) {
105 ERR_raise(ERR_LIB_EC, ERR_R_ENGINE_LIB);
108 ret->engine = engine;
110 ret->engine = ENGINE_get_default_EC();
111 if (ret->engine != NULL) {
112 ret->meth = ENGINE_get_EC(ret->engine);
113 if (ret->meth == NULL) {
114 ERR_raise(ERR_LIB_EC, ERR_R_ENGINE_LIB);
121 ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
123 /* No ex_data inside the FIPS provider */
125 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) {
126 ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
131 if (ret->meth->init != NULL && ret->meth->init(ret) == 0) {
132 ERR_raise(ERR_LIB_EC, ERR_R_INIT_FAIL);
143 EC_KEY *EC_KEY_new_method(ENGINE *engine)
145 return ossl_ec_key_new_method_int(NULL, NULL, engine);
149 int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
151 void *(*KDF) (const void *in, size_t inlen, void *out,
154 unsigned char *sec = NULL;
156 if (eckey->meth->compute_key == NULL) {
157 ERR_raise(ERR_LIB_EC, EC_R_OPERATION_NOT_SUPPORTED);
160 if (outlen > INT_MAX) {
161 ERR_raise(ERR_LIB_EC, EC_R_INVALID_OUTPUT_LENGTH);
164 if (!eckey->meth->compute_key(&sec, &seclen, pub_key, eckey))
167 KDF(sec, seclen, out, &outlen);
171 memcpy(out, sec, outlen);
173 OPENSSL_clear_free(sec, seclen);
177 EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth)
179 EC_KEY_METHOD *ret = OPENSSL_zalloc(sizeof(*meth));
185 ret->flags |= EC_KEY_METHOD_DYNAMIC;
189 void EC_KEY_METHOD_free(EC_KEY_METHOD *meth)
191 if (meth->flags & EC_KEY_METHOD_DYNAMIC)
195 void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth,
196 int (*init)(EC_KEY *key),
197 void (*finish)(EC_KEY *key),
198 int (*copy)(EC_KEY *dest, const EC_KEY *src),
199 int (*set_group)(EC_KEY *key, const EC_GROUP *grp),
200 int (*set_private)(EC_KEY *key,
201 const BIGNUM *priv_key),
202 int (*set_public)(EC_KEY *key,
203 const EC_POINT *pub_key))
206 meth->finish = finish;
208 meth->set_group = set_group;
209 meth->set_private = set_private;
210 meth->set_public = set_public;
213 void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
214 int (*keygen)(EC_KEY *key))
216 meth->keygen = keygen;
219 void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
220 int (*ckey)(unsigned char **psec,
222 const EC_POINT *pub_key,
225 meth->compute_key = ckey;
228 void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth,
229 int (*sign)(int type, const unsigned char *dgst,
230 int dlen, unsigned char *sig,
231 unsigned int *siglen,
232 const BIGNUM *kinv, const BIGNUM *r,
234 int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
235 BIGNUM **kinvp, BIGNUM **rp),
236 ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
238 const BIGNUM *in_kinv,
243 meth->sign_setup = sign_setup;
244 meth->sign_sig = sign_sig;
247 void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth,
248 int (*verify)(int type, const unsigned
249 char *dgst, int dgst_len,
250 const unsigned char *sigbuf,
251 int sig_len, EC_KEY *eckey),
252 int (*verify_sig)(const unsigned char *dgst,
254 const ECDSA_SIG *sig,
257 meth->verify = verify;
258 meth->verify_sig = verify_sig;
261 void EC_KEY_METHOD_get_init(const EC_KEY_METHOD *meth,
262 int (**pinit)(EC_KEY *key),
263 void (**pfinish)(EC_KEY *key),
264 int (**pcopy)(EC_KEY *dest, const EC_KEY *src),
265 int (**pset_group)(EC_KEY *key,
266 const EC_GROUP *grp),
267 int (**pset_private)(EC_KEY *key,
268 const BIGNUM *priv_key),
269 int (**pset_public)(EC_KEY *key,
270 const EC_POINT *pub_key))
275 *pfinish = meth->finish;
278 if (pset_group != NULL)
279 *pset_group = meth->set_group;
280 if (pset_private != NULL)
281 *pset_private = meth->set_private;
282 if (pset_public != NULL)
283 *pset_public = meth->set_public;
286 void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth,
287 int (**pkeygen)(EC_KEY *key))
290 *pkeygen = meth->keygen;
293 void EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD *meth,
294 int (**pck)(unsigned char **pout,
296 const EC_POINT *pub_key,
300 *pck = meth->compute_key;
303 void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth,
304 int (**psign)(int type, const unsigned char *dgst,
305 int dlen, unsigned char *sig,
306 unsigned int *siglen,
307 const BIGNUM *kinv, const BIGNUM *r,
309 int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
310 BIGNUM **kinvp, BIGNUM **rp),
311 ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
313 const BIGNUM *in_kinv,
319 if (psign_setup != NULL)
320 *psign_setup = meth->sign_setup;
321 if (psign_sig != NULL)
322 *psign_sig = meth->sign_sig;
325 void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth,
326 int (**pverify)(int type, const unsigned
327 char *dgst, int dgst_len,
328 const unsigned char *sigbuf,
329 int sig_len, EC_KEY *eckey),
330 int (**pverify_sig)(const unsigned char *dgst,
332 const ECDSA_SIG *sig,
336 *pverify = meth->verify;
337 if (pverify_sig != NULL)
338 *pverify_sig = meth->verify_sig;