blob: 9183874d9beda3f8439d1040c1a1cb3be45d746e [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.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é-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_fprintf fprintf
Rich Evans18b78c72015-02-11 14:06:19 +000034#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Rich Evans18b78c72015-02-11 14:06:19 +000037#if defined(POLARSSL_MD_C) && defined(POLARSSL_FS_IO)
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000038#include "polarssl/md.h"
39
Rich Evans18b78c72015-02-11 14:06:19 +000040#include <stdio.h>
41#include <string.h>
42#endif
43
44#if !defined(POLARSSL_MD_C) || !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000045int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000046{
Paul Bakkercce9d772011-11-18 14:26:47 +000047 ((void) argc);
48 ((void) argv);
49
Rich Evans18b78c72015-02-11 14:06:19 +000050 polarssl_printf("POLARSSL_MD_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000051 return( 0 );
52}
53#else
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000054static int generic_wrapper( const md_info_t *md_info, char *filename, unsigned char *sum )
55{
56 int ret = md_file( md_info, filename, sum );
57
58 if( ret == 1 )
Rich Evansf90016a2015-01-19 14:26:37 +000059 polarssl_fprintf( stderr, "failed to open: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000060
61 if( ret == 2 )
Rich Evansf90016a2015-01-19 14:26:37 +000062 polarssl_fprintf( stderr, "failed to read: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000063
64 return( ret );
65}
66
67static int generic_print( const md_info_t *md_info, char *filename )
68{
69 int i;
70 unsigned char sum[POLARSSL_MD_MAX_SIZE];
71
72 if( generic_wrapper( md_info, filename, sum ) != 0 )
73 return( 1 );
74
75 for( i = 0; i < md_info->size; i++ )
Rich Evansf90016a2015-01-19 14:26:37 +000076 polarssl_printf( "%02x", sum[i] );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000077
Rich Evansf90016a2015-01-19 14:26:37 +000078 polarssl_printf( " %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000079 return( 0 );
80}
81
82static int generic_check( const md_info_t *md_info, char *filename )
83{
84 int i;
85 size_t n;
86 FILE *f;
87 int nb_err1, nb_err2;
88 int nb_tot1, nb_tot2;
89 unsigned char sum[POLARSSL_MD_MAX_SIZE];
90 char buf[POLARSSL_MD_MAX_SIZE * 2 + 1], line[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010091 char diff;
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000092
93 if( ( f = fopen( filename, "rb" ) ) == NULL )
94 {
Rich Evansf90016a2015-01-19 14:26:37 +000095 polarssl_printf( "failed to open: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000096 return( 1 );
97 }
98
99 nb_err1 = nb_err2 = 0;
100 nb_tot1 = nb_tot2 = 0;
101
102 memset( line, 0, sizeof( line ) );
103
104 n = sizeof( line );
105
Paul Bakker23986e52011-04-24 08:57:21 +0000106 while( fgets( line, (int) n - 1, f ) != NULL )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000107 {
108 n = strlen( line );
109
110 if( n < (size_t) 2 * md_info->size + 4 )
111 {
Rich Evansf90016a2015-01-19 14:26:37 +0000112 polarssl_printf("No '%s' hash found on line.\n", md_info->name);
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000113 continue;
114 }
115
116 if( line[2 * md_info->size] != ' ' || line[2 * md_info->size + 1] != ' ' )
117 {
Rich Evansf90016a2015-01-19 14:26:37 +0000118 polarssl_printf("No '%s' hash found on line.\n", md_info->name);
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000119 continue;
120 }
121
122 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
123 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
124
125 nb_tot1++;
126
127 if( generic_wrapper( md_info, line + 2 + 2 * md_info->size, sum ) != 0 )
128 {
129 nb_err1++;
130 continue;
131 }
132
133 nb_tot2++;
134
135 for( i = 0; i < md_info->size; i++ )
136 sprintf( buf + i * 2, "%02x", sum[i] );
137
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100138 /* Use constant-time buffer comparison */
139 diff = 0;
140 for( i = 0; i < 2 * md_info->size; i++ )
141 diff |= line[i] ^ buf[i];
142
143 if( diff != 0 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000144 {
145 nb_err2++;
Rich Evansf90016a2015-01-19 14:26:37 +0000146 polarssl_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000147 }
148
149 n = sizeof( line );
150 }
151
152 if( nb_err1 != 0 )
153 {
Rich Evansf90016a2015-01-19 14:26:37 +0000154 polarssl_printf( "WARNING: %d (out of %d) input files could "
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000155 "not be read\n", nb_err1, nb_tot1 );
156 }
157
158 if( nb_err2 != 0 )
159 {
Rich Evansf90016a2015-01-19 14:26:37 +0000160 polarssl_printf( "WARNING: %d (out of %d) computed checksums did "
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000161 "not match\n", nb_err2, nb_tot2 );
162 }
163
Paul Bakker64abd832014-02-06 15:03:06 +0100164 fclose( f );
165
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000166 return( nb_err1 != 0 || nb_err2 != 0 );
167}
168
169int main( int argc, char *argv[] )
170{
171 int ret, i;
172 const md_info_t *md_info;
173 md_context_t md_ctx;
174
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200175 md_init( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000176
177 if( argc == 1 )
178 {
179 const int *list;
180
Rich Evansf90016a2015-01-19 14:26:37 +0000181 polarssl_printf( "print mode: generic_sum <md> <file> <file> ...\n" );
182 polarssl_printf( "check mode: generic_sum <md> -c <checksum file>\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000183
Rich Evansf90016a2015-01-19 14:26:37 +0000184 polarssl_printf( "\nAvailable message digests:\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000185 list = md_list();
186 while( *list )
187 {
188 md_info = md_info_from_type( *list );
Rich Evansf90016a2015-01-19 14:26:37 +0000189 polarssl_printf( " %s\n", md_info->name );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000190 list++;
191 }
192
Paul Bakkercce9d772011-11-18 14:26:47 +0000193#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000194 polarssl_printf( "\n Press Enter to exit this program.\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000195 fflush( stdout ); getchar();
196#endif
197
198 return( 1 );
199 }
200
201 /*
202 * Read the MD from the command line
203 */
204 md_info = md_info_from_string( argv[1] );
205 if( md_info == NULL )
206 {
Rich Evansf90016a2015-01-19 14:26:37 +0000207 polarssl_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000208 return( 1 );
209 }
210 if( md_init_ctx( &md_ctx, md_info) )
211 {
Rich Evansf90016a2015-01-19 14:26:37 +0000212 polarssl_fprintf( stderr, "Failed to initialize context.\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000213 return( 1 );
214 }
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000215
216 ret = 0;
217 if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
218 {
219 ret |= generic_check( md_info, argv[3] );
220 goto exit;
221 }
222
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000223 for( i = 2; i < argc; i++ )
224 ret |= generic_print( md_info, argv[i] );
225
226exit:
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200227 md_free( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000228
229 return( ret );
230}
Rich Evans18b78c72015-02-11 14:06:19 +0000231#endif /* POLARSSL_MD_C && POLARSSL_FS_IO */