Coverage Report

Created: 2026-10-17 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/mbedtls-3.6.6/library/pk_internal.h
Line
Count
Source
1
/**
2
 * \file pk_internal.h
3
 *
4
 * \brief Public Key abstraction layer: internal (i.e. library only) functions
5
 *        and definitions.
6
 */
7
/*
8
 *  Copyright The Mbed TLS Contributors
9
 *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10
 */
11
#ifndef MBEDTLS_PK_INTERNAL_H
12
#define MBEDTLS_PK_INTERNAL_H
13
14
#include "mbedtls/pk.h"
15
16
#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
17
#include "mbedtls/ecp.h"
18
#endif
19
20
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
21
#include "psa/crypto.h"
22
23
#include "psa_util_internal.h"
24
0
#define PSA_PK_TO_MBEDTLS_ERR(status) psa_pk_status_to_mbedtls(status)
25
#define PSA_PK_RSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status,     \
26
                                                                  psa_to_pk_rsa_errors,            \
27
                                                                  psa_pk_status_to_mbedtls)
28
#define PSA_PK_ECDSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status,   \
29
                                                                    psa_to_pk_ecdsa_errors,        \
30
                                                                    psa_pk_status_to_mbedtls)
31
#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
32
33
/* Headers/footers for PEM files */
34
0
#define PEM_BEGIN_PUBLIC_KEY    "-----BEGIN PUBLIC KEY-----"
35
0
#define PEM_END_PUBLIC_KEY      "-----END PUBLIC KEY-----"
36
0
#define PEM_BEGIN_PRIVATE_KEY_RSA   "-----BEGIN RSA PRIVATE KEY-----"
37
0
#define PEM_END_PRIVATE_KEY_RSA     "-----END RSA PRIVATE KEY-----"
38
0
#define PEM_BEGIN_PUBLIC_KEY_RSA     "-----BEGIN RSA PUBLIC KEY-----"
39
0
#define PEM_END_PUBLIC_KEY_RSA     "-----END RSA PUBLIC KEY-----"
40
0
#define PEM_BEGIN_PRIVATE_KEY_EC    "-----BEGIN EC PRIVATE KEY-----"
41
0
#define PEM_END_PRIVATE_KEY_EC      "-----END EC PRIVATE KEY-----"
42
0
#define PEM_BEGIN_PRIVATE_KEY_PKCS8 "-----BEGIN PRIVATE KEY-----"
43
0
#define PEM_END_PRIVATE_KEY_PKCS8   "-----END PRIVATE KEY-----"
44
0
#define PEM_BEGIN_ENCRYPTED_PRIVATE_KEY_PKCS8 "-----BEGIN ENCRYPTED PRIVATE KEY-----"
45
0
#define PEM_END_ENCRYPTED_PRIVATE_KEY_PKCS8   "-----END ENCRYPTED PRIVATE KEY-----"
46
47
/*
48
 * We're trying to statisfy two kinds of users:
49
 * - those who don't want to use the heap;
50
 * - those who can't afford large stack buffers.
51
 *
52
 * The current compromise is that if ECC is the only key type supported in PK,
53
 * then we export keys on the stack, and otherwise we use the heap.
54
 *
55
 * RSA can either be used directly or indirectly via opaque keys if enabled.
56
 * (RSA_ALT is not relevant here as we can't export from such contexts.)
57
 */
58
#if !defined(MBEDTLS_RSA_C) && \
59
    !(defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY))
60
#define PK_EXPORT_KEYS_ON_THE_STACK
61
#endif
62
63
#if defined(PK_EXPORT_KEYS_ON_THE_STACK)
64
/* We know for ECC, pubkey are longer than privkeys, but double check.
65
 * Also, take the maximum size of legacy and PSA, as PSA might be disabled. */
