blob: bcdc62dbc7930ea29a33e92ac1595905a1c95382 [file] [log] [blame]
Paul Bakkerfb6c7e22011-01-21 10:21:11 +00001/*
2 * generic message digest layer demonstration program
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000057#else
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +010059#include <stdlib.h>
60#define mbedtls_fprintf fprintf
61#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010062#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010063#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +010064#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
65#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_MD_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/md.h"
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000069
Rich Evans18b78c72015-02-11 14:06:19 +000070#include <stdio.h>
71#include <string.h>
72#endif
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000075int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020078 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000079}
80#else
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010081
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083static int generic_wrapper( const mbedtls_md_info_t *md_info, char *filename, unsigned char *sum )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000084{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 int ret = mbedtls_md_file( md_info, filename, sum );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000086
87 if( ret == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_fprintf( stderr, "failed to open: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000089
90 if( ret == 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 mbedtls_fprintf( stderr, "failed to read: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000092
93 return( ret );
94}
95
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096static int generic_print( const mbedtls_md_info_t *md_info, char *filename )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000097{
98 int i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000100
101 if( generic_wrapper( md_info, filename, sum ) != 0 )
102 return( 1 );
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
105 mbedtls_printf( "%02x", sum[i] );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_printf( " %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000108 return( 0 );
109}
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111static int generic_check( const mbedtls_md_info_t *md_info, char *filename )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000112{
113 int i;
114 size_t n;
115 FILE *f;
116 int nb_err1, nb_err2;
117 int nb_tot1, nb_tot2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerd1fe7aa2016-05-12 12:46:02 +0100119 char line[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100120 char diff;
Paul Bakkerd1fe7aa2016-05-12 12:46:02 +0100121#if defined(__clang_analyzer__)
122 char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1] = { };
123#else
124 char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1];
125#endif
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000126
127 if( ( f = fopen( filename, "rb" ) ) == NULL )
128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_printf( "failed to open: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000130 return( 1 );
131 }
132
133 nb_err1 = nb_err2 = 0;
134 nb_tot1 = nb_tot2 = 0;
135
136 memset( line, 0, sizeof( line ) );
137
138 n = sizeof( line );
139
Paul Bakker23986e52011-04-24 08:57:21 +0000140 while( fgets( line, (int) n - 1, f ) != NULL )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000141 {
142 n = strlen( line );
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 if( n < (size_t) 2 * mbedtls_md_get_size( md_info ) + 4 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000147 continue;
148 }
149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 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 +0000151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000153 continue;
154 }
155
156 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
157 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
158
159 nb_tot1++;
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 if( generic_wrapper( md_info, line + 2 + 2 * mbedtls_md_get_size( md_info ), sum ) != 0 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000162 {
163 nb_err1++;
164 continue;
165 }
166
167 nb_tot2++;
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000170 sprintf( buf + i * 2, "%02x", sum[i] );
171
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100172 /* Use constant-time buffer comparison */
173 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 for( i = 0; i < 2 * mbedtls_md_get_size( md_info ); i++ )
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100175 diff |= line[i] ^ buf[i];
176
177 if( diff != 0 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000178 {
179 nb_err2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000181 }
182
183 n = sizeof( line );
184 }
185
186 if( nb_err1 != 0 )
187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_printf( "WARNING: %d (out of %d) input files could "
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000189 "not be read\n", nb_err1, nb_tot1 );
190 }
191
192 if( nb_err2 != 0 )
193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_printf( "WARNING: %d (out of %d) computed checksums did "
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000195 "not match\n", nb_err2, nb_tot2 );
196 }
197
Paul Bakker64abd832014-02-06 15:03:06 +0100198 fclose( f );
199
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000200 return( nb_err1 != 0 || nb_err2 != 0 );
201}
202
203int main( int argc, char *argv[] )
204{
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +0100205 int ret = 1, i;
206 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 const mbedtls_md_info_t *md_info;
208 mbedtls_md_context_t md_ctx;
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_md_init( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000211
212 if( argc == 1 )
213 {
214 const int *list;
215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_printf( "print mode: generic_sum <mbedtls_md> <file> <file> ...\n" );
217 mbedtls_printf( "check mode: generic_sum <mbedtls_md> -c <checksum file>\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_printf( "\nAvailable message digests:\n" );
220 list = mbedtls_md_list();
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000221 while( *list )
222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 md_info = mbedtls_md_info_from_type( *list );
224 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000225 list++;
226 }
227
Paul Bakkercce9d772011-11-18 14:26:47 +0000228#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000230 fflush( stdout ); getchar();
231#endif
232
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200233 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000234 }
235
236 /*
237 * Read the MD from the command line
238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 md_info = mbedtls_md_info_from_string( argv[1] );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000240 if( md_info == NULL )
241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200243 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000244 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 if( mbedtls_md_setup( &md_ctx, md_info, 0 ) )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_fprintf( stderr, "Failed to initialize context.\n" );
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200248 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000249 }
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000250
251 ret = 0;
252 if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
253 {
254 ret |= generic_check( md_info, argv[3] );
255 goto exit;
256 }
257
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000258 for( i = 2; i < argc; i++ )
259 ret |= generic_print( md_info, argv[i] );
260
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +0100261 if ( ret == 0 )
262 exit_code = MBEDTLS_EXIT_SUCCESS;
263
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000264exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_md_free( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000266
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200267 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000268}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */