blob: d6c5cf85d57cc7b2cd142f5faefe73bfac14c840 [file] [log] [blame]
Paul Bakkerfb6c7e22011-01-21 10:21:11 +00001/*
2 * generic message digest layer demonstration program
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * 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:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
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.
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
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 * **********
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000055#else
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +010057#include <stdlib.h>
58#define mbedtls_fprintf fprintf
59#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010060#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010061#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +010062#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
63#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_MD_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/md.h"
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000067
Rich Evans18b78c72015-02-11 14:06:19 +000068#include <stdio.h>
69#include <string.h>
70#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000073int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000074{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020076 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000077}
78#else
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010079
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081static int generic_wrapper( const mbedtls_md_info_t *md_info, char *filename, unsigned char *sum )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000082{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 int ret = mbedtls_md_file( md_info, filename, sum );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000084
85 if( ret == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_fprintf( stderr, "failed to open: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000087
88 if( ret == 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_fprintf( stderr, "failed to read: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000090
91 return( ret );
92}
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094static int generic_print( const mbedtls_md_info_t *md_info, char *filename )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000095{
96 int i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000098
99 if( generic_wrapper( md_info, filename, sum ) != 0 )
100 return( 1 );
101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
103 mbedtls_printf( "%02x", sum[i] );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_printf( " %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000106 return( 0 );
107}
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109static int generic_check( const mbedtls_md_info_t *md_info, char *filename )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000110{
111 int i;
112 size_t n;
113 FILE *f;
114 int nb_err1, nb_err2;
115 int nb_tot1, nb_tot2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerd1fe7aa2016-05-12 12:46:02 +0100117 char line[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100118 char diff;
Paul Bakkerd1fe7aa2016-05-12 12:46:02 +0100119#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
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000124
125 if( ( f = fopen( filename, "rb" ) ) == NULL )
126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_printf( "failed to open: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000128 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
Paul Bakker23986e52011-04-24 08:57:21 +0000138 while( fgets( line, (int) n - 1, f ) != NULL )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000139 {
140 n = strlen( line );
141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 if( n < (size_t) 2 * mbedtls_md_get_size( md_info ) + 4 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000145 continue;
146 }
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 if( line[2 * mbedtls_md_get_size( md_info )] != ' ' || line[2 * mbedtls_md_get_size( md_info ) + 1] != ' ' )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000151 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 if( generic_wrapper( md_info, line + 2 + 2 * mbedtls_md_get_size( md_info ), sum ) != 0 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000160 {
161 nb_err1++;
162 continue;
163 }
164
165 nb_tot2++;
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000168 sprintf( buf + i * 2, "%02x", sum[i] );
169
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100170 /* Use constant-time buffer comparison */
171 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 for( i = 0; i < 2 * mbedtls_md_get_size( md_info ); i++ )
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100173 diff |= line[i] ^ buf[i];
174
175 if( diff != 0 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000176 {
177 nb_err2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000179 }
180
181 n = sizeof( line );
182 }
183
184 if( nb_err1 != 0 )
185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_printf( "WARNING: %d (out of %d) input files could "
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000187 "not be read\n", nb_err1, nb_tot1 );
188 }
189
190 if( nb_err2 != 0 )
191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_printf( "WARNING: %d (out of %d) computed checksums did "
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000193 "not match\n", nb_err2, nb_tot2 );
194 }
195
Paul Bakker64abd832014-02-06 15:03:06 +0100196 fclose( f );
197
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000198 return( nb_err1 != 0 || nb_err2 != 0 );
199}
200
201int main( int argc, char *argv[] )
202{
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +0100203 int ret = 1, i;
204 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 const mbedtls_md_info_t *md_info;
206 mbedtls_md_context_t md_ctx;
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_md_init( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000209
210 if( argc == 1 )
211 {
212 const int *list;
213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_printf( "print mode: generic_sum <mbedtls_md> <file> <file> ...\n" );
215 mbedtls_printf( "check mode: generic_sum <mbedtls_md> -c <checksum file>\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_printf( "\nAvailable message digests:\n" );
218 list = mbedtls_md_list();
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000219 while( *list )
220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 md_info = mbedtls_md_info_from_type( *list );
222 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000223 list++;
224 }
225
Paul Bakkercce9d772011-11-18 14:26:47 +0000226#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000228 fflush( stdout ); getchar();
229#endif
230
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200231 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000232 }
233
234 /*
235 * Read the MD from the command line
236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 md_info = mbedtls_md_info_from_string( argv[1] );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000238 if( md_info == NULL )
239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200241 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000242 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 if( mbedtls_md_setup( &md_ctx, md_info, 0 ) )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_fprintf( stderr, "Failed to initialize context.\n" );
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200246 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000247 }
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000248
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
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000256 for( i = 2; i < argc; i++ )
257 ret |= generic_print( md_info, argv[i] );
258
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +0100259 if ( ret == 0 )
260 exit_code = MBEDTLS_EXIT_SUCCESS;
261
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000262exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 mbedtls_md_free( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000264
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200265 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000266}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */