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 | |
Valerio Setti | 483738e | 2023-05-17 15:37:29 +0200 | [diff] [blame] | 20 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 21 | #include "psa/crypto.h" |
| 22 | #endif |
| 23 | |
Valerio Setti | 8a62250 | 2023-05-18 18:46:38 +0200 | [diff] [blame] | 24 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
Manuel Pégourié-Gonnard | 2be8c63 | 2023-06-07 13:07:21 +0200 | [diff] [blame] | 25 | #include "psa_util_internal.h" |
Valerio Setti | 8a62250 | 2023-05-18 18:46:38 +0200 | [diff] [blame] | 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 | #endif |
| 34 | |
Valerio Setti | d3925d2 | 2023-10-06 13:13:19 +0200 | [diff] [blame] | 35 | #if !defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 36 | /** |
| 37 | * 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] | 38 | * wrapped ecp_keypair structure pointed to the pk_ctx. However this is not |
| 39 | * 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] | 40 | * structure (pk_context) fields. |
| 41 | * 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] | 42 | * 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] | 43 | * ECP_C. |
| 44 | * These variants embed the "ro" or "rw" keywords in their name to make the |
| 45 | * usage of the returned pointer explicit. Of course the returned value is |
| 46 | * const or non-const accordingly. |
| 47 | */ |
| 48 | static inline const mbedtls_ecp_keypair *mbedtls_pk_ec_ro(const mbedtls_pk_context pk) |
| 49 | { |
| 50 | switch (mbedtls_pk_get_type(&pk)) { |
| 51 | case MBEDTLS_PK_ECKEY: |
| 52 | case MBEDTLS_PK_ECKEY_DH: |
| 53 | case MBEDTLS_PK_ECDSA: |
Valerio Setti | f70b3e0 | 2023-05-15 12:57:40 +0200 | [diff] [blame] | 54 | return (const mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx); |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 55 | default: |
| 56 | return NULL; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static inline mbedtls_ecp_keypair *mbedtls_pk_ec_rw(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: |
| 66 | return (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx); |
| 67 | default: |
| 68 | return NULL; |
| 69 | } |
| 70 | } |
Valerio Setti | d3925d2 | 2023-10-06 13:13:19 +0200 | [diff] [blame] | 71 | #endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */ |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 72 | |
Valerio Setti | d3925d2 | 2023-10-06 13:13:19 +0200 | [diff] [blame] | 73 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Valerio Setti | e0e6311 | 2023-05-18 18:48:07 +0200 | [diff] [blame] | 74 | static inline mbedtls_ecp_group_id mbedtls_pk_get_group_id(const mbedtls_pk_context *pk) |
| 75 | { |
| 76 | mbedtls_ecp_group_id id; |
valerio | a87601d | 2023-05-31 12:01:55 +0200 | [diff] [blame] | 77 | |
| 78 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
valerio | a87601d | 2023-05-31 12:01:55 +0200 | [diff] [blame] | 79 | if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_OPAQUE) { |
Valerio Setti | ede0c46 | 2023-06-05 11:08:28 +0200 | [diff] [blame] | 80 | psa_key_attributes_t opaque_attrs = PSA_KEY_ATTRIBUTES_INIT; |
| 81 | psa_key_type_t opaque_key_type; |
| 82 | psa_ecc_family_t curve; |
| 83 | |
valerio | a87601d | 2023-05-31 12:01:55 +0200 | [diff] [blame] | 84 | if (psa_get_key_attributes(pk->priv_id, &opaque_attrs) != PSA_SUCCESS) { |
| 85 | return MBEDTLS_ECP_DP_NONE; |
| 86 | } |
| 87 | opaque_key_type = psa_get_key_type(&opaque_attrs); |
| 88 | curve = PSA_KEY_TYPE_ECC_GET_FAMILY(opaque_key_type); |
| 89 | id = mbedtls_ecc_group_of_psa(curve, psa_get_key_bits(&opaque_attrs), 0); |
| 90 | psa_reset_key_attributes(&opaque_attrs); |
| 91 | } else |
| 92 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 93 | { |
Valerio Setti | e0e6311 | 2023-05-18 18:48:07 +0200 | [diff] [blame] | 94 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
valerio | a87601d | 2023-05-31 12:01:55 +0200 | [diff] [blame] | 95 | id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0); |
Valerio Setti | e0e6311 | 2023-05-18 18:48:07 +0200 | [diff] [blame] | 96 | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
valerio | a87601d | 2023-05-31 12:01:55 +0200 | [diff] [blame] | 97 | id = mbedtls_pk_ec_ro(*pk)->grp.id; |
Valerio Setti | e0e6311 | 2023-05-18 18:48:07 +0200 | [diff] [blame] | 98 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
valerio | a87601d | 2023-05-31 12:01:55 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Valerio Setti | e0e6311 | 2023-05-18 18:48:07 +0200 | [diff] [blame] | 101 | return id; |
| 102 | } |
| 103 | |
| 104 | /* Helper for Montgomery curves */ |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 105 | #if defined(MBEDTLS_ECP_HAVE_CURVE25519) || defined(MBEDTLS_ECP_HAVE_CURVE448) |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 106 | #define MBEDTLS_PK_HAVE_RFC8410_CURVES |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 107 | #endif /* MBEDTLS_ECP_HAVE_CURVE25519 || MBEDTLS_ECP_DP_CURVE448 */ |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 108 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 109 | |
Manuel Pégourié-Gonnard | 116175c | 2023-07-25 12:06:55 +0200 | [diff] [blame] | 110 | /* Helper for (deterministic) ECDSA */ |
| 111 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 112 | #define MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET PSA_ALG_DETERMINISTIC_ECDSA |
| 113 | #else |
| 114 | #define MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET PSA_ALG_ECDSA |
| 115 | #endif |
Waleed Elmelegy | 1db5cda | 2023-09-20 18:00:48 +0100 | [diff] [blame] | 116 | |
Manuel Pégourié-Gonnard | 116175c | 2023-07-25 12:06:55 +0200 | [diff] [blame] | 117 | #if defined(MBEDTLS_TEST_HOOKS) |
Waleed Elmelegy | 1db5cda | 2023-09-20 18:00:48 +0100 | [diff] [blame] | 118 | MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der( |
| 119 | mbedtls_pk_context *pk, |
| 120 | unsigned char *key, size_t keylen, |
| 121 | const unsigned char *pwd, size_t pwdlen, |
| 122 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); |
Waleed Elmelegy | 1db5cda | 2023-09-20 18:00:48 +0100 | [diff] [blame] | 123 | #endif |
Valerio Setti | 483738e | 2023-05-17 15:37:29 +0200 | [diff] [blame] | 124 | |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 125 | #endif /* MBEDTLS_PK_INTERNAL_H */ |