blob: 2e6884dbf7da1855b034898125935dc60ffaebdc [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Paul Bakker9e36f042013-06-30 14:34:05 +02002 * sha256sum demonstration program
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Paul Bakkerd2681d82013-06-30 14:49:12 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * 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 Bakker5121ce52009-01-03 21:22:43 +000027
28#include <string.h>
29#include <stdio.h>
30
Paul Bakkerd2681d82013-06-30 14:49:12 +020031#include "polarssl/sha256.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakker9e36f042013-06-30 14:34:05 +020033#if !defined(POLARSSL_SHA256_C) || !defined(POLARSSL_FS_IO)
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 Bakker9e36f042013-06-30 14:34:05 +020039 printf("POLARSSL_SHA256_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000040 return( 0 );
41}
42#else
Paul Bakker9e36f042013-06-30 14:34:05 +020043static int sha256_wrapper( char *filename, unsigned char *sum )
Paul Bakker5121ce52009-01-03 21:22:43 +000044{
Paul Bakker9e36f042013-06-30 14:34:05 +020045 int ret = sha256_file( filename, sum, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000046
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
Paul Bakker9e36f042013-06-30 14:34:05 +020056static int sha256_print( char *filename )
Paul Bakker5121ce52009-01-03 21:22:43 +000057{
58 int i;
59 unsigned char sum[32];
60
Paul Bakker9e36f042013-06-30 14:34:05 +020061 if( sha256_wrapper( filename, sum ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000062 return( 1 );
63
64 for( i = 0; i < 32; i++ )
65 printf( "%02x", sum[i] );
66
67 printf( " %s\n", filename );
68 return( 0 );
69}
70
Paul Bakker9e36f042013-06-30 14:34:05 +020071static int sha256_check( char *filename )
Paul Bakker5121ce52009-01-03 21:22:43 +000072{
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[32];
79 char buf[65], line[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010080 char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +000081
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 Bakker5121ce52009-01-03 21:22:43 +000096 {
97 n = strlen( line );
98
99 if( n < 68 )
100 continue;
101
102 if( line[64] != ' ' || line[65] != ' ' )
103 continue;
104
105 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
106 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
107
108 nb_tot1++;
109
Paul Bakker9e36f042013-06-30 14:34:05 +0200110 if( sha256_wrapper( line + 66, sum ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 {
112 nb_err1++;
113 continue;
114 }
115
116 nb_tot2++;
117
118 for( i = 0; i < 32; i++ )
119 sprintf( buf + i * 2, "%02x", sum[i] );
120
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100121 /* Use constant-time buffer comparison */
122 diff = 0;
123 for( i = 0; i < 64; i++ )
124 diff |= line[i] ^ buf[i];
125
126 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 {
128 nb_err2++;
129 fprintf( stderr, "wrong checksum: %s\n", line + 66 );
130 }
131
132 n = sizeof( line );
133 }
134
Paul Bakker0c226102014-04-17 16:02:36 +0200135 fclose( f );
136
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 if( nb_err1 != 0 )
138 {
139 printf( "WARNING: %d (out of %d) input files could "
140 "not be read\n", nb_err1, nb_tot1 );
141 }
142
143 if( nb_err2 != 0 )
144 {
145 printf( "WARNING: %d (out of %d) computed checksums did "
146 "not match\n", nb_err2, nb_tot2 );
147 }
148
149 return( nb_err1 != 0 || nb_err2 != 0 );
150}
151
152int main( int argc, char *argv[] )
153{
154 int ret, i;
155
156 if( argc == 1 )
157 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200158 printf( "print mode: sha256sum <file> <file> ...\n" );
159 printf( "check mode: sha256sum -c <checksum file>\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
Paul Bakkercce9d772011-11-18 14:26:47 +0000161#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 printf( "\n Press Enter to exit this program.\n" );
163 fflush( stdout ); getchar();
164#endif
165
166 return( 1 );
167 }
168
169 if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
Paul Bakker9e36f042013-06-30 14:34:05 +0200170 return( sha256_check( argv[2] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
172 ret = 0;
173 for( i = 1; i < argc; i++ )
Paul Bakker9e36f042013-06-30 14:34:05 +0200174 ret |= sha256_print( argv[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000175
176 return( ret );
177}
Paul Bakker9e36f042013-06-30 14:34:05 +0200178#endif /* POLARSSL_SHA256_C && POLARSSL_FS_IO */