Skip to content

Commit

Permalink
Make PSA util compatible with Mbed Crypto 3.0.1
Browse files Browse the repository at this point in the history
Mbed Crypto 3.0.1 ships with TF-M. To make Mbed TLS 2.22.0 compatible
with Mbed Crypto 3.0.1, changes are needed in psa_util.h (which
abstracts some portions of the PSA Crypto API for use with TLS) to deal
with new ECC curve define changes.
  • Loading branch information
Patater committed May 12, 2020
1 parent cbe6729 commit 36e7210
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
85 changes: 85 additions & 0 deletions features/mbedtls/inc/mbedtls/psa_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,86 @@ static inline int mbedtls_psa_get_ecc_oid_from_id(
psa_ecc_curve_t curve, size_t bits,
char const **oid, size_t *oid_len )
{
#if TARGET_TFM
/* Use older Crypto API, at least until TF-M updates its crypto
* implementation to Mbed TLS 2.22.0. */
(void) bits;
switch( curve )
{
#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
case PSA_ECC_CURVE_SECP192R1:
*oid = MBEDTLS_OID_EC_GRP_SECP192R1;
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192R1 );
return( 0 );
#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
case PSA_ECC_CURVE_SECP224R1:
*oid = MBEDTLS_OID_EC_GRP_SECP224R1;
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224R1 );
return( 0 );
#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
case PSA_ECC_CURVE_SECP256R1:
*oid = MBEDTLS_OID_EC_GRP_SECP256R1;
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256R1 );
return( 0 );
#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
case PSA_ECC_CURVE_SECP384R1:
*oid = MBEDTLS_OID_EC_GRP_SECP384R1;
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP384R1 );
return( 0 );
#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
case PSA_ECC_CURVE_SECP521R1:
*oid = MBEDTLS_OID_EC_GRP_SECP521R1;
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP521R1 );
return( 0 );
#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
case PSA_ECC_CURVE_SECP192K1:
*oid = MBEDTLS_OID_EC_GRP_SECP192K1;
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192K1 );
return( 0 );
#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
case PSA_ECC_CURVE_SECP224K1:
*oid = MBEDTLS_OID_EC_GRP_SECP224K1;
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224K1 );
return( 0 );
#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
case PSA_ECC_CURVE_SECP256K1:
*oid = MBEDTLS_OID_EC_GRP_SECP256K1;
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256K1 );
return( 0 );
#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
case PSA_ECC_CURVE_BRAINPOOL_P256R1:
*oid = MBEDTLS_OID_EC_GRP_BP256R1;
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP256R1 );
return( 0 );
#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
case PSA_ECC_CURVE_BRAINPOOL_P384R1:
*oid = MBEDTLS_OID_EC_GRP_BP384R1;
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP384R1 );
return( 0 );
#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
case PSA_ECC_CURVE_BRAINPOOL_P512R1:
*oid = MBEDTLS_OID_EC_GRP_BP512R1;
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP512R1 );
return( 0 );
#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
default:
(void) oid;
(void) oid_len;
return( -1 );
}
#else
/* Use more up to date Crypto API */

switch( curve )
{
case PSA_ECC_CURVE_SECP_R1:
Expand Down Expand Up @@ -250,6 +330,7 @@ static inline int mbedtls_psa_get_ecc_oid_from_id(
(void) oid;
(void) oid_len;
return( -1 );
#endif /* TARGET_TFM */
}

#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH 1
Expand Down Expand Up @@ -369,12 +450,16 @@ static inline int mbedtls_psa_err_translate_pk( psa_status_t status )
static inline psa_key_type_t mbedtls_psa_parse_tls_ecc_group(
uint16_t tls_ecc_grp_reg_id, size_t *bits )
{
#if TARGET_TFM
return( (psa_ecc_curve_t) tls_ecc_grp_reg_id );
#else
const mbedtls_ecp_curve_info *curve_info =
mbedtls_ecp_curve_info_from_tls_id( tls_ecc_grp_reg_id );
if( curve_info == NULL )
return( 0 );
return( PSA_KEY_TYPE_ECC_KEY_PAIR(
mbedtls_ecc_group_to_psa( curve_info->grp_id, bits ) ) );
#endif
}
#endif /* MBEDTLS_ECP_C */

Expand Down
11 changes: 9 additions & 2 deletions features/mbedtls/src/pk.c
Original file line number Diff line number Diff line change
Expand Up @@ -617,12 +617,19 @@ int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk,
if( ( ret = mbedtls_mpi_write_binary( &ec->d, d, d_len ) ) != 0 )
return( ret );

/* prepare the key attributes */
#if TARGET_TFM
curve_id = mbedtls_ecp_curve_info_from_grp_id( ec->grp.id )->tls_id;
key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
mbedtls_psa_parse_tls_ecc_group ( curve_id,
&bits ) );
#else
curve_id = mbedtls_ecc_group_to_psa( ec->grp.id, &bits );
key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( curve_id );

/* prepare the key attributes */
psa_set_key_type( &attributes, key_type );
psa_set_key_bits( &attributes, bits );
#endif
psa_set_key_type( &attributes, key_type );
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
psa_set_key_algorithm( &attributes, PSA_ALG_ECDSA(hash_alg) );

Expand Down

0 comments on commit 36e7210

Please sign in to comment.