0.2.0-dev Download ZIP
188 lines ·6.6 KB Raw
| 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 TF_PSA_CRYPTO_PK_INTERNAL_H |
| 12 | #define TF_PSA_CRYPTO_PK_INTERNAL_H |
| 13 | |
| 14 | #include "mbedtls/pk.h" |
| 15 | #if defined(MBEDTLS_PK_HAVE_PRIVATE_HEADER) |
| 16 | #include <mbedtls/private/pk_private.h> |
| 17 | #endif /* MBEDTLS_PK_HAVE_PRIVATE_HEADER */ |
| 18 | |
| 19 | #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) |
| 20 | #include "mbedtls/private/ecp.h" |
| 21 | #endif |
| 22 | |
| 23 | #include "psa/crypto.h" |
| 24 | |
| 25 | #include "psa_util_internal.h" |
| 26 | #define PSA_PK_TO_MBEDTLS_ERR(status) psa_pk_status_to_mbedtls(status) |
| 27 | #define PSA_PK_RSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \ |
| 28 | psa_to_pk_rsa_errors, \ |
| 29 | psa_pk_status_to_mbedtls) |
| 30 | #define PSA_PK_ECDSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \ |
| 31 | psa_to_pk_ecdsa_errors, \ |
| 32 | psa_pk_status_to_mbedtls) |
| 33 | |
| 34 | /* Headers/footers for PEM files */ |
| 35 | #define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----" |
| 36 | #define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----" |
| 37 | #define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----" |
| 38 | #define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----" |
| 39 | #define PEM_BEGIN_PUBLIC_KEY_RSA "-----BEGIN RSA PUBLIC KEY-----" |
| 40 | #define PEM_END_PUBLIC_KEY_RSA "-----END RSA PUBLIC KEY-----" |
| 41 | #define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----" |
| 42 | #define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----" |
| 43 | #define PEM_BEGIN_PRIVATE_KEY_PKCS8 "-----BEGIN PRIVATE KEY-----" |
| 44 | #define PEM_END_PRIVATE_KEY_PKCS8 "-----END PRIVATE KEY-----" |
| 45 | #define PEM_BEGIN_ENCRYPTED_PRIVATE_KEY_PKCS8 "-----BEGIN ENCRYPTED PRIVATE KEY-----" |
| 46 | #define PEM_END_ENCRYPTED_PRIVATE_KEY_PKCS8 "-----END ENCRYPTED PRIVATE KEY-----" |
| 47 | |
| 48 | /* |
| 49 | * We're trying to statisfy two kinds of users: |
| 50 | * - those who don't want to use the heap; |
| 51 | * - those who can't afford large stack buffers. |
| 52 | * |
| 53 | * The current compromise is that if ECC is the only key type supported in PK, |
| 54 | * then we export keys on the stack, and otherwise we use the heap. |
| 55 | * |
| 56 | * Note: add && !ML-DSA when adding support for ML-DSA */ |
| 57 | #if !defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) |
| 58 | #define PK_EXPORT_KEYS_ON_THE_STACK |
| 59 | #endif |
| 60 | |
| 61 | #if defined(PK_EXPORT_KEYS_ON_THE_STACK) |
| 62 | /* We know for ECC, pubkey are longer than privkeys, but double check */ |
| 63 | #define PK_EXPORT_KEY_STACK_BUFFER_SIZE MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH |
| 64 | #if MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH > PK_EXPORT_KEY_STACK_BUFFER_SIZE |
| 65 | #undef PK_EXPORT_KEY_STACK_BUFFER_SIZE |
| 66 | #define PK_EXPORT_KEY_STACK_BUFFER_SIZE MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH |
| 67 | #endif |
| 68 | #endif |
| 69 | |
| 70 | #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) |
| 71 | |
| 72 | static inline mbedtls_ecp_group_id mbedtls_pk_get_ec_group_id(const mbedtls_pk_context *pk) |
| 73 | { |
| 74 | mbedtls_ecp_group_id id; |
| 75 | |
| 76 | if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_OPAQUE) { |
| 77 | psa_key_attributes_t opaque_attrs = PSA_KEY_ATTRIBUTES_INIT; |
| 78 | psa_key_type_t opaque_key_type; |
| 79 | psa_ecc_family_t curve; |
| 80 | |
| 81 | if (psa_get_key_attributes(pk->priv_id, &opaque_attrs) != PSA_SUCCESS) { |
| 82 | return MBEDTLS_ECP_DP_NONE; |
| 83 | } |
| 84 | opaque_key_type = psa_get_key_type(&opaque_attrs); |
| 85 | curve = PSA_KEY_TYPE_ECC_GET_FAMILY(opaque_key_type); |
| 86 | id = mbedtls_ecc_group_from_psa(curve, psa_get_key_bits(&opaque_attrs)); |
| 87 | psa_reset_key_attributes(&opaque_attrs); |
| 88 | } else { |
| 89 | id = mbedtls_ecc_group_from_psa(pk->ec_family, pk->bits); |
| 90 | } |
| 91 | |
| 92 | return id; |
| 93 | } |
| 94 | |
| 95 | /* Helper for Montgomery curves */ |
| 96 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) || defined(PSA_WANT_ECC_MONTGOMERY_448) |
| 97 | #define MBEDTLS_PK_HAVE_RFC8410_CURVES |
| 98 | #endif /* PSA_WANT_ECC_MONTGOMERY_255 || PSA_WANT_ECC_MONTGOMERY_448 */ |
| 99 | |
| 100 | #define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \ |
| 101 | ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448)) |
| 102 | |
| 103 | static inline int mbedtls_pk_is_rfc8410(const mbedtls_pk_context *pk) |
| 104 | { |
| 105 | mbedtls_ecp_group_id id = mbedtls_pk_get_ec_group_id(pk); |
| 106 | |
| 107 | return MBEDTLS_PK_IS_RFC8410_GROUP_ID(id); |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Set the group used by this key. |
| 112 | * |
| 113 | * [in/out] pk: in: must have been pk_setup() to an ECC type |
| 114 | * out: will have group (curve) information set |
| 115 | * [in] grp_in: a supported group ID (not NONE) |
| 116 | */ |
| 117 | int mbedtls_pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id); |
| 118 | |
| 119 | /* |
| 120 | * Set the private key material |
| 121 | * |
| 122 | * [in/out] pk: in: must have the group set already, see mbedtls_pk_ecc_set_group(). |
| 123 | * out: will have the private key set. |
| 124 | * [in] key, key_len: the raw private key (no ASN.1 wrapping). |
| 125 | */ |
| 126 | int mbedtls_pk_ecc_set_key(mbedtls_pk_context *pk, unsigned char *key, size_t key_len); |
| 127 | |
| 128 | /* |
| 129 | * Set the public key. |
| 130 | * |
| 131 | * [in/out] pk: in: must have its group set, see mbedtls_pk_ecc_set_group(). |
| 132 | * out: will have the public key set. |
| 133 | * [in] pub, pub_len: the raw public key (an ECPoint). |
| 134 | * |
| 135 | * Return: |
| 136 | * - 0 on success; |
| 137 | * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid |
| 138 | * but not supported; |
| 139 | * - another error code otherwise. |
| 140 | */ |
| 141 | int mbedtls_pk_ecc_set_pubkey(mbedtls_pk_context *pk, const unsigned char *pub, size_t pub_len); |
| 142 | #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 143 | |
| 144 | #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) |
| 145 | /* |
| 146 | * Parse a private RSA key. |
| 147 | */ |
| 148 | int mbedtls_pk_rsa_set_key(mbedtls_pk_context *pk, const unsigned char *key, size_t key_len); |
| 149 | |
| 150 | /* |
| 151 | * Parse an RSA public key. |
| 152 | */ |
| 153 | int mbedtls_pk_rsa_set_pubkey(mbedtls_pk_context *pk, const unsigned char *key, size_t key_len); |
| 154 | #endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */ |
| 155 | |
| 156 | /* |
| 157 | * Fill the public key fields of the given PK context by exporting it from |
| 158 | * the private counterpart. |
| 159 | * |
| 160 | * [in/out] pk: must have been populated with private key. |
| 161 | * |
| 162 | * Return: |
| 163 | * - 0 on success; |
| 164 | * - error code otherwise. |
| 165 | */ |
| 166 | int mbedtls_pk_set_pubkey_from_prv(mbedtls_pk_context *pk); |
| 167 | |
| 168 | #if defined(MBEDTLS_TEST_HOOKS) |
| 169 | |
| 170 | MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der( |
| 171 | mbedtls_pk_context *pk, |
| 172 | unsigned char *key, size_t keylen, |
| 173 | const unsigned char *pwd, size_t pwdlen); |
| 174 | |
| 175 | #if defined(MBEDTLS_PK_PARSE_C) |
| 176 | MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_unencrypted_der( |
| 177 | mbedtls_pk_context *pk, |
| 178 | const unsigned char *key, |
| 179 | size_t keylen); |
| 180 | #endif /* MBEDTLS_PK_PARSE_C */ |
| 181 | |
| 182 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 183 | |
| 184 | #if defined(MBEDTLS_FS_IO) |
| 185 | int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n); |
| 186 | #endif |
| 187 | |
| 188 | #endif /* TF_PSA_CRYPTO_PK_INTERNAL_H */ |