few missing allocation failure checks and releases on error paths
[openssl.git] / crypto / ec / ec_kmeth.c
1 /*
2  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project.
4  */
5 /* ====================================================================
6  * Copyright (c) 2015 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  */
53
54 #include <string.h>
55 #include <openssl/ec.h>
56 #include <openssl/engine.h>
57 #include <openssl/err.h>
58 #include "ec_lcl.h"
59
60
61 static const EC_KEY_METHOD openssl_ec_key_method = {
62     "OpenSSL EC_KEY method",
63     0,
64     0,0,0,0,0,0,
65     ossl_ec_key_gen,
66     ossl_ecdh_compute_key,
67     ossl_ecdsa_sign,
68     ossl_ecdsa_sign_setup,
69     ossl_ecdsa_sign_sig,
70     ossl_ecdsa_verify,
71     ossl_ecdsa_verify_sig
72 };
73
74 static const EC_KEY_METHOD *default_ec_key_meth = &openssl_ec_key_method;
75
76 const EC_KEY_METHOD *EC_KEY_OpenSSL(void)
77 {
78     return &openssl_ec_key_method;
79 }
80
81 const EC_KEY_METHOD *EC_KEY_get_default_method(void)
82 {
83     return default_ec_key_meth;
84 }
85
86 void EC_KEY_set_default_method(const EC_KEY_METHOD *meth)
87 {
88     if (meth == NULL)
89         default_ec_key_meth = &openssl_ec_key_method;
90     else
91         default_ec_key_meth = meth;
92 }
93
94 const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key)
95 {
96     return key->meth;
97 }
98
99 int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth)
100 {
101     void (*finish)(EC_KEY *key) = key->meth->finish;
102
103     if (finish != NULL)
104         finish(key);
105
106 #ifndef OPENSSL_NO_ENGINE
107     ENGINE_finish(key->engine);
108     key->engine = NULL;
109 #endif
110
111     key->meth = meth;
112     if (meth->init != NULL)
113         return meth->init(key);
114     return 1;
115 }
116
117 EC_KEY *EC_KEY_new_method(ENGINE *engine)
118 {
119     EC_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
120
121     if (ret == NULL) {
122         ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_MALLOC_FAILURE);
123         return NULL;
124     }
125     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) {
126         OPENSSL_free(ret);
127         return NULL;
128     }
129
130     ret->lock = CRYPTO_THREAD_lock_new();
131     if (ret->lock == NULL) {
132         ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_MALLOC_FAILURE);
133         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data);
134         OPENSSL_free(ret);
135         return NULL;
136     }
137
138     ret->meth = EC_KEY_get_default_method();
139 #ifndef OPENSSL_NO_ENGINE
140     if (engine != NULL) {
141         if (!ENGINE_init(engine)) {
142             ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_ENGINE_LIB);
143             CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data);
144             CRYPTO_THREAD_lock_free(ret->lock);
145             OPENSSL_free(ret);
146             return NULL;
147         }
148         ret->engine = engine;
149     } else
150         ret->engine = ENGINE_get_default_EC();
151     if (ret->engine != NULL) {
152         ret->meth = ENGINE_get_EC(ret->engine);
153         if (ret->meth == NULL) {
154             ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_ENGINE_LIB);
155             ENGINE_finish(ret->engine);
156             CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data);
157             CRYPTO_THREAD_lock_free(ret->lock);
158             OPENSSL_free(ret);
159             return NULL;
160         }
161     }
162 #endif
163
164     ret->version = 1;
165     ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
166     ret->references = 1;
167
168     if (ret->meth->init != NULL && ret->meth->init(ret) == 0) {
169         ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_INIT_FAIL);
170         EC_KEY_free(ret);
171         return NULL;
172     }
173     return ret;
174 }
175
176 int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
177                      const EC_KEY *eckey,
178                      void *(*KDF) (const void *in, size_t inlen, void *out,
179                                    size_t *outlen))
180 {
181     unsigned char *sec = NULL;
182     size_t seclen;
183     if (eckey->meth->compute_key == NULL) {
184         ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
185         return 0;
186     }
187     if (outlen > INT_MAX) {
188         ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_INVALID_OUTPUT_LENGTH);
189         return 0;
190     }
191     if (!eckey->meth->compute_key(&sec, &seclen, pub_key, eckey))
192         return 0;
193     if (KDF != NULL) {
194         KDF(sec, seclen, out, &outlen);
195     } else {
196         if (outlen > seclen)
197             outlen = seclen;
198         memcpy(out, sec, outlen);
199     }
200     OPENSSL_clear_free(sec, seclen);
201     return outlen;
202 }
203
204 EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth)
205 {
206     EC_KEY_METHOD *ret = OPENSSL_zalloc(sizeof(*meth));
207
208     if (ret == NULL)
209         return NULL;
210     if (meth != NULL)
211         *ret = *meth;
212     ret->flags |= EC_KEY_METHOD_DYNAMIC;
213     return ret;
214 }
215
216 void EC_KEY_METHOD_free(EC_KEY_METHOD *meth)
217 {
218     if (meth->flags & EC_KEY_METHOD_DYNAMIC)
219         OPENSSL_free(meth);
220 }
221
222 void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth,
223                             int (*init)(EC_KEY *key),
224                             void (*finish)(EC_KEY *key),
225                             int (*copy)(EC_KEY *dest, const EC_KEY *src),
226                             int (*set_group)(EC_KEY *key, const EC_GROUP *grp),
227                             int (*set_private)(EC_KEY *key,
228                                                const BIGNUM *priv_key),
229                             int (*set_public)(EC_KEY *key,
230                                               const EC_POINT *pub_key))
231 {
232     meth->init = init;
233     meth->finish = finish;
234     meth->copy = copy;
235     meth->set_group = set_group;
236     meth->set_private = set_private;
237     meth->set_public = set_public;
238 }
239
240 void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
241                               int (*keygen)(EC_KEY *key))
242 {
243     meth->keygen = keygen;
244 }
245
246 void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
247                                    int (*ckey)(unsigned char **psec,
248                                                size_t *pseclen,
249                                                const EC_POINT *pub_key,
250                                                const EC_KEY *ecdh))
251 {
252     meth->compute_key = ckey;
253 }
254
255 void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth,
256                             int (*sign)(int type, const unsigned char *dgst,
257                                         int dlen, unsigned char *sig,
258                                         unsigned int *siglen,
259                                         const BIGNUM *kinv, const BIGNUM *r,
260                                         EC_KEY *eckey),
261                             int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
262                                               BIGNUM **kinvp, BIGNUM **rp),
263                             ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
264                                                    int dgst_len,
265                                                    const BIGNUM *in_kinv,
266                                                    const BIGNUM *in_r,
267                                                    EC_KEY *eckey))
268 {
269     meth->sign = sign;
270     meth->sign_setup = sign_setup;
271     meth->sign_sig = sign_sig;
272 }
273
274 void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth,
275                               int (*verify)(int type, const unsigned
276                                             char *dgst, int dgst_len,
277                                             const unsigned char *sigbuf,
278                                             int sig_len, EC_KEY *eckey),
279                               int (*verify_sig)(const unsigned char *dgst,
280                                                 int dgst_len,
281                                                 const ECDSA_SIG *sig,
282                                                 EC_KEY *eckey))
283 {
284     meth->verify = verify;
285     meth->verify_sig = verify_sig;
286 }
287
288 void EC_KEY_METHOD_get_init(EC_KEY_METHOD *meth,
289                             int (**pinit)(EC_KEY *key),
290                             void (**pfinish)(EC_KEY *key),
291                             int (**pcopy)(EC_KEY *dest, const EC_KEY *src),
292                             int (**pset_group)(EC_KEY *key,
293                                                const EC_GROUP *grp),
294                             int (**pset_private)(EC_KEY *key,
295                                                  const BIGNUM *priv_key),
296                             int (**pset_public)(EC_KEY *key,
297                                                 const EC_POINT *pub_key))
298 {
299     if (pinit != NULL)
300         *pinit = meth->init;
301     if (pfinish != NULL)
302         *pfinish = meth->finish;
303     if (pcopy != NULL)
304         *pcopy = meth->copy;
305     if (pset_group != NULL)
306         *pset_group = meth->set_group;
307     if (pset_private != NULL)
308         *pset_private = meth->set_private;
309     if (pset_public != NULL)
310         *pset_public = meth->set_public;
311 }
312
313 void EC_KEY_METHOD_get_keygen(EC_KEY_METHOD *meth,
314                               int (**pkeygen)(EC_KEY *key))
315 {
316     if (pkeygen != NULL)
317         *pkeygen = meth->keygen;
318 }
319
320 void EC_KEY_METHOD_get_compute_key(EC_KEY_METHOD *meth,
321                                    int (**pck)(unsigned char **pout,
322                                                size_t *poutlen,
323                                                const EC_POINT *pub_key,
324                                                const EC_KEY *ecdh))
325 {
326     if (pck != NULL)
327         *pck = meth->compute_key;
328 }
329
330 void EC_KEY_METHOD_get_sign(EC_KEY_METHOD *meth,
331                             int (**psign)(int type, const unsigned char *dgst,
332                                           int dlen, unsigned char *sig,
333                                           unsigned int *siglen,
334                                           const BIGNUM *kinv, const BIGNUM *r,
335                                           EC_KEY *eckey),
336                             int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
337                                                 BIGNUM **kinvp, BIGNUM **rp),
338                             ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
339                                                      int dgst_len,
340                                                      const BIGNUM *in_kinv,
341                                                      const BIGNUM *in_r,
342                                                      EC_KEY *eckey))
343 {
344     if (psign != NULL)
345         *psign = meth->sign;
346     if (psign_setup != NULL)
347         *psign_setup = meth->sign_setup;
348     if (psign_sig != NULL)
349         *psign_sig = meth->sign_sig;
350 }
351
352 void EC_KEY_METHOD_get_verify(EC_KEY_METHOD *meth,
353                               int (**pverify)(int type, const unsigned
354                                               char *dgst, int dgst_len,
355                                               const unsigned char *sigbuf,
356                                               int sig_len, EC_KEY *eckey),
357                               int (**pverify_sig)(const unsigned char *dgst,
358                                                   int dgst_len,
359                                                   const ECDSA_SIG *sig,
360                                                   EC_KEY *eckey))
361 {
362     if (pverify != NULL)
363         *pverify = meth->verify;
364     if (pverify_sig != NULL)
365         *pverify_sig = meth->verify_sig;
366 }