2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2013-06-30 14:34:05 +02:00
|
|
|
* sha256sum demonstration program
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
2015-01-23 10:45:19 +01:00
|
|
|
* Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2015-01-22 15:28:16 +01:00
|
|
|
* This file is part of mbed TLS (http://www.polarssl.org)
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2009-01-03 22:22:43 +01: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 12:39:06 +02:00
|
|
|
#if !defined(POLARSSL_CONFIG_FILE)
|
2013-09-20 13:30:43 +02:00
|
|
|
#include "polarssl/config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
|
|
|
#include POLARSSL_CONFIG_FILE
|
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-06-30 14:49:12 +02:00
|
|
|
#include "polarssl/sha256.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-06-30 14:34:05 +02:00
|
|
|
#if !defined(POLARSSL_SHA256_C) || !defined(POLARSSL_FS_IO)
|
2011-11-18 15:26:47 +01:00
|
|
|
int main( int argc, char *argv[] )
|
2011-05-26 15:16:06 +02:00
|
|
|
{
|
2011-11-18 15:26:47 +01:00
|
|
|
((void) argc);
|
|
|
|
((void) argv);
|
|
|
|
|
2013-06-30 14:34:05 +02:00
|
|
|
printf("POLARSSL_SHA256_C and/or POLARSSL_FS_IO not defined.\n");
|
2011-05-26 15:16:06 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#else
|
2013-06-30 14:34:05 +02:00
|
|
|
static int sha256_wrapper( char *filename, unsigned char *sum )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2013-06-30 14:34:05 +02:00
|
|
|
int ret = sha256_file( filename, sum, 0 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
if( ret == 1 )
|
|
|
|
fprintf( stderr, "failed to open: %s\n", filename );
|
|
|
|
|
|
|
|
if( ret == 2 )
|
|
|
|
fprintf( stderr, "failed to read: %s\n", filename );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-06-30 14:34:05 +02:00
|
|
|
static int sha256_print( char *filename )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char sum[32];
|
|
|
|
|
2013-06-30 14:34:05 +02:00
|
|
|
if( sha256_wrapper( filename, sum ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
return( 1 );
|
|
|
|
|
|
|
|
for( i = 0; i < 32; i++ )
|
|
|
|
printf( "%02x", sum[i] );
|
|
|
|
|
|
|
|
printf( " %s\n", filename );
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-06-30 14:34:05 +02:00
|
|
|
static int sha256_check( char *filename )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
size_t n;
|
|
|
|
FILE *f;
|
|
|
|
int nb_err1, nb_err2;
|
|
|
|
int nb_tot1, nb_tot2;
|
|
|
|
unsigned char sum[32];
|
|
|
|
char buf[65], line[1024];
|
2013-10-28 12:51:32 +01:00
|
|
|
char diff;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
|
|
|
{
|
|
|
|
printf( "failed to open: %s\n", filename );
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
nb_err1 = nb_err2 = 0;
|
|
|
|
nb_tot1 = nb_tot2 = 0;
|
|
|
|
|
|
|
|
memset( line, 0, sizeof( line ) );
|
|
|
|
|
|
|
|
n = sizeof( line );
|
|
|
|
|
2011-04-24 10:57:21 +02:00
|
|
|
while( fgets( line, (int) n - 1, f ) != NULL )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
n = strlen( line );
|
|
|
|
|
|
|
|
if( n < 68 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( line[64] != ' ' || line[65] != ' ' )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
|
|
|
|
if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
|
|
|
|
|
|
|
|
nb_tot1++;
|
|
|
|
|
2013-06-30 14:34:05 +02:00
|
|
|
if( sha256_wrapper( line + 66, sum ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
nb_err1++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nb_tot2++;
|
|
|
|
|
|
|
|
for( i = 0; i < 32; i++ )
|
|
|
|
sprintf( buf + i * 2, "%02x", sum[i] );
|
|
|
|
|
2013-10-28 12:51:32 +01:00
|
|
|
/* Use constant-time buffer comparison */
|
|
|
|
diff = 0;
|
|
|
|
for( i = 0; i < 64; i++ )
|
|
|
|
diff |= line[i] ^ buf[i];
|
|
|
|
|
|
|
|
if( diff != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
nb_err2++;
|
|
|
|
fprintf( stderr, "wrong checksum: %s\n", line + 66 );
|
|
|
|
}
|
|
|
|
|
|
|
|
n = sizeof( line );
|
|
|
|
}
|
|
|
|
|
2014-04-17 16:02:36 +02:00
|
|
|
fclose( f );
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
if( nb_err1 != 0 )
|
|
|
|
{
|
|
|
|
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 "
|
|
|
|
"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 )
|
|
|
|
{
|
2013-06-30 14:34:05 +02:00
|
|
|
printf( "print mode: sha256sum <file> <file> ...\n" );
|
|
|
|
printf( "check mode: sha256sum -c <checksum file>\n" );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-11-18 15:26:47 +01:00
|
|
|
#if defined(_WIN32)
|
2009-01-03 22:22:43 +01:00
|
|
|
printf( "\n Press Enter to exit this program.\n" );
|
|
|
|
fflush( stdout ); getchar();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
|
2013-06-30 14:34:05 +02:00
|
|
|
return( sha256_check( argv[2] ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
for( i = 1; i < argc; i++ )
|
2013-06-30 14:34:05 +02:00
|
|
|
ret |= sha256_print( argv[i] );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2013-06-30 14:34:05 +02:00
|
|
|
#endif /* POLARSSL_SHA256_C && POLARSSL_FS_IO */
|