blob: 2080b4e3382a5d8981e8d00bb3aa52a470c15d59 [file] [log] [blame]
Paul Bakkerfb6c7e22011-01-21 10:21:11 +00001/*
2 * generic message digest layer demonstration program
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-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úti4e9f7122020-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 Garcia77d2de22018-04-29 22:35:36 +010057#include <stdlib.h>
58#define mbedtls_fprintf fprintf
59#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020060#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010061#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia77d2de22018-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 Stachowiaka0865222019-04-24 14:24:46 +020076 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000077}
78#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079static int generic_wrapper( const mbedtls_md_info_t *md_info, char *filename, unsigned char *sum )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000080{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 int ret = mbedtls_md_file( md_info, filename, sum );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000082
83 if( ret == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_fprintf( stderr, "failed to open: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000085
86 if( ret == 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_fprintf( stderr, "failed to read: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000088
89 return( ret );
90}
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092static int generic_print( const mbedtls_md_info_t *md_info, char *filename )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000093{
94 int i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000096
97 if( generic_wrapper( md_info, filename, sum ) != 0 )
98 return( 1 );
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
101 mbedtls_printf( "%02x", sum[i] );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_printf( " %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000104 return( 0 );
105}
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107static int generic_check( const mbedtls_md_info_t *md_info, char *filename )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000108{
109 int i;
110 size_t n;
111 FILE *f;
112 int nb_err1, nb_err2;
113 int nb_tot1, nb_tot2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerd1fe7aa2016-05-12 12:46:02 +0100115 char line[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100116 char diff;
Paul Bakkerd1fe7aa2016-05-12 12:46:02 +0100117#if defined(__clang_analyzer__)
118 char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1] = { };
119#else
120 char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1];
121#endif
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000122
123 if( ( f = fopen( filename, "rb" ) ) == NULL )
124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_printf( "failed to open: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000126 return( 1 );
127 }
128
129 nb_err1 = nb_err2 = 0;
130 nb_tot1 = nb_tot2 = 0;
131
132 memset( line, 0, sizeof( line ) );
133
134 n = sizeof( line );
135
Paul Bakker23986e52011-04-24 08:57:21 +0000136 while( fgets( line, (int) n - 1, f ) != NULL )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000137 {
138 n = strlen( line );
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 if( n < (size_t) 2 * mbedtls_md_get_size( md_info ) + 4 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000143 continue;
144 }
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 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 +0000147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000149 continue;
150 }
151
152 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
153 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
154
155 nb_tot1++;
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 if( generic_wrapper( md_info, line + 2 + 2 * mbedtls_md_get_size( md_info ), sum ) != 0 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000158 {
159 nb_err1++;
160 continue;
161 }
162
163 nb_tot2++;
164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000166 sprintf( buf + i * 2, "%02x", sum[i] );
167
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100168 /* Use constant-time buffer comparison */
169 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 for( i = 0; i < 2 * mbedtls_md_get_size( md_info ); i++ )
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100171 diff |= line[i] ^ buf[i];
172
173 if( diff != 0 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000174 {
175 nb_err2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000177 }
178
179 n = sizeof( line );
180 }
181
182 if( nb_err1 != 0 )
183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_printf( "WARNING: %d (out of %d) input files could "
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000185 "not be read\n", nb_err1, nb_tot1 );
186 }
187
188 if( nb_err2 != 0 )
189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_printf( "WARNING: %d (out of %d) computed checksums did "
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000191 "not match\n", nb_err2, nb_tot2 );
192 }
193
Paul Bakker64abd832014-02-06 15:03:06 +0100194 fclose( f );
195
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000196 return( nb_err1 != 0 || nb_err2 != 0 );
197}
198
199int main( int argc, char *argv[] )
200{
Andres Amaya Garcia77d2de22018-04-29 22:35:36 +0100201 int ret = 1, i;
202 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 const mbedtls_md_info_t *md_info;
204 mbedtls_md_context_t md_ctx;
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_md_init( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000207
208 if( argc == 1 )
209 {
210 const int *list;
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 mbedtls_printf( "print mode: generic_sum <mbedtls_md> <file> <file> ...\n" );
213 mbedtls_printf( "check mode: generic_sum <mbedtls_md> -c <checksum file>\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_printf( "\nAvailable message digests:\n" );
216 list = mbedtls_md_list();
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000217 while( *list )
218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 md_info = mbedtls_md_info_from_type( *list );
220 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000221 list++;
222 }
223
Paul Bakkercce9d772011-11-18 14:26:47 +0000224#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000226 fflush( stdout ); getchar();
227#endif
228
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200229 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000230 }
231
232 /*
233 * Read the MD from the command line
234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 md_info = mbedtls_md_info_from_string( argv[1] );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000236 if( md_info == NULL )
237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200239 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000240 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 if( mbedtls_md_setup( &md_ctx, md_info, 0 ) )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_fprintf( stderr, "Failed to initialize context.\n" );
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200244 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000245 }
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000246
247 ret = 0;
248 if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
249 {
250 ret |= generic_check( md_info, argv[3] );
251 goto exit;
252 }
253
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000254 for( i = 2; i < argc; i++ )
255 ret |= generic_print( md_info, argv[i] );
256
Andres Amaya Garcia77d2de22018-04-29 22:35:36 +0100257 if ( ret == 0 )
258 exit_code = MBEDTLS_EXIT_SUCCESS;
259
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000260exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_md_free( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000262
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200263 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000264}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */