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