@@ -584,6 +584,20 @@ static psa_status_t psa_import_rsa_key( psa_key_type_t type,
584
584
#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
585
585
586
586
#if defined(MBEDTLS_ECP_C )
587
+ static psa_status_t psa_prepare_import_ec_key ( psa_ecc_curve_t curve ,
588
+ mbedtls_ecp_keypair * * p_ecp )
589
+ {
590
+ mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE ;
591
+ * p_ecp = mbedtls_calloc ( 1 , sizeof ( mbedtls_ecp_keypair ) );
592
+ if ( * p_ecp == NULL )
593
+ return ( PSA_ERROR_INSUFFICIENT_MEMORY );
594
+ mbedtls_ecp_keypair_init ( * p_ecp );
595
+
596
+ /* Load the group. */
597
+ grp_id = mbedtls_ecc_group_of_psa ( curve );
598
+ return ( mbedtls_to_psa_error (
599
+ mbedtls_ecp_group_load ( & ( * p_ecp )-> grp , grp_id ) ) );
600
+ }
587
601
588
602
/* Import a public key given as the uncompressed representation defined by SEC1
589
603
* 2.3.3 as the content of an ECPoint. */
@@ -594,19 +608,11 @@ static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
594
608
{
595
609
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED ;
596
610
mbedtls_ecp_keypair * ecp = NULL ;
597
- mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa ( curve );
598
-
599
- * p_ecp = NULL ;
600
- ecp = mbedtls_calloc ( 1 , sizeof ( * ecp ) );
601
- if ( ecp == NULL )
602
- return ( PSA_ERROR_INSUFFICIENT_MEMORY );
603
- mbedtls_ecp_keypair_init ( ecp );
604
611
605
- /* Load the group. */
606
- status = mbedtls_to_psa_error (
607
- mbedtls_ecp_group_load ( & ecp -> grp , grp_id ) );
612
+ status = psa_prepare_import_ec_key ( curve , & ecp );
608
613
if ( status != PSA_SUCCESS )
609
614
goto exit ;
615
+
610
616
/* Load the public value. */
611
617
status = mbedtls_to_psa_error (
612
618
mbedtls_ecp_point_read_binary ( & ecp -> grp , & ecp -> Q ,
@@ -631,9 +637,7 @@ static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
631
637
}
632
638
return ( status );
633
639
}
634
- #endif /* defined(MBEDTLS_ECP_C) */
635
640
636
- #if defined(MBEDTLS_ECP_C )
637
641
/* Import a private key given as a byte string which is the private value
638
642
* in big-endian order. */
639
643
static psa_status_t psa_import_ec_private_key ( psa_ecc_curve_t curve ,
@@ -643,22 +647,14 @@ static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
643
647
{
644
648
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED ;
645
649
mbedtls_ecp_keypair * ecp = NULL ;
646
- mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa ( curve );
647
650
648
651
if ( PSA_BITS_TO_BYTES ( PSA_ECC_CURVE_BITS ( curve ) ) != data_length )
649
652
return ( PSA_ERROR_INVALID_ARGUMENT );
650
653
651
- * p_ecp = NULL ;
652
- ecp = mbedtls_calloc ( 1 , sizeof ( mbedtls_ecp_keypair ) );
653
- if ( ecp == NULL )
654
- return ( PSA_ERROR_INSUFFICIENT_MEMORY );
655
- mbedtls_ecp_keypair_init ( ecp );
656
-
657
- /* Load the group. */
658
- status = mbedtls_to_psa_error (
659
- mbedtls_ecp_group_load ( & ecp -> grp , grp_id ) );
654
+ status = psa_prepare_import_ec_key ( curve , & ecp );
660
655
if ( status != PSA_SUCCESS )
661
656
goto exit ;
657
+
662
658
/* Load the secret value. */
663
659
status = mbedtls_to_psa_error (
664
660
mbedtls_mpi_read_binary ( & ecp -> d , data , data_length ) );
0 commit comments