Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 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 |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 9 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 10 | */ |
| 11 | #ifndef MBEDTLS_PK_INTERNAL_H |
| 12 | #define MBEDTLS_PK_INTERNAL_H |
| 13 | |
Valerio Setti | 483738e | 2023-05-17 15:37:29 +0200 | [diff] [blame] | 14 | #include "mbedtls/pk.h" |
| 15 | |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 16 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 17 | #include "mbedtls/ecp.h" |
| 18 | #endif |
| 19 | |
Gilles Peskine | fc3d866 | 2024-02-09 19:26:37 +0100 | [diff] [blame] | 20 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) |
Valerio Setti | 483738e | 2023-05-17 15:37:29 +0200 | [diff] [blame] | 21 | #include "psa/crypto.h" |
Tomi Fontanilles | 9f41770 | 2023-12-16 15:28:51 +0200 | [diff] [blame] | 22 | |
| 23 | #include "psa_util_internal.h" |
| 24 | #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) |
Gilles Peskine | fc3d866 | 2024-02-09 19:26:37 +0100 | [diff] [blame] | 31 | #endif /* MBEDTLS_PSA_CRYPTO_CLIENT */ |
Valerio Setti | 483738e | 2023-05-17 15:37:29 +0200 | [diff] [blame] | 32 | |
Valerio Setti | 854c737 | 2023-11-28 08:37:57 +0100 | [diff] [blame] | 33 | /* Headers/footers for PEM files */ |
| 34 | #define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----" |
| 35 | #define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----" |
| 36 | #define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----" |
| 37 | #define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----" |
| 38 | #define PEM_BEGIN_PUBLIC_KEY_RSA "-----BEGIN RSA PUBLIC KEY-----" |
| 39 | #define PEM_END_PUBLIC_KEY_RSA "-----END RSA PUBLIC KEY-----" |
| 40 | #define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----" |
| 41 | #define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----" |
| 42 | #define PEM_BEGIN_PRIVATE_KEY_PKCS8 "-----BEGIN PRIVATE KEY-----" |
| 43 | #define PEM_END_PRIVATE_KEY_PKCS8 "-----END PRIVATE KEY-----" |
| 44 | #define PEM_BEGIN_ENCRYPTED_PRIVATE_KEY_PKCS8 "-----BEGIN ENCRYPTED PRIVATE KEY-----" |
| 45 | #define PEM_END_ENCRYPTED_PRIVATE_KEY_PKCS8 "-----END ENCRYPTED PRIVATE KEY-----" |
| 46 | |
Tomi Fontanilles | 851d8df | 2023-12-19 15:44:52 +0200 | [diff] [blame] | 47 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) && !defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 48 | /** |
| 49 | * Public function mbedtls_pk_ec() can be used to get direct access to the |
Valerio Setti | 4ac9d44 | 2023-05-17 12:32:13 +0200 | [diff] [blame] | 50 | * wrapped ecp_keypair structure pointed to the pk_ctx. However this is not |
| 51 | * ideal because it bypasses the PK module on the control of its internal |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 52 | * structure (pk_context) fields. |
| 53 | * For backward compatibility we keep mbedtls_pk_ec() when ECP_C is defined, but |
Valerio Setti | 4ac9d44 | 2023-05-17 12:32:13 +0200 | [diff] [blame] | 54 | * we provide 2 very similar functions when only ECP_LIGHT is enabled and not |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 55 | * ECP_C. |
| 56 | * These variants embed the "ro" or "rw" keywords in their name to make the |
| 57 | * usage of the returned pointer explicit. Of course the returned value is |
| 58 | * const or non-const accordingly. |
| 59 | */ |
| 60 | static inline const mbedtls_ecp_keypair *mbedtls_pk_ec_ro(const mbedtls_pk_context pk) |
| 61 | { |
| 62 | switch (mbedtls_pk_get_type(&pk)) { |
| 63 | case MBEDTLS_PK_ECKEY: |
| 64 | case MBEDTLS_PK_ECKEY_DH: |
| 65 | case MBEDTLS_PK_ECDSA: |
Valerio Setti | f70b3e0 | 2023-05-15 12:57:40 +0200 | [diff] [blame] | 66 | return (const mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx); |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 67 | default: |
| 68 | return NULL; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static inline mbedtls_ecp_keypair *mbedtls_pk_ec_rw(const mbedtls_pk_context pk) |
| 73 | { |
| 74 | switch (mbedtls_pk_get_type(&pk)) { |
| 75 | case MBEDTLS_PK_ECKEY: |
| 76 | case MBEDTLS_PK_ECKEY_DH: |
| 77 | case MBEDTLS_PK_ECDSA: |
| 78 | return (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx); |
| 79 | default: |
| 80 | return NULL; |
| 81 | } |
| 82 | } |
Tomi Fontanilles | 851d8df | 2023-12-19 15:44:52 +0200 | [diff] [blame] | 83 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS && !MBEDTLS_PK_USE_PSA_EC_DATA */ |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 84 | |
Valerio Setti | d3925d2 | 2023-10-06 13:13:19 +0200 | [diff] [blame] | 85 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Valerio Setti | f9362b7 | 2023-11-29 08:42:27 +0100 | [diff] [blame] | 86 | static inline mbedtls_ecp_group_id mbedtls_pk_get_ec_group_id(const mbedtls_pk_context *pk) |
Valerio Setti | e0e6311 | 2023-05-18 18:48:07 +0200 | [diff] [blame] | 87 | { |
| 88 | mbedtls_ecp_group_id id; |
valerio | a87601d | 2023-05-31 12:01:55 +0200 | [diff] [blame] | 89 | |
| 90 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
valerio | a87601d | 2023-05-31 12:01:55 +0200 | [diff] [blame] | 91 | if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_OPAQUE) { |
Valerio Setti | ede0c46 | 2023-06-05 11:08:28 +0200 | [diff] [blame] | 92 | psa_key_attributes_t opaque_attrs = PSA_KEY_ATTRIBUTES_INIT; |
| 93 | psa_key_type_t opaque_key_type; |
| 94 | psa_ecc_family_t curve; |
| 95 | |
valerio | a87601d | 2023-05-31 12:01:55 +0200 | [diff] [blame] | 96 | if (psa_get_key_attributes(pk->priv_id, &opaque_attrs) != PSA_SUCCESS) { |
| 97 | return MBEDTLS_ECP_DP_NONE; |
| 98 | } |
| 99 | opaque_key_type = psa_get_key_type(&opaque_attrs); |
| 100 | curve = PSA_KEY_TYPE_ECC_GET_FAMILY(opaque_key_type); |
Valerio Setti | d36c313 | 2023-12-21 14:03:51 +0100 | [diff] [blame] | 101 | id = mbedtls_ecc_group_from_psa(curve, psa_get_key_bits(&opaque_attrs)); |
valerio | a87601d | 2023-05-31 12:01:55 +0200 | [diff] [blame] | 102 | psa_reset_key_attributes(&opaque_attrs); |
| 103 | } else |
| 104 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 105 | { |
Valerio Setti | e0e6311 | 2023-05-18 18:48:07 +0200 | [diff] [blame] | 106 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
Valerio Setti | d36c313 | 2023-12-21 14:03:51 +0100 | [diff] [blame] | 107 | id = mbedtls_ecc_group_from_psa(pk->ec_family, pk->ec_bits); |
Valerio Setti | e0e6311 | 2023-05-18 18:48:07 +0200 | [diff] [blame] | 108 | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
valerio | a87601d | 2023-05-31 12:01:55 +0200 | [diff] [blame] | 109 | id = mbedtls_pk_ec_ro(*pk)->grp.id; |
Valerio Setti | e0e6311 | 2023-05-18 18:48:07 +0200 | [diff] [blame] | 110 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
valerio | a87601d | 2023-05-31 12:01:55 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Valerio Setti | e0e6311 | 2023-05-18 18:48:07 +0200 | [diff] [blame] | 113 | return id; |
| 114 | } |
| 115 | |
| 116 | /* Helper for Montgomery curves */ |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 117 | #if defined(MBEDTLS_ECP_HAVE_CURVE25519) || defined(MBEDTLS_ECP_HAVE_CURVE448) |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 118 | #define MBEDTLS_PK_HAVE_RFC8410_CURVES |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 119 | #endif /* MBEDTLS_ECP_HAVE_CURVE25519 || MBEDTLS_ECP_DP_CURVE448 */ |
Valerio Setti | bcd3059 | 2023-11-28 16:27:55 +0100 | [diff] [blame] | 120 | |
| 121 | #define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \ |
| 122 | ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448)) |
| 123 | |
| 124 | static inline int mbedtls_pk_is_rfc8410(const mbedtls_pk_context *pk) |
| 125 | { |
Valerio Setti | f9362b7 | 2023-11-29 08:42:27 +0100 | [diff] [blame] | 126 | mbedtls_ecp_group_id id = mbedtls_pk_get_ec_group_id(pk); |
Valerio Setti | bcd3059 | 2023-11-28 16:27:55 +0100 | [diff] [blame] | 127 | |
| 128 | return MBEDTLS_PK_IS_RFC8410_GROUP_ID(id); |
| 129 | } |
Valerio Setti | 3bfad3a | 2024-02-01 11:28:27 +0100 | [diff] [blame] | 130 | |
Valerio Setti | 1346075 | 2024-02-14 08:14:27 +0100 | [diff] [blame] | 131 | /* |
| 132 | * Set the group used by this key. |
| 133 | * |
| 134 | * [in/out] pk: in: must have been pk_setup() to an ECC type |
| 135 | * out: will have group (curve) information set |
| 136 | * [in] grp_in: a supported group ID (not NONE) |
| 137 | */ |
Valerio Setti | 3bfad3a | 2024-02-01 11:28:27 +0100 | [diff] [blame] | 138 | int mbedtls_pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id); |
Valerio Setti | 1346075 | 2024-02-14 08:14:27 +0100 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * Set the private key material |
| 142 | * |
| 143 | * [in/out] pk: in: must have the group set already, see mbedtls_pk_ecc_set_group(). |
| 144 | * out: will have the private key set. |
| 145 | * [in] key, key_len: the raw private key (no ASN.1 wrapping). |
| 146 | */ |
Valerio Setti | 3bfad3a | 2024-02-01 11:28:27 +0100 | [diff] [blame] | 147 | int mbedtls_pk_ecc_set_key(mbedtls_pk_context *pk, unsigned char *key, size_t key_len); |
Valerio Setti | 1346075 | 2024-02-14 08:14:27 +0100 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * Set the public key. |
| 151 | * |
| 152 | * [in/out] pk: in: must have its group set, see mbedtls_pk_ecc_set_group(). |
| 153 | * out: will have the public key set. |
| 154 | * [in] pub, pub_len: the raw public key (an ECPoint). |
| 155 | * |
| 156 | * Return: |
| 157 | * - 0 on success; |
| 158 | * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid |
| 159 | * but not supported; |
| 160 | * - another error code otherwise. |
| 161 | */ |
Valerio Setti | 3bfad3a | 2024-02-01 11:28:27 +0100 | [diff] [blame] | 162 | int mbedtls_pk_ecc_set_pubkey(mbedtls_pk_context *pk, const unsigned char *pub, size_t pub_len); |
Valerio Setti | 1346075 | 2024-02-14 08:14:27 +0100 | [diff] [blame] | 163 | |
| 164 | /* |
| 165 | * Derive a public key from its private counterpart. |
| 166 | * Computationally intensive, only use when public key is not available. |
| 167 | * |
| 168 | * [in/out] pk: in: must have the private key set, see mbedtls_pk_ecc_set_key(). |
| 169 | * out: will have the public key set. |
| 170 | * [in] prv, prv_len: the raw private key (see note below). |
| 171 | * [in] f_rng, p_rng: RNG function and context. |
| 172 | * |
| 173 | * Note: the private key information is always available from pk, |
| 174 | * however for convenience the serialized version is also passed, |
| 175 | * as it's available at each calling site, and useful in some configs |
| 176 | * (as otherwise we would have to re-serialize it from the pk context). |
| 177 | * |
| 178 | * There are three implementations of this function: |
| 179 | * 1. MBEDTLS_PK_USE_PSA_EC_DATA, |
| 180 | * 2. MBEDTLS_USE_PSA_CRYPTO but not MBEDTLS_PK_USE_PSA_EC_DATA, |
| 181 | * 3. not MBEDTLS_USE_PSA_CRYPTO. |
| 182 | */ |
Valerio Setti | 3bfad3a | 2024-02-01 11:28:27 +0100 | [diff] [blame] | 183 | int mbedtls_pk_ecc_set_pubkey_from_prv(mbedtls_pk_context *pk, |
| 184 | const unsigned char *prv, size_t prv_len, |
| 185 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 186 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 187 | |
Manuel Pégourié-Gonnard | 116175c | 2023-07-25 12:06:55 +0200 | [diff] [blame] | 188 | /* Helper for (deterministic) ECDSA */ |
| 189 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 190 | #define MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET PSA_ALG_DETERMINISTIC_ECDSA |
| 191 | #else |
| 192 | #define MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET PSA_ALG_ECDSA |
| 193 | #endif |
Waleed Elmelegy | 1db5cda | 2023-09-20 18:00:48 +0100 | [diff] [blame] | 194 | |
Manuel Pégourié-Gonnard | 116175c | 2023-07-25 12:06:55 +0200 | [diff] [blame] | 195 | #if defined(MBEDTLS_TEST_HOOKS) |
Waleed Elmelegy | 1db5cda | 2023-09-20 18:00:48 +0100 | [diff] [blame] | 196 | MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der( |
| 197 | mbedtls_pk_context *pk, |
| 198 | unsigned char *key, size_t keylen, |
| 199 | const unsigned char *pwd, size_t pwdlen, |
| 200 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); |
Waleed Elmelegy | 1db5cda | 2023-09-20 18:00:48 +0100 | [diff] [blame] | 201 | #endif |
Valerio Setti | 483738e | 2023-05-17 15:37:29 +0200 | [diff] [blame] | 202 | |
Valerio Setti | 639d567 | 2024-01-17 11:04:56 +0100 | [diff] [blame] | 203 | #if defined(MBEDTLS_FS_IO) |
| 204 | int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n); |
| 205 | #endif |
| 206 | |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 207 | #endif /* MBEDTLS_PK_INTERNAL_H */ |