Merge pull request #3512 from gilles-peskine-arm/ecp-alloc-202007

Reduce the number of allocations in ECP operations
This commit is contained in:
Gilles Peskine 2021-04-02 00:08:35 +02:00 committed by GitHub
commit d5200371ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 156 additions and 87 deletions

View file

@ -266,6 +266,21 @@ void ecp_clear_precomputed( mbedtls_ecp_group *grp )
#define ecp_clear_precomputed( g )
#endif
#if defined(MBEDTLS_ECP_C)
static int set_ecp_curve( const char *string, mbedtls_ecp_curve_info *curve )
{
const mbedtls_ecp_curve_info *found =
mbedtls_ecp_curve_info_from_name( string );
if( found != NULL )
{
*curve = *found;
return( 1 );
}
else
return( 0 );
}
#endif
unsigned char buf[BUFSIZE];
typedef struct {
@ -289,6 +304,17 @@ int main( int argc, char *argv[] )
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
unsigned char alloc_buf[HEAP_SIZE] = { 0 };
#endif
#if defined(MBEDTLS_ECP_C)
mbedtls_ecp_curve_info single_curve[2] = {
{ MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
{ MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
};
const mbedtls_ecp_curve_info *curve_list = mbedtls_ecp_curve_list( );
#endif
#if defined(MBEDTLS_ECP_C)
(void) curve_list; /* Unused in some configurations where no benchmark uses ECC */
#endif
if( argc <= 1 )
{
@ -356,6 +382,10 @@ int main( int argc, char *argv[] )
todo.ecdsa = 1;
else if( strcmp( argv[i], "ecdh" ) == 0 )
todo.ecdh = 1;
#if defined(MBEDTLS_ECP_C)
else if( set_ecp_curve( argv[i], single_curve ) )
curve_list = single_curve;
#endif
else
{
mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
@ -845,7 +875,7 @@ int main( int argc, char *argv[] )
memset( buf, 0x2A, sizeof( buf ) );
for( curve_info = mbedtls_ecp_curve_list();
for( curve_info = curve_list;
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++ )
{
@ -867,7 +897,7 @@ int main( int argc, char *argv[] )
mbedtls_ecdsa_free( &ecdsa );
}
for( curve_info = mbedtls_ecp_curve_list();
for( curve_info = curve_list;
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++ )
{
@ -911,8 +941,23 @@ int main( int argc, char *argv[] )
};
const mbedtls_ecp_curve_info *curve_info;
size_t olen;
const mbedtls_ecp_curve_info *selected_montgomery_curve_list =
montgomery_curve_list;
for( curve_info = mbedtls_ecp_curve_list();
if( curve_list == (const mbedtls_ecp_curve_info*) &single_curve )
{
mbedtls_ecp_group grp;
mbedtls_ecp_group_init( &grp );
if( mbedtls_ecp_group_load( &grp, curve_list->grp_id ) != 0 )
mbedtls_exit( 1 );
if( mbedtls_ecp_get_type( &grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
selected_montgomery_curve_list = single_curve;
else /* empty list */
selected_montgomery_curve_list = single_curve + 1;
mbedtls_ecp_group_free( &grp );
}
for( curve_info = curve_list;
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++ )
{
@ -938,7 +983,7 @@ int main( int argc, char *argv[] )
}
/* Montgomery curves need to be handled separately */
for ( curve_info = montgomery_curve_list;
for ( curve_info = selected_montgomery_curve_list;
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++ )
{
@ -960,7 +1005,7 @@ int main( int argc, char *argv[] )
mbedtls_mpi_free( &z );
}
for( curve_info = mbedtls_ecp_curve_list();
for( curve_info = curve_list;
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++ )
{
@ -986,7 +1031,7 @@ int main( int argc, char *argv[] )
}
/* Montgomery curves need to be handled separately */
for ( curve_info = montgomery_curve_list;
for ( curve_info = selected_montgomery_curve_list;
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++)
{
@ -1015,7 +1060,6 @@ int main( int argc, char *argv[] )
{
mbedtls_ecdh_context ecdh_srv, ecdh_cli;
unsigned char buf_srv[BUFSIZE], buf_cli[BUFSIZE];
const mbedtls_ecp_curve_info * curve_list = mbedtls_ecp_curve_list();
const mbedtls_ecp_curve_info *curve_info;
size_t olen;