]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ec_kmeth.c
Copyright consolidation 06/10
[thirdparty/openssl.git] / crypto / ec / ec_kmeth.c
CommitLineData
28572b57 1/*
4f22f405 2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
28572b57 3 *
4f22f405
RS
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
28572b57
DSH
8 */
9
e2285d87 10#include <string.h>
28572b57 11#include <openssl/ec.h>
3c27208f 12#include <openssl/engine.h>
28572b57
DSH
13#include <openssl/err.h>
14#include "ec_lcl.h"
15
16
17static const EC_KEY_METHOD openssl_ec_key_method = {
18 "OpenSSL EC_KEY method",
5a6a1029 19 0,
3475bc96 20 0,0,0,0,0,0,
a22a7e70 21 ossl_ec_key_gen,
8c6ef786 22 ossl_ecdh_compute_key,
a200a817 23 ossl_ecdsa_sign,
8c6ef786
DSH
24 ossl_ecdsa_sign_setup,
25 ossl_ecdsa_sign_sig,
a200a817 26 ossl_ecdsa_verify,
8c6ef786 27 ossl_ecdsa_verify_sig
28572b57
DSH
28};
29
a0ffedaf 30static const EC_KEY_METHOD *default_ec_key_meth = &openssl_ec_key_method;
28572b57
DSH
31
32const EC_KEY_METHOD *EC_KEY_OpenSSL(void)
33{
34 return &openssl_ec_key_method;
35}
36
37const EC_KEY_METHOD *EC_KEY_get_default_method(void)
38{
39 return default_ec_key_meth;
40}
41
42void 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
3aef36ff
RS
50const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key)
51{
52 return key->meth;
53}
54
55int 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
7c96dbcd
RS
63 ENGINE_finish(key->engine);
64 key->engine = NULL;
3aef36ff
RS
65#endif
66
67 key->meth = meth;
68 if (meth->init != NULL)
69 return meth->init(key);
70 return 1;
71}
72
28572b57
DSH
73EC_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);
9b398ef2 79 return NULL;
28572b57 80 }
3aef36ff
RS
81 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) {
82 OPENSSL_free(ret);
83 return NULL;
84 }
85
9b398ef2
AG
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
28572b57
DSH
94 ret->meth = EC_KEY_get_default_method();
95#ifndef OPENSSL_NO_ENGINE
91e7bcc2 96 if (engine != NULL) {
28572b57
DSH
97 if (!ENGINE_init(engine)) {
98 ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_ENGINE_LIB);
9b398ef2 99 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data);
c001ce33 100 CRYPTO_THREAD_lock_free(ret->lock);
28572b57
DSH
101 OPENSSL_free(ret);
102 return NULL;
103 }
104 ret->engine = engine;
105 } else
7d711cbc 106 ret->engine = ENGINE_get_default_EC();
91e7bcc2 107 if (ret->engine != NULL) {
7d711cbc 108 ret->meth = ENGINE_get_EC(ret->engine);
91e7bcc2 109 if (ret->meth == NULL) {
28572b57
DSH
110 ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_ENGINE_LIB);
111 ENGINE_finish(ret->engine);
9b398ef2 112 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data);
c001ce33 113 CRYPTO_THREAD_lock_free(ret->lock);
28572b57
DSH
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;
9b398ef2 123
91e7bcc2 124 if (ret->meth->init != NULL && ret->meth->init(ret) == 0) {
cb1d435c 125 ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_INIT_FAIL);
0d6ff6d3
DSH
126 EC_KEY_free(ret);
127 return NULL;
128 }
129 return ret;
28572b57 130}
a22a7e70
DSH
131
132int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
2c61a5ec 133 const EC_KEY *eckey,
a22a7e70
DSH
134 void *(*KDF) (const void *in, size_t inlen, void *out,
135 size_t *outlen))
136{
e2285d87
DSH
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;
a22a7e70 158}
f8d7d2d6
DSH
159
160EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth)
161{
3aef36ff
RS
162 EC_KEY_METHOD *ret = OPENSSL_zalloc(sizeof(*meth));
163
f8d7d2d6
DSH
164 if (ret == NULL)
165 return NULL;
91e7bcc2 166 if (meth != NULL)
f8d7d2d6
DSH
167 *ret = *meth;
168 ret->flags |= EC_KEY_METHOD_DYNAMIC;
169 return ret;
170}
171
172void EC_KEY_METHOD_free(EC_KEY_METHOD *meth)
173{
174 if (meth->flags & EC_KEY_METHOD_DYNAMIC)
175 OPENSSL_free(meth);
176}
177
178void 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
196void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
197 int (*keygen)(EC_KEY *key))
198{
199 meth->keygen = keygen;
200}
201
202void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
e2285d87
DSH
203 int (*ckey)(unsigned char **psec,
204 size_t *pseclen,
f8d7d2d6 205 const EC_POINT *pub_key,
e2285d87 206 const EC_KEY *ecdh))
f8d7d2d6
DSH
207{
208 meth->compute_key = ckey;
209}
210
211void 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
230void 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
244void 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
269void 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
276void EC_KEY_METHOD_get_compute_key(EC_KEY_METHOD *meth,
e2285d87
DSH
277 int (**pck)(unsigned char **pout,
278 size_t *poutlen,
f8d7d2d6 279 const EC_POINT *pub_key,
e2285d87 280 const EC_KEY *ecdh))
f8d7d2d6
DSH
281{
282 if (pck != NULL)
283 *pck = meth->compute_key;
284}
285
286void 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
308void 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}