Commit f90016aa authored by Rich Evans's avatar Rich Evans Committed by Paul Bakker
Browse files

Use platform layer in programs for consistency.

parent e94e6e5b
......@@ -26,6 +26,15 @@
#include POLARSSL_CONFIG_FILE
#endif
#if defined(POLARSSL_PLATFORM_C)
#include "polarssl/platform.h"
#else
#define polarssl_printf printf
#define polarssl_fprintf fprintf
#define polarssl_malloc malloc
#define polarssl_free free
#endif
#if defined(_WIN32)
#include <windows.h>
#if !defined(_WIN32_WCE)
......@@ -58,7 +67,7 @@ int main( int argc, char *argv[] )
{
((void) argc);
((void) argv);
printf("POLARSSL_AES_C and/or POLARSSL_SHA256_C not defined.\n");
polarssl_printf("POLARSSL_AES_C and/or POLARSSL_SHA256_C not defined.\n");
return( 0 );
}
#else
......@@ -98,10 +107,10 @@ int main( int argc, char *argv[] )
*/
if( argc != 5 )
{
printf( USAGE );
polarssl_printf( USAGE );
#if defined(_WIN32)
printf( "\n Press Enter to exit this program.\n" );
polarssl_printf( "\n Press Enter to exit this program.\n" );
fflush( stdout ); getchar();
#endif
......@@ -112,25 +121,25 @@ int main( int argc, char *argv[] )
if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
{
fprintf( stderr, "invalide operation mode\n" );
polarssl_fprintf( stderr, "invalide operation mode\n" );
goto exit;
}
if( strcmp( argv[2], argv[3] ) == 0 )
{
fprintf( stderr, "input and output filenames must differ\n" );
polarssl_fprintf( stderr, "input and output filenames must differ\n" );
goto exit;
}
if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
{
fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
polarssl_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
goto exit;
}
if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
{
fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
polarssl_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
goto exit;
}
......@@ -183,7 +192,7 @@ int main( int argc, char *argv[] )
if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
{
fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
polarssl_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
goto exit;
}
......@@ -199,7 +208,7 @@ int main( int argc, char *argv[] )
if( fseek( fin, 0, SEEK_SET ) < 0 )
{
fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
polarssl_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
goto exit;
}
......@@ -235,7 +244,7 @@ int main( int argc, char *argv[] )
*/
if( fwrite( IV, 1, 16, fout ) != 16 )
{
fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
goto exit;
}
......@@ -268,7 +277,7 @@ int main( int argc, char *argv[] )
if( fread( buffer, 1, n, fin ) != (size_t) n )
{
fprintf( stderr, "fread(%d bytes) failed\n", n );
polarssl_fprintf( stderr, "fread(%d bytes) failed\n", n );
goto exit;
}
......@@ -280,7 +289,7 @@ int main( int argc, char *argv[] )
if( fwrite( buffer, 1, 16, fout ) != 16 )
{
fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
goto exit;
}
......@@ -294,7 +303,7 @@ int main( int argc, char *argv[] )
if( fwrite( digest, 1, 32, fout ) != 32 )
{
fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
goto exit;
}
}
......@@ -314,13 +323,13 @@ int main( int argc, char *argv[] )
*/
if( filesize < 48 )
{
fprintf( stderr, "File too short to be encrypted.\n" );
polarssl_fprintf( stderr, "File too short to be encrypted.\n" );
goto exit;
}
if( ( filesize & 0x0F ) != 0 )
{
fprintf( stderr, "File size not a multiple of 16.\n" );
polarssl_fprintf( stderr, "File size not a multiple of 16.\n" );
goto exit;
}
......@@ -334,7 +343,7 @@ int main( int argc, char *argv[] )
*/
if( fread( buffer, 1, 16, fin ) != 16 )
{
fprintf( stderr, "fread(%d bytes) failed\n", 16 );
polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
goto exit;
}
......@@ -367,7 +376,7 @@ int main( int argc, char *argv[] )
{
if( fread( buffer, 1, 16, fin ) != 16 )
{
fprintf( stderr, "fread(%d bytes) failed\n", 16 );
polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
goto exit;
}
......@@ -386,7 +395,7 @@ int main( int argc, char *argv[] )
if( fwrite( buffer, 1, n, fout ) != (size_t) n )
{
fprintf( stderr, "fwrite(%d bytes) failed\n", n );
polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
goto exit;
}
}
......@@ -398,7 +407,7 @@ int main( int argc, char *argv[] )
if( fread( buffer, 1, 32, fin ) != 32 )
{
fprintf( stderr, "fread(%d bytes) failed\n", 32 );
polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
goto exit;
}
......@@ -409,7 +418,7 @@ int main( int argc, char *argv[] )
if( diff != 0 )
{
fprintf( stderr, "HMAC check failed: wrong key, "
polarssl_fprintf( stderr, "HMAC check failed: wrong key, "
"or file corrupted.\n" );
goto exit;
}
......
......@@ -27,6 +27,15 @@
#include POLARSSL_CONFIG_FILE
#endif
#if defined(POLARSSL_PLATFORM_C)
#include "polarssl/platform.h"
#else
#define polarssl_printf printf
#define polarssl_fprintf fprintf
#define polarssl_malloc malloc
#define polarssl_free free
#endif
#if defined(_WIN32)
#include <windows.h>
#if !defined(_WIN32_WCE)
......@@ -60,7 +69,7 @@ int main( int argc, char *argv[] )
((void) argc);
((void) argv);
printf("POLARSSL_CIPHER_C and/or POLARSSL_MD_C not defined.\n");
polarssl_printf("POLARSSL_CIPHER_C and/or POLARSSL_MD_C not defined.\n");
return( 0 );
}
#else
......@@ -102,28 +111,28 @@ int main( int argc, char *argv[] )
{
const int *list;
printf( USAGE );
polarssl_printf( USAGE );
printf( "Available ciphers:\n" );
polarssl_printf( "Available ciphers:\n" );
list = cipher_list();
while( *list )
{
cipher_info = cipher_info_from_type( *list );
printf( " %s\n", cipher_info->name );
polarssl_printf( " %s\n", cipher_info->name );
list++;
}
printf( "\nAvailable message digests:\n" );
polarssl_printf( "\nAvailable message digests:\n" );
list = md_list();
while( *list )
{
md_info = md_info_from_type( *list );
printf( " %s\n", md_info->name );
polarssl_printf( " %s\n", md_info->name );
list++;
}
#if defined(_WIN32)
printf( "\n Press Enter to exit this program.\n" );
polarssl_printf( "\n Press Enter to exit this program.\n" );
fflush( stdout ); getchar();
#endif
......@@ -134,25 +143,25 @@ int main( int argc, char *argv[] )
if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
{
fprintf( stderr, "invalid operation mode\n" );
polarssl_fprintf( stderr, "invalid operation mode\n" );
goto exit;
}
if( strcmp( argv[2], argv[3] ) == 0 )
{
fprintf( stderr, "input and output filenames must differ\n" );
polarssl_fprintf( stderr, "input and output filenames must differ\n" );
goto exit;
}
if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
{
fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
polarssl_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
goto exit;
}
if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
{
fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
polarssl_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
goto exit;
}
......@@ -162,19 +171,19 @@ int main( int argc, char *argv[] )
cipher_info = cipher_info_from_string( argv[4] );
if( cipher_info == NULL )
{
fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
polarssl_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
goto exit;
}
if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info) ) != 0 )
{
fprintf( stderr, "cipher_init_ctx failed\n" );
polarssl_fprintf( stderr, "cipher_init_ctx failed\n" );
goto exit;
}
md_info = md_info_from_string( argv[5] );
if( md_info == NULL )
{
fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
polarssl_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
goto exit;
}
md_init_ctx( &md_ctx, md_info);
......@@ -228,7 +237,7 @@ int main( int argc, char *argv[] )
if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
{
fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
polarssl_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
goto exit;
}
......@@ -244,7 +253,7 @@ int main( int argc, char *argv[] )
if( fseek( fin, 0, SEEK_SET ) < 0 )
{
fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
polarssl_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
goto exit;
}
......@@ -280,7 +289,7 @@ int main( int argc, char *argv[] )
*/
if( fwrite( IV, 1, 16, fout ) != 16 )
{
fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
goto exit;
}
......@@ -305,17 +314,17 @@ int main( int argc, char *argv[] )
if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
POLARSSL_ENCRYPT ) != 0 )
{
fprintf( stderr, "cipher_setkey() returned error\n");
polarssl_fprintf( stderr, "cipher_setkey() returned error\n");
goto exit;
}
if( cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
{
fprintf( stderr, "cipher_set_iv() returned error\n");
polarssl_fprintf( stderr, "cipher_set_iv() returned error\n");
goto exit;
}
if( cipher_reset( &cipher_ctx ) != 0 )
{
fprintf( stderr, "cipher_reset() returned error\n");
polarssl_fprintf( stderr, "cipher_reset() returned error\n");
goto exit;
}
......@@ -331,13 +340,13 @@ int main( int argc, char *argv[] )
if( fread( buffer, 1, ilen, fin ) != ilen )
{
fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
polarssl_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");
polarssl_fprintf( stderr, "cipher_update() returned error\n");
goto exit;
}
......@@ -345,21 +354,21 @@ int main( int argc, char *argv[] )
if( fwrite( output, 1, olen, fout ) != olen )
{
fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
goto exit;
}
}
if( cipher_finish( &cipher_ctx, output, &olen ) != 0 )
{
fprintf( stderr, "cipher_finish() returned error\n" );
polarssl_fprintf( stderr, "cipher_finish() returned error\n" );
goto exit;
}
md_hmac_update( &md_ctx, output, olen );
if( fwrite( output, 1, olen, fout ) != olen )
{
fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
goto exit;
}
......@@ -370,7 +379,7 @@ int main( int argc, char *argv[] )
if( fwrite( digest, 1, md_get_size( md_info ), fout ) != md_get_size( md_info ) )
{
fprintf( stderr, "fwrite(%d bytes) failed\n", md_get_size( md_info ) );
polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", md_get_size( md_info ) );
goto exit;
}
}
......@@ -388,14 +397,14 @@ int main( int argc, char *argv[] )
*/
if( filesize < 16 + md_get_size( md_info ) )
{
fprintf( stderr, "File too short to be encrypted.\n" );
polarssl_fprintf( stderr, "File too short to be encrypted.\n" );
goto exit;
}
if( ( ( filesize - md_get_size( md_info ) ) %
cipher_get_block_size( &cipher_ctx ) ) != 0 )
{
fprintf( stderr, "File content not a multiple of the block size (%d).\n",
polarssl_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
cipher_get_block_size( &cipher_ctx ));
goto exit;
}
......@@ -410,7 +419,7 @@ int main( int argc, char *argv[] )
*/
if( fread( buffer, 1, 16, fin ) != 16 )
{
fprintf( stderr, "fread(%d bytes) failed\n", 16 );
polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
goto exit;
}
......@@ -437,19 +446,19 @@ int main( int argc, char *argv[] )
if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
POLARSSL_DECRYPT ) != 0 )
{
fprintf( stderr, "cipher_setkey() returned error\n" );
polarssl_fprintf( stderr, "cipher_setkey() returned error\n" );
goto exit;
}
if( cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
{
fprintf( stderr, "cipher_set_iv() returned error\n" );
polarssl_fprintf( stderr, "cipher_set_iv() returned error\n" );
goto exit;
}
if( cipher_reset( &cipher_ctx ) != 0 )
{
fprintf( stderr, "cipher_reset() returned error\n" );
polarssl_fprintf( stderr, "cipher_reset() returned error\n" );
goto exit;
}
......@@ -463,7 +472,7 @@ int main( int argc, char *argv[] )
if( fread( buffer, 1, cipher_get_block_size( &cipher_ctx ), fin ) !=
(size_t) cipher_get_block_size( &cipher_ctx ) )
{
fprintf( stderr, "fread(%d bytes) failed\n",
polarssl_fprintf( stderr, "fread(%d bytes) failed\n",
cipher_get_block_size( &cipher_ctx ) );
goto exit;
}
......@@ -473,13 +482,13 @@ int main( int argc, char *argv[] )
cipher_get_block_size( &cipher_ctx ),
output, &olen ) != 0 )
{
fprintf( stderr, "cipher_update() returned error\n" );
polarssl_fprintf( stderr, "cipher_update() returned error\n" );
goto exit;
}
if( fwrite( output, 1, olen, fout ) != olen )
{
fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
goto exit;
}
}
......@@ -491,7 +500,7 @@ int main( int argc, char *argv[] )
if( fread( buffer, 1, md_get_size( md_info ), fin ) != md_get_size( md_info ) )
{
fprintf( stderr, "fread(%d bytes) failed\n", md_get_size( md_info ) );
polarssl_fprintf( stderr, "fread(%d bytes) failed\n", md_get_size( md_info ) );
goto exit;
}
......@@ -502,7 +511,7 @@ int main( int argc, char *argv[] )
if( diff != 0 )
{
fprintf( stderr, "HMAC check failed: wrong key, "
polarssl_fprintf( stderr, "HMAC check failed: wrong key, "
"or file corrupted.\n" );
goto exit;
}
......@@ -514,7 +523,7 @@ int main( int argc, char *argv[] )
if( fwrite( output, 1, olen, fout ) != olen )
{
fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
goto exit;
}
}
......
......@@ -26,6 +26,15 @@
#include POLARSSL_CONFIG_FILE
#endif
#if defined(POLARSSL_PLATFORM_C)
#include "polarssl/platform.h"
#else
#define polarssl_printf printf
#define polarssl_fprintf fprintf
#define polarssl_malloc malloc
#define polarssl_free free
#endif
#include <string.h>
#include <stdio.h>
......@@ -37,7 +46,7 @@ int main( int argc, char *argv[] )
((void) argc);
((void) argv);
printf("POLARSSL_MD_C not defined.\n");
polarssl_printf("POLARSSL_MD_C not defined.\n");
return( 0 );
}
#else
......@@ -46,10 +55,10 @@ static int generic_wrapper( const md_info_t *md_info, char *filename, unsigned c
int ret = md_file( md_info, filename, sum );
if( ret == 1 )
fprintf( stderr, "failed to open: %s\n", filename );
polarssl_fprintf( stderr, "failed to open: %s\n", filename );
if( ret == 2 )
fprintf( stderr, "failed to read: %s\n", filename );
polarssl_fprintf( stderr, "failed to read: %s\n", filename );
return( ret );
}
......@@ -63,9 +72,9 @@ static int generic_print( const md_info_t *md_info, char *filename )
return( 1 );
for( i = 0; i < md_info->size; i++ )
printf( "%02x", sum[i] );
polarssl_printf( "%02x", sum[i] );
printf( " %s\n", filename );
polarssl_printf( " %s\n", filename );
return( 0 );
}
......@@ -82,7 +91,7 @@ static int generic_check( const md_info_t *md_info, char *filename )
if( ( f = fopen( filename, "rb" ) ) == NULL )
{
printf( "failed to open: %s\n", filename );
polarssl_printf( "failed to open: %s\n", filename );
return( 1 );
}
......@@ -99,13 +108,13 @@ static int generic_check( const md_info_t *md_info, char *filename )
if( n < (size_t) 2 * md_info->size + 4 )
{
printf("No '%s' hash found on line.\n", md_info->name);
polarssl_printf("No '%s' hash found on line.\n", md_info->name);
continue;
}
if( line[2 * md_info->size] != ' ' || line[2 * md_info->size + 1] != ' ' )
{
printf("No '%s' hash found on line.\n", md_info->name);
polarssl_printf("No '%s' hash found on line.\n", md_info->name);
continue;
}
......@@ -133,7 +142,7 @@ static int generic_check( const md_info_t *md_info, char *filename )
if( diff != 0 )
{
nb_err2++;
fprintf( stderr, "wrong checksum: %s\n", line + 66 );
polarssl_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
}
n = sizeof( line );
......@@ -141,13 +150,13 @@ static int generic_check( const md_info_t *md_info, char *filename )
if( nb_err1 != 0 )
{
printf( "WARNING: %d (out of %d) input files could "
polarssl_printf( "WARNING: %d (out of %d) input files could "
"not be read\n", nb_err1, nb_tot1 );
}
if( nb_err2 != 0 )
{
printf( "WARNING: %d (out of %d) computed checksums did "
polarssl_printf( "WARNING: %d (out of %d) computed checksums did "
"not match\n", nb_err2, nb_tot2 );
}
......@@ -168,20 +177,20 @@ int main( int argc, char *argv[] )
{
const int *list;
printf( "print mode: generic_sum <md> <file> <file> ...\n" );
printf( "check mode: generic_sum <md> -c <checksum file>\n" );
polarssl_printf( "print mode: generic_sum <md> <file> <file> ...\n" );
polarssl_printf( "check mode: generic_sum <md> -c <checksum file>\n" );
printf( "\nAvailable message digests:\n" );
polarssl_printf( "\nAvailable message digests:\n" );
list = md_list();
while( *list )
{
md_info = md_info_from_type( *list );
printf( " %s\n", md_info->name );
polarssl_printf( " %s\n", md_info->name );
list++;
}
#if defined(_WIN32)
printf( "\n Press Enter to exit this program.\n" );
polarssl_printf( "\n Press Enter to exit this program.\n" );
fflush( stdout ); getchar();
#endif
......@@ -194,12 +203,12 @@ int main( int argc, char *argv[] )
md_info = md_info_from_string( argv[1] );
if( md_info == NULL )
{
fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
polarssl_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
return( 1 );
}
if( md_init_ctx( &md_ctx, md_info) )
{