Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/ec/ecx_backend.c
Line
Count
Source
1
/*
2
 * Copyright 2020-2024 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
#include <string.h>
11
#include <openssl/core_names.h>
12
#include <openssl/params.h>
13
#include <openssl/ec.h>
14
#include <openssl/rand.h>
15
#include <openssl/err.h>
16
#ifndef FIPS_MODULE
17
#include <openssl/x509.h>
18
#endif
19
#include "crypto/ecx.h"
20
#include "ecx_backend.h"
21
22
/*
23
 * The intention with the "backend" source file is to offer backend support
24
 * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
25
 * implementations alike.
26
 */
27
28
int ossl_ecx_public_from_private(ECX_KEY *key)
29
69
{
30
69
    switch (key->type) {
31
21
    case ECX_KEY_TYPE_X25519:
32
21
        ossl_x25519_public_from_private(key->pubkey, key->privkey);
33
21
        break;
34
18
    case ECX_KEY_TYPE_ED25519:
35
18
        if (!ossl_ed25519_public_from_private(key->libctx, key->pubkey,
36
18
                key->privkey, key->propq)) {
37
0
            ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
38
0
            return 0;
39
0
        }
40
18
        break;
41
18
    case ECX_KEY_TYPE_X448:
42
14
        ossl_x448_public_from_private(key->pubkey, key->privkey);
43
14
        break;
44
16
    case ECX_KEY_TYPE_ED448:
45
16
        if (!ossl_ed448_public_from_private(key->libctx, key->pubkey,
46
16
                key->privkey, key->propq)) {
47
0
            ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
48
0
            return 0;
49
0
        }
50
16
        break;
51
69
    }
52
69
    return 1;
53
69
}
54
55
int ossl_ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM params[],
56
    int include_private)
57
0
{
58
0
    size_t privkeylen = 0, pubkeylen = 0;
59
0
    const OSSL_PARAM *param_priv_key = NULL, *param_pub_key;
60
0
    unsigned char *pubkey;
61
62
0
    if (ecx == NULL)
63
0
        return 0;
64
65
0
    param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
66
0
    if (include_private)
67
0
        param_priv_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
68
69
0
    if (param_pub_key == NULL && param_priv_key == NULL)
70
0
        return 0;
71
72
0
    if (param_priv_key != NULL) {
73
0
        if (!OSSL_PARAM_get_octet_string(param_priv_key,
74
0
                (void **)&ecx->privkey, ecx->keylen,
75
0
                &privkeylen))
76
0
            return 0;
77
0
        if (privkeylen != ecx->keylen) {
78
            /*
79
             * Invalid key length. We will clear what we've received now. We
80
             * can't leave it to ossl_ecx_key_free() because that will call
81
             * OPENSSL_secure_clear_free() and assume the correct key length
82
             */
83
0
            OPENSSL_secure_clear_free(ecx->privkey, privkeylen);
84
0
            ecx->privkey = NULL;
85
0
            return 0;
86
0
        }
87
0
    }
88
89
0
    pubkey = ecx->pubkey;
90
0
    if (param_pub_key != NULL
91
0
        && !OSSL_PARAM_get_octet_string(param_pub_key,
92
0
            (void **)&pubkey,
93
0
            sizeof(ecx->pubkey), &pubkeylen))
94
0
        return 0;
95
96
0
    if ((param_pub_key != NULL && pubkeylen != ecx->keylen))
97
0
        return 0;
98
99
0
    if (param_pub_key == NULL && !ossl_ecx_public_from_private(ecx))
100
0
        return 0;
101
102
0
    ecx->haspubkey = 1;
103
104
0
    return 1;
105
0
}
106
107
ECX_KEY *ossl_ecx_key_dup(const ECX_KEY *key, int selection)
108
28.0k
{
109
28.0k
    ECX_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
110
111
28.0k
    if (ret == NULL)
112
0
        return NULL;
113
114
28.0k
    ret->libctx = key->libctx;
115
28.0k
    ret->haspubkey = 0;
116
28.0k
    ret->keylen = key->keylen;
117
28.0k
    ret->type = key->type;
118
119
28.0k
    if (!CRYPTO_NEW_REF(&ret->references, 1))
120
0
        goto err;
121
122
28.0k
    if (key->propq != NULL) {
123
21
        ret->propq = OPENSSL_strdup(key->propq);
124
21
        if (ret->propq == NULL)
125
0
            goto err;
126
21
    }
127
128
28.0k
    if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0
129
41
        && key->haspubkey == 1) {
130
41
        memcpy(ret->pubkey, key->pubkey, sizeof(ret->pubkey));
131
41
        ret->haspubkey = 1;
132
41
    }
133
134
28.0k
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
135
41
        && key->privkey != NULL) {
136
21
        if (ossl_ecx_key_allocate_privkey(ret) == NULL) {
137
0
            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
138
0
            goto err;
139
0
        }
140
21
        memcpy(ret->privkey, key->privkey, ret->keylen);
141
21
    }
