Skip to content

Commit 006a991

Browse files
author
Andrzej Kurek
committed
Add return value printing for failed platform initialization
Unify error handling in tests
1 parent 307117a commit 006a991

File tree

4 files changed

+59
-41
lines changed

4 files changed

+59
-41
lines changed

authcrypt/main.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@
2525

2626
int main() {
2727
mbedtls_platform_context platform_ctx;
28-
if(mbedtls_platform_setup(&platform_ctx) != 0) {
29-
return -1;
28+
int exit_code = MBEDTLS_EXIT_FAILURE;
29+
30+
if((exit_code = mbedtls_platform_setup(&platform_ctx)) != 0) {
31+
printf("Platform initialization failed with error %d\r\n", exit_code);
32+
return MBEDTLS_EXIT_FAILURE;
3033
}
3134

32-
int exit_code = MBEDTLS_EXIT_SUCCESS;
3335
Authcrypt *authcrypt = new Authcrypt(&platform_ctx);
3436

35-
if (authcrypt->run() != 0) {
37+
if ((exit_code = authcrypt->run()) != 0) {
38+
mbedtls_printf("Example failed with error %d\r\n", exit_code);
3639
exit_code = MBEDTLS_EXIT_FAILURE;
37-
mbedtls_printf("\r\nFAIL\r\n");
3840
}
3941

4042
delete authcrypt;

benchmark/main.cpp

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -654,17 +654,17 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
654654
mbedtls_ctr_drbg_init( &ctr_drbg );
655655

656656
if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
657-
mbedtls_exit(1);
657+
return(1);
658658
TIME_AND_TSC( "CTR_DRBG (NOPR)",
659659
if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
660-
mbedtls_exit(1) );
660+
return(1) );
661661

662662
if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
663-
mbedtls_exit(1);
663+
return(1);
664664
mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
665665
TIME_AND_TSC( "CTR_DRBG (PR)",
666666
if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
667-
mbedtls_exit(1) );
667+
return(1) );
668668
mbedtls_ctr_drbg_free( &ctr_drbg );
669669
}
670670
#endif
@@ -679,43 +679,43 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
679679

680680
#if defined(MBEDTLS_SHA1_C)
681681
if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
682-
mbedtls_exit(1);
682+
return(1);
683683

684684
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
685-
mbedtls_exit(1);
685+
return(1);
686686
TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
687687
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
688-
mbedtls_exit(1) );
688+
return(1) );
689689
mbedtls_hmac_drbg_free( &hmac_drbg );
690690

691691
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
692-
mbedtls_exit(1);
692+
return(1);
693693
mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
694694
MBEDTLS_HMAC_DRBG_PR_ON );
695695
TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
696696
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
697-
mbedtls_exit(1) );
697+
return(1) );
698698
mbedtls_hmac_drbg_free( &hmac_drbg );
699699
#endif
700700

701701
#if defined(MBEDTLS_SHA256_C)
702702
if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
703-
mbedtls_exit(1);
703+
return(1);
704704

705705
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
706-
mbedtls_exit(1);
706+
return(1);
707707
TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
708708
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
709-
mbedtls_exit(1) );
709+
return(1) );
710710
mbedtls_hmac_drbg_free( &hmac_drbg );
711711

712712
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
713-
mbedtls_exit(1);
713+
return(1);
714714
mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
715715
MBEDTLS_HMAC_DRBG_PR_ON );
716716
TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
717717
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
718-
mbedtls_exit(1) );
718+
return(1) );
719719
mbedtls_hmac_drbg_free( &hmac_drbg );
720720
#endif
721721
}
@@ -774,13 +774,13 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
774774
if( mbedtls_mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 ||
775775
mbedtls_mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 )
776776
{
777-
mbedtls_exit( 1 );
777+
return( 1 );
778778
}
779779

780780
dhm.len = mbedtls_mpi_size( &dhm.P );
781781
mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
782782
if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
783-
mbedtls_exit( 1 );
783+
return( 1 );
784784

