From 993f02cda0272c1f02d2d77dd44ac9c2d30553f4 Mon Sep 17 00:00:00 2001
From: Paul Bakker
Date: Thu, 17 Apr 2014 16:00:59 +0200
Subject: [PATCH] Added return value checking for correctness in programs
---
programs/aes/crypt_and_hash.c | 39 ++++++++++++++++++++++++++++-------
programs/pkey/dh_genprime.c | 7 ++++++-
programs/test/ssl_test.c | 8 ++++++-
3 files changed, 44 insertions(+), 10 deletions(-)
diff --git a/programs/aes/crypt_and_hash.c b/programs/aes/crypt_and_hash.c
index 0448440a7..fc86337b4 100644
--- a/programs/aes/crypt_and_hash.c
+++ b/programs/aes/crypt_and_hash.c
@@ -168,7 +168,11 @@ int main( int argc, char *argv[] )
fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
goto exit;
}
- cipher_init_ctx( &cipher_ctx, cipher_info);
+ if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info) ) != 0 )
+ {
+ fprintf( stderr, "cipher_init_ctx failed\n" );
+ goto exit;
+ }
md_info = md_info_from_string( argv[5] );
if( md_info == NULL )
@@ -325,11 +329,16 @@ int main( int argc, char *argv[] )
if( fread( buffer, 1, ilen, fin ) != ilen )
{
- fprintf( stderr, "fread(%ld bytes) failed\n", (long) n );
+ fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
+ goto exit;
+ }
+
+ if( cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
+ {
+ fprintf( stderr, "cipher_update() returned error\n");
goto exit;
}
- cipher_update( &cipher_ctx, buffer, ilen, output, &olen );
md_hmac_update( &md_ctx, output, olen );
if( fwrite( output, 1, olen, fout ) != olen )
@@ -423,9 +432,18 @@ int main( int argc, char *argv[] )
memset( key, 0, sizeof( key ) );
- cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
- POLARSSL_DECRYPT );
- cipher_reset( &cipher_ctx, IV);
+ if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
+ POLARSSL_DECRYPT ) != 0 )
+ {
+ fprintf( stderr, "cipher_setkey() returned error\n" );
+ goto exit;
+ }
+
+ if( cipher_reset( &cipher_ctx, IV ) != 0 )
+ {
+ fprintf( stderr, "cipher_reset() returned error\n" );
+ goto exit;
+ }
md_hmac_starts( &md_ctx, digest, 32 );
@@ -443,8 +461,13 @@ int main( int argc, char *argv[] )
}
md_hmac_update( &md_ctx, buffer, cipher_get_block_size( &cipher_ctx ) );
- cipher_update( &cipher_ctx, buffer, cipher_get_block_size( &cipher_ctx ),
- output, &olen );
+ if( cipher_update( &cipher_ctx, buffer,
+ cipher_get_block_size( &cipher_ctx ),
+ output, &olen ) != 0 )
+ {
+ fprintf( stderr, "cipher_update() returned error\n" );
+ goto exit;
+ }
if( fwrite( output, 1, olen, fout ) != olen )
{
diff --git a/programs/pkey/dh_genprime.c b/programs/pkey/dh_genprime.c
index 5732f507e..b11199181 100644
--- a/programs/pkey/dh_genprime.c
+++ b/programs/pkey/dh_genprime.c
@@ -69,7 +69,12 @@ int main( int argc, char *argv[] )
((void) argv);
mpi_init( &G ); mpi_init( &P ); mpi_init( &Q );
- mpi_read_string( &G, 10, GENERATOR );
+
+ if( ( ret = mpi_read_string( &G, 10, GENERATOR ) ) != 0 )
+ {
+ printf( " failed\n ! mpi_read_string returned %d\n", ret );
+ goto exit;
+ }
printf( "\nWARNING: You should not generate and use your own DHM primes\n" );
printf( " unless you are very certain of what you are doing!\n" );
diff --git a/programs/test/ssl_test.c b/programs/test/ssl_test.c
index c090a853f..04adf297e 100644
--- a/programs/test/ssl_test.c
+++ b/programs/test/ssl_test.c
@@ -278,7 +278,13 @@ static int ssl_test( struct options *opt )
else ssl_set_ciphersuites( &ssl, opt->force_ciphersuite );
if( opt->iomode == IOMODE_NONBLOCK )
- net_set_nonblock( client_fd );
+ {
+ if( ( ret = net_set_nonblock( client_fd ) ) != 0 )
+ {
+ printf( " ! net_set_nonblock returned %d\n\n", ret );
+ return( ret );
+ }
+ }
read_buf = (unsigned char *) malloc( opt->buffer_size );
write_buf = (unsigned char *) malloc( opt->buffer_size );