Skip to content

Commit

Permalink
Unify the example programs' termination
Browse files Browse the repository at this point in the history
This is done to account for platforms, for which we want custom behavior
upon the program termination, hence we call `mbedtls_exit()` instead of
returning from `main()`.
  • Loading branch information
Krzysztof Stachowiak authored and k-stachowiak committed Aug 16, 2019
1 parent beec142 commit 5e1b195
Show file tree
Hide file tree
Showing 44 changed files with 102 additions and 101 deletions.
4 changes: 2 additions & 2 deletions programs/aes/aescrypt2.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int main( void )
mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_SHA256_C "
"and/or MBEDTLS_FS_IO and/or MBEDTLS_MD_C "
"not defined.\n");
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand Down Expand Up @@ -465,6 +465,6 @@ int main( int argc, char *argv[] )
mbedtls_aes_free( &aes_ctx );
mbedtls_md_free( &sha_ctx );

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_AES_C && MBEDTLS_SHA256_C && MBEDTLS_FS_IO */
4 changes: 2 additions & 2 deletions programs/aes/crypt_and_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
int main( void )
{
mbedtls_printf("MBEDTLS_CIPHER_C and/or MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand Down Expand Up @@ -562,6 +562,6 @@ int main( int argc, char *argv[] )
mbedtls_cipher_free( &cipher_ctx );
mbedtls_md_free( &md_ctx );

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */
10 changes: 5 additions & 5 deletions programs/hash/generic_sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
int main( void )
{
mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand Down Expand Up @@ -203,7 +203,7 @@ int main( int argc, char *argv[] )
fflush( stdout ); getchar();
#endif

return( exit_code );
mbedtls_exit( exit_code );
}

/*
Expand All @@ -213,12 +213,12 @@ int main( int argc, char *argv[] )
if( md_info == NULL )
{
mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
return( exit_code );
mbedtls_exit( exit_code );
}
if( mbedtls_md_setup( &md_ctx, md_info, 0 ) )
{
mbedtls_fprintf( stderr, "Failed to initialize context.\n" );
return( exit_code );
mbedtls_exit( exit_code );
}

ret = 0;
Expand All @@ -237,6 +237,6 @@ int main( int argc, char *argv[] )
exit:
mbedtls_md_free( &md_ctx );

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */
6 changes: 3 additions & 3 deletions programs/hash/hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
int main( void )
{
mbedtls_printf("MBEDTLS_MD5_C not defined.\n");
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand All @@ -58,7 +58,7 @@ int main( void )
mbedtls_printf( "\n MD5('%s') = ", str );

if( ( ret = mbedtls_md5_ret( (unsigned char *) str, 13, digest ) ) != 0 )
return( MBEDTLS_EXIT_FAILURE );
mbedtls_exit( MBEDTLS_EXIT_FAILURE );

for( i = 0; i < 16; i++ )
mbedtls_printf( "%02x", digest[i] );
Expand All @@ -70,6 +70,6 @@ int main( void )
fflush( stdout ); getchar();
#endif

return( MBEDTLS_EXIT_SUCCESS );
mbedtls_exit( MBEDTLS_EXIT_SUCCESS );
}
#endif /* MBEDTLS_MD5_C */
4 changes: 2 additions & 2 deletions programs/pkey/dh_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ int main( void )
"and/or MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
"MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO and/or "
"MBEDTLS_CTR_DRBG_C not defined.\n");
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand Down Expand Up @@ -307,7 +307,7 @@ int main( void )
fflush( stdout ); getchar();
#endif

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
Expand Down
6 changes: 3 additions & 3 deletions programs/pkey/dh_genprime.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int main( void )
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
"MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or "
"MBEDTLS_GENPRIME not defined.\n");
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand Down Expand Up @@ -91,7 +91,7 @@ int main( int argc, char **argv )
{
usage:
mbedtls_printf( USAGE );
return( exit_code );
mbedtls_exit( exit_code );
}