66
#define PK_EXPORT_KEY_STACK_BUFFER_SIZE  MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
67
#if PK_EXPORT_KEY_STACK_BUFFER_SIZE < MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH
68
#undef PK_EXPORT_KEY_STACK_BUFFER_SIZE
69
#define PK_EXPORT_KEY_STACK_BUFFER_SIZE  MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH
70
#endif
71
#if PK_EXPORT_KEY_STACK_BUFFER_SIZE < MBEDTLS_ECP_MAX_BYTES
72
#undef PK_EXPORT_KEY_STACK_BUFFER_SIZE
73
#define PK_EXPORT_KEY_STACK_BUFFER_SIZE  MBEDTLS_ECP_MAX_BYTES
74
#endif
75
#if PK_EXPORT_KEY_STACK_BUFFER_SIZE < MBEDTLS_ECP_MAX_PT_LEN
76
#undef PK_EXPORT_KEY_STACK_BUFFER_SIZE
77
#define PK_EXPORT_KEY_STACK_BUFFER_SIZE  MBEDTLS_ECP_MAX_PT_LEN
78
#endif
79
#endif
80
81
#if defined(MBEDTLS_PK_HAVE_ECC_KEYS) && !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
82
/**
83
 * Public function mbedtls_pk_ec() can be used to get direct access to the
84
 * wrapped ecp_keypair structure pointed to the pk_ctx. However this is not
85
 * ideal because it bypasses the PK module on the control of its internal
86
 * structure (pk_context) fields.
87
 * For backward compatibility we keep mbedtls_pk_ec() when ECP_C is defined, but
88
 * we provide 2 very similar functions when only ECP_LIGHT is enabled and not
89
 * ECP_C.
90
 * These variants embed the "ro" or "rw" keywords in their name to make the
91
 * usage of the returned pointer explicit. Of course the returned value is
92
 * const or non-const accordingly.
93
 */
