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