Skip to content
0.2.0-dev Download ZIP
175 lines ·6.0 KB Raw
1/*
2 * ECC setters for PK.
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8#include "tf_psa_crypto_common.h"
9
10#include "mbedtls/pk.h"
11#if defined(MBEDTLS_PK_HAVE_PRIVATE_HEADER)
12#include <mbedtls/private/pk_private.h>
13#endif /* MBEDTLS_PK_HAVE_PRIVATE_HEADER */
14#include "mbedtls/private/error_common.h"
15#include "mbedtls/private/ecp.h"
16#include "pk_internal.h"
17
18#if defined(MBEDTLS_PK_C) && defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
19
20int mbedtls_pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id)
21{
22 size_t ec_bits;
23 psa_ecc_family_t ec_family = mbedtls_ecc_group_to_psa(grp_id, &ec_bits);
24
25 /* group may already be initialized; if so, make sure IDs match */
26 if ((pk->ec_family != 0 && pk->ec_family != ec_family) ||
27 (pk->bits != 0 && pk->bits != ec_bits)) {
28 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
29 }
30
31 /* set group */
32 pk->ec_family = ec_family;
33 pk->bits = ec_bits;
34
35 return 0;
36}
37
38int mbedtls_pk_ecc_set_key(mbedtls_pk_context *pk, unsigned char *key, size_t key_len)
39{
40 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
41 psa_key_usage_t flags;
42 psa_status_t status;
43
44 pk->psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family);
45 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
46 if (pk->ec_family == PSA_ECC_FAMILY_MONTGOMERY) {
47 /* Do not set algorithm here because Montgomery keys cannot do ECDSA and
48 * the PK module cannot do ECDH. When the key will be used in TLS for
49 * ECDH, it will be exported and then re-imported with proper flags
50 * and algorithm. */
51 flags = PSA_KEY_USAGE_EXPORT;
52 } else {
53 psa_set_key_algorithm(&attributes,
54 MBEDTLS_PK_ALG_ECDSA(PSA_ALG_ANY_HASH));
55 flags = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_SIGN_MESSAGE |
56 PSA_KEY_USAGE_EXPORT;
57 }
58 psa_set_key_usage_flags(&attributes, flags);
59
60 status = psa_import_key(&attributes, key, key_len, &pk->priv_id);
61 return psa_pk_status_to_mbedtls(status);
62}
63
64/*
65 * Set the public key.
66 *
67 * Normally we only use PSA functions to handle keys. However, currently
68 * psa_import_key() does not support compressed points. In case that support
69 * was explicitly requested, this fallback uses ECP functions to get the job
70 * done. This is the reason why MBEDTLS_PK_PARSE_EC_COMPRESSED auto-enables
71 * MBEDTLS_ECP_LIGHT.
72 *
73 * [in/out] pk: in: must have the group set, see mbedtls_pk_ecc_set_group().
74 * out: will have the public key set.
75 * [in] pub, pub_len: the public key as an ECPoint,
76 * in any format supported by ECP.
77 *
78 * Return:
79 * - 0 on success;
80 * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
81 * but not supported;
82 * - another error code otherwise.
83 */
84static int pk_ecc_set_pubkey_psa_ecp_fallback(mbedtls_pk_context *pk,
85 const unsigned char *pub,
86 size_t pub_len)
87{
88#if !defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
89 (void) pk;
90 (void) pub;
91 (void) pub_len;
92 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
93#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
94 mbedtls_ecp_keypair ecp_key;
95 mbedtls_ecp_group_id ecp_group_id;
96 int ret;
97
98 ecp_group_id = mbedtls_ecc_group_from_psa(pk->ec_family, pk->bits);
99
100 mbedtls_ecp_keypair_init(&ecp_key);
101 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
102 if (ret != 0) {
103 goto exit;
104 }
105 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
106 pub, pub_len);
107 if (ret != 0) {
108 goto exit;
109 }
110 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
111 MBEDTLS_ECP_PF_UNCOMPRESSED,
112 &pk->pub_raw_len, pk->pub_raw,
113 sizeof(pk->pub_raw));
114
115exit:
116 mbedtls_ecp_keypair_free(&ecp_key);
117 return ret;
118#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
119}
120
121int mbedtls_pk_ecc_set_pubkey(mbedtls_pk_context *pk, const unsigned char *pub, size_t pub_len)
122{
123 /* We need to read the first byte to determine the format (compressed,
124 * uncompressed or 0). */
125 if (pub_len == 0) {
126 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
127 }
128
129 /* Load the key */
130 if (!PSA_ECC_FAMILY_IS_WEIERSTRASS(pk->ec_family) || *pub == 0x04) {
131 /* Format directly supported by PSA:
132 * - non-Weierstrass curves that only have one format;
133 * - uncompressed format for Weierstrass curves. */
134 if (pub_len > sizeof(pk->pub_raw)) {
135 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
136 }
137 memcpy(pk->pub_raw, pub, pub_len);
138 pk->pub_raw_len = pub_len;
139 } else {
140 /* Other format, try the fallback */
141 int ret = pk_ecc_set_pubkey_psa_ecp_fallback(pk, pub, pub_len);
142 if (ret != 0) {
143 return ret;
144 }
145 }
146
147 /* Validate the key by trying to import it */
148 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
149 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
150 psa_key_type_t key_type = PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family);
151
152 psa_set_key_usage_flags(&key_attrs, 0);
153 psa_set_key_type(&key_attrs, key_type);
154 psa_set_key_bits(&key_attrs, pk->bits);
155
156 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
157 &key_id) != PSA_SUCCESS) ||
158 (psa_destroy_key(key_id) != PSA_SUCCESS)) {
159 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
160 }
161
162 if (pk->psa_type == PSA_KEY_TYPE_NONE) {
163 pk->psa_type = key_type;
164 } else {
165 /* If pk->psa_type is already set, ensure its public counterpart
166 * matches with the public key type we used above when testing the key. */
167 if (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(pk->psa_type) != key_type) {
168 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
169 }
170 }
171
172 return 0;
173}
174
175#endif /* MBEDTLS_PK_C && PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */