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 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 085ab04 | 2015-01-23 11:06:27 +0000 | [diff] [blame^] | 6 | * This file is part of mbed TLS (https://www.polarssl.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8 | * 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é-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
| 29 | #include <string.h> |
| 30 | #include <stdio.h> |
| 31 | |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 32 | #include "polarssl/sha256.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 33 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 34 | #if !defined(POLARSSL_SHA256_C) || !defined(POLARSSL_FS_IO) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 35 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 36 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 37 | ((void) argc); |
| 38 | ((void) argv); |
| 39 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 40 | printf("POLARSSL_SHA256_C and/or POLARSSL_FS_IO not defined.\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 41 | return( 0 ); |
| 42 | } |
| 43 | #else |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 44 | static int sha256_wrapper( char *filename, unsigned char *sum ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 45 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 46 | int ret = sha256_file( filename, sum, 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 47 | |
| 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 Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 57 | static int sha256_print( char *filename ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 58 | { |
| 59 | int i; |
| 60 | unsigned char sum[32]; |
| 61 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 62 | if( sha256_wrapper( filename, sum ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 63 | 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 Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 72 | static int sha256_check( char *filename ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 73 | { |
| 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é-Gonnard | 291f9af | 2013-10-28 12:51:32 +0100 | [diff] [blame] | 81 | char diff; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 82 | |
| 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 Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 96 | while( fgets( line, (int) n - 1, f ) != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 97 | { |
| 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 Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 111 | if( sha256_wrapper( line + 66, sum ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 112 | { |
| 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é-Gonnard | 291f9af | 2013-10-28 12:51:32 +0100 | [diff] [blame] | 122 | /* 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 128 | { |
| 129 | nb_err2++; |
| 130 | fprintf( stderr, "wrong checksum: %s\n", line + 66 ); |
| 131 | } |
| 132 | |
| 133 | n = sizeof( line ); |
| 134 | } |
| 135 | |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 136 | fclose( f ); |
| 137 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 138 | 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 | |
| 153 | int main( int argc, char *argv[] ) |
| 154 | { |
| 155 | int ret, i; |
| 156 | |
| 157 | if( argc == 1 ) |
| 158 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 159 | printf( "print mode: sha256sum <file> <file> ...\n" ); |
| 160 | printf( "check mode: sha256sum -c <checksum file>\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 161 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 162 | #if defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 163 | 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 Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 171 | return( sha256_check( argv[2] ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 172 | |
| 173 | ret = 0; |
| 174 | for( i = 1; i < argc; i++ ) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 175 | ret |= sha256_print( argv[i] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 176 | |
| 177 | return( ret ); |
| 178 | } |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 179 | #endif /* POLARSSL_SHA256_C && POLARSSL_FS_IO */ |