blob: f73e5f8399700613c111e23b48076f7973a1ad72 [file] [log] [blame]
Paul Bakkerfb6c7e22011-01-21 10:21:11 +00001/*
2 * generic message digest layer demonstration program
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000018 */
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000028#else
Rich Evans18b78c72015-02-11 14:06:19 +000029#include <stdio.h>
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +010030#include <stdlib.h>
31#define mbedtls_fprintf fprintf
32#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010033#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010034#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +010035#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
36#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_MD_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/md.h"
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000040
Rich Evans18b78c72015-02-11 14:06:19 +000041#include <stdio.h>
42#include <string.h>
43#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000046int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000047{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020049 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000050}
51#else
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010052
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054static int generic_wrapper( const mbedtls_md_info_t *md_info, char *filename, unsigned char *sum )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000055{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 int ret = mbedtls_md_file( md_info, filename, sum );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000057
58 if( ret == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 mbedtls_fprintf( stderr, "failed to open: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000060
61 if( ret == 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_fprintf( stderr, "failed to read: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000063
64 return( ret );
65}
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067static int generic_print( const mbedtls_md_info_t *md_info, char *filename )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000068{
69 int i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000071
72 if( generic_wrapper( md_info, filename, sum ) != 0 )
73 return( 1 );
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
76 mbedtls_printf( "%02x", sum[i] );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 mbedtls_printf( " %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000079 return( 0 );
80}
81
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082static int generic_check( const mbedtls_md_info_t *md_info, char *filename )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000083{
84 int i;
85 size_t n;
86 FILE *f;
87 int nb_err1, nb_err2;
88 int nb_tot1, nb_tot2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerd1fe7aa2016-05-12 12:46:02 +010090 char line[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010091 char diff;
Paul Bakkerd1fe7aa2016-05-12 12:46:02 +010092#if defined(__clang_analyzer__)
93 char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1] = { };
94#else
95 char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1];
96#endif
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000097
98 if( ( f = fopen( filename, "rb" ) ) == NULL )
99 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_printf( "failed to open: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000101 return( 1 );
102 }
103
104 nb_err1 = nb_err2 = 0;
105 nb_tot1 = nb_tot2 = 0;
106
107 memset( line, 0, sizeof( line ) );
108
109 n = sizeof( line );
110
Paul Bakker23986e52011-04-24 08:57:21 +0000111 while( fgets( line, (int) n - 1, f ) != NULL )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000112 {
113 n = strlen( line );
114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 if( n < (size_t) 2 * mbedtls_md_get_size( md_info ) + 4 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000118 continue;
119 }
120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 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 +0000122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000124 continue;
125 }
126
127 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
128 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
129
130 nb_tot1++;
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 if( generic_wrapper( md_info, line + 2 + 2 * mbedtls_md_get_size( md_info ), sum ) != 0 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000133 {
134 nb_err1++;
135 continue;
136 }
137
138 nb_tot2++;
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000141 sprintf( buf + i * 2, "%02x", sum[i] );
142
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100143 /* Use constant-time buffer comparison */
144 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 for( i = 0; i < 2 * mbedtls_md_get_size( md_info ); i++ )
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100146 diff |= line[i] ^ buf[i];
147
148 if( diff != 0 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000149 {
150 nb_err2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000152 }
153
154 n = sizeof( line );
155 }
156
157 if( nb_err1 != 0 )
158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_printf( "WARNING: %d (out of %d) input files could "
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000160 "not be read\n", nb_err1, nb_tot1 );
161 }
162
163 if( nb_err2 != 0 )
164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_printf( "WARNING: %d (out of %d) computed checksums did "
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000166 "not match\n", nb_err2, nb_tot2 );
167 }
168
Paul Bakker64abd832014-02-06 15:03:06 +0100169 fclose( f );
170
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000171 return( nb_err1 != 0 || nb_err2 != 0 );
172}
173
174int main( int argc, char *argv[] )
175{
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +0100176 int ret = 1, i;
177 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 const mbedtls_md_info_t *md_info;
179 mbedtls_md_context_t md_ctx;
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_md_init( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000182
183 if( argc == 1 )
184 {
185 const int *list;
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_printf( "print mode: generic_sum <mbedtls_md> <file> <file> ...\n" );
188 mbedtls_printf( "check mode: generic_sum <mbedtls_md> -c <checksum file>\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_printf( "\nAvailable message digests:\n" );
191 list = mbedtls_md_list();
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000192 while( *list )
193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 md_info = mbedtls_md_info_from_type( *list );
195 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000196 list++;
197 }
198
Paul Bakkercce9d772011-11-18 14:26:47 +0000199#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000201 fflush( stdout ); getchar();
202#endif
203
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200204 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000205 }
206
207 /*
208 * Read the MD from the command line
209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 md_info = mbedtls_md_info_from_string( argv[1] );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000211 if( md_info == NULL )
212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200214 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000215 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 if( mbedtls_md_setup( &md_ctx, md_info, 0 ) )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 mbedtls_fprintf( stderr, "Failed to initialize context.\n" );
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200219 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000220 }
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000221
222 ret = 0;
223 if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
224 {
225 ret |= generic_check( md_info, argv[3] );
226 goto exit;
227 }
228
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000229 for( i = 2; i < argc; i++ )
230 ret |= generic_print( md_info, argv[i] );
231
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +0100232 if ( ret == 0 )
233 exit_code = MBEDTLS_EXIT_SUCCESS;
234
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000235exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_md_free( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000237
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200238 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000239}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */