Skip to content

Commit 4cd3277

Browse files
Factor common code of psa_import_ec_{public,private}_key
1 parent 46c3380 commit 4cd3277

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

library/psa_crypto.c

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,20 @@ static psa_status_t psa_import_rsa_key( psa_key_type_t type,
584584
#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
585585

586586
#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+
}
587601

588602
/* Import a public key given as the uncompressed representation defined by SEC1
589603
* 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,
594608
{
595609
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
596610
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 );
604611

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 );
608613
if( status != PSA_SUCCESS )
609614
goto exit;
615+
610616
/* Load the public value. */
611617
status = mbedtls_to_psa_error(
612618
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,
631637
}
632638
return( status );
633639
}
634-
#endif /* defined(MBEDTLS_ECP_C) */
635640

636-
#if defined(MBEDTLS_ECP_C)
637641
/* Import a private key given as a byte string which is the private value
638642
* in big-endian order. */
639643
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,
643647
{
644648
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
645649
mbedtls_ecp_keypair *ecp = NULL;
646-
mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
647650

648651
if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
649652
return( PSA_ERROR_INVALID_ARGUMENT );
650653

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 );
660655
if( status != PSA_SUCCESS )
661656
goto exit;
657+
662658
/* Load the secret value. */
663659
status = mbedtls_to_psa_error(
664660
mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );

0 commit comments

Comments
 (0)