for( i = 1; i < argc; i++ )
Expand Down Expand Up @@ -197,7 +197,7 @@ int main( int argc, char **argv )
fflush( stdout ); getchar();
#endif

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO &&
MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */
4 changes: 2 additions & 2 deletions programs/pkey/dh_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ int main( void )
"and/or MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
"MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO and/or "
"MBEDTLS_CTR_DRBG_C not defined.\n");
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand Down Expand Up @@ -330,7 +330,7 @@ int main( void )
fflush( stdout ); getchar();
#endif

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
Expand Down
4 changes: 2 additions & 2 deletions programs/pkey/ecdh_curve25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int main( void )
"MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or "
"MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
"not defined\n" );
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand Down Expand Up @@ -238,7 +238,7 @@ int main( int argc, char *argv[] )
mbedtls_ctr_drbg_free( &ctr_drbg );
mbedtls_entropy_free( &entropy );

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
4 changes: 2 additions & 2 deletions programs/pkey/ecdsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ int main( void )
{
mbedtls_printf("MBEDTLS_ECDSA_C and/or MBEDTLS_SHA256_C and/or "
"MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined\n");
return( 0 );
mbedtls_exit( 0 );
}
#else
#if defined(VERBOSE)
Expand Down Expand Up @@ -248,7 +248,7 @@ int main( int argc, char *argv[] )
mbedtls_ctr_drbg_free( &ctr_drbg );
mbedtls_entropy_free( &entropy );

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
ECPARAMS */
4 changes: 2 additions & 2 deletions programs/pkey/gen_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ int main( void )
"MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
"MBEDTLS_PEM_WRITE_C"
"not defined.\n" );
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand Down Expand Up @@ -446,7 +446,7 @@ int main( int argc, char *argv[] )
fflush( stdout ); getchar();
#endif

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_PEM_WRITE_C && MBEDTLS_FS_IO &&
* MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
4 changes: 2 additions & 2 deletions programs/pkey/key_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ int main( void )
{
mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
"MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand Down Expand Up @@ -311,6 +311,6 @@ int main( int argc, char *argv[] )
fflush( stdout ); getchar();
#endif

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
4 changes: 2 additions & 2 deletions programs/pkey/key_app_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
int main( void )
{
mbedtls_printf( "MBEDTLS_PK_PARSE_C and/or MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO not defined.\n" );
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand Down Expand Up @@ -436,6 +436,6 @@ int main( int argc, char *argv[] )
fflush( stdout ); getchar();
#endif

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */
4 changes: 2 additions & 2 deletions programs/pkey/mpi_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
int main( void )
{
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_FS_IO not defined.\n");
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand Down Expand Up @@ -112,6 +112,6 @@ int main( void )
fflush( stdout ); getchar();
#endif

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_FS_IO */
4 changes: 2 additions & 2 deletions programs/pkey/pk_decrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int main( void )
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
"MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
"MBEDTLS_CTR_DRBG_C not defined.\n");
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand Down Expand Up @@ -171,7 +171,7 @@ int main( int argc, char *argv[] )
fflush( stdout ); getchar();
#endif

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
4 changes: 2 additions & 2 deletions programs/pkey/pk_encrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ int main( void )
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
"MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
"MBEDTLS_CTR_DRBG_C not defined.\n");
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand Down Expand Up @@ -176,7 +176,7 @@ int main( int argc, char *argv[] )
fflush( stdout ); getchar();
#endif

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_ENTROPY_C &&
MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
4 changes: 2 additions & 2 deletions programs/pkey/pk_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int main( void )
"MBEDTLS_SHA256_C and/or MBEDTLS_MD_C and/or "
"MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
"MBEDTLS_CTR_DRBG_C not defined.\n");
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand Down Expand Up @@ -174,7 +174,7 @@ int main( int argc, char *argv[] )
fflush( stdout ); getchar();
#endif

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
Expand Down
4 changes: 2 additions & 2 deletions programs/pkey/pk_verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int main( void )
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_MD_C and/or "
"MBEDTLS_SHA256_C and/or MBEDTLS_PK_PARSE_C and/or "
"MBEDTLS_FS_IO not defined.\n");
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand Down Expand Up @@ -147,7 +147,7 @@ int main( int argc, char *argv[] )
fflush( stdout ); getchar();
#endif

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_SHA256_C &&
MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
4 changes: 2 additions & 2 deletions programs/pkey/rsa_decrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int main( void )
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
"MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
"MBEDTLS_CTR_DRBG_C not defined.\n");
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand Down Expand Up @@ -207,6 +207,6 @@ int main( int argc, char *argv[] )
fflush( stdout ); getchar();
#endif

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */
4 changes: 2 additions & 2 deletions programs/pkey/rsa_encrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int main( void )
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
"MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
"MBEDTLS_CTR_DRBG_C not defined.\n");
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand Down Expand Up @@ -184,7 +184,7 @@ int main( int argc, char *argv[] )
fflush( stdout ); getchar();
#endif

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
4 changes: 2 additions & 2 deletions programs/pkey/rsa_genkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ int main( void )
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
"MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
"MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
return( 0 );
mbedtls_exit( 0 );
}
#else

Expand Down Expand Up @@ -186,7 +186,7 @@ int main( void )
fflush( stdout ); getchar();
#endif

return( exit_code );
mbedtls_exit( exit_code );
}
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
Loading

0 comments on commit 5e1b195

Please sign in to comment.