2 * generic message digest layer demonstration program
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
33 #define mbedtls_fprintf fprintf
34 #define mbedtls_printf printf
35 #define mbedtls_exit exit
36 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
37 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
38 #endif /* MBEDTLS_PLATFORM_C */
40 #if defined(MBEDTLS_MD_C) && defined(MBEDTLS_FS_IO)
41 #include "mbedtls/md.h"
47 #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_FS_IO)
50 mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
55 #if defined(MBEDTLS_CHECK_PARAMS)
56 #include "mbedtls/platform_util.h"
57 void mbedtls_param_failed( const char *failure_condition,
61 mbedtls_printf( "%s:%i: Input param failed - %s\n",
62 file, line, failure_condition );
63 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
67 static int generic_wrapper( const mbedtls_md_info_t *md_info, char *filename, unsigned char *sum )
69 int ret = mbedtls_md_file( md_info, filename, sum );
72 mbedtls_fprintf( stderr, "failed to open: %s\n", filename );
75 mbedtls_fprintf( stderr, "failed to read: %s\n", filename );
80 static int generic_print( const mbedtls_md_info_t *md_info, char *filename )
83 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
85 if( generic_wrapper( md_info, filename, sum ) != 0 )
88 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
89 mbedtls_printf( "%02x", sum[i] );
91 mbedtls_printf( " %s\n", filename );
95 static int generic_check( const mbedtls_md_info_t *md_info, char *filename )
100 int nb_err1, nb_err2;
101 int nb_tot1, nb_tot2;
102 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
105 #if defined(__clang_analyzer__)
106 char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1] = { };
108 char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1];
111 if( ( f = fopen( filename, "rb" ) ) == NULL )
113 mbedtls_printf( "failed to open: %s\n", filename );
117 nb_err1 = nb_err2 = 0;
118 nb_tot1 = nb_tot2 = 0;
120 memset( line, 0, sizeof( line ) );
124 while( fgets( line, (int) n - 1, f ) != NULL )
128 if( n < (size_t) 2 * mbedtls_md_get_size( md_info ) + 4 )
130 mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
134 if( line[2 * mbedtls_md_get_size( md_info )] != ' ' || line[2 * mbedtls_md_get_size( md_info ) + 1] != ' ' )
136 mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
140 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
141 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
145 if( generic_wrapper( md_info, line + 2 + 2 * mbedtls_md_get_size( md_info ), sum ) != 0 )
153 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
154 sprintf( buf + i * 2, "%02x", sum[i] );
156 /* Use constant-time buffer comparison */
158 for( i = 0; i < 2 * mbedtls_md_get_size( md_info ); i++ )
159 diff |= line[i] ^ buf[i];
164 mbedtls_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
172 mbedtls_printf( "WARNING: %d (out of %d) input files could "
173 "not be read\n", nb_err1, nb_tot1 );
178 mbedtls_printf( "WARNING: %d (out of %d) computed checksums did "
179 "not match\n", nb_err2, nb_tot2 );
184 return( nb_err1 != 0 || nb_err2 != 0 );
187 int main( int argc, char *argv[] )
190 int exit_code = MBEDTLS_EXIT_FAILURE;
191 const mbedtls_md_info_t *md_info;
192 mbedtls_md_context_t md_ctx;
194 mbedtls_md_init( &md_ctx );
200 mbedtls_printf( "print mode: generic_sum <mbedtls_md> <file> <file> ...\n" );
201 mbedtls_printf( "check mode: generic_sum <mbedtls_md> -c <checksum file>\n" );
203 mbedtls_printf( "\nAvailable message digests:\n" );
204 list = mbedtls_md_list();
207 md_info = mbedtls_md_info_from_type( *list );
208 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
213 mbedtls_printf( "\n Press Enter to exit this program.\n" );
214 fflush( stdout ); getchar();
221 * Read the MD from the command line
223 md_info = mbedtls_md_info_from_string( argv[1] );
224 if( md_info == NULL )
226 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
229 if( mbedtls_md_setup( &md_ctx, md_info, 0 ) )
231 mbedtls_fprintf( stderr, "Failed to initialize context.\n" );
236 if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
238 ret |= generic_check( md_info, argv[3] );
242 for( i = 2; i < argc; i++ )
243 ret |= generic_print( md_info, argv[i] );
246 exit_code = MBEDTLS_EXIT_SUCCESS;
249 mbedtls_md_free( &md_ctx );
253 #endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */