blob: 4d007cf7a969038184c444b6f08d5c21dd41e7a6 [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é-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000031
32#include <string.h>
33#include <stdio.h>
34
35#include "polarssl/md.h"
36
Paul Bakker5690efc2011-05-26 13:16:06 +000037#if !defined(POLARSSL_MD_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000038int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000039{
Paul Bakkercce9d772011-11-18 14:26:47 +000040 ((void) argc);
41 ((void) argv);
42
Paul Bakker5690efc2011-05-26 13:16:06 +000043 printf("POLARSSL_MD_C not defined.\n");
44 return( 0 );
45}
46#else
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000047static int generic_wrapper( const md_info_t *md_info, char *filename, unsigned char *sum )
48{
49 int ret = md_file( md_info, filename, sum );
50
51 if( ret == 1 )
52 fprintf( stderr, "failed to open: %s\n", filename );
53
54 if( ret == 2 )
55 fprintf( stderr, "failed to read: %s\n", filename );
56
57 return( ret );
58}
59
60static int generic_print( const md_info_t *md_info, char *filename )
61{
62 int i;
63 unsigned char sum[POLARSSL_MD_MAX_SIZE];
64
65 if( generic_wrapper( md_info, filename, sum ) != 0 )
66 return( 1 );
67
68 for( i = 0; i < md_info->size; i++ )
69 printf( "%02x", sum[i] );
70
71 printf( " %s\n", filename );
72 return( 0 );
73}
74
75static int generic_check( const md_info_t *md_info, char *filename )
76{
77 int i;
78 size_t n;
79 FILE *f;
80 int nb_err1, nb_err2;
81 int nb_tot1, nb_tot2;
82 unsigned char sum[POLARSSL_MD_MAX_SIZE];
83 char buf[POLARSSL_MD_MAX_SIZE * 2 + 1], line[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010084 char diff;
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000085
86 if( ( f = fopen( filename, "rb" ) ) == NULL )
87 {
88 printf( "failed to open: %s\n", filename );
89 return( 1 );
90 }
91
92 nb_err1 = nb_err2 = 0;
93 nb_tot1 = nb_tot2 = 0;
94
95 memset( line, 0, sizeof( line ) );
96
97 n = sizeof( line );
98
Paul Bakker23986e52011-04-24 08:57:21 +000099 while( fgets( line, (int) n - 1, f ) != NULL )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000100 {
101 n = strlen( line );
102
103 if( n < (size_t) 2 * md_info->size + 4 )
104 {
105 printf("No '%s' hash found on line.\n", md_info->name);
106 continue;
107 }
108
109 if( line[2 * md_info->size] != ' ' || line[2 * md_info->size + 1] != ' ' )
110 {
111 printf("No '%s' hash found on line.\n", md_info->name);
112 continue;
113 }
114
115 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
116 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
117
118 nb_tot1++;
119
120 if( generic_wrapper( md_info, line + 2 + 2 * md_info->size, sum ) != 0 )
121 {
122 nb_err1++;
123 continue;
124 }
125
126 nb_tot2++;
127
128 for( i = 0; i < md_info->size; i++ )
129 sprintf( buf + i * 2, "%02x", sum[i] );
130
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100131 /* Use constant-time buffer comparison */
132 diff = 0;
133 for( i = 0; i < 2 * md_info->size; i++ )
134 diff |= line[i] ^ buf[i];
135
136 if( diff != 0 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000137 {
138 nb_err2++;
139 fprintf( stderr, "wrong checksum: %s\n", line + 66 );
140 }
141
142 n = sizeof( line );
143 }
144
145 if( nb_err1 != 0 )
146 {
147 printf( "WARNING: %d (out of %d) input files could "
148 "not be read\n", nb_err1, nb_tot1 );
149 }
150
151 if( nb_err2 != 0 )
152 {
153 printf( "WARNING: %d (out of %d) computed checksums did "
154 "not match\n", nb_err2, nb_tot2 );
155 }
156
Paul Bakker64abd832014-02-06 15:03:06 +0100157 fclose( f );
158
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000159 return( nb_err1 != 0 || nb_err2 != 0 );
160}
161
162int main( int argc, char *argv[] )
163{
164 int ret, i;
165 const md_info_t *md_info;
166 md_context_t md_ctx;
167
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200168 md_init( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000169
170 if( argc == 1 )
171 {
172 const int *list;
173
174 printf( "print mode: generic_sum <md> <file> <file> ...\n" );
175 printf( "check mode: generic_sum <md> -c <checksum file>\n" );
176
177 printf( "\nAvailable message digests:\n" );
178 list = md_list();
179 while( *list )
180 {
181 md_info = md_info_from_type( *list );
182 printf( " %s\n", md_info->name );
183 list++;
184 }
185
Paul Bakkercce9d772011-11-18 14:26:47 +0000186#if defined(_WIN32)
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000187 printf( "\n Press Enter to exit this program.\n" );
188 fflush( stdout ); getchar();
189#endif
190
191 return( 1 );
192 }
193
194 /*
195 * Read the MD from the command line
196 */
197 md_info = md_info_from_string( argv[1] );
198 if( md_info == NULL )
199 {
200 fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
201 return( 1 );
202 }
203 if( md_init_ctx( &md_ctx, md_info) )
204 {
205 fprintf( stderr, "Failed to initialize context.\n" );
206 return( 1 );
207 }
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000208
209 ret = 0;
210 if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
211 {
212 ret |= generic_check( md_info, argv[3] );
213 goto exit;
214 }
215
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000216 for( i = 2; i < argc; i++ )
217 ret |= generic_print( md_info, argv[i] );
218
219exit:
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200220 md_free( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000221
222 return( ret );
223}
Paul Bakker5690efc2011-05-26 13:16:06 +0000224#endif /* POLARSSL_MD_C */