Skip to content

Commit 0961f31

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. Signed-off-by: Jaeden Amero <[email protected]> Signed-off-by: Devaraj Ranganna <[email protected]>
1 parent 908c33b commit 0961f31

File tree

3 files changed

+104
-8
lines changed

3 files changed

+104
-8
lines changed

features/mbedtls/inc/mbedtls/psa_util.h

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* change at any time.
88
*/
99
/*
10-
* Copyright (C) 2006-2018, ARM Limited, All Rights Reserved
10+
* Copyright (C) 2006-2020, ARM Limited, All Rights Reserved
1111
* SPDX-License-Identifier: Apache-2.0
1212
*
1313
* Licensed under the Apache License, Version 2.0 (the "License"); you may
@@ -163,6 +163,87 @@ 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. See
169+
* https://github.com/ARMmbed/mbed-os/issues/13025 for details. */
170+
(void) bits;
171+
switch( curve )
172+
{
173+
#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
174+
case PSA_ECC_CURVE_SECP192R1:
175+
*oid = MBEDTLS_OID_EC_GRP_SECP192R1;
176+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192R1 );
177+
return( 0 );
178+
#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
179+
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
180+
case PSA_ECC_CURVE_SECP224R1:
181+
*oid = MBEDTLS_OID_EC_GRP_SECP224R1;
182+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224R1 );
183+
return( 0 );
184+
#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
185+
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
186+
case PSA_ECC_CURVE_SECP256R1:
187+
*oid = MBEDTLS_OID_EC_GRP_SECP256R1;
188+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256R1 );
189+
return( 0 );
190+
#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
191+
#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
192+
case PSA_ECC_CURVE_SECP384R1:
193+
*oid = MBEDTLS_OID_EC_GRP_SECP384R1;
194+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP384R1 );
195+
return( 0 );
196+
#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
197+
#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
198+
case PSA_ECC_CURVE_SECP521R1:
199+
*oid = MBEDTLS_OID_EC_GRP_SECP521R1;
200+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP521R1 );
201+
return( 0 );
202+
#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
203+
#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
204+
case PSA_ECC_CURVE_SECP192K1:
205+
*oid = MBEDTLS_OID_EC_GRP_SECP192K1;
206+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192K1 );
207+
return( 0 );
208+
#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
209+
#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
210+
case PSA_ECC_CURVE_SECP224K1:
211+
*oid = MBEDTLS_OID_EC_GRP_SECP224K1;
212+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224K1 );
213+
return( 0 );
214+
#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
215+
#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
216+
case PSA_ECC_CURVE_SECP256K1:
217+
*oid = MBEDTLS_OID_EC_GRP_SECP256K1;
218+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256K1 );
219+
return( 0 );
220+
#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
221+
#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
222+
case PSA_ECC_CURVE_BRAINPOOL_P256R1:
223+
*oid = MBEDTLS_OID_EC_GRP_BP256R1;
224+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP256R1 );
225+
return( 0 );
226+
#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
227+
#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
228+
case PSA_ECC_CURVE_BRAINPOOL_P384R1:
229+
*oid = MBEDTLS_OID_EC_GRP_BP384R1;
230+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP384R1 );
231+
return( 0 );
232+
#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
233+
#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
234+
case PSA_ECC_CURVE_BRAINPOOL_P512R1:
235+
*oid = MBEDTLS_OID_EC_GRP_BP512R1;
236+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP512R1 );
237+
return( 0 );
238+
#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
239+
default:
240+
(void) oid;
241+
(void) oid_len;
242+
return( -1 );
243+
}
244+
#else
245+
/* Use more up to date Crypto API */
246+
166247
switch( curve )
167248
{
168249
case PSA_ECC_CURVE_SECP_R1:
@@ -250,6 +331,7 @@ static inline int mbedtls_psa_get_ecc_oid_from_id(
250331
(void) oid;
251332
(void) oid_len;
252333
return( -1 );
334+
#endif /* TARGET_TFM */
253335
}
254336

255337
#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH 1
@@ -369,12 +451,16 @@ static inline int mbedtls_psa_err_translate_pk( psa_status_t status )
369451
static inline psa_key_type_t mbedtls_psa_parse_tls_ecc_group(
370452
uint16_t tls_ecc_grp_reg_id, size_t *bits )
371453
{
454+
#if TARGET_TFM
455+
return( (psa_ecc_curve_t) tls_ecc_grp_reg_id );
456+
#else
372457
const mbedtls_ecp_curve_info *curve_info =
373458
mbedtls_ecp_curve_info_from_tls_id( tls_ecc_grp_reg_id );
374459
if( curve_info == NULL )
375460
return( 0 );
376461
return( PSA_KEY_TYPE_ECC_KEY_PAIR(
377462
mbedtls_ecc_group_to_psa( curve_info->grp_id, bits ) ) );
463+
#endif
378464
}
379465
#endif /* MBEDTLS_ECP_C */
380466

features/mbedtls/src/pk.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Public Key abstraction layer
33
*
4-
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
4+
* Copyright (C) 2006-2020, ARM Limited, All Rights Reserved
55
* SPDX-License-Identifier: Apache-2.0
66
*
77
* Licensed under the Apache License, Version 2.0 (the "License"); you may
@@ -617,12 +617,18 @@ 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 );
622-
623-
/* prepare the key attributes */
624-
psa_set_key_type( &attributes, key_type );
629+
#endif
625630
psa_set_key_bits( &attributes, bits );
631+
psa_set_key_type( &attributes, key_type );
626632
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
627633
psa_set_key_algorithm( &attributes, PSA_ALG_ECDSA(hash_alg) );
628634

features/mbedtls/src/pk_wrap.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Public Key abstraction layer: wrapper functions
33
*
4-
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
4+
* Copyright (C) 2006-2020, ARM Limited, All Rights Reserved
55
* SPDX-License-Identifier: Apache-2.0
66
*
77
* Licensed under the Apache License, Version 2.0 (the "License"); you may
@@ -559,8 +559,12 @@ static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
559559
mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
560560
psa_algorithm_t psa_sig_md, psa_md;
561561
size_t curve_bits;
562-
psa_ecc_curve_t curve =
563-
mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
562+
psa_ecc_curve_t curve;
563+
#if TARGET_TFM
564+
curve = mbedtls_ecp_curve_info_from_grp_id( ctx->grp.id )->tls_id;
565+
#else
566+
curve = mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
567+
#endif
564568
const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
565569

566570
if( curve == 0 )

0 commit comments

Comments
 (0)