| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  sha1sum demonstration program | 
|  | 3 | * | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 4 | *  Copyright (C) 2006-2011, Brainspark B.V. | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * | 
|  | 6 | *  This file is part of PolarSSL (http://www.polarssl.org) | 
| Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 7 | *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 8 | * | 
| Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 9 | *  All rights reserved. | 
| Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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é-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #if !defined(POLARSSL_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 27 | #include "polarssl/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 28 | #else | 
|  | 29 | #include POLARSSL_CONFIG_FILE | 
|  | 30 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 |  | 
|  | 32 | #include <string.h> | 
|  | 33 | #include <stdio.h> | 
|  | 34 |  | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 35 | #include "polarssl/sha1.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 36 |  | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 37 | #if !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_FS_IO) | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 38 | int main( int argc, char *argv[] ) | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 39 | { | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 40 | ((void) argc); | 
|  | 41 | ((void) argv); | 
|  | 42 |  | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 43 | printf("POLARSSL_SHA1_C and/or POLARSSL_FS_IO not defined.\n"); | 
|  | 44 | return( 0 ); | 
|  | 45 | } | 
|  | 46 | #else | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 47 | static int sha1_wrapper( char *filename, unsigned char *sum ) | 
|  | 48 | { | 
|  | 49 | int ret = sha1_file( 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 |  | 
|  | 60 | static int sha1_print( char *filename ) | 
|  | 61 | { | 
|  | 62 | int i; | 
|  | 63 | unsigned char sum[20]; | 
|  | 64 |  | 
|  | 65 | if( sha1_wrapper( filename, sum ) != 0 ) | 
|  | 66 | return( 1 ); | 
|  | 67 |  | 
|  | 68 | for( i = 0; i < 20; i++ ) | 
|  | 69 | printf( "%02x", sum[i] ); | 
|  | 70 |  | 
|  | 71 | printf( "  %s\n", filename ); | 
|  | 72 | return( 0 ); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | static int sha1_check( 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[20]; | 
|  | 83 | char buf[41], line[1024]; | 
| Manuel Pégourié-Gonnard | 291f9af | 2013-10-28 12:51:32 +0100 | [diff] [blame] | 84 | char diff; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 |  | 
|  | 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 Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 99 | while( fgets( line, (int) n - 1, f ) != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 100 | { | 
|  | 101 | n = strlen( line ); | 
|  | 102 |  | 
|  | 103 | if( n < 44 ) | 
|  | 104 | continue; | 
|  | 105 |  | 
|  | 106 | if( line[40] != ' ' || line[41] != ' ' ) | 
|  | 107 | continue; | 
|  | 108 |  | 
|  | 109 | if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; } | 
|  | 110 | if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; } | 
|  | 111 |  | 
|  | 112 | nb_tot1++; | 
|  | 113 |  | 
|  | 114 | if( sha1_wrapper( line + 42, sum ) != 0 ) | 
|  | 115 | { | 
|  | 116 | nb_err1++; | 
|  | 117 | continue; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | nb_tot2++; | 
|  | 121 |  | 
|  | 122 | for( i = 0; i < 20; i++ ) | 
|  | 123 | sprintf( buf + i * 2, "%02x", sum[i] ); | 
|  | 124 |  | 
| Manuel Pégourié-Gonnard | 291f9af | 2013-10-28 12:51:32 +0100 | [diff] [blame] | 125 | /* Use constant-time buffer comparison */ | 
|  | 126 | diff = 0; | 
|  | 127 | for( i = 0; i < 40; i++ ) | 
|  | 128 | diff |= line[i] ^ buf[i]; | 
|  | 129 |  | 
|  | 130 | if( diff != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 | { | 
|  | 132 | nb_err2++; | 
|  | 133 | fprintf( stderr, "wrong checksum: %s\n", line + 42 ); | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | n = sizeof( line ); | 
|  | 137 | } | 
|  | 138 |  | 
| Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 139 | fclose( f ); | 
|  | 140 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 | if( nb_err1 != 0 ) | 
|  | 142 | { | 
|  | 143 | printf( "WARNING: %d (out of %d) input files could " | 
|  | 144 | "not be read\n", nb_err1, nb_tot1 ); | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | if( nb_err2 != 0 ) | 
|  | 148 | { | 
|  | 149 | printf( "WARNING: %d (out of %d) computed checksums did " | 
|  | 150 | "not match\n", nb_err2, nb_tot2 ); | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | return( nb_err1 != 0 || nb_err2 != 0 ); | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | int main( int argc, char *argv[] ) | 
|  | 157 | { | 
|  | 158 | int ret, i; | 
|  | 159 |  | 
|  | 160 | if( argc == 1 ) | 
|  | 161 | { | 
|  | 162 | printf( "print mode:  sha1sum <file> <file> ...\n" ); | 
|  | 163 | printf( "check mode:  sha1sum -c <checksum file>\n" ); | 
|  | 164 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 165 | #if defined(_WIN32) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 166 | printf( "\n  Press Enter to exit this program.\n" ); | 
|  | 167 | fflush( stdout ); getchar(); | 
|  | 168 | #endif | 
|  | 169 |  | 
|  | 170 | return( 1 ); | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | if( argc == 3 && strcmp( "-c", argv[1] ) == 0 ) | 
|  | 174 | return( sha1_check( argv[2] ) ); | 
|  | 175 |  | 
|  | 176 | ret = 0; | 
|  | 177 | for( i = 1; i < argc; i++ ) | 
|  | 178 | ret |= sha1_print( argv[i] ); | 
|  | 179 |  | 
|  | 180 | return( ret ); | 
|  | 181 | } | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 182 | #endif /* POLARSSL_SHA1_C && POLARSSL_FS_IO */ |