Skip to content

Commit f6af90a

Browse files
committed
Make PSA util compatible with Mbed Crypto 3.0.1
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.
1 parent a823a2f commit f6af90a

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

features/mbedtls/inc/mbedtls/psa_util.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,86 @@ static inline int mbedtls_psa_get_ecc_oid_from_id(
163163
psa_ecc_curve_t curve, size_t bits,
164164
char const **oid, size_t *oid_len )
165165
{
166+
#if TARGET_TFM
167+
/* Use older Crypto API, at least until TF-M updates its crypto
168+
* implementation to Mbed TLS 2.22.0. */
169+
(void) bits;
170+
switch( curve )
171+
{
172+
#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
173+
case PSA_ECC_CURVE_SECP192R1:
174+
*oid = MBEDTLS_OID_EC_GRP_SECP192R1;
175+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192R1 );
176+
return( 0 );
177+
#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
178+
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
179+
case PSA_ECC_CURVE_SECP224R1:
180+
*oid = MBEDTLS_OID_EC_GRP_SECP224R1;
181+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224R1 );
182+
return( 0 );
183+
#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
184+
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
185+
case PSA_ECC_CURVE_SECP256R1:
186+
*oid = MBEDTLS_OID_EC_GRP_SECP256R1;
187+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256R1 );
188+
return( 0 );
189+
#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
190+
#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
191+
case PSA_ECC_CURVE_SECP384R1:
192+
*oid = MBEDTLS_OID_EC_GRP_SECP384R1;
193+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP384R1 );
194+
return( 0 );
195+
#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
196+
#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
197+
case PSA_ECC_CURVE_SECP521R1:
198+
*oid = MBEDTLS_OID_EC_GRP_SECP521R1;
199+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP521R1 );
200+
return( 0 );
201+
#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
202+
#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
203+
case PSA_ECC_CURVE_SECP192K1:
204+
*oid = MBEDTLS_OID_EC_GRP_SECP192K1;
205+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192K1 );
206+
return( 0 );
207+
#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
208+
#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
209+
case PSA_ECC_CURVE_SECP224K1:
210+
*oid = MBEDTLS_OID_EC_GRP_SECP224K1;
211+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224K1 );
212+
return( 0 );
213+
#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
214+
#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
215+
case PSA_ECC_CURVE_SECP256K1:
216+
*oid = MBEDTLS_OID_EC_GRP_SECP256K1;
217+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256K1 );
218+
return( 0 );
219+
#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
220+
#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
221+
case PSA_ECC_CURVE_BRAINPOOL_P256R1:
222+
*oid = MBEDTLS_OID_EC_GRP_BP256R1;
223+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP256R1 );
224+
return( 0 );
225+
#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
226+
#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
227+
case PSA_ECC_CURVE_BRAINPOOL_P384R1:
228+
*oid = MBEDTLS_OID_EC_GRP_BP384R1;
229+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP384R1 );
230+
return( 0 );
231+
#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
232+
#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
233+
case PSA_ECC_CURVE_BRAINPOOL_P512R1:
234+
*oid = MBEDTLS_OID_EC_GRP_BP512R1;
235+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP512R1 );
236+
return( 0 );
237+
#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
238+
default:
239+
(void) oid;
240+
(void) oid_len;
241+
return( -1 );
242+
}
243+
#else
244+
/* Use more up to date Crypto API */
245+
166246
switch( curve )
167247
{
168248
case PSA_ECC_CURVE_SECP_R1:
@@ -250,6 +330,7 @@ static inline int mbedtls_psa_get_ecc_oid_from_id(
250330
(void) oid;
251331
(void) oid_len;
252332
return( -1 );
333+
#endif /* TARGET_TFM */
253334
}
254335

255336
#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH 1
@@ -369,12 +450,16 @@ static inline int mbedtls_psa_err_translate_pk( psa_status_t status )
369450
static inline psa_key_type_t mbedtls_psa_parse_tls_ecc_group(
370451
uint16_t tls_ecc_grp_reg_id, size_t *bits )
371452
{
453+
#if TARGET_TFM
454+
return( (psa_ecc_curve_t) tls_ecc_grp_reg_id );
455+
#else
372456
const mbedtls_ecp_curve_info *curve_info =
373457
mbedtls_ecp_curve_info_from_tls_id( tls_ecc_grp_reg_id );
374458
if( curve_info == NULL )
375459
return( 0 );
376460
return( PSA_KEY_TYPE_ECC_KEY_PAIR(
377461
mbedtls_ecc_group_to_psa( curve_info->grp_id, bits ) ) );
462+
#endif
378463
}
379464
#endif /* MBEDTLS_ECP_C */
380465

features/mbedtls/src/pk.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,12 +617,19 @@ int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk,
617617
if( ( ret = mbedtls_mpi_write_binary( &ec->d, d, d_len ) ) != 0 )
618618
return( ret );
619619

620+
/* prepare the key attributes */
621+
#if TARGET_TFM
622+
curve_id = mbedtls_ecp_curve_info_from_grp_id( ec->grp.id )->tls_id;
623+
key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
624+
mbedtls_psa_parse_tls_ecc_group ( curve_id,
625+
&bits ) );
626+
#else
620627
curve_id = mbedtls_ecc_group_to_psa( ec->grp.id, &bits );
621628
key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( curve_id );
622629

623-
/* prepare the key attributes */
624-
psa_set_key_type( &attributes, key_type );
625630
psa_set_key_bits( &attributes, bits );
631+
#endif
632+
psa_set_key_type( &attributes, key_type );
626633
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
627634
psa_set_key_algorithm( &attributes, PSA_ALG_ECDSA(hash_alg) );
628635

0 commit comments

Comments
 (0)