blob: 4b7fe37be50aab323effa16540abe98e3eaa9d03 [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
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +010032#include <stdlib.h>
33#define mbedtls_fprintf fprintf
34#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010035#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010036#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +010037#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
38#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_MD_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/md.h"
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000042
Rich Evans18b78c72015-02-11 14:06:19 +000043#include <stdio.h>
44#include <string.h>
45#endif
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000048int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000049{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000051 return( 0 );
52}
53#else
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010054
55#if defined(MBEDTLS_CHECK_PARAMS)
56#include "mbedtls/platform_util.h"
57void mbedtls_param_failed( const char *failure_condition,
58 const char *file,
59 int line )
60{
61 mbedtls_printf( "%s:%i: Input param failed - %s\n",
62 file, line, failure_condition );
63 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
64}
65#endif
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067static int generic_wrapper( const mbedtls_md_info_t *md_info, char *filename, unsigned char *sum )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000068{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 int ret = mbedtls_md_file( md_info, filename, sum );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000070
71 if( ret == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 mbedtls_fprintf( stderr, "failed to open: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000073
74 if( ret == 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 mbedtls_fprintf( stderr, "failed to read: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000076
77 return( ret );
78}
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080static int generic_print( const mbedtls_md_info_t *md_info, char *filename )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000081{
82 int i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000084
85 if( generic_wrapper( md_info, filename, sum ) != 0 )
86 return( 1 );
87
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
89 mbedtls_printf( "%02x", sum[i] );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 mbedtls_printf( " %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000092 return( 0 );
93}
94
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095static int generic_check( const mbedtls_md_info_t *md_info, char *filename )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000096{
97 int i;
98 size_t n;
99 FILE *f;
100 int nb_err1, nb_err2;
101 int nb_tot1, nb_tot2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerd1fe7aa2016-05-12 12:46:02 +0100103 char line[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100104 char diff;
Paul Bakkerd1fe7aa2016-05-12 12:46:02 +0100105#if defined(__clang_analyzer__)
106 char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1] = { };
107#else
108 char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1];
109#endif
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000110
111 if( ( f = fopen( filename, "rb" ) ) == NULL )
112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_printf( "failed to open: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000114 return( 1 );
115 }
116
117 nb_err1 = nb_err2 = 0;
118 nb_tot1 = nb_tot2 = 0;
119
120 memset( line, 0, sizeof( line ) );
121
122 n = sizeof( line );
123
Paul Bakker23986e52011-04-24 08:57:21 +0000124 while( fgets( line, (int) n - 1, f ) != NULL )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000125 {
126 n = strlen( line );
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 if( n < (size_t) 2 * mbedtls_md_get_size( md_info ) + 4 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000131 continue;
132 }
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 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 +0000135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000137 continue;
138 }
139
140 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
141 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
142
143 nb_tot1++;
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 if( generic_wrapper( md_info, line + 2 + 2 * mbedtls_md_get_size( md_info ), sum ) != 0 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000146 {
147 nb_err1++;
148 continue;
149 }
150
151 nb_tot2++;
152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000154 sprintf( buf + i * 2, "%02x", sum[i] );
155
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100156 /* Use constant-time buffer comparison */
157 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 for( i = 0; i < 2 * mbedtls_md_get_size( md_info ); i++ )
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100159 diff |= line[i] ^ buf[i];
160
161 if( diff != 0 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000162 {
163 nb_err2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000165 }
166
167 n = sizeof( line );
168 }
169
170 if( nb_err1 != 0 )
171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_printf( "WARNING: %d (out of %d) input files could "
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000173 "not be read\n", nb_err1, nb_tot1 );
174 }
175
176 if( nb_err2 != 0 )
177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_printf( "WARNING: %d (out of %d) computed checksums did "
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000179 "not match\n", nb_err2, nb_tot2 );
180 }
181
Paul Bakker64abd832014-02-06 15:03:06 +0100182 fclose( f );
183
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000184 return( nb_err1 != 0 || nb_err2 != 0 );
185}
186
187int main( int argc, char *argv[] )
188{
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +0100189 int ret = 1, i;
190 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 const mbedtls_md_info_t *md_info;
192 mbedtls_md_context_t md_ctx;
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_md_init( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000195
196 if( argc == 1 )
197 {
198 const int *list;
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_printf( "print mode: generic_sum <mbedtls_md> <file> <file> ...\n" );
201 mbedtls_printf( "check mode: generic_sum <mbedtls_md> -c <checksum file>\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_printf( "\nAvailable message digests:\n" );
204 list = mbedtls_md_list();
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000205 while( *list )
206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 md_info = mbedtls_md_info_from_type( *list );
208 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000209 list++;
210 }
211
Paul Bakkercce9d772011-11-18 14:26:47 +0000212#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000214 fflush( stdout ); getchar();
215#endif
216
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +0100217 return( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000218 }
219
220 /*
221 * Read the MD from the command line
222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 md_info = mbedtls_md_info_from_string( argv[1] );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000224 if( md_info == NULL )
225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +0100227 return( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000228 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 if( mbedtls_md_setup( &md_ctx, md_info, 0 ) )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_fprintf( stderr, "Failed to initialize context.\n" );
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +0100232 return( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000233 }
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000234
235 ret = 0;
236 if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
237 {
238 ret |= generic_check( md_info, argv[3] );
239 goto exit;
240 }
241
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000242 for( i = 2; i < argc; i++ )
243 ret |= generic_print( md_info, argv[i] );
244
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +0100245 if ( ret == 0 )
246 exit_code = MBEDTLS_EXIT_SUCCESS;
247
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000248exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_md_free( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000250
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +0100251 return( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000252}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */