2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 #ifndef OPENSSL_NO_ENGINE
63 ENGINE_finish(key->engine);
68 if (meth->init != NULL)
69 return meth->init(key);
73 EC_KEY *EC_KEY_new_method(ENGINE *engine)
75 EC_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
78 ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_MALLOC_FAILURE);
83 ret->lock = CRYPTO_THREAD_lock_new();
84 if (ret->lock == NULL) {
85 ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_MALLOC_FAILURE);
90 ret->meth = EC_KEY_get_default_method();
91 #ifndef OPENSSL_NO_ENGINE
93 if (!ENGINE_init(engine)) {
94 ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_ENGINE_LIB);
99 ret->engine = ENGINE_get_default_EC();
100 if (ret->engine != NULL) {
101 ret->meth = ENGINE_get_EC(ret->engine);
102 if (ret->meth == NULL) {
103 ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_ENGINE_LIB);
110 ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
112 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) {
116 if (ret->meth->init != NULL && ret->meth->init(ret) == 0) {
117 ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_INIT_FAIL);
127 int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
129 void *(*KDF) (const void *in, size_t inlen, void *out,
132 unsigned char *sec = NULL;
134 if (eckey->meth->compute_key == NULL) {
135 ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
138 if (outlen > INT_MAX) {
139 ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_INVALID_OUTPUT_LENGTH);
142 if (!eckey->meth->compute_key(&sec, &seclen, pub_key, eckey))
145 KDF(sec, seclen, out, &outlen);
149 memcpy(out, sec, outlen);
151 OPENSSL_clear_free(sec, seclen);
155 EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth)
157 EC_KEY_METHOD *ret = OPENSSL_zalloc(sizeof(*meth));
163 ret->flags |= EC_KEY_METHOD_DYNAMIC;
167 void EC_KEY_METHOD_free(EC_KEY_METHOD *meth)
169 if (meth->flags & EC_KEY_METHOD_DYNAMIC)
173 void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth,
174 int (*init)(EC_KEY *key),
175 void (*finish)(EC_KEY *key),
176 int (*copy)(EC_KEY *dest, const EC_KEY *src),
177 int (*set_group)(EC_KEY *key, const EC_GROUP *grp),
178 int (*set_private)(EC_KEY *key,
179 const BIGNUM *priv_key),
180 int (*set_public)(EC_KEY *key,
181 const EC_POINT *pub_key))
184 meth->finish = finish;
186 meth->set_group = set_group;
187 meth->set_private = set_private;
188 meth->set_public = set_public;
191 void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
192 int (*keygen)(EC_KEY *key))
194 meth->keygen = keygen;
197 void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
198 int (*ckey)(unsigned char **psec,
200 const EC_POINT *pub_key,
203 meth->compute_key = ckey;
206 void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth,
207 int (*sign)(int type, const unsigned char *dgst,
208 int dlen, unsigned char *sig,
209 unsigned int *siglen,
210 const BIGNUM *kinv, const BIGNUM *r,
212 int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
213 BIGNUM **kinvp, BIGNUM **rp),
214 ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
216 const BIGNUM *in_kinv,
221 meth->sign_setup = sign_setup;
222 meth->sign_sig = sign_sig;
225 void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth,
226 int (*verify)(int type, const unsigned
227 char *dgst, int dgst_len,
228 const unsigned char *sigbuf,
229 int sig_len, EC_KEY *eckey),
230 int (*verify_sig)(const unsigned char *dgst,
232 const ECDSA_SIG *sig,
235 meth->verify = verify;
236 meth->verify_sig = verify_sig;
239 void EC_KEY_METHOD_get_init(const EC_KEY_METHOD *meth,
240 int (**pinit)(EC_KEY *key),
241 void (**pfinish)(EC_KEY *key),
242 int (**pcopy)(EC_KEY *dest, const EC_KEY *src),
243 int (**pset_group)(EC_KEY *key,
244 const EC_GROUP *grp),
245 int (**pset_private)(EC_KEY *key,
246 const BIGNUM *priv_key),
247 int (**pset_public)(EC_KEY *key,
248 const EC_POINT *pub_key))
253 *pfinish = meth->finish;
256 if (pset_group != NULL)
257 *pset_group = meth->set_group;
258 if (pset_private != NULL)
259 *pset_private = meth->set_private;
260 if (pset_public != NULL)
261 *pset_public = meth->set_public;
264 void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth,
265 int (**pkeygen)(EC_KEY *key))
268 *pkeygen = meth->keygen;
271 void EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD *meth,
272 int (**pck)(unsigned char **pout,
274 const EC_POINT *pub_key,
278 *pck = meth->compute_key;
281 void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth,
282 int (**psign)(int type, const unsigned char *dgst,
283 int dlen, unsigned char *sig,
284 unsigned int *siglen,
285 const BIGNUM *kinv, const BIGNUM *r,
287 int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
288 BIGNUM **kinvp, BIGNUM **rp),
289 ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
291 const BIGNUM *in_kinv,
297 if (psign_setup != NULL)
298 *psign_setup = meth->sign_setup;
299 if (psign_sig != NULL)
300 *psign_sig = meth->sign_sig;
303 void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth,
304 int (**pverify)(int type, const unsigned
305 char *dgst, int dgst_len,
306 const unsigned char *sigbuf,
307 int sig_len, EC_KEY *eckey),
308 int (**pverify_sig)(const unsigned char *dgst,
310 const ECDSA_SIG *sig,
314 *pverify = meth->verify;
315 if (pverify_sig != NULL)
316 *pverify_sig = meth->verify_sig;