blob: 8ca4d9205b85c226b80f45e55673ea69d89da6c6 [file] [log] [blame]
Paul Bakkerfb6c7e22011-01-21 10:21:11 +00001/*
2 * generic message digest layer demonstration program
3 *
Paul Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerfb6c7e22011-01-21 10:21:11 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000027
28#include <string.h>
29#include <stdio.h>
30
31#include "polarssl/md.h"
32
Paul Bakker5690efc2011-05-26 13:16:06 +000033#if !defined(POLARSSL_MD_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000034int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000035{
Paul Bakkercce9d772011-11-18 14:26:47 +000036 ((void) argc);
37 ((void) argv);
38
Paul Bakker5690efc2011-05-26 13:16:06 +000039 printf("POLARSSL_MD_C not defined.\n");
40 return( 0 );
41}
42#else
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000043static int generic_wrapper( const md_info_t *md_info, char *filename, unsigned char *sum )
44{
45 int ret = md_file( md_info, filename, sum );
46
47 if( ret == 1 )
48 fprintf( stderr, "failed to open: %s\n", filename );
49
50 if( ret == 2 )
51 fprintf( stderr, "failed to read: %s\n", filename );
52
53 return( ret );
54}
55
56static int generic_print( const md_info_t *md_info, char *filename )
57{
58 int i;
59 unsigned char sum[POLARSSL_MD_MAX_SIZE];
60
61 if( generic_wrapper( md_info, filename, sum ) != 0 )
62 return( 1 );
63
64 for( i = 0; i < md_info->size; i++ )
65 printf( "%02x", sum[i] );
66
67 printf( " %s\n", filename );
68 return( 0 );
69}
70
71static int generic_check( const md_info_t *md_info, char *filename )
72{
73 int i;
74 size_t n;
75 FILE *f;
76 int nb_err1, nb_err2;
77 int nb_tot1, nb_tot2;
78 unsigned char sum[POLARSSL_MD_MAX_SIZE];
79 char buf[POLARSSL_MD_MAX_SIZE * 2 + 1], line[1024];
80
81 if( ( f = fopen( filename, "rb" ) ) == NULL )
82 {
83 printf( "failed to open: %s\n", filename );
84 return( 1 );
85 }
86
87 nb_err1 = nb_err2 = 0;
88 nb_tot1 = nb_tot2 = 0;
89
90 memset( line, 0, sizeof( line ) );
91
92 n = sizeof( line );
93
Paul Bakker23986e52011-04-24 08:57:21 +000094 while( fgets( line, (int) n - 1, f ) != NULL )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000095 {
96 n = strlen( line );
97
98 if( n < (size_t) 2 * md_info->size + 4 )
99 {
100 printf("No '%s' hash found on line.\n", md_info->name);
101 continue;
102 }
103
104 if( line[2 * md_info->size] != ' ' || line[2 * md_info->size + 1] != ' ' )
105 {
106 printf("No '%s' hash found on line.\n", md_info->name);
107 continue;
108 }
109
110 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
111 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
112
113 nb_tot1++;
114
115 if( generic_wrapper( md_info, line + 2 + 2 * md_info->size, sum ) != 0 )
116 {
117 nb_err1++;
118 continue;
119 }
120
121 nb_tot2++;
122
123 for( i = 0; i < md_info->size; i++ )
124 sprintf( buf + i * 2, "%02x", sum[i] );
125
126 if( memcmp( line, buf, 2 * md_info->size ) != 0 )
127 {
128 nb_err2++;
129 fprintf( stderr, "wrong checksum: %s\n", line + 66 );
130 }
131
132 n = sizeof( line );
133 }
134
135 if( nb_err1 != 0 )
136 {
137 printf( "WARNING: %d (out of %d) input files could "
138 "not be read\n", nb_err1, nb_tot1 );
139 }
140
141 if( nb_err2 != 0 )
142 {
143 printf( "WARNING: %d (out of %d) computed checksums did "
144 "not match\n", nb_err2, nb_tot2 );
145 }
146
147 return( nb_err1 != 0 || nb_err2 != 0 );
148}
149
150int main( int argc, char *argv[] )
151{
152 int ret, i;
153 const md_info_t *md_info;
154 md_context_t md_ctx;
155
156 memset( &md_ctx, 0, sizeof( md_context_t ));
157
158 if( argc == 1 )
159 {
160 const int *list;
161
162 printf( "print mode: generic_sum <md> <file> <file> ...\n" );
163 printf( "check mode: generic_sum <md> -c <checksum file>\n" );
164
165 printf( "\nAvailable message digests:\n" );
166 list = md_list();
167 while( *list )
168 {
169 md_info = md_info_from_type( *list );
170 printf( " %s\n", md_info->name );
171 list++;
172 }
173
Paul Bakkercce9d772011-11-18 14:26:47 +0000174#if defined(_WIN32)
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000175 printf( "\n Press Enter to exit this program.\n" );
176 fflush( stdout ); getchar();
177#endif
178
179 return( 1 );
180 }
181
182 /*
183 * Read the MD from the command line
184 */
185 md_info = md_info_from_string( argv[1] );
186 if( md_info == NULL )
187 {
188 fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
189 return( 1 );
190 }
191 if( md_init_ctx( &md_ctx, md_info) )
192 {
193 fprintf( stderr, "Failed to initialize context.\n" );
194 return( 1 );
195 }
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000196
197 ret = 0;
198 if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
199 {
200 ret |= generic_check( md_info, argv[3] );
201 goto exit;
202 }
203
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000204 for( i = 2; i < argc; i++ )
205 ret |= generic_print( md_info, argv[i] );
206
207exit:
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000208 md_free_ctx( &md_ctx );
209
210 return( ret );
211}
Paul Bakker5690efc2011-05-26 13:16:06 +0000212#endif /* POLARSSL_MD_C */