94
static inline const mbedtls_ecp_keypair *mbedtls_pk_ec_ro(const mbedtls_pk_context pk)
95
0
{
96
0
    switch (mbedtls_pk_get_type(&pk)) {
97
0
        case MBEDTLS_PK_ECKEY:
98
0
        case MBEDTLS_PK_ECKEY_DH:
99
0
        case MBEDTLS_PK_ECDSA:
100
0
            return (const mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
101
0
        default:
102
0
            return NULL;
103
0
    }
104
0
}
Unexecuted instantiation: x509.c:mbedtls_pk_ec_ro
Unexecuted instantiation: x509_crl.c:mbedtls_pk_ec_ro
Unexecuted instantiation: x509_crt.c:mbedtls_pk_ec_ro
Unexecuted instantiation: x509write_csr.c:mbedtls_pk_ec_ro
Unexecuted instantiation: x509_create.c:mbedtls_pk_ec_ro
Unexecuted instantiation: x509write.c:mbedtls_pk_ec_ro
Unexecuted instantiation: pk.c:mbedtls_pk_ec_ro
Unexecuted instantiation: pk_ecc.c:mbedtls_pk_ec_ro
Unexecuted instantiation: pk_wrap.c:mbedtls_pk_ec_ro
Unexecuted instantiation: pkparse.c:mbedtls_pk_ec_ro
Unexecuted instantiation: pkwrite.c:mbedtls_pk_ec_ro
105
106
static inline mbedtls_ecp_keypair *mbedtls_pk_ec_rw(const mbedtls_pk_context pk)
107
0
{
108
0
    switch (mbedtls_pk_get_type(&pk)) {
109
0
        case MBEDTLS_PK_ECKEY:
110
0
        case MBEDTLS_PK_ECKEY_DH:
111
0
        case MBEDTLS_PK_ECDSA:
112
0
            return (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
113
0
        default:
114
0
            return NULL;
115
0
    }
116
0
}
Unexecuted instantiation: x509.c:mbedtls_pk_ec_rw
Unexecuted instantiation: x509_crl.c:mbedtls_pk_ec_rw
Unexecuted instantiation: x509_crt.c:mbedtls_pk_ec_rw
Unexecuted instantiation: x509write_csr.c:mbedtls_pk_ec_rw
Unexecuted instantiation: x509_create.c:mbedtls_pk_ec_rw
Unexecuted instantiation: x509write.c:mbedtls_pk_ec_rw
Unexecuted instantiation: pk.c:mbedtls_pk_ec_rw
Unexecuted instantiation: pk_ecc.c:mbedtls_pk_ec_rw
Unexecuted instantiation: pk_wrap.c:mbedtls_pk_ec_rw
Unexecuted instantiation: pkparse.c:mbedtls_pk_ec_rw
Unexecuted instantiation: pkwrite.c:mbedtls_pk_ec_rw
117
#endif /* MBEDTLS_PK_HAVE_ECC_KEYS && !MBEDTLS_PK_USE_PSA_EC_DATA */
118
119
#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
120
static inline mbedtls_ecp_group_id mbedtls_pk_get_ec_group_id(const mbedtls_pk_context *pk)
121
0
{
122
0
    mbedtls_ecp_group_id id;
123
124
#if defined(MBEDTLS_USE_PSA_CRYPTO)
125
    if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_OPAQUE) {
126
        psa_key_attributes_t opaque_attrs = PSA_KEY_ATTRIBUTES_INIT;
127
        psa_key_type_t opaque_key_type;
128
        psa_ecc_family_t curve;
129
130
        if (psa_get_key_attributes(pk->priv_id, &opaque_attrs) != PSA_SUCCESS) {
131
            return MBEDTLS_ECP_DP_NONE;
132
        }
133
        opaque_key_type = psa_get_key_type(&opaque_attrs);
134
        curve = PSA_KEY_TYPE_ECC_GET_FAMILY(opaque_key_type);
135
        id = mbedtls_ecc_group_from_psa(curve, psa_get_key_bits(&opaque_attrs));
136
        psa_reset_key_attributes(&opaque_attrs);
137
    } else
138
#endif /* MBEDTLS_USE_PSA_CRYPTO */
139
0
    {
140
#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
141
        id = mbedtls_ecc_group_from_psa(pk->ec_family, pk->ec_bits);
142
#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
143
0
        id = mbedtls_pk_ec_ro(*pk)->grp.id;
144
0
#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
145
0
    }
146
147
0
    return id;
148
0
}
Unexecuted instantiation: x509.c:mbedtls_pk_get_ec_group_id
Unexecuted instantiation: x509_crl.c:mbedtls_pk_get_ec_group_id
Unexecuted instantiation: x509_crt.c:mbedtls_pk_get_ec_group_id
Unexecuted instantiation: x509write_csr.c:mbedtls_pk_get_ec_group_id
Unexecuted instantiation: x509_create.c:mbedtls_pk_get_ec_group_id
Unexecuted instantiation: x509write.c:mbedtls_pk_get_ec_group_id
Unexecuted instantiation: pk.c:mbedtls_pk_get_ec_group_id
Unexecuted instantiation: pk_ecc.c:mbedtls_pk_get_ec_group_id
Unexecuted instantiation: pk_wrap.c:mbedtls_pk_get_ec_group_id
Unexecuted instantiation: pkparse.c:mbedtls_pk_get_ec_group_id
Unexecuted instantiation: pkwrite.c:mbedtls_pk_get_ec_group_id
149
150
/* Helper for Montgomery curves */
151
#if defined(MBEDTLS_ECP_HAVE_CURVE25519) || defined(MBEDTLS_ECP_HAVE_CURVE448)
152
#define MBEDTLS_PK_HAVE_RFC8410_CURVES
153
#endif /* MBEDTLS_ECP_HAVE_CURVE25519 || MBEDTLS_ECP_DP_CURVE448 */
154
155
#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id)  \
156
0
    ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
157
158
static inline int mbedtls_pk_is_rfc8410(const mbedtls_pk_context *pk)
159
0
{
160
0
    mbedtls_ecp_group_id id = mbedtls_pk_get_ec_group_id(pk);
161
162
0
    return MBEDTLS_PK_IS_RFC8410_GROUP_ID(id);
163
0
}
Unexecuted instantiation: x509.c:mbedtls_pk_is_rfc8410
Unexecuted instantiation: x509_crl.c:mbedtls_pk_is_rfc8410
Unexecuted instantiation: x509_crt.c:mbedtls_pk_is_rfc8410
Unexecuted instantiation: x509write_csr.c:mbedtls_pk_is_rfc8410
Unexecuted instantiation: x509_create.c:mbedtls_pk_is_rfc8410
Unexecuted instantiation: x509write.c:mbedtls_pk_is_rfc8410
Unexecuted instantiation: pk.c:mbedtls_pk_is_rfc8410
Unexecuted instantiation: pk_ecc.c:mbedtls_pk_is_rfc8410
Unexecuted instantiation: pk_wrap.c:mbedtls_pk_is_rfc8410
Unexecuted instantiation: pkparse.c:mbedtls_pk_is_rfc8410
Unexecuted instantiation: pkwrite.c:mbedtls_pk_is_rfc8410
164
165
/*
166
 * Set the group used by this key.
167
 *
168
 * [in/out] pk: in: must have been pk_setup() to an ECC type
169
 *              out: will have group (curve) information set
170
 * [in] grp_in: a supported group ID (not NONE)
171
 */
172
int mbedtls_pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id);
173
174
/*
175
 * Set the private key material
176
 *
177
 * [in/out] pk: in: must have the group set already, see mbedtls_pk_ecc_set_group().
178
 *              out: will have the private key set.
179
 * [in] key, key_len: the raw private key (no ASN.1 wrapping).
180
 */
