Coverage Report

Created: 2025-12-31 06:58

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