blob: 50227470427c483d73253a5628349c81cd041cc2 [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 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
29#include <string.h>
30#include <stdio.h>
31
Paul Bakkerd2681d82013-06-30 14:49:12 +020032#include "polarssl/sha256.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker9e36f042013-06-30 14:34:05 +020034#if !defined(POLARSSL_SHA256_C) || !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000035int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000036{
Paul Bakkercce9d772011-11-18 14:26:47 +000037 ((void) argc);
38 ((void) argv);
39
Paul Bakker9e36f042013-06-30 14:34:05 +020040 printf("POLARSSL_SHA256_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000041 return( 0 );
42}
43#else
Paul Bakker9e36f042013-06-30 14:34:05 +020044static int sha256_wrapper( char *filename, unsigned char *sum )
Paul Bakker5121ce52009-01-03 21:22:43 +000045{
Paul Bakker9e36f042013-06-30 14:34:05 +020046 int ret = sha256_file( filename, sum, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000047
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
Paul Bakker9e36f042013-06-30 14:34:05 +020057static int sha256_print( char *filename )
Paul Bakker5121ce52009-01-03 21:22:43 +000058{
59 int i;
60 unsigned char sum[32];
61
Paul Bakker9e36f042013-06-30 14:34:05 +020062 if( sha256_wrapper( filename, sum ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000063 return( 1 );
64
65 for( i = 0; i < 32; i++ )
66 printf( "%02x", sum[i] );
67
68 printf( " %s\n", filename );
69 return( 0 );
70}
71
Paul Bakker9e36f042013-06-30 14:34:05 +020072static int sha256_check( char *filename )
Paul Bakker5121ce52009-01-03 21:22:43 +000073{
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[32];
80 char buf[65], line[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010081 char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +000082
83 if( ( f = fopen( filename, "rb" ) ) == NULL )
84 {
85 printf( "failed to open: %s\n", filename );
86 return( 1 );
87 }
88
89 nb_err1 = nb_err2 = 0;
90 nb_tot1 = nb_tot2 = 0;
91
92 memset( line, 0, sizeof( line ) );
93
94 n = sizeof( line );
95
Paul Bakker23986e52011-04-24 08:57:21 +000096 while( fgets( line, (int) n - 1, f ) != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000097 {
98 n = strlen( line );
99
100 if( n < 68 )
101 continue;
102
103 if( line[64] != ' ' || line[65] != ' ' )
104 continue;
105
106 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
107 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
108
109 nb_tot1++;
110
Paul Bakker9e36f042013-06-30 14:34:05 +0200111 if( sha256_wrapper( line + 66, sum ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 {
113 nb_err1++;
114 continue;
115 }
116
117 nb_tot2++;
118
119 for( i = 0; i < 32; i++ )
120 sprintf( buf + i * 2, "%02x", sum[i] );
121
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100122 /* Use constant-time buffer comparison */
123 diff = 0;
124 for( i = 0; i < 64; i++ )
125 diff |= line[i] ^ buf[i];
126
127 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 {
129 nb_err2++;
130 fprintf( stderr, "wrong checksum: %s\n", line + 66 );
131 }
132
133 n = sizeof( line );
134 }
135
Paul Bakker0c226102014-04-17 16:02:36 +0200136 fclose( f );
137
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 if( nb_err1 != 0 )
139 {
140 printf( "WARNING: %d (out of %d) input files could "
141 "not be read\n", nb_err1, nb_tot1 );
142 }
143
144 if( nb_err2 != 0 )
145 {
146 printf( "WARNING: %d (out of %d) computed checksums did "
147 "not match\n", nb_err2, nb_tot2 );
148 }
149
150 return( nb_err1 != 0 || nb_err2 != 0 );
151}
152
153int main( int argc, char *argv[] )
154{
155 int ret, i;
156
157 if( argc == 1 )
158 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200159 printf( "print mode: sha256sum <file> <file> ...\n" );
160 printf( "check mode: sha256sum -c <checksum file>\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
Paul Bakkercce9d772011-11-18 14:26:47 +0000162#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 printf( "\n Press Enter to exit this program.\n" );
164 fflush( stdout ); getchar();
165#endif
166
167 return( 1 );
168 }
169
170 if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
Paul Bakker9e36f042013-06-30 14:34:05 +0200171 return( sha256_check( argv[2] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
173 ret = 0;
174 for( i = 1; i < argc; i++ )
Paul Bakker9e36f042013-06-30 14:34:05 +0200175 ret |= sha256_print( argv[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
177 return( ret );
178}
Paul Bakker9e36f042013-06-30 14:34:05 +0200179#endif /* POLARSSL_SHA256_C && POLARSSL_FS_IO */