785785
mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
786786
TIME_PUBLIC( title, "handshake",
@@ -813,7 +813,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
813813
mbedtls_ecdsa_init( &ecdsa );
814814

815815
if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
816-
mbedtls_exit( 1 );
816+
return( 1 );
817817
ecp_clear_precomputed( &ecdsa.grp );
818818

819819
mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
@@ -835,7 +835,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
835835
mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
836836
tmp, &sig_len, myrand, NULL ) != 0 )
837837
{
838-
mbedtls_exit( 1 );
838+
return( 1 );
839839
}
840840
ecp_clear_precomputed( &ecdsa.grp );
841841

@@ -871,7 +871,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
871871
myrand, NULL ) != 0 ||
872872
mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
873873
{
874-
mbedtls_exit( 1 );
874+
return( 1 );
875875
}
876876
ecp_clear_precomputed( &ecdh.grp );
877877

@@ -893,7 +893,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
893893
if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 ||
894894
mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
895895
{
896-
mbedtls_exit( 1 );
896+
return( 1 );
897897
}
898898

899899
TIME_PUBLIC( "ECDHE-Curve25519", "handshake",
@@ -919,7 +919,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
919919
mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf ),
920920
myrand, NULL ) != 0 )
921921
{
922-
mbedtls_exit( 1 );
922+
return( 1 );
923923
}
924924
ecp_clear_precomputed( &ecdh.grp );
925925

@@ -941,7 +941,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
941941
myrand, NULL ) != 0 ||
942942
mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
943943
{
944-
mbedtls_exit( 1 );
944+
return( 1 );
945945
}
946946

947947
TIME_PUBLIC( "ECDH-Curve25519", "handshake",
@@ -965,13 +965,19 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
965965

966966
int main(void) {
967967
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;
970973
}
971974

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;
975979
}
980+
976981
mbedtls_platform_teardown(&platform_ctx);
982+
return exit_code;
977983
}

hashing/main.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,19 @@ static int example(mbedtls_platform_context* ctx)
158158

159159
int main() {
160160
mbedtls_platform_context platform_ctx;
161-
if(mbedtls_platform_setup(&platform_ctx) != 0) {
162-
return -1;
161+
int exit_code = MBEDTLS_EXIT_FAILURE;
162+
163+
if((exit_code = mbedtls_platform_setup(&platform_ctx)) != 0) {
164+
printf("Platform initialization failed with error %d\r\n", exit_code);
165+
return MBEDTLS_EXIT_FAILURE;
163166
}
164167

165-
int ret = example(&platform_ctx);
166-
if (ret != 0) {
167-
mbedtls_printf("Example failed with error %d\r\n", ret);
168+
exit_code = example(&platform_ctx);
169+
if (exit_code != 0) {
170+
mbedtls_printf("Example failed with error %d\r\n", exit_code);
171+
exit_code = MBEDTLS_EXIT_FAILURE;
168172
}
173+
169174
mbedtls_platform_teardown(&platform_ctx);
175+
return exit_code;
170176
}

tls-client/main.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,11 @@ class HelloHTTPS {
417417
*/
418418
int main() {
419419
mbedtls_platform_context platform_ctx;
420-
if(mbedtls_platform_setup(&platform_ctx) != 0) {
421-
return -1;
420+
int ret = MBEDTLS_EXIT_FAILURE;
421+
422+
if((ret = mbedtls_platform_setup(&platform_ctx)) != 0) {
423+
printf("Platform initialization failed with error %d\r\n", ret);
424+
return MBEDTLS_EXIT_FAILURE;
422425
}
423426
/* The default 9600 bps is too slow to print full TLS debug info and could
424427
* cause the other party to time out. */
@@ -441,12 +444,13 @@ int main() {
441444
if (NULL == network) {
442445
printf("Connecting to the network failed... See serial output.\n");
443446
mbedtls_platform_teardown(&platform_ctx);
444-
return 1;
447+
return MBEDTLS_EXIT_FAILURE;
445448
}
446449

447450
HelloHTTPS *hello = new HelloHTTPS(HTTPS_SERVER_NAME, HTTPS_SERVER_PORT, network, &platform_ctx);
448451
hello->startTest(HTTPS_PATH);
449452
delete hello;
450-
453+
451454
mbedtls_platform_teardown(&platform_ctx);
455+
return MBEDTLS_EXIT_SUCCESS;
452456
}

0 commit comments

Comments
 (0)