Coverage Report

Created: 2025-12-31 06:58

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