Coverage Report

Created: 2023-06-08 06:42

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