1 /*
2  *  generic message digest layer demonstration program
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: Apache-2.0
6  *
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
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21 
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27 
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
30 #else
31 #include <stdio.h>
32 #define mbedtls_fprintf    fprintf
33 #define mbedtls_printf     printf
34 #endif
35 
36 #if defined(MBEDTLS_MD_C) && defined(MBEDTLS_FS_IO)
37 #include "mbedtls/md.h"
38 
39 #include <stdio.h>
40 #include <string.h>
41 #endif
42 
43 #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_FS_IO)
main(void)44 int main( void )
45 {
46     mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
47     return( 0 );
48 }
49 #else
generic_wrapper(const mbedtls_md_info_t * md_info,char * filename,unsigned char * sum)50 static int generic_wrapper( const mbedtls_md_info_t *md_info, char *filename, unsigned char *sum )
51 {
52     int ret = mbedtls_md_file( md_info, filename, sum );
53 
54     if( ret == 1 )
55         mbedtls_fprintf( stderr, "failed to open: %s\n", filename );
56 
57     if( ret == 2 )
58         mbedtls_fprintf( stderr, "failed to read: %s\n", filename );
59 
60     return( ret );
61 }
62 
generic_print(const mbedtls_md_info_t * md_info,char * filename)63 static int generic_print( const mbedtls_md_info_t *md_info, char *filename )
64 {
65     int i;
66     unsigned char sum[MBEDTLS_MD_MAX_SIZE];
67 
68     if( generic_wrapper( md_info, filename, sum ) != 0 )
69         return( 1 );
70 
71     for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
72         mbedtls_printf( "%02x", sum[i] );
73 
74     mbedtls_printf( "  %s\n", filename );
75     return( 0 );
76 }
77 
generic_check(const mbedtls_md_info_t * md_info,char * filename)78 static int generic_check( const mbedtls_md_info_t *md_info, char *filename )
79 {
80     int i;
81     size_t n;
82     FILE *f;
83     int nb_err1, nb_err2;
84     int nb_tot1, nb_tot2;
85     unsigned char sum[MBEDTLS_MD_MAX_SIZE];
86     char line[1024];
87     char diff;
88 #if defined(__clang_analyzer__)
89     char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1] = { };
90 #else
91     char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1];
92 #endif
93 
94     if( ( f = fopen( filename, "rb" ) ) == NULL )
95     {
96         mbedtls_printf( "failed to open: %s\n", filename );
97         return( 1 );
98     }
99 
100     nb_err1 = nb_err2 = 0;
101     nb_tot1 = nb_tot2 = 0;
102 
103     memset( line, 0, sizeof( line ) );
104 
105     n = sizeof( line );
106 
107     while( fgets( line, (int) n - 1, f ) != NULL )
108     {
109         n = strlen( line );
110 
111         if( n < (size_t) 2 * mbedtls_md_get_size( md_info ) + 4 )
112         {
113             mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
114             continue;
115         }
116 
117         if( line[2 * mbedtls_md_get_size( md_info )] != ' ' || line[2 * mbedtls_md_get_size( md_info ) + 1] != ' ' )
118         {
119             mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
120             continue;
121         }
122 
123         if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
124         if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
125 
126         nb_tot1++;
127 
128         if( generic_wrapper( md_info, line + 2 + 2 * mbedtls_md_get_size( md_info ), sum ) != 0 )
129         {
130             nb_err1++;
131             continue;
132         }
133 
134         nb_tot2++;
135 
136         for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
137             sprintf( buf + i * 2, "%02x", sum[i] );
138 
139         /* Use constant-time buffer comparison */
140         diff = 0;
141         for( i = 0; i < 2 * mbedtls_md_get_size( md_info ); i++ )
142             diff |= line[i] ^ buf[i];
143 
144         if( diff != 0 )
145         {
146             nb_err2++;
147             mbedtls_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
148         }
149 
150         n = sizeof( line );
151     }
152 
153     if( nb_err1 != 0 )
154     {
155         mbedtls_printf( "WARNING: %d (out of %d) input files could "
156                 "not be read\n", nb_err1, nb_tot1 );
157     }
158 
159     if( nb_err2 != 0 )
160     {
161         mbedtls_printf( "WARNING: %d (out of %d) computed checksums did "
162                 "not match\n", nb_err2, nb_tot2 );
163     }
164 
165     fclose( f );
166 
167     return( nb_err1 != 0 || nb_err2 != 0 );
168 }
169 
main(int argc,char * argv[])170 int main( int argc, char *argv[] )
171 {
172     int ret, i;
173     const mbedtls_md_info_t *md_info;
174     mbedtls_md_context_t md_ctx;
175 
176     mbedtls_md_init( &md_ctx );
177 
178     if( argc == 1 )
179     {
180         const int *list;
181 
182         mbedtls_printf( "print mode:  generic_sum <mbedtls_md> <file> <file> ...\n" );
183         mbedtls_printf( "check mode:  generic_sum <mbedtls_md> -c <checksum file>\n" );
184 
185         mbedtls_printf( "\nAvailable message digests:\n" );
186         list = mbedtls_md_list();
187         while( *list )
188         {
189             md_info = mbedtls_md_info_from_type( *list );
190             mbedtls_printf( "  %s\n", mbedtls_md_get_name( md_info ) );
191             list++;
192         }
193 
194 #if defined(_WIN32)
195         mbedtls_printf( "\n  Press Enter to exit this program.\n" );
196         fflush( stdout ); getchar();
197 #endif
198 
199         return( 1 );
200     }
201 
202     /*
203      * Read the MD from the command line
204      */
205     md_info = mbedtls_md_info_from_string( argv[1] );
206     if( md_info == NULL )
207     {
208         mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
209         return( 1 );
210     }
211     if( mbedtls_md_setup( &md_ctx, md_info, 0 ) )
212     {
213         mbedtls_fprintf( stderr, "Failed to initialize context.\n" );
214         return( 1 );
215     }
216 
217     ret = 0;
218     if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
219     {
220         ret |= generic_check( md_info, argv[3] );
221         goto exit;
222     }
223 
224     for( i = 2; i < argc; i++ )
225         ret |= generic_print( md_info, argv[i] );
226 
227 exit:
228     mbedtls_md_free( &md_ctx );
229 
230     return( ret );
231 }
232 #endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */
233