diff --git a/programs/pkey/rsa_decrypt.c b/programs/pkey/rsa_decrypt.c index 2da3fbf11..0a252d2ad 100644 --- a/programs/pkey/rsa_decrypt.c +++ b/programs/pkey/rsa_decrypt.c @@ -30,11 +30,11 @@ #else #include #include -#define mbedtls_printf printf -#define mbedtls_exit exit +#define mbedtls_printf printf +#define mbedtls_exit exit #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE -#endif +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \ defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \ @@ -61,7 +61,9 @@ int main( void ) int main( int argc, char *argv[] ) { FILE *f; - int return_val, exit_val, c; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; + int c; size_t i; mbedtls_rsa_context rsa; mbedtls_mpi N, P, Q, D, E, DP, DQ, QP; @@ -73,7 +75,6 @@ int main( int argc, char *argv[] ) ((void) argv); memset(result, 0, sizeof( result ) ); - exit_val = MBEDTLS_EXIT_SUCCESS; if( argc != 1 ) { @@ -83,7 +84,7 @@ int main( int argc, char *argv[] ) mbedtls_printf( "\n" ); #endif - mbedtls_exit( MBEDTLS_EXIT_FAILURE ); + mbedtls_exit( exit_code ); } mbedtls_printf( "\n . Seeding the random number generator..." ); @@ -96,14 +97,13 @@ int main( int argc, char *argv[] ) mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP ); - return_val = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, + ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) pers, strlen( pers ) ); - if( return_val != 0 ) + if( ret != 0 ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", - return_val ); + ret ); goto exit; } @@ -112,40 +112,38 @@ int main( int argc, char *argv[] ) if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \ " ! Please run rsa_genkey first\n\n" ); goto exit; } - if( ( return_val = mbedtls_mpi_read_file( &N , 16, f ) ) != 0 || - ( return_val = mbedtls_mpi_read_file( &E , 16, f ) ) != 0 || - ( return_val = mbedtls_mpi_read_file( &D , 16, f ) ) != 0 || - ( return_val = mbedtls_mpi_read_file( &P , 16, f ) ) != 0 || - ( return_val = mbedtls_mpi_read_file( &Q , 16, f ) ) != 0 || - ( return_val = mbedtls_mpi_read_file( &DP , 16, f ) ) != 0 || - ( return_val = mbedtls_mpi_read_file( &DQ , 16, f ) ) != 0 || - ( return_val = mbedtls_mpi_read_file( &QP , 16, f ) ) != 0 ) + if( ( ret = mbedtls_mpi_read_file( &N , 16, f ) ) != 0 || + ( ret = mbedtls_mpi_read_file( &E , 16, f ) ) != 0 || + ( ret = mbedtls_mpi_read_file( &D , 16, f ) ) != 0 || + ( ret = mbedtls_mpi_read_file( &P , 16, f ) ) != 0 || + ( ret = mbedtls_mpi_read_file( &Q , 16, f ) ) != 0 || + ( ret = mbedtls_mpi_read_file( &DP , 16, f ) ) != 0 || + ( ret = mbedtls_mpi_read_file( &DQ , 16, f ) ) != 0 || + ( ret = mbedtls_mpi_read_file( &QP , 16, f ) ) != 0 ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", - return_val ); + ret ); fclose( f ); goto exit; } fclose( f ); - if( ( return_val = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 ) + if( ( ret = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 ) { mbedtls_printf( " failed\n ! mbedtls_rsa_import returned %d\n\n", - return_val ); + ret ); goto exit; } - if( ( return_val = mbedtls_rsa_complete( &rsa ) ) != 0 ) + if( ( ret = mbedtls_rsa_complete( &rsa ) ) != 0 ) { mbedtls_printf( " failed\n ! mbedtls_rsa_complete returned %d\n\n", - return_val ); + ret ); goto exit; } @@ -154,7 +152,6 @@ int main( int argc, char *argv[] ) */ if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" ); goto exit; } @@ -169,7 +166,6 @@ int main( int argc, char *argv[] ) if( i != rsa.len ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( "\n ! Invalid RSA signature format\n\n" ); goto exit; } @@ -180,14 +176,13 @@ int main( int argc, char *argv[] ) mbedtls_printf( "\n . Decrypting the encrypted data" ); fflush( stdout ); - return_val = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random, + ret = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, MBEDTLS_RSA_PRIVATE, &i, buf, result, 1024 ); - if( return_val != 0 ) + if( ret != 0 ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n", - return_val ); + ret ); goto exit; } @@ -195,6 +190,8 @@ int main( int argc, char *argv[] ) mbedtls_printf( "The decrypted result is: '%s'\n\n", result ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_ctr_drbg_free( &ctr_drbg ); mbedtls_entropy_free( &entropy ); @@ -208,6 +205,6 @@ exit: fflush( stdout ); getchar(); #endif - return( exit_val ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */