@@ -537,25 +537,53 @@ static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
537
537
}
538
538
#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
539
539
540
- #if defined(MBEDTLS_ECP_C ) && defined(MBEDTLS_PK_PARSE_C )
541
- /* Import an elliptic curve parsed by the mbedtls pk module. */
542
- static psa_status_t psa_import_ecp_key ( psa_ecc_curve_t expected_curve ,
543
- mbedtls_pk_context * pk ,
544
- mbedtls_ecp_keypair * * p_ecp )
540
+ #if defined(MBEDTLS_ECP_C )
541
+ /* Import a public key given as a byte string which is a SEC1 2.3.3 ECPoint. */
542
+ static psa_status_t psa_import_ec_public_key ( psa_ecc_curve_t curve ,
543
+ const uint8_t * data ,
544
+ size_t data_length ,
545
+ mbedtls_ecp_keypair * * p_ecp )
545
546
{
546
- if ( mbedtls_pk_get_type ( pk ) != MBEDTLS_PK_ECKEY )
547
- return ( PSA_ERROR_INVALID_ARGUMENT );
548
- else
547
+ psa_status_t status = PSA_ERROR_TAMPERING_DETECTED ;
548
+ mbedtls_ecp_keypair * ecp = NULL ;
549
+ mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa ( curve );
550
+
551
+ * p_ecp = NULL ;
552
+ ecp = mbedtls_calloc ( 1 , sizeof ( * ecp ) );
553
+ if ( ecp == NULL )
554
+ return ( PSA_ERROR_INSUFFICIENT_MEMORY );
555
+ mbedtls_ecp_keypair_init ( ecp );
556
+
557
+ /* Load the group. */
558
+ status = mbedtls_to_psa_error (
559
+ mbedtls_ecp_group_load ( & ecp -> grp , grp_id ) );
560
+ if ( status != PSA_SUCCESS )
561
+ goto exit ;
562
+ /* Load the public value. */
563
+ status = mbedtls_to_psa_error (
564
+ mbedtls_ecp_point_read_binary ( & ecp -> grp , & ecp -> Q ,
565
+ data , data_length ) );
566
+ if ( status != PSA_SUCCESS )
567
+ goto exit ;
568
+
569
+ /* Check that the point belongs to the group. */
570
+ status = mbedtls_to_psa_error (
571
+ mbedtls_ecp_check_pubkey ( & ecp -> grp , & ecp -> Q ) );
572
+ if ( status != PSA_SUCCESS )
573
+ goto exit ;
574
+
575
+ * p_ecp = ecp ;
576
+ return ( PSA_SUCCESS );
577
+
578
+ exit :
579
+ if ( ecp != NULL )
549
580
{
550
- mbedtls_ecp_keypair * ecp = mbedtls_pk_ec ( * pk );
551
- psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa ( ecp -> grp .id );
552
- if ( actual_curve != expected_curve )
553
- return ( PSA_ERROR_INVALID_ARGUMENT );
554
- * p_ecp = ecp ;
555
- return ( PSA_SUCCESS );
581
+ mbedtls_ecp_keypair_free ( ecp );
582
+ mbedtls_free ( ecp );
556
583
}
584
+ return ( status );
557
585
}
558
- #endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
586
+ #endif /* defined(MBEDTLS_ECP_C) */
559
587
560
588
#if defined(MBEDTLS_ECP_C )
561
589
/* Import a private key given as a byte string which is the private value
@@ -642,11 +670,20 @@ psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
642
670
if ( status != PSA_SUCCESS )
643
671
return ( status );
644
672
}
673
+ else if ( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY ( slot -> type ) )
674
+ {
675
+ status = psa_import_ec_public_key (
676
+ PSA_KEY_TYPE_GET_CURVE ( slot -> type ),
677
+ data , data_length ,
678
+ & slot -> data .ecp );
679
+
680
+ if ( status != PSA_SUCCESS )
681
+ return ( status );
682
+ }
645
683
else
646
684
#endif /* MBEDTLS_ECP_C */
647
- #if defined(MBEDTLS_PK_PARSE_C )
648
- if ( PSA_KEY_TYPE_IS_RSA ( slot -> type ) ||
649
- PSA_KEY_TYPE_IS_ECC ( slot -> type ) )
685
+ #if defined(MBEDTLS_RSA_C ) && defined(MBEDTLS_PK_PARSE_C )
686
+ if ( PSA_KEY_TYPE_IS_RSA ( slot -> type ) )
650
687
{
651
688
int ret ;
652
689
mbedtls_pk_context pk ;
@@ -663,20 +700,7 @@ psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
663
700
/* We have something that the pkparse module recognizes.
664
701
* If it has the expected type and passes any type-specific
665
702
* checks, store it. */
666
- #if defined(MBEDTLS_RSA_C )
667
- if ( PSA_KEY_TYPE_IS_RSA ( slot -> type ) )
668
- status = psa_import_rsa_key ( & pk , & slot -> data .rsa );
669
- else
670
- #endif /* MBEDTLS_RSA_C */
671
- #if defined(MBEDTLS_ECP_C )
672
- if ( PSA_KEY_TYPE_IS_ECC ( slot -> type ) )
673
- status = psa_import_ecp_key ( PSA_KEY_TYPE_GET_CURVE ( slot -> type ),
674
- & pk , & slot -> data .ecp );
675
- else
676
- #endif /* MBEDTLS_ECP_C */
677
- {
678
- status = PSA_ERROR_NOT_SUPPORTED ;
679
- }
703
+ status = psa_import_rsa_key ( & pk , & slot -> data .rsa );
680
704
681
705
/* Free the content of the pk object only on error. On success,
682
706
* the content of the object has been stored in the slot. */
@@ -687,7 +711,7 @@ psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
687
711
}
688
712
}
689
713
else
690
- #endif /* defined(MBEDTLS_PK_PARSE_C) */
714
+ #endif /* defined(MBEDTLS_RSA_C) && defined( MBEDTLS_PK_PARSE_C) */
691
715
{
692
716
return ( PSA_ERROR_NOT_SUPPORTED );
693
717
}
@@ -900,7 +924,7 @@ psa_status_t psa_get_key_information( psa_key_handle_t handle,
900
924
return ( PSA_SUCCESS );
901
925
}
902
926
903
- #if defined(MBEDTLS_RSA_C )
927
+ #if defined(MBEDTLS_RSA_C ) || defined( MBEDTLS_ECP_C )
904
928
static int pk_write_pubkey_simple ( mbedtls_pk_context * key ,
905
929
unsigned char * buf , size_t size )
906
930
{
@@ -914,7 +938,7 @@ static int pk_write_pubkey_simple( mbedtls_pk_context *key,
914
938
915
939
return ( (int ) len );
916
940
}
917
- #endif /* defined(MBEDTLS_RSA_C) */
941
+ #endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
918
942
919
943
static psa_status_t psa_internal_export_key ( psa_key_slot_t * slot ,
920
944
uint8_t * data ,
@@ -987,14 +1011,7 @@ static psa_status_t psa_internal_export_key( psa_key_slot_t *slot,
987
1011
}
988
1012
if ( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY ( slot -> type ) )
989
1013
{
990
- if ( PSA_KEY_TYPE_IS_RSA ( slot -> type ) )
991
- {
992
- ret = pk_write_pubkey_simple ( & pk , data , data_size );
993
- }
994
- else
995
- {
996
- ret = mbedtls_pk_write_pubkey_der ( & pk , data , data_size );
997
- }
1014
+ ret = pk_write_pubkey_simple ( & pk , data , data_size );
998
1015
}
999
1016
else
1000
1017
{
@@ -4041,32 +4058,17 @@ static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4041
4058
size_t shared_secret_size ,
4042
4059
size_t * shared_secret_length )
4043
4060
{
4044
- mbedtls_pk_context pk ;
4045
4061
mbedtls_ecp_keypair * their_key = NULL ;
4046
4062
mbedtls_ecdh_context ecdh ;
4047
4063
psa_status_t status ;
4048
4064
mbedtls_ecdh_init ( & ecdh );
4049
- mbedtls_pk_init ( & pk );
4050
4065
4051
- status = mbedtls_to_psa_error (
4052
- mbedtls_pk_parse_public_key ( & pk , peer_key , peer_key_length ) );
4066
+ status = psa_import_ec_public_key (
4067
+ mbedtls_ecc_group_to_psa ( our_key -> grp .id ),
4068
+ peer_key , peer_key_length ,
4069
+ & their_key );
4053
4070
if ( status != PSA_SUCCESS )
4054
4071
goto exit ;
4055
- switch ( mbedtls_pk_get_type ( & pk ) )
4056
- {
4057
- case MBEDTLS_PK_ECKEY :
4058
- case MBEDTLS_PK_ECKEY_DH :
4059
- break ;
4060
- default :
4061
- status = PSA_ERROR_INVALID_ARGUMENT ;
4062
- goto exit ;
4063
- }
4064
- their_key = mbedtls_pk_ec ( pk );
4065
- if ( their_key -> grp .id != our_key -> grp .id )
4066
- {
4067
- status = PSA_ERROR_INVALID_ARGUMENT ;
4068
- goto exit ;
4069
- }
4070
4072
4071
4073
status = mbedtls_to_psa_error (
4072
4074
mbedtls_ecdh_get_params ( & ecdh , their_key , MBEDTLS_ECDH_THEIRS ) );
@@ -4084,8 +4086,9 @@ static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4084
4086
& global_data .ctr_drbg ) );
4085
4087
4086
4088
exit :
4087
- mbedtls_pk_free ( & pk );
4088
4089
mbedtls_ecdh_free ( & ecdh );
4090
+ mbedtls_ecp_keypair_free ( their_key );
4091
+ mbedtls_free ( their_key );
4089
4092
return ( status );
4090
4093
}
4091
4094
#endif /* MBEDTLS_ECDH_C */
0 commit comments