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