Deprecate the ECDH functions.
[openssl.git] / crypto / ec / ec_kmeth.c
1 /*
2  * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*
11  * ECDH and ECDSA low level APIs are deprecated for public use, but still ok
12  * for internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <string.h>
17 #include <openssl/ec.h>
18 #include <openssl/engine.h>
19 #include <openssl/err.h>
20 #include "ec_local.h"
21
22
23 static const EC_KEY_METHOD openssl_ec_key_method = {
24     "OpenSSL EC_KEY method",
25     0,
26     0,0,0,0,0,0,
27     ossl_ec_key_gen,
28     ossl_ecdh_compute_key,
29     ossl_ecdsa_sign,
30     ossl_ecdsa_sign_setup,
31     ossl_ecdsa_sign_sig,
32     ossl_ecdsa_verify,
33     ossl_ecdsa_verify_sig
34 };
35
36 static const EC_KEY_METHOD *default_ec_key_meth = &openssl_ec_key_method;
37
38 const EC_KEY_METHOD *EC_KEY_OpenSSL(void)
39 {
40     return &openssl_ec_key_method;
41 }
42
43 const EC_KEY_METHOD *EC_KEY_get_default_method(void)
44 {
45     return default_ec_key_meth;
46 }
47
48 void EC_KEY_set_default_method(const EC_KEY_METHOD *meth)
49 {
50     if (meth == NULL)
51         default_ec_key_meth = &openssl_ec_key_method;
52     else
53         default_ec_key_meth = meth;
54 }
55
56 const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key)
57 {
58     return key->meth;
59 }
60
61 int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth)
62 {
63     void (*finish)(EC_KEY *key) = key->meth->finish;
64
65     if (finish != NULL)
66         finish(key);
67
68 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
69     ENGINE_finish(key->engine);
70     key->engine = NULL;
71 #endif
72
73     key->meth = meth;
74     if (meth->init != NULL)
75         return meth->init(key);
76     return 1;
77 }
78
79 EC_KEY *ec_key_new_method_int(OPENSSL_CTX *libctx, ENGINE *engine)
80 {
81     EC_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
82
83     if (ret == NULL) {
84         ECerr(EC_F_EC_KEY_NEW_METHOD_INT, ERR_R_MALLOC_FAILURE);
85         return NULL;
86     }
87
88     ret->libctx = libctx;
89
90     ret->references = 1;
91     ret->lock = CRYPTO_THREAD_lock_new();
92     if (ret->lock == NULL) {
93         ECerr(EC_F_EC_KEY_NEW_METHOD_INT, ERR_R_MALLOC_FAILURE);
94         OPENSSL_free(ret);
95         return NULL;
96     }
97
98     ret->meth = EC_KEY_get_default_method();
99 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
100     if (engine != NULL) {
101         if (!ENGINE_init(engine)) {
102             ECerr(EC_F_EC_KEY_NEW_METHOD_INT, ERR_R_ENGINE_LIB);
103             goto err;
104         }
105         ret->engine = engine;
106     } else
107         ret->engine = ENGINE_get_default_EC();
108     if (ret->engine != NULL) {
109         ret->meth = ENGINE_get_EC(ret->engine);
110         if (ret->meth == NULL) {
111             ECerr(EC_F_EC_KEY_NEW_METHOD_INT, ERR_R_ENGINE_LIB);
112             goto err;
113         }
114     }
115 #endif
116
117     ret->version = 1;
118     ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
119
120 /* No ex_data inside the FIPS provider */
121 #ifndef FIPS_MODE
122     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) {
123         goto err;
124     }
125 #endif
126
127     if (ret->meth->init != NULL && ret->meth->init(ret) == 0) {
128         ECerr(EC_F_EC_KEY_NEW_METHOD_INT, ERR_R_INIT_FAIL);
129         goto err;
130     }
131     return ret;
132
133  err:
134     EC_KEY_free(ret);
135     return NULL;
136 }
137
138 #ifndef FIPS_MODE
139 EC_KEY *EC_KEY_new_method(ENGINE *engine)
140 {
141     return ec_key_new_method_int(NULL, engine);
142 }
143 #endif
144
145 int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
146                      const EC_KEY *eckey,
147                      void *(*KDF) (const void *in, size_t inlen, void *out,
148                                    size_t *outlen))
149 {
150     unsigned char *sec = NULL;
151     size_t seclen;
152     if (eckey->meth->compute_key == NULL) {
153         ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
154         return 0;
155     }
156     if (outlen > INT_MAX) {
157         ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_INVALID_OUTPUT_LENGTH);
158         return 0;
159     }
160     if (!eckey->meth->compute_key(&sec, &seclen, pub_key, eckey))
161         return 0;
162     if (KDF != NULL) {
163         KDF(sec, seclen, out, &outlen);
164     } else {
165         if (outlen > seclen)
166             outlen = seclen;
167         memcpy(out, sec, outlen);
168     }
169     OPENSSL_clear_free(sec, seclen);
170     return outlen;
171 }
172
173 EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth)
174 {
175     EC_KEY_METHOD *ret = OPENSSL_zalloc(sizeof(*meth));
176
177     if (ret == NULL)
178         return NULL;
179     if (meth != NULL)
180         *ret = *meth;
181     ret->flags |= EC_KEY_METHOD_DYNAMIC;
182     return ret;
183 }
184
185 void EC_KEY_METHOD_free(EC_KEY_METHOD *meth)
186 {
187     if (meth->flags & EC_KEY_METHOD_DYNAMIC)
188         OPENSSL_free(meth);
189 }
190
191 void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth,
192                             int (*init)(EC_KEY *key),
193                             void (*finish)(EC_KEY *key),
194                             int (*copy)(EC_KEY *dest, const EC_KEY *src),
195                             int (*set_group)(EC_KEY *key, const EC_GROUP *grp),
196                             int (*set_private)(EC_KEY *key,
197                                                const BIGNUM *priv_key),
198                             int (*set_public)(EC_KEY *key,
199                                               const EC_POINT *pub_key))
200 {
201     meth->init = init;
202     meth->finish = finish;
203     meth->copy = copy;
204     meth->set_group = set_group;
205     meth->set_private = set_private;
206     meth->set_public = set_public;
207 }
208
209 void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
210                               int (*keygen)(EC_KEY *key))
211 {
212     meth->keygen = keygen;
213 }
214
215 void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
216                                    int (*ckey)(unsigned char **psec,
217                                                size_t *pseclen,
218                                                const EC_POINT *pub_key,
219                                                const EC_KEY *ecdh))
220 {
221     meth->compute_key = ckey;
222 }
223
224 void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth,
225                             int (*sign)(int type, const unsigned char *dgst,
226                                         int dlen, unsigned char *sig,
227                                         unsigned int *siglen,
228                                         const BIGNUM *kinv, const BIGNUM *r,
229                                         EC_KEY *eckey),
230                             int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
231                                               BIGNUM **kinvp, BIGNUM **rp),
232                             ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
233                                                    int dgst_len,
234                                                    const BIGNUM *in_kinv,
235                                                    const BIGNUM *in_r,
236                                                    EC_KEY *eckey))
237 {
238     meth->sign = sign;
239     meth->sign_setup = sign_setup;
240     meth->sign_sig = sign_sig;
241 }
242
243 void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth,
244                               int (*verify)(int type, const unsigned
245                                             char *dgst, int dgst_len,
246                                             const unsigned char *sigbuf,
247                                             int sig_len, EC_KEY *eckey),
248                               int (*verify_sig)(const unsigned char *dgst,
249                                                 int dgst_len,
250                                                 const ECDSA_SIG *sig,
251                                                 EC_KEY *eckey))
252 {
253     meth->verify = verify;
254     meth->verify_sig = verify_sig;
255 }
256
257 void EC_KEY_METHOD_get_init(const EC_KEY_METHOD *meth,
258                             int (**pinit)(EC_KEY *key),
259                             void (**pfinish)(EC_KEY *key),
260                             int (**pcopy)(EC_KEY *dest, const EC_KEY *src),
261                             int (**pset_group)(EC_KEY *key,
262                                                const EC_GROUP *grp),
263                             int (**pset_private)(EC_KEY *key,
264                                                  const BIGNUM *priv_key),
265                             int (**pset_public)(EC_KEY *key,
266                                                 const EC_POINT *pub_key))
267 {
268     if (pinit != NULL)
269         *pinit = meth->init;
270     if (pfinish != NULL)
271         *pfinish = meth->finish;
272     if (pcopy != NULL)
273         *pcopy = meth->copy;
274     if (pset_group != NULL)
275         *pset_group = meth->set_group;
276     if (pset_private != NULL)
277         *pset_private = meth->set_private;
278     if (pset_public != NULL)
279         *pset_public = meth->set_public;
280 }
281
282 void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth,
283                               int (**pkeygen)(EC_KEY *key))
284 {
285     if (pkeygen != NULL)
286         *pkeygen = meth->keygen;
287 }
288
289 void EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD *meth,
290                                    int (**pck)(unsigned char **pout,
291                                                size_t *poutlen,
292                                                const EC_POINT *pub_key,
293                                                const EC_KEY *ecdh))
294 {
295     if (pck != NULL)
296         *pck = meth->compute_key;
297 }
298
299 void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth,
300                             int (**psign)(int type, const unsigned char *dgst,
301                                           int dlen, unsigned char *sig,
302                                           unsigned int *siglen,
303                                           const BIGNUM *kinv, const BIGNUM *r,
304                                           EC_KEY *eckey),
305                             int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
306                                                 BIGNUM **kinvp, BIGNUM **rp),
307                             ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
308                                                      int dgst_len,
309                                                      const BIGNUM *in_kinv,
310                                                      const BIGNUM *in_r,
311                                                      EC_KEY *eckey))
312 {
313     if (psign != NULL)
314         *psign = meth->sign;
315     if (psign_setup != NULL)
316         *psign_setup = meth->sign_setup;
317     if (psign_sig != NULL)
318         *psign_sig = meth->sign_sig;
319 }
320
321 void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth,
322                               int (**pverify)(int type, const unsigned
323                                               char *dgst, int dgst_len,
324                                               const unsigned char *sigbuf,
325                                               int sig_len, EC_KEY *eckey),
326                               int (**pverify_sig)(const unsigned char *dgst,
327                                                   int dgst_len,
328                                                   const ECDSA_SIG *sig,
329                                                   EC_KEY *eckey))
330 {
331     if (pverify != NULL)
332         *pverify = meth->verify;
333     if (pverify_sig != NULL)
334         *pverify_sig = meth->verify_sig;
335 }