2 * Copyright 2015-2018 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 #include <openssl/ec.h>
12 #include <openssl/engine.h>
13 #include <openssl/err.h>
17 static const EC_KEY_METHOD openssl_ec_key_method = {
18 "OpenSSL EC_KEY method",
22 ossl_ecdh_compute_key,
24 ossl_ecdsa_sign_setup,
30 static const EC_KEY_METHOD *default_ec_key_meth = &openssl_ec_key_method;
32 const EC_KEY_METHOD *EC_KEY_OpenSSL(void)
34 return &openssl_ec_key_method;
37 const EC_KEY_METHOD *EC_KEY_get_default_method(void)
39 return default_ec_key_meth;
42 void EC_KEY_set_default_method(const EC_KEY_METHOD *meth)
45 default_ec_key_meth = &openssl_ec_key_method;
47 default_ec_key_meth = meth;
50 const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key)
55 int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth)
57 void (*finish)(EC_KEY *key) = key->meth->finish;
62 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
63 ENGINE_finish(key->engine);
68 if (meth->init != NULL)
69 return meth->init(key);
73 EC_KEY *ec_key_new_method_int(OPENSSL_CTX *libctx, ENGINE *engine)
75 EC_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
78 ECerr(EC_F_EC_KEY_NEW_METHOD_INT, ERR_R_MALLOC_FAILURE);
85 ret->lock = CRYPTO_THREAD_lock_new();
86 if (ret->lock == NULL) {
87 ECerr(EC_F_EC_KEY_NEW_METHOD_INT, ERR_R_MALLOC_FAILURE);
92 ret->meth = EC_KEY_get_default_method();
93 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
95 if (!ENGINE_init(engine)) {
96 ECerr(EC_F_EC_KEY_NEW_METHOD_INT, ERR_R_ENGINE_LIB);
101 ret->engine = ENGINE_get_default_EC();
102 if (ret->engine != NULL) {
103 ret->meth = ENGINE_get_EC(ret->engine);
104 if (ret->meth == NULL) {
105 ECerr(EC_F_EC_KEY_NEW_METHOD_INT, ERR_R_ENGINE_LIB);
112 ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
114 /* No ex_data inside the FIPS provider */
116 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) {
121 if (ret->meth->init != NULL && ret->meth->init(ret) == 0) {
122 ECerr(EC_F_EC_KEY_NEW_METHOD_INT, ERR_R_INIT_FAIL);
133 EC_KEY *EC_KEY_new_method(ENGINE *engine)
135 return ec_key_new_method_int(NULL, engine);
139 int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
141 void *(*KDF) (const void *in, size_t inlen, void *out,
144 unsigned char *sec = NULL;
146 if (eckey->meth->compute_key == NULL) {
147 ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
150 if (outlen > INT_MAX) {
151 ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_INVALID_OUTPUT_LENGTH);
154 if (!eckey->meth->compute_key(&sec, &seclen, pub_key, eckey))
157 KDF(sec, seclen, out, &outlen);
161 memcpy(out, sec, outlen);
163 OPENSSL_clear_free(sec, seclen);
167 EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth)
169 EC_KEY_METHOD *ret = OPENSSL_zalloc(sizeof(*meth));
175 ret->flags |= EC_KEY_METHOD_DYNAMIC;
179 void EC_KEY_METHOD_free(EC_KEY_METHOD *meth)
181 if (meth->flags & EC_KEY_METHOD_DYNAMIC)
185 void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth,
186 int (*init)(EC_KEY *key),
187 void (*finish)(EC_KEY *key),
188 int (*copy)(EC_KEY *dest, const EC_KEY *src),
189 int (*set_group)(EC_KEY *key, const EC_GROUP *grp),
190 int (*set_private)(EC_KEY *key,
191 const BIGNUM *priv_key),
192 int (*set_public)(EC_KEY *key,
193 const EC_POINT *pub_key))
196 meth->finish = finish;
198 meth->set_group = set_group;
199 meth->set_private = set_private;
200 meth->set_public = set_public;
203 void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
204 int (*keygen)(EC_KEY *key))
206 meth->keygen = keygen;
209 void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
210 int (*ckey)(unsigned char **psec,
212 const EC_POINT *pub_key,
215 meth->compute_key = ckey;
218 void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth,
219 int (*sign)(int type, const unsigned char *dgst,
220 int dlen, unsigned char *sig,
221 unsigned int *siglen,
222 const BIGNUM *kinv, const BIGNUM *r,
224 int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
225 BIGNUM **kinvp, BIGNUM **rp),
226 ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
228 const BIGNUM *in_kinv,
233 meth->sign_setup = sign_setup;
234 meth->sign_sig = sign_sig;
237 void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth,
238 int (*verify)(int type, const unsigned
239 char *dgst, int dgst_len,
240 const unsigned char *sigbuf,
241 int sig_len, EC_KEY *eckey),
242 int (*verify_sig)(const unsigned char *dgst,
244 const ECDSA_SIG *sig,
247 meth->verify = verify;
248 meth->verify_sig = verify_sig;
251 void EC_KEY_METHOD_get_init(const EC_KEY_METHOD *meth,
252 int (**pinit)(EC_KEY *key),
253 void (**pfinish)(EC_KEY *key),
254 int (**pcopy)(EC_KEY *dest, const EC_KEY *src),
255 int (**pset_group)(EC_KEY *key,
256 const EC_GROUP *grp),
257 int (**pset_private)(EC_KEY *key,
258 const BIGNUM *priv_key),
259 int (**pset_public)(EC_KEY *key,
260 const EC_POINT *pub_key))
265 *pfinish = meth->finish;
268 if (pset_group != NULL)
269 *pset_group = meth->set_group;
270 if (pset_private != NULL)
271 *pset_private = meth->set_private;
272 if (pset_public != NULL)
273 *pset_public = meth->set_public;
276 void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth,
277 int (**pkeygen)(EC_KEY *key))
280 *pkeygen = meth->keygen;
283 void EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD *meth,
284 int (**pck)(unsigned char **pout,
286 const EC_POINT *pub_key,
290 *pck = meth->compute_key;
293 void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth,
294 int (**psign)(int type, const unsigned char *dgst,
295 int dlen, unsigned char *sig,
296 unsigned int *siglen,
297 const BIGNUM *kinv, const BIGNUM *r,
299 int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
300 BIGNUM **kinvp, BIGNUM **rp),
301 ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
303 const BIGNUM *in_kinv,
309 if (psign_setup != NULL)
310 *psign_setup = meth->sign_setup;
311 if (psign_sig != NULL)
312 *psign_sig = meth->sign_sig;
315 void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth,
316 int (**pverify)(int type, const unsigned
317 char *dgst, int dgst_len,
318 const unsigned char *sigbuf,
319 int sig_len, EC_KEY *eckey),
320 int (**pverify_sig)(const unsigned char *dgst,
322 const ECDSA_SIG *sig,
326 *pverify = meth->verify;
327 if (pverify_sig != NULL)
328 *pverify_sig = meth->verify_sig;