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);
81 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) {
86 ret->lock = CRYPTO_THREAD_lock_new();
87 if (ret->lock == NULL) {
88 ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_MALLOC_FAILURE);
89 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data);
94 ret->meth = EC_KEY_get_default_method();
95 #ifndef OPENSSL_NO_ENGINE
97 if (!ENGINE_init(engine)) {
98 ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_ENGINE_LIB);
99 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data);
100 CRYPTO_THREAD_lock_free(ret->lock);
104 ret->engine = engine;
106 ret->engine = ENGINE_get_default_EC();
107 if (ret->engine != NULL) {
108 ret->meth = ENGINE_get_EC(ret->engine);
109 if (ret->meth == NULL) {
110 ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_ENGINE_LIB);
111 ENGINE_finish(ret->engine);
112 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data);
113 CRYPTO_THREAD_lock_free(ret->lock);
121 ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
124 if (ret->meth->init != NULL && ret->meth->init(ret) == 0) {
125 ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_INIT_FAIL);
132 int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
134 void *(*KDF) (const void *in, size_t inlen, void *out,
137 unsigned char *sec = NULL;
139 if (eckey->meth->compute_key == NULL) {
140 ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
143 if (outlen > INT_MAX) {
144 ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_INVALID_OUTPUT_LENGTH);
147 if (!eckey->meth->compute_key(&sec, &seclen, pub_key, eckey))
150 KDF(sec, seclen, out, &outlen);
154 memcpy(out, sec, outlen);
156 OPENSSL_clear_free(sec, seclen);
160 EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth)
162 EC_KEY_METHOD *ret = OPENSSL_zalloc(sizeof(*meth));
168 ret->flags |= EC_KEY_METHOD_DYNAMIC;
172 void EC_KEY_METHOD_free(EC_KEY_METHOD *meth)
174 if (meth->flags & EC_KEY_METHOD_DYNAMIC)
178 void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth,
179 int (*init)(EC_KEY *key),
180 void (*finish)(EC_KEY *key),
181 int (*copy)(EC_KEY *dest, const EC_KEY *src),
182 int (*set_group)(EC_KEY *key, const EC_GROUP *grp),
183 int (*set_private)(EC_KEY *key,
184 const BIGNUM *priv_key),
185 int (*set_public)(EC_KEY *key,
186 const EC_POINT *pub_key))
189 meth->finish = finish;
191 meth->set_group = set_group;
192 meth->set_private = set_private;
193 meth->set_public = set_public;
196 void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
197 int (*keygen)(EC_KEY *key))
199 meth->keygen = keygen;
202 void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
203 int (*ckey)(unsigned char **psec,
205 const EC_POINT *pub_key,
208 meth->compute_key = ckey;
211 void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth,
212 int (*sign)(int type, const unsigned char *dgst,
213 int dlen, unsigned char *sig,
214 unsigned int *siglen,
215 const BIGNUM *kinv, const BIGNUM *r,
217 int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
218 BIGNUM **kinvp, BIGNUM **rp),
219 ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
221 const BIGNUM *in_kinv,
226 meth->sign_setup = sign_setup;
227 meth->sign_sig = sign_sig;
230 void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth,
231 int (*verify)(int type, const unsigned
232 char *dgst, int dgst_len,
233 const unsigned char *sigbuf,
234 int sig_len, EC_KEY *eckey),
235 int (*verify_sig)(const unsigned char *dgst,
237 const ECDSA_SIG *sig,
240 meth->verify = verify;
241 meth->verify_sig = verify_sig;
244 void EC_KEY_METHOD_get_init(EC_KEY_METHOD *meth,
245 int (**pinit)(EC_KEY *key),
246 void (**pfinish)(EC_KEY *key),
247 int (**pcopy)(EC_KEY *dest, const EC_KEY *src),
248 int (**pset_group)(EC_KEY *key,
249 const EC_GROUP *grp),
250 int (**pset_private)(EC_KEY *key,
251 const BIGNUM *priv_key),
252 int (**pset_public)(EC_KEY *key,
253 const EC_POINT *pub_key))
258 *pfinish = meth->finish;
261 if (pset_group != NULL)
262 *pset_group = meth->set_group;
263 if (pset_private != NULL)
264 *pset_private = meth->set_private;
265 if (pset_public != NULL)
266 *pset_public = meth->set_public;
269 void EC_KEY_METHOD_get_keygen(EC_KEY_METHOD *meth,
270 int (**pkeygen)(EC_KEY *key))
273 *pkeygen = meth->keygen;
276 void EC_KEY_METHOD_get_compute_key(EC_KEY_METHOD *meth,
277 int (**pck)(unsigned char **pout,
279 const EC_POINT *pub_key,
283 *pck = meth->compute_key;
286 void EC_KEY_METHOD_get_sign(EC_KEY_METHOD *meth,
287 int (**psign)(int type, const unsigned char *dgst,
288 int dlen, unsigned char *sig,
289 unsigned int *siglen,
290 const BIGNUM *kinv, const BIGNUM *r,
292 int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
293 BIGNUM **kinvp, BIGNUM **rp),
294 ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
296 const BIGNUM *in_kinv,
302 if (psign_setup != NULL)
303 *psign_setup = meth->sign_setup;
304 if (psign_sig != NULL)
305 *psign_sig = meth->sign_sig;
308 void EC_KEY_METHOD_get_verify(EC_KEY_METHOD *meth,
309 int (**pverify)(int type, const unsigned
310 char *dgst, int dgst_len,
311 const unsigned char *sigbuf,
312 int sig_len, EC_KEY *eckey),
313 int (**pverify_sig)(const unsigned char *dgst,
315 const ECDSA_SIG *sig,
319 *pverify = meth->verify;
320 if (pverify_sig != NULL)
321 *pverify_sig = meth->verify_sig;