blob: 5af63f5e7a38c189cdae6fbe278244bc29147fb8 [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
34#define polarssl_malloc malloc
35#define polarssl_free free
36#endif
37
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000038#include <string.h>
39#include <stdio.h>
40
41#include "polarssl/md.h"
42
Paul Bakker5690efc2011-05-26 13:16:06 +000043#if !defined(POLARSSL_MD_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000044int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000045{
Paul Bakkercce9d772011-11-18 14:26:47 +000046 ((void) argc);
47 ((void) argv);
48
Rich Evansf90016a2015-01-19 14:26:37 +000049 polarssl_printf("POLARSSL_MD_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000050 return( 0 );
51}
52#else
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000053static int generic_wrapper( const md_info_t *md_info, char *filename, unsigned char *sum )
54{
55 int ret = md_file( md_info, filename, sum );
56
57 if( ret == 1 )
Rich Evansf90016a2015-01-19 14:26:37 +000058 polarssl_fprintf( stderr, "failed to open: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000059
60 if( ret == 2 )
Rich Evansf90016a2015-01-19 14:26:37 +000061 polarssl_fprintf( stderr, "failed to read: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000062
63 return( ret );
64}
65
66static int generic_print( const md_info_t *md_info, char *filename )
67{
68 int i;
69 unsigned char sum[POLARSSL_MD_MAX_SIZE];
70
71 if( generic_wrapper( md_info, filename, sum ) != 0 )
72 return( 1 );
73
74 for( i = 0; i < md_info->size; i++ )
Rich Evansf90016a2015-01-19 14:26:37 +000075 polarssl_printf( "%02x", sum[i] );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000076
Rich Evansf90016a2015-01-19 14:26:37 +000077 polarssl_printf( " %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000078 return( 0 );
79}
80
81static int generic_check( const md_info_t *md_info, char *filename )
82{
83 int i;
84 size_t n;
85 FILE *f;
86 int nb_err1, nb_err2;
87 int nb_tot1, nb_tot2;
88 unsigned char sum[POLARSSL_MD_MAX_SIZE];
89 char buf[POLARSSL_MD_MAX_SIZE * 2 + 1], line[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010090 char diff;
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000091
92 if( ( f = fopen( filename, "rb" ) ) == NULL )
93 {
Rich Evansf90016a2015-01-19 14:26:37 +000094 polarssl_printf( "failed to open: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000095 return( 1 );
96 }
97
98 nb_err1 = nb_err2 = 0;
99 nb_tot1 = nb_tot2 = 0;
100
101 memset( line, 0, sizeof( line ) );
102
103 n = sizeof( line );
104
Paul Bakker23986e52011-04-24 08:57:21 +0000105 while( fgets( line, (int) n - 1, f ) != NULL )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000106 {
107 n = strlen( line );
108
109 if( n < (size_t) 2 * md_info->size + 4 )
110 {
Rich Evansf90016a2015-01-19 14:26:37 +0000111 polarssl_printf("No '%s' hash found on line.\n", md_info->name);
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000112 continue;
113 }
114
115 if( line[2 * md_info->size] != ' ' || line[2 * md_info->size + 1] != ' ' )
116 {
Rich Evansf90016a2015-01-19 14:26:37 +0000117 polarssl_printf("No '%s' hash found on line.\n", md_info->name);
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000118 continue;
119 }
120
121 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
122 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
123
124 nb_tot1++;
125
126 if( generic_wrapper( md_info, line + 2 + 2 * md_info->size, sum ) != 0 )
127 {
128 nb_err1++;
129 continue;
130 }
131
132 nb_tot2++;
133
134 for( i = 0; i < md_info->size; i++ )
135 sprintf( buf + i * 2, "%02x", sum[i] );
136
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100137 /* Use constant-time buffer comparison */
138 diff = 0;
139 for( i = 0; i < 2 * md_info->size; i++ )
140 diff |= line[i] ^ buf[i];
141
142 if( diff != 0 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000143 {
144 nb_err2++;
Rich Evansf90016a2015-01-19 14:26:37 +0000145 polarssl_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000146 }
147
148 n = sizeof( line );
149 }
150
151 if( nb_err1 != 0 )
152 {
Rich Evansf90016a2015-01-19 14:26:37 +0000153 polarssl_printf( "WARNING: %d (out of %d) input files could "
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000154 "not be read\n", nb_err1, nb_tot1 );
155 }
156
157 if( nb_err2 != 0 )
158 {
Rich Evansf90016a2015-01-19 14:26:37 +0000159 polarssl_printf( "WARNING: %d (out of %d) computed checksums did "
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000160 "not match\n", nb_err2, nb_tot2 );
161 }
162
Paul Bakker64abd832014-02-06 15:03:06 +0100163 fclose( f );
164
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000165 return( nb_err1 != 0 || nb_err2 != 0 );
166}
167
168int main( int argc, char *argv[] )
169{
170 int ret, i;
171 const md_info_t *md_info;
172 md_context_t md_ctx;
173
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200174 md_init( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000175
176 if( argc == 1 )
177 {
178 const int *list;
179
Rich Evansf90016a2015-01-19 14:26:37 +0000180 polarssl_printf( "print mode: generic_sum <md> <file> <file> ...\n" );
181 polarssl_printf( "check mode: generic_sum <md> -c <checksum file>\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000182
Rich Evansf90016a2015-01-19 14:26:37 +0000183 polarssl_printf( "\nAvailable message digests:\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000184 list = md_list();
185 while( *list )
186 {
187 md_info = md_info_from_type( *list );
Rich Evansf90016a2015-01-19 14:26:37 +0000188 polarssl_printf( " %s\n", md_info->name );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000189 list++;
190 }
191
Paul Bakkercce9d772011-11-18 14:26:47 +0000192#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000193 polarssl_printf( "\n Press Enter to exit this program.\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000194 fflush( stdout ); getchar();
195#endif
196
197 return( 1 );
198 }
199
200 /*
201 * Read the MD from the command line
202 */
203 md_info = md_info_from_string( argv[1] );
204 if( md_info == NULL )
205 {
Rich Evansf90016a2015-01-19 14:26:37 +0000206 polarssl_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000207 return( 1 );
208 }
209 if( md_init_ctx( &md_ctx, md_info) )
210 {
Rich Evansf90016a2015-01-19 14:26:37 +0000211 polarssl_fprintf( stderr, "Failed to initialize context.\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000212 return( 1 );
213 }
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000214
215 ret = 0;
216 if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
217 {
218 ret |= generic_check( md_info, argv[3] );
219 goto exit;
220 }
221
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000222 for( i = 2; i < argc; i++ )
223 ret |= generic_print( md_info, argv[i] );
224
225exit:
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200226 md_free( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000227
228 return( ret );
229}
Paul Bakker5690efc2011-05-26 13:16:06 +0000230#endif /* POLARSSL_MD_C */