Coverage Report

Created: 2025-12-08 06:22

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