source: trunk/programs/hash/generic_sum.c @ 1122

Revision 1122, 5.2 KB checked in by paul, 6 months ago (diff)
  • Lots of minimal changes to better support WINCE as a build target
Line 
1/*
2 *  generic message digest layer demonstration program
3 *
4 *  Copyright (C) 2006-2011, 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/config.h"
34
35#include "polarssl/md.h"
36
37#if !defined(POLARSSL_MD_C)
38int main( int argc, char *argv[] )
39{
40    ((void) argc);
41    ((void) argv);
42
43    printf("POLARSSL_MD_C not defined.\n");
44    return( 0 );
45}
46#else
47static 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];
84
85    if( ( f = fopen( filename, "rb" ) ) == NULL )
86    {
87        printf( "failed to open: %s\n", filename );
88        return( 1 );
89    }
90
91    nb_err1 = nb_err2 = 0;
92    nb_tot1 = nb_tot2 = 0;
93
94    memset( line, 0, sizeof( line ) );
95
96    n = sizeof( line );
97
98    while( fgets( line, (int) n - 1, f ) != NULL )
99    {
100        n = strlen( line );
101
102        if( n < (size_t) 2 * md_info->size + 4 )
103        {
104            printf("No '%s' hash found on line.\n", md_info->name);
105            continue;
106        }
107
108        if( line[2 * md_info->size] != ' ' || line[2 * md_info->size + 1] != ' ' )
109        {
110            printf("No '%s' hash found on line.\n", md_info->name);
111            continue;
112        }
113
114        if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
115        if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
116
117        nb_tot1++;
118
119        if( generic_wrapper( md_info, line + 2 + 2 * md_info->size, sum ) != 0 )
120        {
121            nb_err1++;
122            continue;
123        }
124
125        nb_tot2++;
126
127        for( i = 0; i < md_info->size; i++ )
128            sprintf( buf + i * 2, "%02x", sum[i] );
129
130        if( memcmp( line, buf, 2 * md_info->size ) != 0 )
131        {
132            nb_err2++;
133            fprintf( stderr, "wrong checksum: %s\n", line + 66 );
134        }
135
136        n = sizeof( line );
137    }
138
139    if( nb_err1 != 0 )
140    {
141        printf( "WARNING: %d (out of %d) input files could "
142                "not be read\n", nb_err1, nb_tot1 );
143    }
144
145    if( nb_err2 != 0 )
146    {
147        printf( "WARNING: %d (out of %d) computed checksums did "
148                "not match\n", nb_err2, nb_tot2 );
149    }
150
151    return( nb_err1 != 0 || nb_err2 != 0 );
152}
153
154int main( int argc, char *argv[] )
155{
156    int ret, i;
157    const md_info_t *md_info;
158    md_context_t md_ctx;
159
160    memset( &md_ctx, 0, sizeof( md_context_t ));
161
162    if( argc == 1 )
163    {
164        const int *list;
165
166        printf( "print mode:  generic_sum <md> <file> <file> ...\n" );
167        printf( "check mode:  generic_sum <md> -c <checksum file>\n" );
168
169        printf( "\nAvailable message digests:\n" );
170        list = md_list();
171        while( *list )
172        {
173            md_info = md_info_from_type( *list );
174            printf( "  %s\n", md_info->name );
175            list++;
176        }
177
178#if defined(_WIN32)
179        printf( "\n  Press Enter to exit this program.\n" );
180        fflush( stdout ); getchar();
181#endif
182
183        return( 1 );
184    }
185
186    /*
187     * Read the MD from the command line
188     */
189    md_info = md_info_from_string( argv[1] );
190    if( md_info == NULL )
191    {
192        fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
193        return( 1 );
194    }
195    if( md_init_ctx( &md_ctx, md_info) )
196    {
197        fprintf( stderr, "Failed to initialize context.\n" );
198        return( 1 );
199    }
200
201    ret = 0;
202    if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
203    {
204        ret |= generic_check( md_info, argv[3] );
205        goto exit;
206    }
207
208    for( i = 2; i < argc; i++ )
209        ret |= generic_print( md_info, argv[i] );
210
211exit:
212    md_free_ctx( &md_ctx );
213
214    return( ret );
215}
216#endif /* POLARSSL_MD_C */
Note: See TracBrowser for help on using the repository browser.

What are you looking for?