2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* md5sum demonstration program
|
|
|
|
*
|
2015-01-23 09:45:19 +00:00
|
|
|
* Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2015-03-06 13:17:10 +00:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2014-04-29 10:39:06 +00:00
|
|
|
#if !defined(POLARSSL_CONFIG_FILE)
|
2013-09-20 11:30:43 +00:00
|
|
|
#include "polarssl/config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
|
|
|
#include POLARSSL_CONFIG_FILE
|
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
#if defined(POLARSSL_PLATFORM_C)
|
|
|
|
#include "polarssl/platform.h"
|
|
|
|
#else
|
2015-02-11 14:06:19 +00:00
|
|
|
#include <stdio.h>
|
2015-01-19 14:26:37 +00:00
|
|
|
#define polarssl_fprintf fprintf
|
2015-02-11 14:06:19 +00:00
|
|
|
#define polarssl_printf printf
|
2015-01-19 14:26:37 +00:00
|
|
|
#endif
|
|
|
|
|
2015-02-11 14:06:19 +00:00
|
|
|
#if defined(POLARSSL_MD5_C) && defined(POLARSSL_FS_IO)
|
2009-01-03 21:51:57 +00:00
|
|
|
#include "polarssl/md5.h"
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-02-11 14:06:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
2011-05-26 13:16:06 +00:00
|
|
|
#if !defined(POLARSSL_MD5_C) || !defined(POLARSSL_FS_IO)
|
2015-02-12 11:37:29 +00:00
|
|
|
int main( void )
|
2011-05-26 13:16:06 +00:00
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf("POLARSSL_MD5_C and/or POLARSSL_FS_IO not defined.\n");
|
2011-05-26 13:16:06 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#else
|
2009-01-03 21:22:43 +00:00
|
|
|
static int md5_wrapper( char *filename, unsigned char *sum )
|
|
|
|
{
|
|
|
|
int ret = md5_file( filename, sum );
|
|
|
|
|
|
|
|
if( ret == 1 )
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_fprintf( stderr, "failed to open: %s\n", filename );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( ret == 2 )
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_fprintf( stderr, "failed to read: %s\n", filename );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int md5_print( char *filename )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char sum[16];
|
|
|
|
|
|
|
|
if( md5_wrapper( filename, sum ) != 0 )
|
|
|
|
return( 1 );
|
|
|
|
|
|
|
|
for( i = 0; i < 16; i++ )
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( "%02x", sum[i] );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " %s\n", filename );
|
2009-01-03 21:22:43 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int md5_check( char *filename )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
size_t n;
|
|
|
|
FILE *f;
|
|
|
|
int nb_err1, nb_err2;
|
|
|
|
int nb_tot1, nb_tot2;
|
|
|
|
unsigned char sum[16];
|
|
|
|
char buf[33], line[1024];
|
2013-10-28 11:51:32 +00:00
|
|
|
char diff;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( "failed to open: %s\n", filename );
|
2009-01-03 21:22:43 +00:00
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
nb_err1 = nb_err2 = 0;
|
|
|
|
nb_tot1 = nb_tot2 = 0;
|
|
|
|
|
|
|
|
memset( line, 0, sizeof( line ) );
|
|
|
|
|
|
|
|
n = sizeof( line );
|
|
|
|
|
2011-04-24 08:57:21 +00:00
|
|
|
while( fgets( line, (int) n - 1, f ) != NULL )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
n = strlen( line );
|
|
|
|
|
|
|
|
if( n < 36 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( line[32] != ' ' || line[33] != ' ' )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
|
|
|
|
if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
|
|
|
|
|
|
|
|
nb_tot1++;
|
|
|
|
|
|
|
|
if( md5_wrapper( line + 34, sum ) != 0 )
|
|
|
|
{
|
|
|
|
nb_err1++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nb_tot2++;
|
|
|
|
|
|
|
|
for( i = 0; i < 16; i++ )
|
|
|
|
sprintf( buf + i * 2, "%02x", sum[i] );
|
|
|
|
|
2013-10-28 11:51:32 +00:00
|
|
|
/* Use constant-time buffer comparison */
|
|
|
|
diff = 0;
|
|
|
|
for( i = 0; i < 32; i++ )
|
|
|
|
diff |= line[i] ^ buf[i];
|
|
|
|
|
|
|
|
if( diff != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
nb_err2++;
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_fprintf( stderr, "wrong checksum: %s\n", line + 34 );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n = sizeof( line );
|
|
|
|
}
|
|
|
|
|
2014-04-17 14:02:36 +00:00
|
|
|
fclose( f );
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
if( nb_err1 != 0 )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( "WARNING: %d (out of %d) input files could "
|
2009-01-03 21:22:43 +00:00
|
|
|
"not be read\n", nb_err1, nb_tot1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( nb_err2 != 0 )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( "WARNING: %d (out of %d) computed checksums did "
|
2009-01-03 21:22:43 +00:00
|
|
|
"not match\n", nb_err2, nb_tot2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( nb_err1 != 0 || nb_err2 != 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int main( int argc, char *argv[] )
|
|
|
|
{
|
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
if( argc == 1 )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( "print mode: md5sum <file> <file> ...\n" );
|
|
|
|
polarssl_printf( "check mode: md5sum -c <checksum file>\n" );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2011-11-18 14:26:47 +00:00
|
|
|
#if defined(_WIN32)
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( "\n Press Enter to exit this program.\n" );
|
2009-01-03 21:22:43 +00:00
|
|
|
fflush( stdout ); getchar();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
|
|
|
|
return( md5_check( argv[2] ) );
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
for( i = 1; i < argc; i++ )
|
|
|
|
ret |= md5_print( argv[i] );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2011-05-26 13:16:06 +00:00
|
|
|
#endif /* POLARSSL_MD5_C && POLARSSL_FS_IO */
|