blob: 04e1c045737c7853c325d402dcabe5e48f517109 [file] [log] [blame]
Paul Bakkerfb6c7e22011-01-21 10:21:11 +00001/*
2 * generic message digest layer demonstration program
3 *
4 * Copyright (C) 2006-2010, Brainspark B.V.
5 *
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
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdio.h>
32
Paul Bakker5690efc2011-05-26 13:16:06 +000033#include "polarssl/config.h"
34
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000035#include "polarssl/md.h"
36
Paul Bakker5690efc2011-05-26 13:16:06 +000037#if !defined(POLARSSL_MD_C)
38int main( void )
39{
40 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];
81
82 if( ( f = fopen( filename, "rb" ) ) == NULL )
83 {
84 printf( "failed to open: %s\n", filename );
85 return( 1 );
86 }
87
88 nb_err1 = nb_err2 = 0;
89 nb_tot1 = nb_tot2 = 0;
90
91 memset( line, 0, sizeof( line ) );
92
93 n = sizeof( line );
94
Paul Bakker23986e52011-04-24 08:57:21 +000095 while( fgets( line, (int) n - 1, f ) != NULL )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000096 {
97 n = strlen( line );
98
99 if( n < (size_t) 2 * md_info->size + 4 )
100 {
101 printf("No '%s' hash found on line.\n", md_info->name);
102 continue;
103 }
104
105 if( line[2 * md_info->size] != ' ' || line[2 * md_info->size + 1] != ' ' )
106 {
107 printf("No '%s' hash found on line.\n", md_info->name);
108 continue;
109 }
110
111 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
112 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
113
114 nb_tot1++;
115
116 if( generic_wrapper( md_info, line + 2 + 2 * md_info->size, sum ) != 0 )
117 {
118 nb_err1++;
119 continue;
120 }
121
122 nb_tot2++;
123
124 for( i = 0; i < md_info->size; i++ )
125 sprintf( buf + i * 2, "%02x", sum[i] );
126
127 if( memcmp( line, buf, 2 * md_info->size ) != 0 )
128 {
129 nb_err2++;
130 fprintf( stderr, "wrong checksum: %s\n", line + 66 );
131 }
132
133 n = sizeof( line );
134 }
135
136 if( nb_err1 != 0 )
137 {
138 printf( "WARNING: %d (out of %d) input files could "
139 "not be read\n", nb_err1, nb_tot1 );
140 }
141
142 if( nb_err2 != 0 )
143 {
144 printf( "WARNING: %d (out of %d) computed checksums did "
145 "not match\n", nb_err2, nb_tot2 );
146 }
147
148 return( nb_err1 != 0 || nb_err2 != 0 );
149}
150
151int main( int argc, char *argv[] )
152{
153 int ret, i;
154 const md_info_t *md_info;
155 md_context_t md_ctx;
156
157 memset( &md_ctx, 0, sizeof( md_context_t ));
158
159 if( argc == 1 )
160 {
161 const int *list;
162
163 printf( "print mode: generic_sum <md> <file> <file> ...\n" );
164 printf( "check mode: generic_sum <md> -c <checksum file>\n" );
165
166 printf( "\nAvailable message digests:\n" );
167 list = md_list();
168 while( *list )
169 {
170 md_info = md_info_from_type( *list );
171 printf( " %s\n", md_info->name );
172 list++;
173 }
174
175#ifdef WIN32
176 printf( "\n Press Enter to exit this program.\n" );
177 fflush( stdout ); getchar();
178#endif
179
180 return( 1 );
181 }
182
183 /*
184 * Read the MD from the command line
185 */
186 md_info = md_info_from_string( argv[1] );
187 if( md_info == NULL )
188 {
189 fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
190 return( 1 );
191 }
192 if( md_init_ctx( &md_ctx, md_info) )
193 {
194 fprintf( stderr, "Failed to initialize context.\n" );
195 return( 1 );
196 }
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000197
198 ret = 0;
199 if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
200 {
201 ret |= generic_check( md_info, argv[3] );
202 goto exit;
203 }
204
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000205 for( i = 2; i < argc; i++ )
206 ret |= generic_print( md_info, argv[i] );
207
208exit:
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000209 md_free_ctx( &md_ctx );
210
211 return( ret );
212}
Paul Bakker5690efc2011-05-26 13:16:06 +0000213#endif /* POLARSSL_MD_C */