@@ -654,17 +654,17 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
654
654
mbedtls_ctr_drbg_init ( &ctr_drbg );
655
655
656
656
if ( mbedtls_ctr_drbg_seed ( &ctr_drbg, myrand, NULL , NULL , 0 ) != 0 )
657
- mbedtls_exit (1 );
657
+ return (1 );
658
658
TIME_AND_TSC ( " CTR_DRBG (NOPR)" ,
659
659
if ( mbedtls_ctr_drbg_random ( &ctr_drbg, buf, BUFSIZE ) != 0 )
660
- mbedtls_exit (1 ) );
660
+ return (1 ) );
661
661
662
662
if ( mbedtls_ctr_drbg_seed ( &ctr_drbg, myrand, NULL , NULL , 0 ) != 0 )
663
- mbedtls_exit (1 );
663
+ return (1 );
664
664
mbedtls_ctr_drbg_set_prediction_resistance ( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
665
665
TIME_AND_TSC ( " CTR_DRBG (PR)" ,
666
666
if ( mbedtls_ctr_drbg_random ( &ctr_drbg, buf, BUFSIZE ) != 0 )
667
- mbedtls_exit (1 ) );
667
+ return (1 ) );
668
668
mbedtls_ctr_drbg_free ( &ctr_drbg );
669
669
}
670
670
#endif
@@ -679,43 +679,43 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
679
679
680
680
#if defined(MBEDTLS_SHA1_C)
681
681
if ( ( md_info = mbedtls_md_info_from_type ( MBEDTLS_MD_SHA1 ) ) == NULL )
682
- mbedtls_exit (1 );
682
+ return (1 );
683
683
684
684
if ( mbedtls_hmac_drbg_seed ( &hmac_drbg, md_info, myrand, NULL , NULL , 0 ) != 0 )
685
- mbedtls_exit (1 );
685
+ return (1 );
686
686
TIME_AND_TSC ( " HMAC_DRBG SHA-1 (NOPR)" ,
687
687
if ( mbedtls_hmac_drbg_random ( &hmac_drbg, buf, BUFSIZE ) != 0 )
688
- mbedtls_exit (1 ) );
688
+ return (1 ) );
689
689
mbedtls_hmac_drbg_free ( &hmac_drbg );
690
690
691
691
if ( mbedtls_hmac_drbg_seed ( &hmac_drbg, md_info, myrand, NULL , NULL , 0 ) != 0 )
692
- mbedtls_exit (1 );
692
+ return (1 );
693
693
mbedtls_hmac_drbg_set_prediction_resistance ( &hmac_drbg,
694
694
MBEDTLS_HMAC_DRBG_PR_ON );
695
695
TIME_AND_TSC ( " HMAC_DRBG SHA-1 (PR)" ,
696
696
if ( mbedtls_hmac_drbg_random ( &hmac_drbg, buf, BUFSIZE ) != 0 )
697
- mbedtls_exit (1 ) );
697
+ return (1 ) );
698
698
mbedtls_hmac_drbg_free ( &hmac_drbg );
699
699
#endif
700
700
701
701
#if defined(MBEDTLS_SHA256_C)
702
702
if ( ( md_info = mbedtls_md_info_from_type ( MBEDTLS_MD_SHA256 ) ) == NULL )
703
- mbedtls_exit (1 );
703
+ return (1 );
704
704
705
705
if ( mbedtls_hmac_drbg_seed ( &hmac_drbg, md_info, myrand, NULL , NULL , 0 ) != 0 )
706
- mbedtls_exit (1 );
706
+ return (1 );
707
707
TIME_AND_TSC ( " HMAC_DRBG SHA-256 (NOPR)" ,
708
708
if ( mbedtls_hmac_drbg_random ( &hmac_drbg, buf, BUFSIZE ) != 0 )
709
- mbedtls_exit (1 ) );
709
+ return (1 ) );
710
710
mbedtls_hmac_drbg_free ( &hmac_drbg );
711
711
712
712
if ( mbedtls_hmac_drbg_seed ( &hmac_drbg, md_info, myrand, NULL , NULL , 0 ) != 0 )
713
- mbedtls_exit (1 );
713
+ return (1 );
714
714
mbedtls_hmac_drbg_set_prediction_resistance ( &hmac_drbg,
715
715
MBEDTLS_HMAC_DRBG_PR_ON );
716
716
TIME_AND_TSC ( " HMAC_DRBG SHA-256 (PR)" ,
717
717
if ( mbedtls_hmac_drbg_random ( &hmac_drbg, buf, BUFSIZE ) != 0 )
718
- mbedtls_exit (1 ) );
718
+ return (1 ) );
719
719
mbedtls_hmac_drbg_free ( &hmac_drbg );
720
720
#endif
721
721
}
@@ -774,13 +774,13 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
774
774
if ( mbedtls_mpi_read_string ( &dhm.P , 16 , dhm_P[i] ) != 0 ||
775
775
mbedtls_mpi_read_string ( &dhm.G , 16 , dhm_G[i] ) != 0 )
776
776
{
777
- mbedtls_exit ( 1 );
777
+ return ( 1 );
778
778
}
779
779
780
780
dhm.len = mbedtls_mpi_size ( &dhm.P );
781
781
mbedtls_dhm_make_public ( &dhm, (int ) dhm.len , buf, dhm.len , myrand, NULL );
782
782
if ( mbedtls_mpi_copy ( &dhm.GY , &dhm.GX ) != 0 )
783
- mbedtls_exit ( 1 );
783
+ return ( 1 );
784
784
785
785
mbedtls_snprintf ( title, sizeof ( title ), " DHE-%d" , dhm_sizes[i] );
786
786
TIME_PUBLIC ( title, " handshake" ,
@@ -813,7 +813,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
813
813
mbedtls_ecdsa_init ( &ecdsa );
814
814
815
815
if ( mbedtls_ecdsa_genkey ( &ecdsa, curve_info->grp_id , myrand, NULL ) != 0 )
816
- mbedtls_exit ( 1 );
816
+ return ( 1 );
817
817
ecp_clear_precomputed ( &ecdsa.grp );
818
818
819
819
mbedtls_snprintf ( title, sizeof ( title ), " ECDSA-%s" ,
@@ -835,7 +835,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
835
835
mbedtls_ecdsa_write_signature ( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size ,
836
836
tmp, &sig_len, myrand, NULL ) != 0 )
837
837
{
838
- mbedtls_exit ( 1 );
838
+ return ( 1 );
839
839
}
840
840
ecp_clear_precomputed ( &ecdsa.grp );
841
841
@@ -871,7 +871,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
871
871
myrand, NULL ) != 0 ||
872
872
mbedtls_ecp_copy ( &ecdh.Qp , &ecdh.Q ) != 0 )
873
873
{
874
- mbedtls_exit ( 1 );
874
+ return ( 1 );
875
875
}
876
876
ecp_clear_precomputed ( &ecdh.grp );
877
877
@@ -893,7 +893,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
893
893
if ( mbedtls_ecp_group_load ( &ecdh.grp , MBEDTLS_ECP_DP_CURVE25519 ) != 0 ||
894
894
mbedtls_ecdh_gen_public ( &ecdh.grp , &ecdh.d , &ecdh.Qp , myrand, NULL ) != 0 )
895
895
{
896
- mbedtls_exit ( 1 );
896
+ return ( 1 );
897
897
}
898
898
899
899
TIME_PUBLIC ( " ECDHE-Curve25519" , " handshake" ,
@@ -919,7 +919,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
919
919
mbedtls_ecdh_make_public ( &ecdh, &olen, buf, sizeof ( buf ),
920
920
myrand, NULL ) != 0 )
921
921
{
922
- mbedtls_exit ( 1 );
922
+ return ( 1 );
923
923
}
924
924
ecp_clear_precomputed ( &ecdh.grp );
925
925
@@ -941,7 +941,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
941
941
myrand, NULL ) != 0 ||
942
942
mbedtls_ecdh_gen_public ( &ecdh.grp , &ecdh.d , &ecdh.Q , myrand, NULL ) != 0 )
943
943
{
944
- mbedtls_exit ( 1 );
944
+ return ( 1 );
945
945
}
946
946
947
947
TIME_PUBLIC ( " ECDH-Curve25519" , " handshake" ,
@@ -965,13 +965,19 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
965
965
966
966
int main (void ) {
967
967
mbedtls_platform_context platform_ctx;
968
- if (mbedtls_platform_setup (&platform_ctx) != 0 ) {
969
- return -1 ;
968
+ int exit_code = MBEDTLS_EXIT_FAILURE;
969
+
970
+ if ((exit_code = mbedtls_platform_setup (&platform_ctx)) != 0 ) {
971
+ printf (" Platform initialization failed with error %d\r\n " , exit_code);
972
+ return MBEDTLS_EXIT_FAILURE;
970
973
}
971
974
972
- int ret = benchmark (0 , NULL , &platform_ctx);
973
- if (ret != 0 ) {
974
- mbedtls_printf (" Benchmark failed with error %d\r\n " , ret);
975
+ exit_code = benchmark (0 , NULL , &platform_ctx);
976
+ if (exit_code != 0 ) {
977
+ mbedtls_printf (" Benchmark failed with error %d\r\n " , exit_code);
978
+ exit_code = MBEDTLS_EXIT_FAILURE;
975
979
}
980
+
976
981
mbedtls_platform_teardown (&platform_ctx);
982
+ return exit_code;
977
983
}
0 commit comments