blob: 74f3650d58d05051f8b3fc89d78dc104a8947656 [file] [log] [blame]
Paul Bakkerfb6c7e22011-01-21 10:21:11 +00001/*
2 * generic message digest layer demonstration program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerfb6c7e22011-01-21 10:21:11 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerfb6c7e22011-01-21 10:21:11 +00007 *
Paul Bakkerfb6c7e22011-01-21 10:21:11 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_fprintf fprintf
34#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_MD_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/md.h"
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000039
Rich Evans18b78c72015-02-11 14:06:19 +000040#include <stdio.h>
41#include <string.h>
42#endif
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000045int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000046{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000048 return( 0 );
49}
50#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051static int generic_wrapper( const mbedtls_md_info_t *md_info, char *filename, unsigned char *sum )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000052{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053 int ret = mbedtls_md_file( md_info, filename, sum );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000054
55 if( ret == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_fprintf( stderr, "failed to open: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000057
58 if( ret == 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 mbedtls_fprintf( stderr, "failed to read: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000060
61 return( ret );
62}
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064static int generic_print( const mbedtls_md_info_t *md_info, char *filename )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000065{
66 int i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000068
69 if( generic_wrapper( md_info, filename, sum ) != 0 )
70 return( 1 );
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
73 mbedtls_printf( "%02x", sum[i] );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 mbedtls_printf( " %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000076 return( 0 );
77}
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079static int generic_check( const mbedtls_md_info_t *md_info, char *filename )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000080{
81 int i;
82 size_t n;
83 FILE *f;
84 int nb_err1, nb_err2;
85 int nb_tot1, nb_tot2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
87 char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1], line[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010088 char diff;
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000089
90 if( ( f = fopen( filename, "rb" ) ) == NULL )
91 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_printf( "failed to open: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000093 return( 1 );
94 }
95
96 nb_err1 = nb_err2 = 0;
97 nb_tot1 = nb_tot2 = 0;
98
99 memset( line, 0, sizeof( line ) );
100
101 n = sizeof( line );
102
Paul Bakker23986e52011-04-24 08:57:21 +0000103 while( fgets( line, (int) n - 1, f ) != NULL )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000104 {
105 n = strlen( line );
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 if( n < (size_t) 2 * mbedtls_md_get_size( md_info ) + 4 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000110 continue;
111 }
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 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 +0000114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000116 continue;
117 }
118
119 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
120 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
121
122 nb_tot1++;
123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 if( generic_wrapper( md_info, line + 2 + 2 * mbedtls_md_get_size( md_info ), sum ) != 0 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000125 {
126 nb_err1++;
127 continue;
128 }
129
130 nb_tot2++;
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000133 sprintf( buf + i * 2, "%02x", sum[i] );
134
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100135 /* Use constant-time buffer comparison */
136 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 for( i = 0; i < 2 * mbedtls_md_get_size( md_info ); i++ )
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100138 diff |= line[i] ^ buf[i];
139
140 if( diff != 0 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000141 {
142 nb_err2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000144 }
145
146 n = sizeof( line );
147 }
148
149 if( nb_err1 != 0 )
150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( "WARNING: %d (out of %d) input files could "
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000152 "not be read\n", nb_err1, nb_tot1 );
153 }
154
155 if( nb_err2 != 0 )
156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_printf( "WARNING: %d (out of %d) computed checksums did "
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000158 "not match\n", nb_err2, nb_tot2 );
159 }
160
Paul Bakker64abd832014-02-06 15:03:06 +0100161 fclose( f );
162
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000163 return( nb_err1 != 0 || nb_err2 != 0 );
164}
165
166int main( int argc, char *argv[] )
167{
168 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 const mbedtls_md_info_t *md_info;
170 mbedtls_md_context_t md_ctx;
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_md_init( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000173
174 if( argc == 1 )
175 {
176 const int *list;
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_printf( "print mode: generic_sum <mbedtls_md> <file> <file> ...\n" );
179 mbedtls_printf( "check mode: generic_sum <mbedtls_md> -c <checksum file>\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_printf( "\nAvailable message digests:\n" );
182 list = mbedtls_md_list();
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000183 while( *list )
184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 md_info = mbedtls_md_info_from_type( *list );
186 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000187 list++;
188 }
189
Paul Bakkercce9d772011-11-18 14:26:47 +0000190#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000192 fflush( stdout ); getchar();
193#endif
194
195 return( 1 );
196 }
197
198 /*
199 * Read the MD from the command line
200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 md_info = mbedtls_md_info_from_string( argv[1] );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000202 if( md_info == NULL )
203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000205 return( 1 );
206 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 if( mbedtls_md_setup( &md_ctx, md_info, 0 ) )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_fprintf( stderr, "Failed to initialize context.\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000210 return( 1 );
211 }
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000212
213 ret = 0;
214 if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
215 {
216 ret |= generic_check( md_info, argv[3] );
217 goto exit;
218 }
219
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000220 for( i = 2; i < argc; i++ )
221 ret |= generic_print( md_info, argv[i] );
222
223exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_md_free( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000225
226 return( ret );
227}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */