blob: 34814d5db320a6c106ae4e56b1e3a2b97ae1a59d [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
29#include <string.h>
30#include <stdio.h>
31
32#include "polarssl/md.h"
33
Paul Bakker5690efc2011-05-26 13:16:06 +000034#if !defined(POLARSSL_MD_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000035int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000036{
Paul Bakkercce9d772011-11-18 14:26:47 +000037 ((void) argc);
38 ((void) argv);
39
Paul Bakker5690efc2011-05-26 13:16:06 +000040 printf("POLARSSL_MD_C not defined.\n");
41 return( 0 );
42}
43#else
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000044static int generic_wrapper( const md_info_t *md_info, char *filename, unsigned char *sum )
45{
46 int ret = md_file( md_info, filename, sum );
47
48 if( ret == 1 )
49 fprintf( stderr, "failed to open: %s\n", filename );
50
51 if( ret == 2 )
52 fprintf( stderr, "failed to read: %s\n", filename );
53
54 return( ret );
55}
56
57static int generic_print( const md_info_t *md_info, char *filename )
58{
59 int i;
60 unsigned char sum[POLARSSL_MD_MAX_SIZE];
61
62 if( generic_wrapper( md_info, filename, sum ) != 0 )
63 return( 1 );
64
65 for( i = 0; i < md_info->size; i++ )
66 printf( "%02x", sum[i] );
67
68 printf( " %s\n", filename );
69 return( 0 );
70}
71
72static int generic_check( const md_info_t *md_info, char *filename )
73{
74 int i;
75 size_t n;
76 FILE *f;
77 int nb_err1, nb_err2;
78 int nb_tot1, nb_tot2;
79 unsigned char sum[POLARSSL_MD_MAX_SIZE];
80 char buf[POLARSSL_MD_MAX_SIZE * 2 + 1], line[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010081 char diff;
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000082
83 if( ( f = fopen( filename, "rb" ) ) == NULL )
84 {
85 printf( "failed to open: %s\n", filename );
86 return( 1 );
87 }
88
89 nb_err1 = nb_err2 = 0;
90 nb_tot1 = nb_tot2 = 0;
91
92 memset( line, 0, sizeof( line ) );
93
94 n = sizeof( line );
95
Paul Bakker23986e52011-04-24 08:57:21 +000096 while( fgets( line, (int) n - 1, f ) != NULL )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000097 {
98 n = strlen( line );
99
100 if( n < (size_t) 2 * md_info->size + 4 )
101 {
102 printf("No '%s' hash found on line.\n", md_info->name);
103 continue;
104 }
105
106 if( line[2 * md_info->size] != ' ' || line[2 * md_info->size + 1] != ' ' )
107 {
108 printf("No '%s' hash found on line.\n", md_info->name);
109 continue;
110 }
111
112 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
113 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
114
115 nb_tot1++;
116
117 if( generic_wrapper( md_info, line + 2 + 2 * md_info->size, sum ) != 0 )
118 {
119 nb_err1++;
120 continue;
121 }
122
123 nb_tot2++;
124
125 for( i = 0; i < md_info->size; i++ )
126 sprintf( buf + i * 2, "%02x", sum[i] );
127
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100128 /* Use constant-time buffer comparison */
129 diff = 0;
130 for( i = 0; i < 2 * md_info->size; i++ )
131 diff |= line[i] ^ buf[i];
132
133 if( diff != 0 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000134 {
135 nb_err2++;
136 fprintf( stderr, "wrong checksum: %s\n", line + 66 );
137 }
138
139 n = sizeof( line );
140 }
141
142 if( nb_err1 != 0 )
143 {
144 printf( "WARNING: %d (out of %d) input files could "
145 "not be read\n", nb_err1, nb_tot1 );
146 }
147
148 if( nb_err2 != 0 )
149 {
150 printf( "WARNING: %d (out of %d) computed checksums did "
151 "not match\n", nb_err2, nb_tot2 );
152 }
153
Paul Bakker64abd832014-02-06 15:03:06 +0100154 fclose( f );
155
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000156 return( nb_err1 != 0 || nb_err2 != 0 );
157}
158
159int main( int argc, char *argv[] )
160{
161 int ret, i;
162 const md_info_t *md_info;
163 md_context_t md_ctx;
164
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200165 md_init( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000166
167 if( argc == 1 )
168 {
169 const int *list;
170
171 printf( "print mode: generic_sum <md> <file> <file> ...\n" );
172 printf( "check mode: generic_sum <md> -c <checksum file>\n" );
173
174 printf( "\nAvailable message digests:\n" );
175 list = md_list();
176 while( *list )
177 {
178 md_info = md_info_from_type( *list );
179 printf( " %s\n", md_info->name );
180 list++;
181 }
182
Paul Bakkercce9d772011-11-18 14:26:47 +0000183#if defined(_WIN32)
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000184 printf( "\n Press Enter to exit this program.\n" );
185 fflush( stdout ); getchar();
186#endif
187
188 return( 1 );
189 }
190
191 /*
192 * Read the MD from the command line
193 */
194 md_info = md_info_from_string( argv[1] );
195 if( md_info == NULL )
196 {
197 fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
198 return( 1 );
199 }
200 if( md_init_ctx( &md_ctx, md_info) )
201 {
202 fprintf( stderr, "Failed to initialize context.\n" );
203 return( 1 );
204 }
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000205
206 ret = 0;
207 if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
208 {
209 ret |= generic_check( md_info, argv[3] );
210 goto exit;
211 }
212
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000213 for( i = 2; i < argc; i++ )
214 ret |= generic_print( md_info, argv[i] );
215
216exit:
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200217 md_free( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000218
219 return( ret );
220}
Paul Bakker5690efc2011-05-26 13:16:06 +0000221#endif /* POLARSSL_MD_C */