1 /*
2  *  generic message digest layer demonstration program
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  *
7  *  This file is provided under the Apache License 2.0, or the
8  *  GNU General Public License v2.0 or later.
9  *
10  *  **********
11  *  Apache License 2.0:
12  *
13  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
14  *  not use this file except in compliance with the License.
15  *  You may obtain a copy of the License at
16  *
17  *  http://www.apache.org/licenses/LICENSE-2.0
18  *
19  *  Unless required by applicable law or agreed to in writing, software
20  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the License for the specific language governing permissions and
23  *  limitations under the License.
24  *
25  *  **********
26  *
27  *  **********
28  *  GNU General Public License v2.0 or later:
29  *
30  *  This program is free software; you can redistribute it and/or modify
31  *  it under the terms of the GNU General Public License as published by
32  *  the Free Software Foundation; either version 2 of the License, or
33  *  (at your option) any later version.
34  *
35  *  This program is distributed in the hope that it will be useful,
36  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
37  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38  *  GNU General Public License for more details.
39  *
40  *  You should have received a copy of the GNU General Public License along
41  *  with this program; if not, write to the Free Software Foundation, Inc.,
42  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43  *
44  *  **********
45  */
46 
47 #if !defined(MBEDTLS_CONFIG_FILE)
48 #include "mbedtls/config.h"
49 #else
50 #include MBEDTLS_CONFIG_FILE
51 #endif
52 
53 #if defined(MBEDTLS_PLATFORM_C)
54 #include "mbedtls/platform.h"
55 #else
56 #include <stdio.h>
57 #include <stdlib.h>
58 #define mbedtls_fprintf         fprintf
59 #define mbedtls_printf          printf
60 #define mbedtls_exit            exit
61 #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
62 #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
63 #endif /* MBEDTLS_PLATFORM_C */
64 
65 #if defined(MBEDTLS_MD_C) && defined(MBEDTLS_FS_IO)
66 #include "mbedtls/md.h"
67 
68 #include <stdio.h>
69 #include <string.h>
70 #endif
71 
72 #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_FS_IO)
main(void)73 int main( void )
74 {
75     mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
76     mbedtls_exit( 0 );
77 }
78 #else
79 
80 
generic_wrapper(const mbedtls_md_info_t * md_info,char * filename,unsigned char * sum)81 static int generic_wrapper( const mbedtls_md_info_t *md_info, char *filename, unsigned char *sum )
82 {
83     int ret = mbedtls_md_file( md_info, filename, sum );
84 
85     if( ret == 1 )
86         mbedtls_fprintf( stderr, "failed to open: %s\n", filename );
87 
88     if( ret == 2 )
89         mbedtls_fprintf( stderr, "failed to read: %s\n", filename );
90 
91     return( ret );
92 }
93 
generic_print(const mbedtls_md_info_t * md_info,char * filename)94 static int generic_print( const mbedtls_md_info_t *md_info, char *filename )
95 {
96     int i;
97     unsigned char sum[MBEDTLS_MD_MAX_SIZE];
98 
99     if( generic_wrapper( md_info, filename, sum ) != 0 )
100         return( 1 );
101 
102     for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
103         mbedtls_printf( "%02x", sum[i] );
104 
105     mbedtls_printf( "  %s\n", filename );
106     return( 0 );
107 }
108 
generic_check(const mbedtls_md_info_t * md_info,char * filename)109 static int generic_check( const mbedtls_md_info_t *md_info, char *filename )
110 {
111     int i;
112     size_t n;
113     FILE *f;
114     int nb_err1, nb_err2;
115     int nb_tot1, nb_tot2;
116     unsigned char sum[MBEDTLS_MD_MAX_SIZE];
117     char line[1024];
118     char diff;
119 #if defined(__clang_analyzer__)
120     char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1] = { };
121 #else
122     char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1];
123 #endif
124 
125     if( ( f = fopen( filename, "rb" ) ) == NULL )
126     {
127         mbedtls_printf( "failed to open: %s\n", filename );
128         return( 1 );
129     }
130 
131     nb_err1 = nb_err2 = 0;
132     nb_tot1 = nb_tot2 = 0;
133 
134     memset( line, 0, sizeof( line ) );
135 
136     n = sizeof( line );
137 
138     while( fgets( line, (int) n - 1, f ) != NULL )
139     {
140         n = strlen( line );
141 
142         if( n < (size_t) 2 * mbedtls_md_get_size( md_info ) + 4 )
143         {
144             mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
145             continue;
146         }
147 
148         if( line[2 * mbedtls_md_get_size( md_info )] != ' ' || line[2 * mbedtls_md_get_size( md_info ) + 1] != ' ' )
149         {
150             mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
151             continue;
152         }
153 
154         if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
155         if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
156 
157         nb_tot1++;
158 
159         if( generic_wrapper( md_info, line + 2 + 2 * mbedtls_md_get_size( md_info ), sum ) != 0 )
160         {
161             nb_err1++;
162             continue;
163         }
164 
165         nb_tot2++;
166 
167         for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
168             sprintf( buf + i * 2, "%02x", sum[i] );
169 
170         /* Use constant-time buffer comparison */
171         diff = 0;
172         for( i = 0; i < 2 * mbedtls_md_get_size( md_info ); i++ )
173             diff |= line[i] ^ buf[i];
174 
175         if( diff != 0 )
176         {
177             nb_err2++;
178             mbedtls_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
179         }
180 
181         n = sizeof( line );
182     }
183 
184     if( nb_err1 != 0 )
185     {
186         mbedtls_printf( "WARNING: %d (out of %d) input files could "
187                 "not be read\n", nb_err1, nb_tot1 );
188     }
189 
190     if( nb_err2 != 0 )
191     {
192         mbedtls_printf( "WARNING: %d (out of %d) computed checksums did "
193                 "not match\n", nb_err2, nb_tot2 );
194     }
195 
196     fclose( f );
197 
198     return( nb_err1 != 0 || nb_err2 != 0 );
199 }
200 
main(int argc,char * argv[])201 int main( int argc, char *argv[] )
202 {
203     int ret = 1, i;
204     int exit_code = MBEDTLS_EXIT_FAILURE;
205     const mbedtls_md_info_t *md_info;
206     mbedtls_md_context_t md_ctx;
207 
208     mbedtls_md_init( &md_ctx );
209 
210     if( argc == 1 )
211     {
212         const int *list;
213 
214         mbedtls_printf( "print mode:  generic_sum <mbedtls_md> <file> <file> ...\n" );
215         mbedtls_printf( "check mode:  generic_sum <mbedtls_md> -c <checksum file>\n" );
216 
217         mbedtls_printf( "\nAvailable message digests:\n" );
218         list = mbedtls_md_list();
219         while( *list )
220         {
221             md_info = mbedtls_md_info_from_type( *list );
222             mbedtls_printf( "  %s\n", mbedtls_md_get_name( md_info ) );
223             list++;
224         }
225 
226 #if defined(_WIN32)
227         mbedtls_printf( "\n  Press Enter to exit this program.\n" );
228         fflush( stdout ); getchar();
229 #endif
230 
231         mbedtls_exit( exit_code );
232     }
233 
234     /*
235      * Read the MD from the command line
236      */
237     md_info = mbedtls_md_info_from_string( argv[1] );
238     if( md_info == NULL )
239     {
240         mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
241         mbedtls_exit( exit_code );
242     }
243     if( mbedtls_md_setup( &md_ctx, md_info, 0 ) )
244     {
245         mbedtls_fprintf( stderr, "Failed to initialize context.\n" );
246         mbedtls_exit( exit_code );
247     }
248 
249     ret = 0;
250     if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
251     {
252         ret |= generic_check( md_info, argv[3] );
253         goto exit;
254     }
255 
256     for( i = 2; i < argc; i++ )
257         ret |= generic_print( md_info, argv[i] );
258 
259     if ( ret == 0 )
260         exit_code = MBEDTLS_EXIT_SUCCESS;
261 
262 exit:
263     mbedtls_md_free( &md_ctx );
264 
265     mbedtls_exit( exit_code );
266 }
267 #endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */
268