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