Coverage Report

Created: 2025-08-25 06:30

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