2011-01-21 10:21:11 +00:00
|
|
|
/*
|
|
|
|
* generic message digest layer demonstration program
|
|
|
|
*
|
2020-08-07 11:07:28 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 12:21:07 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2011-01-21 10:21:11 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-01-21 10:21:11 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-01-21 10:21:11 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2011-01-21 10:21:11 +00:00
|
|
|
*/
|
|
|
|
|
2021-05-27 09:25:03 +00:00
|
|
|
#include "mbedtls/build_info.h"
|
2011-01-21 10:21:11 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/platform.h"
|
2015-01-19 14:26:37 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_MD_C) && defined(MBEDTLS_FS_IO)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/md.h"
|
2011-01-21 10:21:11 +00:00
|
|
|
|
2015-02-11 14:06:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_FS_IO)
|
2015-02-12 11:37:29 +00:00
|
|
|
int main( void )
|
2011-05-26 13:16:06 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
|
2019-04-24 12:24:46 +00:00
|
|
|
mbedtls_exit( 0 );
|
2011-05-26 13:16:06 +00:00
|
|
|
}
|
|
|
|
#else
|
2018-12-10 13:31:45 +00:00
|
|
|
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
static int generic_wrapper( const mbedtls_md_info_t *md_info, char *filename, unsigned char *sum )
|
2011-01-21 10:21:11 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
int ret = mbedtls_md_file( md_info, filename, sum );
|
2011-01-21 10:21:11 +00:00
|
|
|
|
|
|
|
if( ret == 1 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "failed to open: %s\n", filename );
|
2011-01-21 10:21:11 +00:00
|
|
|
|
|
|
|
if( ret == 2 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "failed to read: %s\n", filename );
|
2011-01-21 10:21:11 +00:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
static int generic_print( const mbedtls_md_info_t *md_info, char *filename )
|
2011-01-21 10:21:11 +00:00
|
|
|
{
|
|
|
|
int i;
|
2015-04-08 10:49:31 +00:00
|
|
|
unsigned char sum[MBEDTLS_MD_MAX_SIZE];
|
2011-01-21 10:21:11 +00:00
|
|
|
|
|
|
|
if( generic_wrapper( md_info, filename, sum ) != 0 )
|
|
|
|
return( 1 );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
|
|
|
|
mbedtls_printf( "%02x", sum[i] );
|
2011-01-21 10:21:11 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " %s\n", filename );
|
2011-01-21 10:21:11 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
static int generic_check( const mbedtls_md_info_t *md_info, char *filename )
|
2011-01-21 10:21:11 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
size_t n;
|
|
|
|
FILE *f;
|
|
|
|
int nb_err1, nb_err2;
|
|
|
|
int nb_tot1, nb_tot2;
|
2015-04-08 10:49:31 +00:00
|
|
|
unsigned char sum[MBEDTLS_MD_MAX_SIZE];
|
2016-05-12 11:46:02 +00:00
|
|
|
char line[1024];
|
2013-10-28 11:51:32 +00:00
|
|
|
char diff;
|
2016-05-12 11:46:02 +00:00
|
|
|
#if defined(__clang_analyzer__)
|
|
|
|
char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1] = { };
|
|
|
|
#else
|
|
|
|
char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1];
|
|
|
|
#endif
|
2011-01-21 10:21:11 +00:00
|
|
|
|
|
|
|
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "failed to open: %s\n", filename );
|
2011-01-21 10:21:11 +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 )
|
2011-01-21 10:21:11 +00:00
|
|
|
{
|
|
|
|
n = strlen( line );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( n < (size_t) 2 * mbedtls_md_get_size( md_info ) + 4 )
|
2011-01-21 10:21:11 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
|
2011-01-21 10:21:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( line[2 * mbedtls_md_get_size( md_info )] != ' ' || line[2 * mbedtls_md_get_size( md_info ) + 1] != ' ' )
|
2011-01-21 10:21:11 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
|
2011-01-21 10:21:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
|
|
|
|
if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
|
|
|
|
|
|
|
|
nb_tot1++;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( generic_wrapper( md_info, line + 2 + 2 * mbedtls_md_get_size( md_info ), sum ) != 0 )
|
2011-01-21 10:21:11 +00:00
|
|
|
{
|
|
|
|
nb_err1++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nb_tot2++;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
|
2011-01-21 10:21:11 +00:00
|
|
|
sprintf( buf + i * 2, "%02x", sum[i] );
|
|
|
|
|
2013-10-28 11:51:32 +00:00
|
|
|
/* Use constant-time buffer comparison */
|
|
|
|
diff = 0;
|
2015-04-08 10:49:31 +00:00
|
|
|
for( i = 0; i < 2 * mbedtls_md_get_size( md_info ); i++ )
|
2013-10-28 11:51:32 +00:00
|
|
|
diff |= line[i] ^ buf[i];
|
|
|
|
|
|
|
|
if( diff != 0 )
|
2011-01-21 10:21:11 +00:00
|
|
|
{
|
|
|
|
nb_err2++;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
|
2011-01-21 10:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n = sizeof( line );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( nb_err1 != 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "WARNING: %d (out of %d) input files could "
|
2011-01-21 10:21:11 +00:00
|
|
|
"not be read\n", nb_err1, nb_tot1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( nb_err2 != 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "WARNING: %d (out of %d) computed checksums did "
|
2011-01-21 10:21:11 +00:00
|
|
|
"not match\n", nb_err2, nb_tot2 );
|
|
|
|
}
|
|
|
|
|
2014-02-06 14:03:06 +00:00
|
|
|
fclose( f );
|
|
|
|
|
2011-01-21 10:21:11 +00:00
|
|
|
return( nb_err1 != 0 || nb_err2 != 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int main( int argc, char *argv[] )
|
|
|
|
{
|
2018-04-29 21:35:36 +00:00
|
|
|
int ret = 1, i;
|
|
|
|
int exit_code = MBEDTLS_EXIT_FAILURE;
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_md_info_t *md_info;
|
|
|
|
mbedtls_md_context_t md_ctx;
|
2011-01-21 10:21:11 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_init( &md_ctx );
|
2011-01-21 10:21:11 +00:00
|
|
|
|
|
|
|
if( argc == 1 )
|
|
|
|
{
|
|
|
|
const int *list;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "print mode: generic_sum <mbedtls_md> <file> <file> ...\n" );
|
|
|
|
mbedtls_printf( "check mode: generic_sum <mbedtls_md> -c <checksum file>\n" );
|
2011-01-21 10:21:11 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "\nAvailable message digests:\n" );
|
|
|
|
list = mbedtls_md_list();
|
2011-01-21 10:21:11 +00:00
|
|
|
while( *list )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
md_info = mbedtls_md_info_from_type( *list );
|
|
|
|
mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
|
2011-01-21 10:21:11 +00:00
|
|
|
list++;
|
|
|
|
}
|
|
|
|
|
2019-04-24 12:24:46 +00:00
|
|
|
mbedtls_exit( exit_code );
|
2011-01-21 10:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the MD from the command line
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
md_info = mbedtls_md_info_from_string( argv[1] );
|
2011-01-21 10:21:11 +00:00
|
|
|
if( md_info == NULL )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
|
2019-04-24 12:24:46 +00:00
|
|
|
mbedtls_exit( exit_code );
|
2011-01-21 10:21:11 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
if( mbedtls_md_setup( &md_ctx, md_info, 0 ) )
|
2011-01-21 10:21:11 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "Failed to initialize context.\n" );
|
2019-04-24 12:24:46 +00:00
|
|
|
mbedtls_exit( exit_code );
|
2011-01-21 10:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
|
|
|
|
{
|
|
|
|
ret |= generic_check( md_info, argv[3] );
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 2; i < argc; i++ )
|
|
|
|
ret |= generic_print( md_info, argv[i] );
|
|
|
|
|
2018-04-29 21:35:36 +00:00
|
|
|
if ( ret == 0 )
|
|
|
|
exit_code = MBEDTLS_EXIT_SUCCESS;
|
|
|
|
|
2011-01-21 10:21:11 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_free( &md_ctx );
|
2011-01-21 10:21:11 +00:00
|
|
|
|
2019-04-24 12:24:46 +00:00
|
|
|
mbedtls_exit( exit_code );
|
2011-01-21 10:21:11 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */
|