Skip to content

Commit 59d3b3b

Browse files
Factor common code of psa_import_ec_{public,private}_key
1 parent ef1726c commit 59d3b3b

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

library/psa_crypto.c

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,19 @@ 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+
*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+
}
587600

588601
/* Import a public key given as the uncompressed representation defined by SEC1
589602
* 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,
594607
{
595608
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
596609
mbedtls_ecp_keypair *ecp = NULL;
597-
mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
598610

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 );
608612
if( status != PSA_SUCCESS )
609613
goto exit;
614+
610615
/* Load the public value. */
611616
status = mbedtls_to_psa_error(
612617
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,
631636
}
632637
return( status );
633638
}
634-
#endif /* defined(MBEDTLS_ECP_C) */
635639

636-
#if defined(MBEDTLS_ECP_C)
637640
/* Import a private key given as a byte string which is the private value
638641
* in big-endian order. */
639642
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,
643646
{
644647
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
645648
mbedtls_ecp_keypair *ecp = NULL;
646-
mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
647649

648650
if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
649651
return( PSA_ERROR_INVALID_ARGUMENT );
650652

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 );
660654
if( status != PSA_SUCCESS )
661655
goto exit;
656+
662657
/* Load the secret value. */
663658
status = mbedtls_to_psa_error(
664659
mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );

0 commit comments

Comments
 (0)