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