blob: a103f46e8a39630eb2916cbd9b1360ad4a64745a [file] [log] [blame]
Valerio Setti851f1902024-02-08 09:35:05 +01001#include "common.h"
2
3#include "mbedtls/pk.h"
4#include "mbedtls/error.h"
5#include "mbedtls/ecp.h"
6#include "pk_internal.h"
7
8/***********************************************************************
9 *
10 * ECC setters
11 *
12 * 1. This is an abstraction layer around MBEDTLS_PK_USE_PSA_EC_DATA:
13 * this macro will not appear outside this section.
14 * 2. All inputs are raw (no metadata).
15 *
16 **********************************************************************/
17
18#if defined(MBEDTLS_PK_C) && defined(MBEDTLS_PK_HAVE_ECC_KEYS)
19
20/*
21 * Set the group used by this key.
22 *
23 * [in/out] pk: in: must have been pk_setup() to an ECC type
24 * out: will have group (curve) information set
25 * [in] grp_in: a supported group ID (not NONE)
26 */
27int mbedtls_pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id)
28{
29#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
30 size_t ec_bits;
31 psa_ecc_family_t ec_family = mbedtls_ecc_group_to_psa(grp_id, &ec_bits);
32
33 /* group may already be initialized; if so, make sure IDs match */
34 if ((pk->ec_family != 0 && pk->ec_family != ec_family) ||
35 (pk->ec_bits != 0 && pk->ec_bits != ec_bits)) {
36 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
37 }
38
39 /* set group */
40 pk->ec_family = ec_family;
41 pk->ec_bits = ec_bits;
42
43 return 0;
44#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
45 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
46
47 /* grp may already be initialized; if so, make sure IDs match */
48 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
49 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
50 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
51 }
52
53 /* set group */
54 return mbedtls_ecp_group_load(&(ecp->grp), grp_id);
55#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
56}
57
58/*
59 * Set the private key material
60 *
61 * [in/out] pk: in: must have the group set already, see mbedtls_pk_ecc_set_group().
62 * out: will have the private key set.
63 * [in] key, key_len: the raw private key (no ASN.1 wrapping).
64 */
65int mbedtls_pk_ecc_set_key(mbedtls_pk_context *pk, unsigned char *key, size_t key_len)
66{
67#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
68 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
69 psa_key_usage_t flags;
70 psa_status_t status;
71
72 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
73 if (pk->ec_family == PSA_ECC_FAMILY_MONTGOMERY) {
74 /* Do not set algorithm here because Montgomery keys cannot do ECDSA and
75 * the PK module cannot do ECDH. When the key will be used in TLS for
76 * ECDH, it will be exported and then re-imported with proper flags
77 * and algorithm. */
78 flags = PSA_KEY_USAGE_EXPORT;
79 } else {
80 psa_set_key_algorithm(&attributes,
81 MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(PSA_ALG_ANY_HASH));
82 flags = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_SIGN_MESSAGE |
83 PSA_KEY_USAGE_EXPORT;
84 }
85 psa_set_key_usage_flags(&attributes, flags);
86
87 status = psa_import_key(&attributes, key, key_len, &pk->priv_id);
88 return psa_pk_status_to_mbedtls(status);
89
90#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
91
92 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
93 int ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, key_len);
94 if (ret != 0) {
95 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
96 }
97 return 0;
98#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
99}
100
101/*
102 * Derive a public key from its private counterpart.
103 * Computationally intensive, only use when public key is not available.
104 *
105 * [in/out] pk: in: must have the private key set, see mbedtls_pk_ecc_set_key().
106 * out: will have the public key set.
107 * [in] prv, prv_len: the raw private key (see note below).
108 * [in] f_rng, p_rng: RNG function and context.
109 *
110 * Note: the private key information is always available from pk,
111 * however for convenience the serialized version is also passed,
112 * as it's available at each calling site, and useful in some configs
113 * (as otherwise we would have to re-serialize it from the pk context).
114 *
115 * There are three implementations of this function:
116 * 1. MBEDTLS_PK_USE_PSA_EC_DATA,
117 * 2. MBEDTLS_USE_PSA_CRYPTO but not MBEDTLS_PK_USE_PSA_EC_DATA,
118 * 3. not MBEDTLS_USE_PSA_CRYPTO.
119 */
120int mbedtls_pk_ecc_set_pubkey_from_prv(mbedtls_pk_context *pk,
121 const unsigned char *prv, size_t prv_len,
122 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
123{
124#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
125
126 (void) f_rng;
127 (void) p_rng;
128 (void) prv;
129 (void) prv_len;
130 psa_status_t status;
131
132 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
133 &pk->pub_raw_len);
134 return psa_pk_status_to_mbedtls(status);
135
136#elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
137
138 (void) f_rng;
139 (void) p_rng;
140 psa_status_t status;
141
142 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
143 size_t curve_bits;
144 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
145
146 /* Import private key into PSA, from serialized input */
147 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
148 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
149 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
150 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
151 status = psa_import_key(&key_attr, prv, prv_len, &key_id);
152 if (status != PSA_SUCCESS) {
153 return psa_pk_status_to_mbedtls(status);
154 }
155
156 /* Export public key from PSA */
157 unsigned char pub[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
158 size_t pub_len;
159 status = psa_export_public_key(key_id, pub, sizeof(pub), &pub_len);
160 psa_status_t destruction_status = psa_destroy_key(key_id);
161 if (status != PSA_SUCCESS) {
162 return psa_pk_status_to_mbedtls(status);
163 } else if (destruction_status != PSA_SUCCESS) {
164 return psa_pk_status_to_mbedtls(destruction_status);
165 }
166
167 /* Load serialized public key into ecp_keypair structure */
168 return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, pub, pub_len);
169
170#else /* MBEDTLS_USE_PSA_CRYPTO */
171
172 (void) prv;
173 (void) prv_len;
174
175 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
176 return mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
177
178#endif /* MBEDTLS_USE_PSA_CRYPTO */
179}
180
181#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
182/*
183 * Set the public key: fallback using ECP_LIGHT in the USE_PSA_EC_DATA case.
184 *
185 * Normally, when MBEDTLS_PK_USE_PSA_EC_DATA is enabled, we only use PSA
186 * functions to handle keys. However, currently psa_import_key() does not
187 * support compressed points. In case that support was explicitly requested,
188 * this fallback uses ECP functions to get the job done. This is the reason
189 * why MBEDTLS_PK_PARSE_EC_COMPRESSED auto-enables MBEDTLS_ECP_LIGHT.
190 *
191 * [in/out] pk: in: must have the group set, see mbedtls_pk_ecc_set_group().
192 * out: will have the public key set.
193 * [in] pub, pub_len: the public key as an ECPoint,
194 * in any format supported by ECP.
195 *
196 * Return:
197 * - 0 on success;
198 * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
199 * but not supported;
200 * - another error code otherwise.
201 */
202static int pk_ecc_set_pubkey_psa_ecp_fallback(mbedtls_pk_context *pk,
203 const unsigned char *pub,
204 size_t pub_len)
205{
206#if !defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
207 (void) pk;
208 (void) pub;
209 (void) pub_len;
210 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
211#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
212 mbedtls_ecp_keypair ecp_key;
213 mbedtls_ecp_group_id ecp_group_id;
214 int ret;
215
216 ecp_group_id = mbedtls_ecc_group_from_psa(pk->ec_family, pk->ec_bits);
217
218 mbedtls_ecp_keypair_init(&ecp_key);
219 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
220 if (ret != 0) {
221 goto exit;
222 }
223 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
224 pub, pub_len);
225 if (ret != 0) {
226 goto exit;
227 }
228 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
229 MBEDTLS_ECP_PF_UNCOMPRESSED,
230 &pk->pub_raw_len, pk->pub_raw,
231 sizeof(pk->pub_raw));
232
233exit:
234 mbedtls_ecp_keypair_free(&ecp_key);
235 return ret;
236#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
237}
238#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
239
240/*
241 * Set the public key.
242 *
243 * [in/out] pk: in: must have its group set, see mbedtls_pk_ecc_set_group().
244 * out: will have the public key set.
245 * [in] pub, pub_len: the raw public key (an ECPoint).
246 *
247 * Return:
248 * - 0 on success;
249 * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
250 * but not supported;
251 * - another error code otherwise.
252 */
253int mbedtls_pk_ecc_set_pubkey(mbedtls_pk_context *pk, const unsigned char *pub, size_t pub_len)
254{
255#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
256
257 /* Load the key */
258 if (!PSA_ECC_FAMILY_IS_WEIERSTRASS(pk->ec_family) || *pub == 0x04) {
259 /* Format directly supported by PSA:
260 * - non-Weierstrass curves that only have one format;
261 * - uncompressed format for Weierstrass curves. */
262 if (pub_len > sizeof(pk->pub_raw)) {
263 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
264 }
265 memcpy(pk->pub_raw, pub, pub_len);
266 pk->pub_raw_len = pub_len;
267 } else {
268 /* Other format, try the fallback */
269 int ret = pk_ecc_set_pubkey_psa_ecp_fallback(pk, pub, pub_len);
270 if (ret != 0) {
271 return ret;
272 }
273 }
274
275 /* Validate the key by trying to import it */
276 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
277 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
278
279 psa_set_key_usage_flags(&key_attrs, 0);
280 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
281 psa_set_key_bits(&key_attrs, pk->ec_bits);
282
283 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
284 &key_id) != PSA_SUCCESS) ||
285 (psa_destroy_key(key_id) != PSA_SUCCESS)) {
286 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
287 }
288
289 return 0;
290
291#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
292
293 int ret;
294 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
295 ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q, pub, pub_len);
296 if (ret != 0) {
297 return ret;
298 }
299 return mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
300
301#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
302}
303
304#endif /* MBEDTLS_PK_C && MBEDTLS_PK_HAVE_ECC_KEYS */