142
143
28.0k
    return ret;
144
145
0
err:
146
0
    CRYPTO_FREE_REF(&ret->references);
147
0
    ossl_ecx_key_free(ret);
148
0
    return NULL;
149
28.0k
}
150
151
#ifndef FIPS_MODULE
152
ECX_KEY *ossl_ecx_key_op(const X509_ALGOR *palg,
153
    const unsigned char *p, int plen,
154
    int id, ecx_key_op_t op,
155
    OSSL_LIB_CTX *libctx, const char *propq)
156
16.3k
{
157
16.3k
    ECX_KEY *key = NULL;
158
16.3k
    unsigned char *privkey, *pubkey;
159
160
16.3k
    if (op != KEY_OP_KEYGEN) {
161
16.3k
        if (palg != NULL) {
162
16.3k
            int ptype;
163
164
            /* Algorithm parameters must be absent */
165
16.3k
            X509_ALGOR_get0(NULL, &ptype, NULL, palg);
166
16.3k
            if (ptype != V_ASN1_UNDEF) {
167
2.09k
                ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
168
2.09k
                return 0;
169
2.09k
            }
170
14.2k
            if (id == EVP_PKEY_NONE)
171
234
                id = OBJ_obj2nid(palg->algorithm);
172
13.9k
            else if (id != OBJ_obj2nid(palg->algorithm)) {
173
0
                ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
174
0
                return 0;
175
0
            }
176
14.2k
        }
177
178
14.2k
        if (p == NULL || id == EVP_PKEY_NONE || plen != KEYLENID(id)) {
179
10.9k
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
180
10.9k
            return 0;
181
10.9k
        }
182
14.2k
    }
183
184
3.27k
    key = ossl_ecx_key_new(libctx, KEYNID2TYPE(id), 1, propq);
185
3.27k
    if (key == NULL) {
186
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
187
0
        return 0;
188
0
    }
189
3.27k
    pubkey = key->pubkey;
190
191
3.27k
    if (op == KEY_OP_PUBLIC) {
192
3.20k
        memcpy(pubkey, p, plen);
193
3.20k
    } else {
194
69
        privkey = ossl_ecx_key_allocate_privkey(key);
195
69
        if (privkey == NULL) {
196
0
            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
197
0
            goto err;
198
0
        }
199
69
        if (op == KEY_OP_KEYGEN) {
200
0
            if (id != EVP_PKEY_NONE) {
201
0
                if (RAND_priv_bytes_ex(libctx, privkey, KEYLENID(id), 0) <= 0)
202
0
                    goto err;
203
0
                if (id == EVP_PKEY_X25519) {
204
0
                    privkey[0] &= 248;
205
0
                    privkey[X25519_KEYLEN - 1] &= 127;
206
0
                    privkey[X25519_KEYLEN - 1] |= 64;
207
0
                } else if (id == EVP_PKEY_X448) {
208
0
                    privkey[0] &= 252;
209
0
                    privkey[X448_KEYLEN - 1] |= 128;
210
0
                }
211
0
            }
212
69
        } else {
213
69
            memcpy(privkey, p, KEYLENID(id));
214
69
        }
215
69
        if (!ossl_ecx_public_from_private(key)) {
216
0
            ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
217
0
            goto err;
218
0
        }
219
69
    }
220
221
3.27k
    return key;
222
0
err:
223
0
    ossl_ecx_key_free(key);
224
0
    return NULL;
225
3.27k
}
226
227
ECX_KEY *ossl_ecx_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
228
    OSSL_LIB_CTX *libctx, const char *propq)
229
276
{
230
276
    ECX_KEY *ecx = NULL;
231
276
    const unsigned char *p;
232
276
    int plen;
233
276
    ASN1_OCTET_STRING *oct = NULL;
234
276
    const X509_ALGOR *palg;
235
236
276
    if (!PKCS8_pkey_get0(NULL, &p, &plen, &palg, p8inf))
237
0
        return 0;
238
239
276
    oct = d2i_ASN1_OCTET_STRING(NULL, &p, plen);
240
276
    if (oct == NULL) {
241
130
        p = NULL;
242
130
        plen = 0;
243
146
    } else {
244
146
        p = ASN1_STRING_get0_data(oct);
245
146
        plen = ASN1_STRING_length(oct);
246
146
    }
247
248
    /*
249
     * EVP_PKEY_NONE means that ecx_key_op() has to figure out the key type
250
     * on its own.
251
     */
252
276
    ecx = ossl_ecx_key_op(palg, p, plen, EVP_PKEY_NONE, KEY_OP_PRIVATE,
253
276
        libctx, propq);
254
276
    ASN1_OCTET_STRING_free(oct);
255
276
    return ecx;
256
276
}
257
#endif