Paul Bakker | fb6c7e2 | 2011-01-21 10:21:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * generic message digest layer 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 | fb6c7e2 | 2011-01-21 10:21:11 +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 | fb6c7e2 | 2011-01-21 10:21:11 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | fb6c7e2 | 2011-01-21 10:21:11 +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 | fb6c7e2 | 2011-01-21 10:21:11 +0000 | [diff] [blame] | 28 | |
| 29 | #include <string.h> |
| 30 | #include <stdio.h> |
| 31 | |
| 32 | #include "polarssl/md.h" |
| 33 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 34 | #if !defined(POLARSSL_MD_C) |
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 | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 40 | printf("POLARSSL_MD_C not defined.\n"); |
| 41 | return( 0 ); |
| 42 | } |
| 43 | #else |
Paul Bakker | fb6c7e2 | 2011-01-21 10:21:11 +0000 | [diff] [blame] | 44 | static int generic_wrapper( const md_info_t *md_info, char *filename, unsigned char *sum ) |
| 45 | { |
| 46 | int ret = md_file( md_info, filename, sum ); |
| 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 | |
| 57 | static int generic_print( const md_info_t *md_info, char *filename ) |
| 58 | { |
| 59 | int i; |
| 60 | unsigned char sum[POLARSSL_MD_MAX_SIZE]; |
| 61 | |
| 62 | if( generic_wrapper( md_info, filename, sum ) != 0 ) |
| 63 | return( 1 ); |
| 64 | |
| 65 | for( i = 0; i < md_info->size; i++ ) |
| 66 | printf( "%02x", sum[i] ); |
| 67 | |
| 68 | printf( " %s\n", filename ); |
| 69 | return( 0 ); |
| 70 | } |
| 71 | |
| 72 | static int generic_check( const md_info_t *md_info, char *filename ) |
| 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[POLARSSL_MD_MAX_SIZE]; |
| 80 | char buf[POLARSSL_MD_MAX_SIZE * 2 + 1], line[1024]; |
Manuel Pégourié-Gonnard | 291f9af | 2013-10-28 12:51:32 +0100 | [diff] [blame] | 81 | char diff; |
Paul Bakker | fb6c7e2 | 2011-01-21 10:21:11 +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 | fb6c7e2 | 2011-01-21 10:21:11 +0000 | [diff] [blame] | 97 | { |
| 98 | n = strlen( line ); |
| 99 | |
| 100 | if( n < (size_t) 2 * md_info->size + 4 ) |
| 101 | { |
| 102 | printf("No '%s' hash found on line.\n", md_info->name); |
| 103 | continue; |
| 104 | } |
| 105 | |
| 106 | if( line[2 * md_info->size] != ' ' || line[2 * md_info->size + 1] != ' ' ) |
| 107 | { |
| 108 | printf("No '%s' hash found on line.\n", md_info->name); |
| 109 | continue; |
| 110 | } |
| 111 | |
| 112 | if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; } |
| 113 | if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; } |
| 114 | |
| 115 | nb_tot1++; |
| 116 | |
| 117 | if( generic_wrapper( md_info, line + 2 + 2 * md_info->size, sum ) != 0 ) |
| 118 | { |
| 119 | nb_err1++; |
| 120 | continue; |
| 121 | } |
| 122 | |
| 123 | nb_tot2++; |
| 124 | |
| 125 | for( i = 0; i < md_info->size; i++ ) |
| 126 | sprintf( buf + i * 2, "%02x", sum[i] ); |
| 127 | |
Manuel Pégourié-Gonnard | 291f9af | 2013-10-28 12:51:32 +0100 | [diff] [blame] | 128 | /* Use constant-time buffer comparison */ |
| 129 | diff = 0; |
| 130 | for( i = 0; i < 2 * md_info->size; i++ ) |
| 131 | diff |= line[i] ^ buf[i]; |
| 132 | |
| 133 | if( diff != 0 ) |
Paul Bakker | fb6c7e2 | 2011-01-21 10:21:11 +0000 | [diff] [blame] | 134 | { |
| 135 | nb_err2++; |
| 136 | fprintf( stderr, "wrong checksum: %s\n", line + 66 ); |
| 137 | } |
| 138 | |
| 139 | n = sizeof( line ); |
| 140 | } |
| 141 | |
| 142 | if( nb_err1 != 0 ) |
| 143 | { |
| 144 | printf( "WARNING: %d (out of %d) input files could " |
| 145 | "not be read\n", nb_err1, nb_tot1 ); |
| 146 | } |
| 147 | |
| 148 | if( nb_err2 != 0 ) |
| 149 | { |
| 150 | printf( "WARNING: %d (out of %d) computed checksums did " |
| 151 | "not match\n", nb_err2, nb_tot2 ); |
| 152 | } |
| 153 | |
Paul Bakker | 64abd83 | 2014-02-06 15:03:06 +0100 | [diff] [blame] | 154 | fclose( f ); |
| 155 | |
Paul Bakker | fb6c7e2 | 2011-01-21 10:21:11 +0000 | [diff] [blame] | 156 | return( nb_err1 != 0 || nb_err2 != 0 ); |
| 157 | } |
| 158 | |
| 159 | int main( int argc, char *argv[] ) |
| 160 | { |
| 161 | int ret, i; |
| 162 | const md_info_t *md_info; |
| 163 | md_context_t md_ctx; |
| 164 | |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 165 | md_init( &md_ctx ); |
Paul Bakker | fb6c7e2 | 2011-01-21 10:21:11 +0000 | [diff] [blame] | 166 | |
| 167 | if( argc == 1 ) |
| 168 | { |
| 169 | const int *list; |
| 170 | |
| 171 | printf( "print mode: generic_sum <md> <file> <file> ...\n" ); |
| 172 | printf( "check mode: generic_sum <md> -c <checksum file>\n" ); |
| 173 | |
| 174 | printf( "\nAvailable message digests:\n" ); |
| 175 | list = md_list(); |
| 176 | while( *list ) |
| 177 | { |
| 178 | md_info = md_info_from_type( *list ); |
| 179 | printf( " %s\n", md_info->name ); |
| 180 | list++; |
| 181 | } |
| 182 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 183 | #if defined(_WIN32) |
Paul Bakker | fb6c7e2 | 2011-01-21 10:21:11 +0000 | [diff] [blame] | 184 | printf( "\n Press Enter to exit this program.\n" ); |
| 185 | fflush( stdout ); getchar(); |
| 186 | #endif |
| 187 | |
| 188 | return( 1 ); |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Read the MD from the command line |
| 193 | */ |
| 194 | md_info = md_info_from_string( argv[1] ); |
| 195 | if( md_info == NULL ) |
| 196 | { |
| 197 | fprintf( stderr, "Message Digest '%s' not found\n", argv[1] ); |
| 198 | return( 1 ); |
| 199 | } |
| 200 | if( md_init_ctx( &md_ctx, md_info) ) |
| 201 | { |
| 202 | fprintf( stderr, "Failed to initialize context.\n" ); |
| 203 | return( 1 ); |
| 204 | } |
Paul Bakker | fb6c7e2 | 2011-01-21 10:21:11 +0000 | [diff] [blame] | 205 | |
| 206 | ret = 0; |
| 207 | if( argc == 4 && strcmp( "-c", argv[2] ) == 0 ) |
| 208 | { |
| 209 | ret |= generic_check( md_info, argv[3] ); |
| 210 | goto exit; |
| 211 | } |
| 212 | |
Paul Bakker | fb6c7e2 | 2011-01-21 10:21:11 +0000 | [diff] [blame] | 213 | for( i = 2; i < argc; i++ ) |
| 214 | ret |= generic_print( md_info, argv[i] ); |
| 215 | |
| 216 | exit: |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 217 | md_free( &md_ctx ); |
Paul Bakker | fb6c7e2 | 2011-01-21 10:21:11 +0000 | [diff] [blame] | 218 | |
| 219 | return( ret ); |
| 220 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 221 | #endif /* POLARSSL_MD_C */ |