blob: add0ecd6e135df615683ba033b4cbee54364b2c9 [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
33#include "polarssl/md.h"
34
35static int generic_wrapper( const md_info_t *md_info, char *filename, unsigned char *sum )
36{
37 int ret = md_file( md_info, filename, sum );
38
39 if( ret == 1 )
40 fprintf( stderr, "failed to open: %s\n", filename );
41
42 if( ret == 2 )
43 fprintf( stderr, "failed to read: %s\n", filename );
44
45 return( ret );
46}
47
48static int generic_print( const md_info_t *md_info, char *filename )
49{
50 int i;
51 unsigned char sum[POLARSSL_MD_MAX_SIZE];
52
53 if( generic_wrapper( md_info, filename, sum ) != 0 )
54 return( 1 );
55
56 for( i = 0; i < md_info->size; i++ )
57 printf( "%02x", sum[i] );
58
59 printf( " %s\n", filename );
60 return( 0 );
61}
62
63static int generic_check( const md_info_t *md_info, char *filename )
64{
65 int i;
66 size_t n;
67 FILE *f;
68 int nb_err1, nb_err2;
69 int nb_tot1, nb_tot2;
70 unsigned char sum[POLARSSL_MD_MAX_SIZE];
71 char buf[POLARSSL_MD_MAX_SIZE * 2 + 1], line[1024];
72
73 if( ( f = fopen( filename, "rb" ) ) == NULL )
74 {
75 printf( "failed to open: %s\n", filename );
76 return( 1 );
77 }
78
79 nb_err1 = nb_err2 = 0;
80 nb_tot1 = nb_tot2 = 0;
81
82 memset( line, 0, sizeof( line ) );
83
84 n = sizeof( line );
85
86 while( fgets( line, n - 1, f ) != NULL )
87 {
88 n = strlen( line );
89
90 if( n < (size_t) 2 * md_info->size + 4 )
91 {
92 printf("No '%s' hash found on line.\n", md_info->name);
93 continue;
94 }
95
96 if( line[2 * md_info->size] != ' ' || line[2 * md_info->size + 1] != ' ' )
97 {
98 printf("No '%s' hash found on line.\n", md_info->name);
99 continue;
100 }
101
102 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
103 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
104
105 nb_tot1++;
106
107 if( generic_wrapper( md_info, line + 2 + 2 * md_info->size, sum ) != 0 )
108 {
109 nb_err1++;
110 continue;
111 }
112
113 nb_tot2++;
114
115 for( i = 0; i < md_info->size; i++ )
116 sprintf( buf + i * 2, "%02x", sum[i] );
117
118 if( memcmp( line, buf, 2 * md_info->size ) != 0 )
119 {
120 nb_err2++;
121 fprintf( stderr, "wrong checksum: %s\n", line + 66 );
122 }
123
124 n = sizeof( line );
125 }
126
127 if( nb_err1 != 0 )
128 {
129 printf( "WARNING: %d (out of %d) input files could "
130 "not be read\n", nb_err1, nb_tot1 );
131 }
132
133 if( nb_err2 != 0 )
134 {
135 printf( "WARNING: %d (out of %d) computed checksums did "
136 "not match\n", nb_err2, nb_tot2 );
137 }
138
139 return( nb_err1 != 0 || nb_err2 != 0 );
140}
141
142int main( int argc, char *argv[] )
143{
144 int ret, i;
145 const md_info_t *md_info;
146 md_context_t md_ctx;
147
148 memset( &md_ctx, 0, sizeof( md_context_t ));
149
150 if( argc == 1 )
151 {
152 const int *list;
153
154 printf( "print mode: generic_sum <md> <file> <file> ...\n" );
155 printf( "check mode: generic_sum <md> -c <checksum file>\n" );
156
157 printf( "\nAvailable message digests:\n" );
158 list = md_list();
159 while( *list )
160 {
161 md_info = md_info_from_type( *list );
162 printf( " %s\n", md_info->name );
163 list++;
164 }
165
166#ifdef WIN32
167 printf( "\n Press Enter to exit this program.\n" );
168 fflush( stdout ); getchar();
169#endif
170
171 return( 1 );
172 }
173
174 /*
175 * Read the MD from the command line
176 */
177 md_info = md_info_from_string( argv[1] );
178 if( md_info == NULL )
179 {
180 fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
181 return( 1 );
182 }
183 if( md_init_ctx( &md_ctx, md_info) )
184 {
185 fprintf( stderr, "Failed to initialize context.\n" );
186 return( 1 );
187 }
188 printf("%08x\n", (unsigned int) md_ctx.md_ctx );
189
190 ret = 0;
191 if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
192 {
193 ret |= generic_check( md_info, argv[3] );
194 goto exit;
195 }
196
197 printf("%08x\n", (unsigned int) md_ctx.md_ctx );
198 for( i = 2; i < argc; i++ )
199 ret |= generic_print( md_info, argv[i] );
200
201exit:
202 printf("%08x\n", (unsigned int) md_ctx.md_ctx );
203 md_free_ctx( &md_ctx );
204
205 return( ret );
206}