Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 2 | * sha256sum demonstration program |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3 | * |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, 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 | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 26 | #include "polarssl/config.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 | |
| 28 | #include <string.h> |
| 29 | #include <stdio.h> |
| 30 | |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 31 | #include "polarssl/sha256.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 32 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 33 | #if !defined(POLARSSL_SHA256_C) || !defined(POLARSSL_FS_IO) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 34 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 35 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 36 | ((void) argc); |
| 37 | ((void) argv); |
| 38 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 39 | printf("POLARSSL_SHA256_C and/or POLARSSL_FS_IO not defined.\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 40 | return( 0 ); |
| 41 | } |
| 42 | #else |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 43 | static int sha256_wrapper( char *filename, unsigned char *sum ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 44 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 45 | int ret = sha256_file( filename, sum, 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 46 | |
| 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 Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 56 | static int sha256_print( char *filename ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | { |
| 58 | int i; |
| 59 | unsigned char sum[32]; |
| 60 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 61 | if( sha256_wrapper( filename, sum ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 62 | 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 Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 71 | static int sha256_check( char *filename ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 72 | { |
| 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]; |
| 80 | |
| 81 | if( ( f = fopen( filename, "rb" ) ) == NULL ) |
| 82 | { |
| 83 | printf( "failed to open: %s\n", filename ); |
| 84 | return( 1 ); |
| 85 | } |
| 86 | |
| 87 | nb_err1 = nb_err2 = 0; |
| 88 | nb_tot1 = nb_tot2 = 0; |
| 89 | |
| 90 | memset( line, 0, sizeof( line ) ); |
| 91 | |
| 92 | n = sizeof( line ); |
| 93 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 94 | while( fgets( line, (int) n - 1, f ) != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 95 | { |
| 96 | n = strlen( line ); |
| 97 | |
| 98 | if( n < 68 ) |
| 99 | continue; |
| 100 | |
| 101 | if( line[64] != ' ' || line[65] != ' ' ) |
| 102 | continue; |
| 103 | |
| 104 | if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; } |
| 105 | if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; } |
| 106 | |
| 107 | nb_tot1++; |
| 108 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 109 | if( sha256_wrapper( line + 66, sum ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 110 | { |
| 111 | nb_err1++; |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | nb_tot2++; |
| 116 | |
| 117 | for( i = 0; i < 32; i++ ) |
| 118 | sprintf( buf + i * 2, "%02x", sum[i] ); |
| 119 | |
| 120 | if( memcmp( line, buf, 64 ) != 0 ) |
| 121 | { |
| 122 | nb_err2++; |
| 123 | fprintf( stderr, "wrong checksum: %s\n", line + 66 ); |
| 124 | } |
| 125 | |
| 126 | n = sizeof( line ); |
| 127 | } |
| 128 | |
| 129 | if( nb_err1 != 0 ) |
| 130 | { |
| 131 | printf( "WARNING: %d (out of %d) input files could " |
| 132 | "not be read\n", nb_err1, nb_tot1 ); |
| 133 | } |
| 134 | |
| 135 | if( nb_err2 != 0 ) |
| 136 | { |
| 137 | printf( "WARNING: %d (out of %d) computed checksums did " |
| 138 | "not match\n", nb_err2, nb_tot2 ); |
| 139 | } |
| 140 | |
| 141 | return( nb_err1 != 0 || nb_err2 != 0 ); |
| 142 | } |
| 143 | |
| 144 | int main( int argc, char *argv[] ) |
| 145 | { |
| 146 | int ret, i; |
| 147 | |
| 148 | if( argc == 1 ) |
| 149 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 150 | printf( "print mode: sha256sum <file> <file> ...\n" ); |
| 151 | printf( "check mode: sha256sum -c <checksum file>\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 152 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 153 | #if defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | printf( "\n Press Enter to exit this program.\n" ); |
| 155 | fflush( stdout ); getchar(); |
| 156 | #endif |
| 157 | |
| 158 | return( 1 ); |
| 159 | } |
| 160 | |
| 161 | if( argc == 3 && strcmp( "-c", argv[1] ) == 0 ) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 162 | return( sha256_check( argv[2] ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 163 | |
| 164 | ret = 0; |
| 165 | for( i = 1; i < argc; i++ ) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 166 | ret |= sha256_print( argv[i] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 167 | |
| 168 | return( ret ); |
| 169 | } |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 170 | #endif /* POLARSSL_SHA256_C && POLARSSL_FS_IO */ |