Skip to content

Commit 33786fe

Browse files
Ron EldorRon Eldor
Ron Eldor
authored and
Ron Eldor
committed
make test function return error code
Have the test functions return error code, to avoid resource leak of the platform context, and for cleaner exit.
1 parent d86a33b commit 33786fe

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

benchmark/main.cpp

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ typedef struct {
300300
rsa, dhm, ecdsa, ecdh;
301301
} todo_list;
302302

303-
static void test_md( const todo_list * todo, mbedtls_platform_context* ctx )
303+
static int test_md( const todo_list * todo, mbedtls_platform_context* ctx )
304304
{
305305
unsigned char tmp[200];
306306
// The call below is used to avoid the "unused parameter" warning.
@@ -338,9 +338,10 @@ static void test_md( const todo_list * todo, mbedtls_platform_context* ctx )
338338
if( todo->sha512 )
339339
TIME_AND_TSC( "SHA-512", mbedtls_sha512( buf, BUFSIZE, tmp, 0 ) );
340340
#endif
341+
return ( 0 );
341342
}
342343

343-
static void test_crypt( const todo_list * todo, mbedtls_platform_context* ctx )
344+
static int test_crypt( const todo_list * todo, mbedtls_platform_context* ctx )
344345
{
345346
unsigned char tmp[200];
346347
char title[TITLE_LEN];
@@ -569,9 +570,10 @@ static void test_crypt( const todo_list * todo, mbedtls_platform_context* ctx )
569570
}
570571
#endif
571572

573+
return ( 0 );
572574
}
573575

574-
static void test_rng( const todo_list * todo, mbedtls_platform_context* ctx )
576+
static int test_rng( const todo_list * todo, mbedtls_platform_context* ctx )
575577
{
576578
unsigned char tmp[200];
577579
// The call below is used to avoid the "unused parameter" warning.
@@ -598,17 +600,17 @@ static void test_rng( const todo_list * todo, mbedtls_platform_context* ctx )
598600
mbedtls_ctr_drbg_init( &ctr_drbg );
599601

600602
if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
601-
mbedtls_exit(1);
603+
return(1);
602604
TIME_AND_TSC( "CTR_DRBG (NOPR)",
603605
if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
604-
mbedtls_exit(1) );
606+
return(1) );
605607

606608
if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
607-
mbedtls_exit(1);
609+
return(1);
608610
mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
609611
TIME_AND_TSC( "CTR_DRBG (PR)",
610612
if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
611-
mbedtls_exit(1) );
613+
return(1) );
612614
mbedtls_ctr_drbg_free( &ctr_drbg );
613615
}
614616
#endif
@@ -623,50 +625,51 @@ static void test_rng( const todo_list * todo, mbedtls_platform_context* ctx )
623625

624626
#if defined(MBEDTLS_SHA1_C)
625627
if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
626-
mbedtls_exit(1);
628+
return(1);
627629

628630
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
629631
return(1);
630632
TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
631633
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
632-
mbedtls_exit(1) );
634+
return(1) );
633635
mbedtls_hmac_drbg_free( &hmac_drbg );
634636

635637
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
636-
mbedtls_exit(1);
638+
return(1);
637639
mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
638640
MBEDTLS_HMAC_DRBG_PR_ON );
639641
TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
640642
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
641-
mbedtls_exit(1) );
643+
return(1) );
642644
mbedtls_hmac_drbg_free( &hmac_drbg );
643645
#endif
644646

645647
#if defined(MBEDTLS_SHA256_C)
646648
if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
647-
mbedtls_exit(1);
649+
return(1);
648650

649651
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
650-
mbedtls_exit(1);
652+
return(1);
651653
TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
652654
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
653-
mbedtls_exit(1) );
655+
return(1) );
654656
mbedtls_hmac_drbg_free( &hmac_drbg );
655657

656658
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
657-
mbedtls_exit(1);
659+
return(1);
658660
mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
659661
MBEDTLS_HMAC_DRBG_PR_ON );
660662
TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
661663
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
662-
mbedtls_exit(1) );
664+
return(1) );
663665
mbedtls_hmac_drbg_free( &hmac_drbg );
664666
#endif
665667
}
666668
#endif
669+
return (0 );
667670
}
668671

669-
static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx )
672+
static int test_pk( const todo_list * todo, mbedtls_platform_context* ctx )
670673
{
671674
unsigned char tmp[200];
672675
char title[TITLE_LEN];
@@ -729,13 +732,13 @@ static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx )
729732
if( mbedtls_mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 ||
730733
mbedtls_mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 )
731734
{
732-
mbedtls_exit( 1 );
735+
return( 1 );
733736
}
734737

735738
dhm.len = mbedtls_mpi_size( &dhm.P );
736739
mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
737740
if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
738-
mbedtls_exit( 1 );
741+
return( 1 );
739742

740743
mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
741744
TIME_PUBLIC( title, "handshake",
@@ -768,7 +771,7 @@ static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx )
768771
mbedtls_ecdsa_init( &ecdsa );
769772

770773
if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
771-
mbedtls_exit( 1 );
774+
return( 1 );
772775
ecp_clear_precomputed( &ecdsa.grp );
773776

774777
mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
@@ -790,7 +793,7 @@ static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx )
790793
mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, ( curve_info->bit_size + 7 ) / 8,
791794
tmp, &sig_len, myrand, NULL ) != 0 )
792795
{
793-
mbedtls_exit( 1 );
796+
return( 1 );
794797
}
795798
ecp_clear_precomputed( &ecdsa.grp );
796799

@@ -826,7 +829,7 @@ static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx )
826829
myrand, NULL ) != 0 ||
827830
mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
828831
{
829-
mbedtls_exit( 1 );
832+
return( 1 );
830833
}
831834
ecp_clear_precomputed( &ecdh.grp );
832835

@@ -848,7 +851,7 @@ static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx )
848851
if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 ||
849852
mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
850853
{
851-
mbedtls_exit( 1 );
854+
return( 1 );
852855
}
853856

854857
TIME_PUBLIC( "ECDHE-Curve25519", "handshake",
@@ -874,7 +877,7 @@ static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx )
874877
mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf ),
875878
myrand, NULL ) != 0 )
876879
{
877-
mbedtls_exit( 1 );
880+
return( 1 );
878881
}
879882
ecp_clear_precomputed( &ecdh.grp );
880883

@@ -896,7 +899,7 @@ static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx )
896899
myrand, NULL ) != 0 ||
897900
mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
898901
{
899-
mbedtls_exit( 1 );
902+
return( 1 );
900903
}
901904

902905
TIME_PUBLIC( "ECDH-Curve25519", "handshake",
@@ -908,7 +911,7 @@ static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx )
908911
#endif
909912
}
910913
#endif
911-
914+
return ( 0 );
912915

913916
}
914917

@@ -993,10 +996,14 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
993996
#endif
994997
memset( buf, 0xAA, sizeof( buf ) );
995998

996-
test_md( &todo, ctx );
997-
test_crypt( &todo, ctx );
998-
test_rng( &todo, ctx );
999-
test_pk( &todo, ctx );
999+
if( test_md( &todo, ctx ) != 0)
1000+
return ( 1 );
1001+
if( test_crypt( &todo, ctx ) != 0)
1002+
return ( 1 );
1003+
if( test_rng( &todo, ctx ) != 0)
1004+
return ( 1 );
1005+
if( test_pk( &todo, ctx ) != 0)
1006+
return ( 1 );
10001007

10011008
mbedtls_printf("\r\nDONE\r\n");
10021009

0 commit comments

Comments
 (0)