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