181
int mbedtls_pk_ecc_set_key(mbedtls_pk_context *pk, unsigned char *key, size_t key_len);
182
183
/*
184
 * Set the public key.
185
 *
186
 * [in/out] pk: in: must have its group set, see mbedtls_pk_ecc_set_group().
187
 *              out: will have the public key set.
188
 * [in] pub, pub_len: the raw public key (an ECPoint).
189
 *
190
 * Return:
191
 * - 0 on success;
192
 * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
193
 *   but not supported;
194
 * - another error code otherwise.
195
 */
196
int mbedtls_pk_ecc_set_pubkey(mbedtls_pk_context *pk, const unsigned char *pub, size_t pub_len);
197
198
/*
199
 * Derive a public key from its private counterpart.
200
 * Computationally intensive, only use when public key is not available.
201
 *
202
 * [in/out] pk: in: must have the private key set, see mbedtls_pk_ecc_set_key().
203
 *              out: will have the public key set.
204
 * [in] prv, prv_len: the raw private key (see note below).
205
 * [in] f_rng, p_rng: RNG function and context.
206
 *
207
 * Note: the private key information is always available from pk,
208
 * however for convenience the serialized version is also passed,
209
 * as it's available at each calling site, and useful in some configs
210
 * (as otherwise we would have to re-serialize it from the pk context).
211
 *
212
 * There are three implementations of this function:
213
 * 1. MBEDTLS_PK_USE_PSA_EC_DATA,
214
 * 2. MBEDTLS_USE_PSA_CRYPTO but not MBEDTLS_PK_USE_PSA_EC_DATA,
215
 * 3. not MBEDTLS_USE_PSA_CRYPTO.
216
 */
217
int mbedtls_pk_ecc_set_pubkey_from_prv(mbedtls_pk_context *pk,
218
                                       const unsigned char *prv, size_t prv_len,
219
                                       int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
220
#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
221
222
/* Helper for (deterministic) ECDSA */
223
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
224
#define MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET  PSA_ALG_DETERMINISTIC_ECDSA
225
#else
226
#define MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET  PSA_ALG_ECDSA
227
#endif
228
229
#if defined(MBEDTLS_TEST_HOOKS)
230
MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
231
    mbedtls_pk_context *pk,
232
    unsigned char *key, size_t keylen,
233
    const unsigned char *pwd, size_t pwdlen,
234
    int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
235
#endif
236
237
#if defined(MBEDTLS_FS_IO)
238
int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n);
239
#endif
240
241
#endif /* MBEDTLS_PK_INTERNAL_H */