Copyright consolidation 06/10
[openssl.git] / crypto / ec / ec_kmeth.c
1 /*
2  * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <string.h>
11 #include <openssl/ec.h>
12 #include <openssl/engine.h>
13 #include <openssl/err.h>
14 #include "ec_lcl.h"
15
16
17 static const EC_KEY_METHOD openssl_ec_key_method = {
18     "OpenSSL EC_KEY method",
19     0,
20     0,0,0,0,0,0,
21     ossl_ec_key_gen,
22     ossl_ecdh_compute_key,
23     ossl_ecdsa_sign,
24     ossl_ecdsa_sign_setup,
25     ossl_ecdsa_sign_sig,
26     ossl_ecdsa_verify,
27     ossl_ecdsa_verify_sig
28 };
29
30 static const EC_KEY_METHOD *default_ec_key_meth = &openssl_ec_key_method;
31
32 const EC_KEY_METHOD *EC_KEY_OpenSSL(void)
33 {
34     return &openssl_ec_key_method;
35 }
36
37 const EC_KEY_METHOD *EC_KEY_get_default_method(void)
38 {
39     return default_ec_key_meth;
40 }
41
42 void EC_KEY_set_default_method(const EC_KEY_METHOD *meth)
43 {
44     if (meth == NULL)
45         default_ec_key_meth = &openssl_ec_key_method;
46     else
47         default_ec_key_meth = meth;
48 }
49
50 const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key)
51 {
52     return key->meth;
53 }
54
55 int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth)
56 {
57     void (*finish)(EC_KEY *key) = key->meth->finish;
58
59     if (finish != NULL)
60         finish(key);
61
62 #ifndef OPENSSL_NO_ENGINE
63     ENGINE_finish(key->engine);
64     key->engine = NULL;
65 #endif
66
67     key->meth = meth;
68     if (meth->init != NULL)
69         return meth->init(key);
70     return 1;
71 }
72
73 EC_KEY *EC_KEY_new_method(ENGINE *engine)
74 {
75     EC_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
76
77     if (ret == NULL) {
78         ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_MALLOC_FAILURE);
79         return NULL;
80     }
81     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) {
82         OPENSSL_free(ret);
83         return NULL;
84     }
85
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);
90         OPENSSL_free(ret);
91         return NULL;
92     }
93
94     ret->meth = EC_KEY_get_default_method();
95 #ifndef OPENSSL_NO_ENGINE
96     if (engine != NULL) {
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);
101             OPENSSL_free(ret);
102             return NULL;
103         }
104         ret->engine = engine;
105     } else
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);
114             OPENSSL_free(ret);
115             return NULL;
116         }
117     }
118 #endif
119
120     ret->version = 1;
121     ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
122     ret->references = 1;
123
124     if (ret->meth->init != NULL && ret->meth->init(ret) == 0) {
125         ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_INIT_FAIL);
126         EC_KEY_free(ret);
127         return NULL;
128     }
129     return ret;
130 }
131
132 int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
133                      const EC_KEY *eckey,
134                      void *(*KDF) (const void *in, size_t inlen, void *out,
135                                    size_t *outlen))
136 {
137     unsigned char *sec = NULL;
138     size_t seclen;
139     if (eckey->meth->compute_key == NULL) {
140         ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
141         return 0;
142     }
143     if (outlen > INT_MAX) {
144         ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_INVALID_OUTPUT_LENGTH);
145         return 0;
146     }
147     if (!eckey->meth->compute_key(&sec, &seclen, pub_key, eckey))
148         return 0;
149     if (KDF != NULL) {
150         KDF(sec, seclen, out, &outlen);
151     } else {
152         if (outlen > seclen)
153             outlen = seclen;
154         memcpy(out, sec, outlen);
155     }
156     OPENSSL_clear_free(sec, seclen);
157     return outlen;
158 }
159
160 EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth)
161 {
162     EC_KEY_METHOD *ret = OPENSSL_zalloc(sizeof(*meth));
163
164     if (ret == NULL)
165         return NULL;
166     if (meth != NULL)
167         *ret = *meth;
168     ret->flags |= EC_KEY_METHOD_DYNAMIC;
169     return ret;
170 }
171
172 void EC_KEY_METHOD_free(EC_KEY_METHOD *meth)
173 {
174     if (meth->flags & EC_KEY_METHOD_DYNAMIC)
175         OPENSSL_free(meth);
176 }
177
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))
187 {
188     meth->init = init;
189     meth->finish = finish;
190     meth->copy = copy;
191     meth->set_group = set_group;
192     meth->set_private = set_private;
193     meth->set_public = set_public;
194 }
195
196 void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
197                               int (*keygen)(EC_KEY *key))
198 {
199     meth->keygen = keygen;
200 }
201
202 void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
203                                    int (*ckey)(unsigned char **psec,
204                                                size_t *pseclen,
205                                                const EC_POINT *pub_key,
206                                                const EC_KEY *ecdh))
207 {
208     meth->compute_key = ckey;
209 }
210
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,
216                                         EC_KEY *eckey),
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,
220                                                    int dgst_len,
221                                                    const BIGNUM *in_kinv,
222                                                    const BIGNUM *in_r,
223                                                    EC_KEY *eckey))
224 {
225     meth->sign = sign;
226     meth->sign_setup = sign_setup;
227     meth->sign_sig = sign_sig;
228 }
229
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,
236                                                 int dgst_len,
237                                                 const ECDSA_SIG *sig,
238                                                 EC_KEY *eckey))
239 {
240     meth->verify = verify;
241     meth->verify_sig = verify_sig;
242 }
243
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))
254 {
255     if (pinit != NULL)
256         *pinit = meth->init;
257     if (pfinish != NULL)
258         *pfinish = meth->finish;
259     if (pcopy != NULL)
260         *pcopy = meth->copy;
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;
267 }
268
269 void EC_KEY_METHOD_get_keygen(EC_KEY_METHOD *meth,
270                               int (**pkeygen)(EC_KEY *key))
271 {
272     if (pkeygen != NULL)
273         *pkeygen = meth->keygen;
274 }
275
276 void EC_KEY_METHOD_get_compute_key(EC_KEY_METHOD *meth,
277                                    int (**pck)(unsigned char **pout,
278                                                size_t *poutlen,
279                                                const EC_POINT *pub_key,
280                                                const EC_KEY *ecdh))
281 {
282     if (pck != NULL)
283         *pck = meth->compute_key;
284 }
285
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,
291                                           EC_KEY *eckey),
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,
295                                                      int dgst_len,
296                                                      const BIGNUM *in_kinv,
297                                                      const BIGNUM *in_r,
298                                                      EC_KEY *eckey))
299 {
300     if (psign != NULL)
301         *psign = meth->sign;
302     if (psign_setup != NULL)
303         *psign_setup = meth->sign_setup;
304     if (psign_sig != NULL)
305         *psign_sig = meth->sign_sig;
306 }
307
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,
314                                                   int dgst_len,
315                                                   const ECDSA_SIG *sig,
316                                                   EC_KEY *eckey))
317 {
318     if (pverify != NULL)
319         *pverify = meth->verify;
320     if (pverify_sig != NULL)
321         *pverify_sig = meth->verify_sig;
322 }