| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  md5sum demonstration program | 
|  | 3 | * | 
| Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | *  Copyright (C) 2006-2011, ARM Limited, All Rights Reserved | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | *  This file is part of mbed TLS (https://tls.mbed.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 |  | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 29 | #if defined(POLARSSL_PLATFORM_C) | 
|  | 30 | #include "polarssl/platform.h" | 
|  | 31 | #else | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 32 | #include <stdio.h> | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 33 | #define polarssl_fprintf    fprintf | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 34 | #define polarssl_printf     printf | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 35 | #endif | 
|  | 36 |  | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 37 | #if defined(POLARSSL_MD5_C) && defined(POLARSSL_FS_IO) | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 38 | #include "polarssl/md5.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 39 |  | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 40 | #include <stdio.h> | 
|  | 41 | #include <string.h> | 
|  | 42 | #endif | 
|  | 43 |  | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 44 | #if !defined(POLARSSL_MD5_C) || !defined(POLARSSL_FS_IO) | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 45 | int main( void ) | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 46 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 47 | polarssl_printf("POLARSSL_MD5_C and/or POLARSSL_FS_IO not defined.\n"); | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 48 | return( 0 ); | 
|  | 49 | } | 
|  | 50 | #else | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 51 | static int md5_wrapper( char *filename, unsigned char *sum ) | 
|  | 52 | { | 
|  | 53 | int ret = md5_file( filename, sum ); | 
|  | 54 |  | 
|  | 55 | if( ret == 1 ) | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 56 | polarssl_fprintf( stderr, "failed to open: %s\n", filename ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 |  | 
|  | 58 | if( ret == 2 ) | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 59 | polarssl_fprintf( stderr, "failed to read: %s\n", filename ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 60 |  | 
|  | 61 | return( ret ); | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | static int md5_print( char *filename ) | 
|  | 65 | { | 
|  | 66 | int i; | 
|  | 67 | unsigned char sum[16]; | 
|  | 68 |  | 
|  | 69 | if( md5_wrapper( filename, sum ) != 0 ) | 
|  | 70 | return( 1 ); | 
|  | 71 |  | 
|  | 72 | for( i = 0; i < 16; i++ ) | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 73 | polarssl_printf( "%02x", sum[i] ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 74 |  | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 75 | polarssl_printf( "  %s\n", filename ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 76 | return( 0 ); | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | static int md5_check( char *filename ) | 
|  | 80 | { | 
|  | 81 | int i; | 
|  | 82 | size_t n; | 
|  | 83 | FILE *f; | 
|  | 84 | int nb_err1, nb_err2; | 
|  | 85 | int nb_tot1, nb_tot2; | 
|  | 86 | unsigned char sum[16]; | 
|  | 87 | char buf[33], line[1024]; | 
| Manuel Pégourié-Gonnard | 291f9af | 2013-10-28 12:51:32 +0100 | [diff] [blame] | 88 | char diff; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 89 |  | 
|  | 90 | if( ( f = fopen( filename, "rb" ) ) == NULL ) | 
|  | 91 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 92 | polarssl_printf( "failed to open: %s\n", filename ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 93 | return( 1 ); | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | nb_err1 = nb_err2 = 0; | 
|  | 97 | nb_tot1 = nb_tot2 = 0; | 
|  | 98 |  | 
|  | 99 | memset( line, 0, sizeof( line ) ); | 
|  | 100 |  | 
|  | 101 | n = sizeof( line ); | 
|  | 102 |  | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 103 | while( fgets( line, (int) n - 1, f ) != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 104 | { | 
|  | 105 | n = strlen( line ); | 
|  | 106 |  | 
|  | 107 | if( n < 36 ) | 
|  | 108 | continue; | 
|  | 109 |  | 
|  | 110 | if( line[32] != ' ' || line[33] != ' ' ) | 
|  | 111 | continue; | 
|  | 112 |  | 
|  | 113 | if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; } | 
|  | 114 | if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; } | 
|  | 115 |  | 
|  | 116 | nb_tot1++; | 
|  | 117 |  | 
|  | 118 | if( md5_wrapper( line + 34, sum ) != 0 ) | 
|  | 119 | { | 
|  | 120 | nb_err1++; | 
|  | 121 | continue; | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | nb_tot2++; | 
|  | 125 |  | 
|  | 126 | for( i = 0; i < 16; i++ ) | 
|  | 127 | sprintf( buf + i * 2, "%02x", sum[i] ); | 
|  | 128 |  | 
| Manuel Pégourié-Gonnard | 291f9af | 2013-10-28 12:51:32 +0100 | [diff] [blame] | 129 | /* Use constant-time buffer comparison */ | 
|  | 130 | diff = 0; | 
|  | 131 | for( i = 0; i < 32; i++ ) | 
|  | 132 | diff |= line[i] ^ buf[i]; | 
|  | 133 |  | 
|  | 134 | if( diff != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 135 | { | 
|  | 136 | nb_err2++; | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 137 | polarssl_fprintf( stderr, "wrong checksum: %s\n", line + 34 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 138 | } | 
|  | 139 |  | 
|  | 140 | n = sizeof( line ); | 
|  | 141 | } | 
|  | 142 |  | 
| Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 143 | fclose( f ); | 
|  | 144 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 145 | if( nb_err1 != 0 ) | 
|  | 146 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 147 | polarssl_printf( "WARNING: %d (out of %d) input files could " | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 148 | "not be read\n", nb_err1, nb_tot1 ); | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | if( nb_err2 != 0 ) | 
|  | 152 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 153 | polarssl_printf( "WARNING: %d (out of %d) computed checksums did " | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | "not match\n", nb_err2, nb_tot2 ); | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | return( nb_err1 != 0 || nb_err2 != 0 ); | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | int main( int argc, char *argv[] ) | 
|  | 161 | { | 
|  | 162 | int ret, i; | 
|  | 163 |  | 
|  | 164 | if( argc == 1 ) | 
|  | 165 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 166 | polarssl_printf( "print mode:  md5sum <file> <file> ...\n" ); | 
|  | 167 | polarssl_printf( "check mode:  md5sum -c <checksum file>\n" ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 168 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 169 | #if defined(_WIN32) | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 170 | polarssl_printf( "\n  Press Enter to exit this program.\n" ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 171 | fflush( stdout ); getchar(); | 
|  | 172 | #endif | 
|  | 173 |  | 
|  | 174 | return( 1 ); | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | if( argc == 3 && strcmp( "-c", argv[1] ) == 0 ) | 
|  | 178 | return( md5_check( argv[2] ) ); | 
|  | 179 |  | 
|  | 180 | ret = 0; | 
|  | 181 | for( i = 1; i < argc; i++ ) | 
|  | 182 | ret |= md5_print( argv[i] ); | 
|  | 183 |  | 
|  | 184 | return( ret ); | 
|  | 185 | } | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 186 | #endif /* POLARSSL_MD5_C && POLARSSL_FS_IO */ |