mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2025-06-25 05:31:23 +00:00
Remove non-generic md_file() programs
This commit is contained in:
parent
bfffa908a6
commit
b327168e22
|
@ -37,8 +37,7 @@ endif
|
||||||
|
|
||||||
APPS = aes/aescrypt2$(EXEXT) aes/crypt_and_hash$(EXEXT) \
|
APPS = aes/aescrypt2$(EXEXT) aes/crypt_and_hash$(EXEXT) \
|
||||||
hash/hello$(EXEXT) hash/generic_sum$(EXEXT) \
|
hash/hello$(EXEXT) hash/generic_sum$(EXEXT) \
|
||||||
hash/md5sum$(EXEXT) hash/sha1sum$(EXEXT) \
|
pkey/dh_client$(EXEXT) \
|
||||||
hash/sha2sum$(EXEXT) pkey/dh_client$(EXEXT) \
|
|
||||||
pkey/dh_genprime$(EXEXT) pkey/dh_server$(EXEXT) \
|
pkey/dh_genprime$(EXEXT) pkey/dh_server$(EXEXT) \
|
||||||
pkey/gen_key$(EXEXT) \
|
pkey/gen_key$(EXEXT) \
|
||||||
pkey/key_app$(EXEXT) pkey/key_app_writer$(EXEXT) \
|
pkey/key_app$(EXEXT) pkey/key_app_writer$(EXEXT) \
|
||||||
|
@ -85,18 +84,6 @@ hash/generic_sum$(EXEXT): hash/generic_sum.c ../library/libmbedtls.a
|
||||||
echo " CC hash/generic_sum.c"
|
echo " CC hash/generic_sum.c"
|
||||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) hash/generic_sum.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) hash/generic_sum.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||||
|
|
||||||
hash/md5sum$(EXEXT): hash/md5sum.c ../library/libmbedtls.a
|
|
||||||
echo " CC hash/md5sum.c"
|
|
||||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) hash/md5sum.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
|
||||||
|
|
||||||
hash/sha1sum$(EXEXT): hash/sha1sum.c ../library/libmbedtls.a
|
|
||||||
echo " CC hash/sha1sum.c"
|
|
||||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) hash/sha1sum.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
|
||||||
|
|
||||||
hash/sha2sum$(EXEXT): hash/sha2sum.c ../library/libmbedtls.a
|
|
||||||
echo " CC hash/sha2sum.c"
|
|
||||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) hash/sha2sum.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
|
||||||
|
|
||||||
pkey/dh_client$(EXEXT): pkey/dh_client.c ../library/libmbedtls.a
|
pkey/dh_client$(EXEXT): pkey/dh_client.c ../library/libmbedtls.a
|
||||||
echo " CC pkey/dh_client.c"
|
echo " CC pkey/dh_client.c"
|
||||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/dh_client.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/dh_client.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||||
|
|
|
@ -4,15 +4,6 @@ target_link_libraries(hello mbedtls)
|
||||||
add_executable(generic_sum generic_sum.c)
|
add_executable(generic_sum generic_sum.c)
|
||||||
target_link_libraries(generic_sum mbedtls)
|
target_link_libraries(generic_sum mbedtls)
|
||||||
|
|
||||||
add_executable(md5sum md5sum.c)
|
install(TARGETS hello generic_sum
|
||||||
target_link_libraries(md5sum mbedtls)
|
|
||||||
|
|
||||||
add_executable(sha1sum sha1sum.c)
|
|
||||||
target_link_libraries(sha1sum mbedtls)
|
|
||||||
|
|
||||||
add_executable(sha2sum sha2sum.c)
|
|
||||||
target_link_libraries(sha2sum mbedtls)
|
|
||||||
|
|
||||||
install(TARGETS hello md5sum sha1sum sha2sum generic_sum
|
|
||||||
DESTINATION "bin"
|
DESTINATION "bin"
|
||||||
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
|
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
|
||||||
|
|
|
@ -1,186 +0,0 @@
|
||||||
/*
|
|
||||||
* md5sum demonstration program
|
|
||||||
*
|
|
||||||
* Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
|
|
||||||
*
|
|
||||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
|
||||||
#include "mbedtls/config.h"
|
|
||||||
#else
|
|
||||||
#include MBEDTLS_CONFIG_FILE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_C)
|
|
||||||
#include "mbedtls/platform.h"
|
|
||||||
#else
|
|
||||||
#include <stdio.h>
|
|
||||||
#define mbedtls_fprintf fprintf
|
|
||||||
#define mbedtls_printf printf
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_FS_IO)
|
|
||||||
#include "mbedtls/md5.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if !defined(MBEDTLS_MD5_C) || !defined(MBEDTLS_FS_IO)
|
|
||||||
int main( void )
|
|
||||||
{
|
|
||||||
mbedtls_printf("MBEDTLS_MD5_C and/or MBEDTLS_FS_IO not defined.\n");
|
|
||||||
return( 0 );
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
static int md5_wrapper( char *filename, unsigned char *sum )
|
|
||||||
{
|
|
||||||
int ret = mbedtls_md5_file( filename, sum );
|
|
||||||
|
|
||||||
if( ret == 1 )
|
|
||||||
mbedtls_fprintf( stderr, "failed to open: %s\n", filename );
|
|
||||||
|
|
||||||
if( ret == 2 )
|
|
||||||
mbedtls_fprintf( stderr, "failed to read: %s\n", filename );
|
|
||||||
|
|
||||||
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++ )
|
|
||||||
mbedtls_printf( "%02x", sum[i] );
|
|
||||||
|
|
||||||
mbedtls_printf( " %s\n", filename );
|
|
||||||
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];
|
|
||||||
char diff;
|
|
||||||
|
|
||||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
|
||||||
{
|
|
||||||
mbedtls_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 );
|
|
||||||
|
|
||||||
while( fgets( line, (int) n - 1, f ) != NULL )
|
|
||||||
{
|
|
||||||
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] );
|
|
||||||
|
|
||||||
/* Use constant-time buffer comparison */
|
|
||||||
diff = 0;
|
|
||||||
for( i = 0; i < 32; i++ )
|
|
||||||
diff |= line[i] ^ buf[i];
|
|
||||||
|
|
||||||
if( diff != 0 )
|
|
||||||
{
|
|
||||||
nb_err2++;
|
|
||||||
mbedtls_fprintf( stderr, "wrong checksum: %s\n", line + 34 );
|
|
||||||
}
|
|
||||||
|
|
||||||
n = sizeof( line );
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose( f );
|
|
||||||
|
|
||||||
if( nb_err1 != 0 )
|
|
||||||
{
|
|
||||||
mbedtls_printf( "WARNING: %d (out of %d) input files could "
|
|
||||||
"not be read\n", nb_err1, nb_tot1 );
|
|
||||||
}
|
|
||||||
|
|
||||||
if( nb_err2 != 0 )
|
|
||||||
{
|
|
||||||
mbedtls_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 )
|
|
||||||
{
|
|
||||||
mbedtls_printf( "print mode: md5sum <file> <file> ...\n" );
|
|
||||||
mbedtls_printf( "check mode: md5sum -c <checksum file>\n" );
|
|
||||||
|
|
||||||
#if defined(_WIN32)
|
|
||||||
mbedtls_printf( "\n Press Enter to exit this program.\n" );
|
|
||||||
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 );
|
|
||||||
}
|
|
||||||
#endif /* MBEDTLS_MD5_C && MBEDTLS_FS_IO */
|
|
|
@ -1,186 +0,0 @@
|
||||||
/*
|
|
||||||
* sha1sum demonstration program
|
|
||||||
*
|
|
||||||
* Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
|
|
||||||
*
|
|
||||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
|
||||||
#include "mbedtls/config.h"
|
|
||||||
#else
|
|
||||||
#include MBEDTLS_CONFIG_FILE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_C)
|
|
||||||
#include "mbedtls/platform.h"
|
|
||||||
#else
|
|
||||||
#include <stdio.h>
|
|
||||||
#define mbedtls_fprintf fprintf
|
|
||||||
#define mbedtls_printf printf
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_FS_IO)
|
|
||||||
#include "mbedtls/sha1.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if !defined(MBEDTLS_SHA1_C) || !defined(MBEDTLS_FS_IO)
|
|
||||||
int main( void )
|
|
||||||
{
|
|
||||||
mbedtls_printf("MBEDTLS_SHA1_C and/or MBEDTLS_FS_IO not defined.\n");
|
|
||||||
return( 0 );
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
static int sha1_wrapper( char *filename, unsigned char *sum )
|
|
||||||
{
|
|
||||||
int ret = mbedtls_sha1_file( filename, sum );
|
|
||||||
|
|
||||||
if( ret == 1 )
|
|
||||||
mbedtls_fprintf( stderr, "failed to open: %s\n", filename );
|
|
||||||
|
|
||||||
if( ret == 2 )
|
|
||||||
mbedtls_fprintf( stderr, "failed to read: %s\n", filename );
|
|
||||||
|
|
||||||
return( ret );
|
|
||||||
}
|
|
||||||
|
|
||||||
static int sha1_print( char *filename )
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
unsigned char sum[20];
|
|
||||||
|
|
||||||
if( sha1_wrapper( filename, sum ) != 0 )
|
|
||||||
return( 1 );
|
|
||||||
|
|
||||||
for( i = 0; i < 20; i++ )
|
|
||||||
mbedtls_printf( "%02x", sum[i] );
|
|
||||||
|
|
||||||
mbedtls_printf( " %s\n", filename );
|
|
||||||
return( 0 );
|
|
||||||
}
|
|
||||||
|
|
||||||
static int sha1_check( char *filename )
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
size_t n;
|
|
||||||
FILE *f;
|
|
||||||
int nb_err1, nb_err2;
|
|
||||||
int nb_tot1, nb_tot2;
|
|
||||||
unsigned char sum[20];
|
|
||||||
char buf[41], line[1024];
|
|
||||||
char diff;
|
|
||||||
|
|
||||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
|
||||||
{
|
|
||||||
mbedtls_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 );
|
|
||||||
|
|
||||||
while( fgets( line, (int) n - 1, f ) != NULL )
|
|
||||||
{
|
|
||||||
n = strlen( line );
|
|
||||||
|
|
||||||
if( n < 44 )
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if( line[40] != ' ' || line[41] != ' ' )
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
|
|
||||||
if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
|
|
||||||
|
|
||||||
nb_tot1++;
|
|
||||||
|
|
||||||
if( sha1_wrapper( line + 42, sum ) != 0 )
|
|
||||||
{
|
|
||||||
nb_err1++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
nb_tot2++;
|
|
||||||
|
|
||||||
for( i = 0; i < 20; i++ )
|
|
||||||
sprintf( buf + i * 2, "%02x", sum[i] );
|
|
||||||
|
|
||||||
/* Use constant-time buffer comparison */
|
|
||||||
diff = 0;
|
|
||||||
for( i = 0; i < 40; i++ )
|
|
||||||
diff |= line[i] ^ buf[i];
|
|
||||||
|
|
||||||
if( diff != 0 )
|
|
||||||
{
|
|
||||||
nb_err2++;
|
|
||||||
mbedtls_fprintf( stderr, "wrong checksum: %s\n", line + 42 );
|
|
||||||
}
|
|
||||||
|
|
||||||
n = sizeof( line );
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose( f );
|
|
||||||
|
|
||||||
if( nb_err1 != 0 )
|
|
||||||
{
|
|
||||||
mbedtls_printf( "WARNING: %d (out of %d) input files could "
|
|
||||||
"not be read\n", nb_err1, nb_tot1 );
|
|
||||||
}
|
|
||||||
|
|
||||||
if( nb_err2 != 0 )
|
|
||||||
{
|
|
||||||
mbedtls_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 )
|
|
||||||
{
|
|
||||||
mbedtls_printf( "print mode: sha1sum <file> <file> ...\n" );
|
|
||||||
mbedtls_printf( "check mode: sha1sum -c <checksum file>\n" );
|
|
||||||
|
|
||||||
#if defined(_WIN32)
|
|
||||||
mbedtls_printf( "\n Press Enter to exit this program.\n" );
|
|
||||||
fflush( stdout ); getchar();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return( 1 );
|
|
||||||
}
|
|
||||||
|
|
||||||
if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
|
|
||||||
return( sha1_check( argv[2] ) );
|
|
||||||
|
|
||||||
ret = 0;
|
|
||||||
for( i = 1; i < argc; i++ )
|
|
||||||
ret |= sha1_print( argv[i] );
|
|
||||||
|
|
||||||
return( ret );
|
|
||||||
}
|
|
||||||
#endif /* MBEDTLS_SHA1_C && MBEDTLS_FS_IO */
|
|
|
@ -1,186 +0,0 @@
|
||||||
/*
|
|
||||||
* sha256sum demonstration program
|
|
||||||
*
|
|
||||||
* Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
|
|
||||||
*
|
|
||||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
|
||||||
#include "mbedtls/config.h"
|
|
||||||
#else
|
|
||||||
#include MBEDTLS_CONFIG_FILE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_C)
|
|
||||||
#include "mbedtls/platform.h"
|
|
||||||
#else
|
|
||||||
#include <stdio.h>
|
|
||||||
#define mbedtls_fprintf fprintf
|
|
||||||
#define mbedtls_printf printf
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_FS_IO)
|
|
||||||
#include "mbedtls/sha256.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_FS_IO)
|
|
||||||
int main( void )
|
|
||||||
{
|
|
||||||
mbedtls_printf("MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
|
|
||||||
return( 0 );
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
static int sha256_wrapper( char *filename, unsigned char *sum )
|
|
||||||
{
|
|
||||||
int ret = mbedtls_sha256_file( filename, sum, 0 );
|
|
||||||
|
|
||||||
if( ret == 1 )
|
|
||||||
mbedtls_fprintf( stderr, "failed to open: %s\n", filename );
|
|
||||||
|
|
||||||
if( ret == 2 )
|
|
||||||
mbedtls_fprintf( stderr, "failed to read: %s\n", filename );
|
|
||||||
|
|
||||||
return( ret );
|
|
||||||
}
|
|
||||||
|
|
||||||
static int sha256_print( char *filename )
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
unsigned char sum[32];
|
|
||||||
|
|
||||||
if( sha256_wrapper( filename, sum ) != 0 )
|
|
||||||
return( 1 );
|
|
||||||
|
|
||||||
for( i = 0; i < 32; i++ )
|
|
||||||
mbedtls_printf( "%02x", sum[i] );
|
|
||||||
|
|
||||||
mbedtls_printf( " %s\n", filename );
|
|
||||||
return( 0 );
|
|
||||||
}
|
|
||||||
|
|
||||||
static int sha256_check( char *filename )
|
|
||||||
{
|
|
||||||
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];
|
|
||||||
char diff;
|
|
||||||
|
|
||||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
|
||||||
{
|
|
||||||
mbedtls_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 );
|
|
||||||
|
|
||||||
while( fgets( line, (int) n - 1, f ) != NULL )
|
|
||||||
{
|
|
||||||
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++;
|
|
||||||
|
|
||||||
if( sha256_wrapper( line + 66, sum ) != 0 )
|
|
||||||
{
|
|
||||||
nb_err1++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
nb_tot2++;
|
|
||||||
|
|
||||||
for( i = 0; i < 32; i++ )
|
|
||||||
sprintf( buf + i * 2, "%02x", sum[i] );
|
|
||||||
|
|
||||||
/* Use constant-time buffer comparison */
|
|
||||||
diff = 0;
|
|
||||||
for( i = 0; i < 64; i++ )
|
|
||||||
diff |= line[i] ^ buf[i];
|
|
||||||
|
|
||||||
if( diff != 0 )
|
|
||||||
{
|
|
||||||
nb_err2++;
|
|
||||||
mbedtls_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
|
|
||||||
}
|
|
||||||
|
|
||||||
n = sizeof( line );
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose( f );
|
|
||||||
|
|
||||||
if( nb_err1 != 0 )
|
|
||||||
{
|
|
||||||
mbedtls_printf( "WARNING: %d (out of %d) input files could "
|
|
||||||
"not be read\n", nb_err1, nb_tot1 );
|
|
||||||
}
|
|
||||||
|
|
||||||
if( nb_err2 != 0 )
|
|
||||||
{
|
|
||||||
mbedtls_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 )
|
|
||||||
{
|
|
||||||
mbedtls_printf( "print mode: sha256sum <file> <file> ...\n" );
|
|
||||||
mbedtls_printf( "check mode: sha256sum -c <checksum file>\n" );
|
|
||||||
|
|
||||||
#if defined(_WIN32)
|
|
||||||
mbedtls_printf( "\n Press Enter to exit this program.\n" );
|
|
||||||
fflush( stdout ); getchar();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return( 1 );
|
|
||||||
}
|
|
||||||
|
|
||||||
if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
|
|
||||||
return( sha256_check( argv[2] ) );
|
|
||||||
|
|
||||||
ret = 0;
|
|
||||||
for( i = 1; i < argc; i++ )
|
|
||||||
ret |= sha256_print( argv[i] );
|
|
||||||
|
|
||||||
return( ret );
|
|
||||||
}
|
|
||||||
#endif /* MBEDTLS_SHA256_C && MBEDTLS_FS_IO */
|
|
Loading…
Reference in a new issue