2023-05-15 11:13:55 +02:00
|
|
|
/**
|
|
|
|
* \file pk_internal.h
|
|
|
|
*
|
|
|
|
* \brief Public Key abstraction layer: internal (i.e. library only) functions
|
|
|
|
* and definitions.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2023-05-15 11:13:55 +02:00
|
|
|
*/
|
|
|
|
#ifndef MBEDTLS_PK_INTERNAL_H
|
|
|
|
#define MBEDTLS_PK_INTERNAL_H
|
|
|
|
|
2023-05-17 15:37:29 +02:00
|
|
|
#include "mbedtls/pk.h"
|
|
|
|
|
2023-06-14 14:49:33 +02:00
|
|
|
#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
|
2023-05-15 11:13:55 +02:00
|
|
|
#include "mbedtls/ecp.h"
|
|
|
|
#endif
|
|
|
|
|
2023-05-17 15:37:29 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
#include "psa/crypto.h"
|
|
|
|
#endif
|
|
|
|
|
2023-05-18 18:46:38 +02:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
2023-06-07 13:07:21 +02:00
|
|
|
#include "psa_util_internal.h"
|
2023-05-18 18:46:38 +02:00
|
|
|
#define PSA_PK_TO_MBEDTLS_ERR(status) psa_pk_status_to_mbedtls(status)
|
|
|
|
#define PSA_PK_RSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
|
|
|
psa_to_pk_rsa_errors, \
|
|
|
|
psa_pk_status_to_mbedtls)
|
|
|
|
#define PSA_PK_ECDSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
|
|
|
psa_to_pk_ecdsa_errors, \
|
|
|
|
psa_pk_status_to_mbedtls)
|
|
|
|
#endif
|
|
|
|
|
2023-10-06 13:13:19 +02:00
|
|
|
#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
|
2023-05-15 11:13:55 +02:00
|
|
|
/**
|
|
|
|
* Public function mbedtls_pk_ec() can be used to get direct access to the
|
2023-05-17 12:32:13 +02:00
|
|
|
* wrapped ecp_keypair structure pointed to the pk_ctx. However this is not
|
|
|
|
* ideal because it bypasses the PK module on the control of its internal
|
2023-05-15 11:13:55 +02:00
|
|
|
* structure (pk_context) fields.
|
|
|
|
* For backward compatibility we keep mbedtls_pk_ec() when ECP_C is defined, but
|
2023-05-17 12:32:13 +02:00
|
|
|
* we provide 2 very similar functions when only ECP_LIGHT is enabled and not
|
2023-05-15 11:13:55 +02:00
|
|
|
* ECP_C.
|
|
|
|
* These variants embed the "ro" or "rw" keywords in their name to make the
|
|
|
|
* usage of the returned pointer explicit. Of course the returned value is
|
|
|
|
* const or non-const accordingly.
|
|
|
|
*/
|
|
|
|
static inline const mbedtls_ecp_keypair *mbedtls_pk_ec_ro(const mbedtls_pk_context pk)
|
|
|
|
{
|
|
|
|
switch (mbedtls_pk_get_type(&pk)) {
|
|
|
|
case MBEDTLS_PK_ECKEY:
|
|
|
|
case MBEDTLS_PK_ECKEY_DH:
|
|
|
|
case MBEDTLS_PK_ECDSA:
|
2023-05-15 12:57:40 +02:00
|
|
|
return (const mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
|
2023-05-15 11:13:55 +02:00
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline mbedtls_ecp_keypair *mbedtls_pk_ec_rw(const mbedtls_pk_context pk)
|
|
|
|
{
|
|
|
|
switch (mbedtls_pk_get_type(&pk)) {
|
|
|
|
case MBEDTLS_PK_ECKEY:
|
|
|
|
case MBEDTLS_PK_ECKEY_DH:
|
|
|
|
case MBEDTLS_PK_ECDSA:
|
|
|
|
return (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2023-10-06 13:13:19 +02:00
|
|
|
#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
|
2023-05-17 15:33:07 +02:00
|
|
|
|
2023-10-06 13:13:19 +02:00
|
|
|
#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
|
2023-05-18 18:48:07 +02:00
|
|
|
static inline mbedtls_ecp_group_id mbedtls_pk_get_group_id(const mbedtls_pk_context *pk)
|
2023-05-17 15:33:07 +02:00
|
|
|
{
|
2023-05-18 18:48:07 +02:00
|
|
|
mbedtls_ecp_group_id id;
|
2023-05-31 12:01:55 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_OPAQUE) {
|
2023-06-05 11:08:28 +02:00
|
|
|
psa_key_attributes_t opaque_attrs = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_type_t opaque_key_type;
|
|
|
|
psa_ecc_family_t curve;
|
|
|
|
|
2023-05-31 12:01:55 +02:00
|
|
|
if (psa_get_key_attributes(pk->priv_id, &opaque_attrs) != PSA_SUCCESS) {
|
|
|
|
return MBEDTLS_ECP_DP_NONE;
|
|
|
|
}
|
|
|
|
opaque_key_type = psa_get_key_type(&opaque_attrs);
|
|
|
|
curve = PSA_KEY_TYPE_ECC_GET_FAMILY(opaque_key_type);
|
|
|
|
id = mbedtls_ecc_group_of_psa(curve, psa_get_key_bits(&opaque_attrs), 0);
|
|
|
|
psa_reset_key_attributes(&opaque_attrs);
|
|
|
|
} else
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
{
|
2023-05-18 18:48:07 +02:00
|
|
|
#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
|
2023-05-31 12:01:55 +02:00
|
|
|
id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
|
2023-05-18 18:48:07 +02:00
|
|
|
#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
|
2023-05-31 12:01:55 +02:00
|
|
|
id = mbedtls_pk_ec_ro(*pk)->grp.id;
|
2023-05-18 18:48:07 +02:00
|
|
|
#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
|
2023-05-31 12:01:55 +02:00
|
|
|
}
|
|
|
|
|
2023-05-18 18:48:07 +02:00
|
|
|
return id;
|
2023-05-17 15:33:07 +02:00
|
|
|
}
|
2023-05-18 18:48:07 +02:00
|
|
|
|
|
|
|
/* Helper for Montgomery curves */
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_CURVE25519) || defined(MBEDTLS_ECP_HAVE_CURVE448)
|
2023-05-18 18:48:07 +02:00
|
|
|
#define MBEDTLS_PK_HAVE_RFC8410_CURVES
|
2023-09-01 09:20:51 +02:00
|
|
|
#endif /* MBEDTLS_ECP_HAVE_CURVE25519 || MBEDTLS_ECP_DP_CURVE448 */
|
2023-06-14 14:49:33 +02:00
|
|
|
#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
|
2023-05-15 11:13:55 +02:00
|
|
|
|
2023-07-25 12:06:55 +02:00
|
|
|
/* Helper for (deterministic) ECDSA */
|
|
|
|
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
|
|
|
#define MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET PSA_ALG_DETERMINISTIC_ECDSA
|
|
|
|
#else
|
|
|
|
#define MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET PSA_ALG_ECDSA
|
|
|
|
#endif
|
2023-09-20 19:00:48 +02:00
|
|
|
|
2023-07-25 12:06:55 +02:00
|
|
|
#if defined(MBEDTLS_TEST_HOOKS)
|
2023-09-20 19:00:48 +02:00
|
|
|
MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
|
|
|
|
mbedtls_pk_context *pk,
|
|
|
|
unsigned char *key, size_t keylen,
|
|
|
|
const unsigned char *pwd, size_t pwdlen,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
|
|
|
|
#endif
|
2023-05-17 15:37:29 +02:00
|
|
|
|
2023-05-15 11:13:55 +02:00
|
|
|
#endif /* MBEDTLS_PK_INTERNAL_H */
|