| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  \brief  Generic file encryption program using generic wrappers for configured | 
|  | 3 | *          security. | 
|  | 4 | * | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 5 | *  Copyright (C) 2006-2011, Brainspark B.V. | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 6 | * | 
|  | 7 | *  This file is part of PolarSSL (http://www.polarssl.org) | 
|  | 8 | *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> | 
|  | 9 | * | 
|  | 10 | *  All rights reserved. | 
|  | 11 | * | 
|  | 12 | *  This program is free software; you can redistribute it and/or modify | 
|  | 13 | *  it under the terms of the GNU General Public License as published by | 
|  | 14 | *  the Free Software Foundation; either version 2 of the License, or | 
|  | 15 | *  (at your option) any later version. | 
|  | 16 | * | 
|  | 17 | *  This program is distributed in the hope that it will be useful, | 
|  | 18 | *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 19 | *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 20 | *  GNU General Public License for more details. | 
|  | 21 | * | 
|  | 22 | *  You should have received a copy of the GNU General Public License along | 
|  | 23 | *  with this program; if not, write to the Free Software Foundation, Inc., | 
|  | 24 | *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 
|  | 25 | */ | 
|  | 26 |  | 
|  | 27 | #ifndef _CRT_SECURE_NO_DEPRECATE | 
|  | 28 | #define _CRT_SECURE_NO_DEPRECATE 1 | 
|  | 29 | #endif | 
|  | 30 |  | 
| Paul Bakker | 494c0b8 | 2011-04-24 15:30:07 +0000 | [diff] [blame] | 31 | #if defined(_WIN32) | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 32 | #include <windows.h> | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 33 | #if !defined(_WIN32_WCE) | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 34 | #include <io.h> | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 35 | #endif | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 36 | #else | 
|  | 37 | #include <sys/types.h> | 
|  | 38 | #include <unistd.h> | 
|  | 39 | #endif | 
|  | 40 |  | 
|  | 41 | #include <string.h> | 
|  | 42 | #include <stdlib.h> | 
|  | 43 | #include <stdio.h> | 
|  | 44 | #include <time.h> | 
|  | 45 |  | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 46 | #include "polarssl/config.h" | 
|  | 47 |  | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 48 | #include "polarssl/cipher.h" | 
|  | 49 | #include "polarssl/md.h" | 
|  | 50 |  | 
|  | 51 | #define MODE_ENCRYPT    0 | 
|  | 52 | #define MODE_DECRYPT    1 | 
|  | 53 |  | 
|  | 54 | #define USAGE   \ | 
|  | 55 | "\n  crypt_and_hash <mode> <input filename> <output filename> <cipher> <md> <key>\n" \ | 
|  | 56 | "\n   <mode>: 0 = encrypt, 1 = decrypt\n" \ | 
|  | 57 | "\n  example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \ | 
|  | 58 | "\n" | 
|  | 59 |  | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 60 | #if !defined(POLARSSL_CIPHER_C) || !defined(POLARSSL_MD_C) | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 61 | int main( int argc, char *argv[] ) | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 62 | { | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 63 | ((void) argc); | 
|  | 64 | ((void) argv); | 
|  | 65 |  | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 66 | printf("POLARSSL_CIPHER_C and/or POLARSSL_MD_C not defined.\n"); | 
|  | 67 | return( 0 ); | 
|  | 68 | } | 
|  | 69 | #else | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 70 | int main( int argc, char *argv[] ) | 
|  | 71 | { | 
| Paul Bakker | 25b5fe5 | 2011-05-26 14:02:58 +0000 | [diff] [blame] | 72 | int ret = 1, i, n; | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 73 | int mode, lastn; | 
| Paul Bakker | 25b5fe5 | 2011-05-26 14:02:58 +0000 | [diff] [blame] | 74 | size_t keylen, ilen, olen; | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 75 | FILE *fkey, *fin = NULL, *fout = NULL; | 
|  | 76 |  | 
|  | 77 | char *p; | 
|  | 78 | unsigned char IV[16]; | 
|  | 79 | unsigned char key[512]; | 
|  | 80 | unsigned char digest[POLARSSL_MD_MAX_SIZE]; | 
|  | 81 | unsigned char buffer[1024]; | 
|  | 82 | unsigned char output[1024]; | 
|  | 83 |  | 
|  | 84 | const cipher_info_t *cipher_info; | 
|  | 85 | const md_info_t *md_info; | 
|  | 86 | cipher_context_t cipher_ctx; | 
|  | 87 | md_context_t md_ctx; | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 88 | #if defined(_WIN32_WCE) | 
|  | 89 | long filesize, offset; | 
|  | 90 | #elif defined(_WIN32) | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 91 | LARGE_INTEGER li_size; | 
|  | 92 | __int64 filesize, offset; | 
|  | 93 | #else | 
|  | 94 | off_t filesize, offset; | 
|  | 95 | #endif | 
|  | 96 |  | 
|  | 97 | memset( &cipher_ctx, 0, sizeof( cipher_context_t )); | 
|  | 98 | memset( &md_ctx, 0, sizeof( md_context_t )); | 
|  | 99 |  | 
|  | 100 | /* | 
|  | 101 | * Parse the command-line arguments. | 
|  | 102 | */ | 
|  | 103 | if( argc != 7 ) | 
|  | 104 | { | 
|  | 105 | const int *list; | 
|  | 106 |  | 
|  | 107 | printf( USAGE ); | 
|  | 108 |  | 
|  | 109 | printf( "Available ciphers:\n" ); | 
|  | 110 | list = cipher_list(); | 
|  | 111 | while( *list ) | 
|  | 112 | { | 
|  | 113 | cipher_info = cipher_info_from_type( *list ); | 
|  | 114 | printf( "  %s\n", cipher_info->name ); | 
|  | 115 | list++; | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | printf( "\nAvailable message digests:\n" ); | 
|  | 119 | list = md_list(); | 
|  | 120 | while( *list ) | 
|  | 121 | { | 
|  | 122 | md_info = md_info_from_type( *list ); | 
|  | 123 | printf( "  %s\n", md_info->name ); | 
|  | 124 | list++; | 
|  | 125 | } | 
|  | 126 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 127 | #if defined(_WIN32) | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 128 | printf( "\n  Press Enter to exit this program.\n" ); | 
|  | 129 | fflush( stdout ); getchar(); | 
|  | 130 | #endif | 
|  | 131 |  | 
|  | 132 | goto exit; | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | mode = atoi( argv[1] ); | 
|  | 136 |  | 
|  | 137 | if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT ) | 
|  | 138 | { | 
|  | 139 | fprintf( stderr, "invalid operation mode\n" ); | 
|  | 140 | goto exit; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | if( strcmp( argv[2], argv[3] ) == 0 ) | 
|  | 144 | { | 
|  | 145 | fprintf( stderr, "input and output filenames must differ\n" ); | 
|  | 146 | goto exit; | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | if( ( fin = fopen( argv[2], "rb" ) ) == NULL ) | 
|  | 150 | { | 
|  | 151 | fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] ); | 
|  | 152 | goto exit; | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | if( ( fout = fopen( argv[3], "wb+" ) ) == NULL ) | 
|  | 156 | { | 
|  | 157 | fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] ); | 
|  | 158 | goto exit; | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | /* | 
|  | 162 | * Read the Cipher and MD from the command line | 
|  | 163 | */ | 
|  | 164 | cipher_info = cipher_info_from_string( argv[4] ); | 
|  | 165 | if( cipher_info == NULL ) | 
|  | 166 | { | 
|  | 167 | fprintf( stderr, "Cipher '%s' not found\n", argv[4] ); | 
|  | 168 | goto exit; | 
|  | 169 | } | 
|  | 170 | cipher_init_ctx( &cipher_ctx, cipher_info); | 
|  | 171 |  | 
|  | 172 | md_info = md_info_from_string( argv[5] ); | 
|  | 173 | if( md_info == NULL ) | 
|  | 174 | { | 
|  | 175 | fprintf( stderr, "Message Digest '%s' not found\n", argv[5] ); | 
|  | 176 | goto exit; | 
|  | 177 | } | 
|  | 178 | md_init_ctx( &md_ctx, md_info); | 
|  | 179 |  | 
|  | 180 | /* | 
|  | 181 | * Read the secret key and clean the command line. | 
|  | 182 | */ | 
|  | 183 | if( ( fkey = fopen( argv[6], "rb" ) ) != NULL ) | 
|  | 184 | { | 
|  | 185 | keylen = fread( key, 1, sizeof( key ), fkey ); | 
|  | 186 | fclose( fkey ); | 
|  | 187 | } | 
|  | 188 | else | 
|  | 189 | { | 
|  | 190 | if( memcmp( argv[6], "hex:", 4 ) == 0 ) | 
|  | 191 | { | 
|  | 192 | p = &argv[6][4]; | 
|  | 193 | keylen = 0; | 
|  | 194 |  | 
|  | 195 | while( sscanf( p, "%02X", &n ) > 0 && | 
|  | 196 | keylen < (int) sizeof( key ) ) | 
|  | 197 | { | 
|  | 198 | key[keylen++] = (unsigned char) n; | 
|  | 199 | p += 2; | 
|  | 200 | } | 
|  | 201 | } | 
|  | 202 | else | 
|  | 203 | { | 
|  | 204 | keylen = strlen( argv[6] ); | 
|  | 205 |  | 
|  | 206 | if( keylen > (int) sizeof( key ) ) | 
|  | 207 | keylen = (int) sizeof( key ); | 
|  | 208 |  | 
|  | 209 | memcpy( key, argv[6], keylen ); | 
|  | 210 | } | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | memset( argv[6], 0, strlen( argv[6] ) ); | 
|  | 214 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 215 | #if defined(_WIN32_WCE) | 
|  | 216 | filesize = fseek( fin, 0L, SEEK_END ); | 
|  | 217 | #else | 
|  | 218 | #if defined(_WIN32) | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 219 | /* | 
|  | 220 | * Support large files (> 2Gb) on Win32 | 
|  | 221 | */ | 
|  | 222 | li_size.QuadPart = 0; | 
|  | 223 | li_size.LowPart  = | 
|  | 224 | SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ), | 
|  | 225 | li_size.LowPart, &li_size.HighPart, FILE_END ); | 
|  | 226 |  | 
|  | 227 | if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR ) | 
|  | 228 | { | 
|  | 229 | fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" ); | 
|  | 230 | goto exit; | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 | filesize = li_size.QuadPart; | 
|  | 234 | #else | 
|  | 235 | if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 ) | 
|  | 236 | { | 
|  | 237 | perror( "lseek" ); | 
|  | 238 | goto exit; | 
|  | 239 | } | 
|  | 240 | #endif | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 241 | #endif | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 242 |  | 
|  | 243 | if( fseek( fin, 0, SEEK_SET ) < 0 ) | 
|  | 244 | { | 
|  | 245 | fprintf( stderr, "fseek(0,SEEK_SET) failed\n" ); | 
|  | 246 | goto exit; | 
|  | 247 | } | 
|  | 248 |  | 
|  | 249 | if( mode == MODE_ENCRYPT ) | 
|  | 250 | { | 
|  | 251 | /* | 
|  | 252 | * Generate the initialization vector as: | 
|  | 253 | * IV = SHA-256( filesize || filename )[0..15] | 
|  | 254 | */ | 
|  | 255 | for( i = 0; i < 8; i++ ) | 
|  | 256 | buffer[i] = (unsigned char)( filesize >> ( i << 3 ) ); | 
|  | 257 |  | 
|  | 258 | p = argv[2]; | 
|  | 259 |  | 
|  | 260 | md_starts( &md_ctx ); | 
|  | 261 | md_update( &md_ctx, buffer, 8 ); | 
|  | 262 | md_update( &md_ctx, (unsigned char *) p, strlen( p ) ); | 
|  | 263 | md_finish( &md_ctx, digest ); | 
|  | 264 |  | 
|  | 265 | memcpy( IV, digest, 16 ); | 
|  | 266 |  | 
|  | 267 | /* | 
|  | 268 | * The last four bits in the IV are actually used | 
|  | 269 | * to store the file size modulo the AES block size. | 
|  | 270 | */ | 
|  | 271 | lastn = (int)( filesize & 0x0F ); | 
|  | 272 |  | 
|  | 273 | IV[15] = (unsigned char) | 
|  | 274 | ( ( IV[15] & 0xF0 ) | lastn ); | 
|  | 275 |  | 
|  | 276 | /* | 
|  | 277 | * Append the IV at the beginning of the output. | 
|  | 278 | */ | 
|  | 279 | if( fwrite( IV, 1, 16, fout ) != 16 ) | 
|  | 280 | { | 
|  | 281 | fprintf( stderr, "fwrite(%d bytes) failed\n", 16 ); | 
|  | 282 | goto exit; | 
|  | 283 | } | 
|  | 284 |  | 
|  | 285 | /* | 
|  | 286 | * Hash the IV and the secret key together 8192 times | 
|  | 287 | * using the result to setup the AES context and HMAC. | 
|  | 288 | */ | 
|  | 289 | memset( digest, 0,  32 ); | 
|  | 290 | memcpy( digest, IV, 16 ); | 
|  | 291 |  | 
|  | 292 | for( i = 0; i < 8192; i++ ) | 
|  | 293 | { | 
|  | 294 | md_starts( &md_ctx ); | 
|  | 295 | md_update( &md_ctx, digest, 32 ); | 
|  | 296 | md_update( &md_ctx, key, keylen ); | 
|  | 297 | md_finish( &md_ctx, digest ); | 
|  | 298 |  | 
|  | 299 | } | 
|  | 300 |  | 
|  | 301 | memset( key, 0, sizeof( key ) ); | 
|  | 302 |  | 
| Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 303 | if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length, | 
|  | 304 | POLARSSL_ENCRYPT ) != 0 ) | 
|  | 305 | { | 
|  | 306 | fprintf( stderr, "cipher_setkey() returned error\n"); | 
|  | 307 | goto exit; | 
|  | 308 | } | 
|  | 309 | if( cipher_reset( &cipher_ctx, IV ) != 0 ) | 
|  | 310 | { | 
|  | 311 | fprintf( stderr, "cipher_reset() returned error\n"); | 
|  | 312 | goto exit; | 
|  | 313 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 314 |  | 
|  | 315 | md_hmac_starts( &md_ctx, digest, 32 ); | 
|  | 316 |  | 
|  | 317 | /* | 
|  | 318 | * Encrypt and write the ciphertext. | 
|  | 319 | */ | 
|  | 320 | for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) ) | 
|  | 321 | { | 
| Paul Bakker | 25b5fe5 | 2011-05-26 14:02:58 +0000 | [diff] [blame] | 322 | ilen = ( (unsigned int) filesize - offset > cipher_get_block_size( &cipher_ctx ) ) ? | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 323 | cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset ); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 324 |  | 
| Paul Bakker | 25b5fe5 | 2011-05-26 14:02:58 +0000 | [diff] [blame] | 325 | if( fread( buffer, 1, ilen, fin ) != ilen ) | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 326 | { | 
| Paul Bakker | 2c0994e | 2011-05-25 13:51:57 +0000 | [diff] [blame] | 327 | fprintf( stderr, "fread(%ld bytes) failed\n", (long) n ); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 328 | goto exit; | 
|  | 329 | } | 
|  | 330 |  | 
| Paul Bakker | 25b5fe5 | 2011-05-26 14:02:58 +0000 | [diff] [blame] | 331 | cipher_update( &cipher_ctx, buffer, ilen, output, &olen ); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 332 | md_hmac_update( &md_ctx, output, olen ); | 
|  | 333 |  | 
| Paul Bakker | 2c0994e | 2011-05-25 13:51:57 +0000 | [diff] [blame] | 334 | if( fwrite( output, 1, olen, fout ) != olen ) | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 335 | { | 
| Paul Bakker | 2c0994e | 2011-05-25 13:51:57 +0000 | [diff] [blame] | 336 | fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen ); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 337 | goto exit; | 
|  | 338 | } | 
|  | 339 | } | 
|  | 340 |  | 
| Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 341 | if( cipher_finish( &cipher_ctx, output, &olen ) != 0 ) | 
|  | 342 | { | 
|  | 343 | fprintf( stderr, "cipher_finish() returned error\n" ); | 
|  | 344 | goto exit; | 
|  | 345 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 346 | md_hmac_update( &md_ctx, output, olen ); | 
|  | 347 |  | 
| Paul Bakker | 2c0994e | 2011-05-25 13:51:57 +0000 | [diff] [blame] | 348 | if( fwrite( output, 1, olen, fout ) != olen ) | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 349 | { | 
| Paul Bakker | 25b5fe5 | 2011-05-26 14:02:58 +0000 | [diff] [blame] | 350 | fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen ); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 351 | goto exit; | 
|  | 352 | } | 
| Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 353 |  | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 354 | /* | 
|  | 355 | * Finally write the HMAC. | 
|  | 356 | */ | 
|  | 357 | md_hmac_finish( &md_ctx, digest ); | 
|  | 358 |  | 
| Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 359 | if( fwrite( digest, 1, md_get_size( md_info ), fout ) != md_get_size( md_info ) ) | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 360 | { | 
| Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 361 | fprintf( stderr, "fwrite(%d bytes) failed\n", md_get_size( md_info ) ); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 362 | goto exit; | 
|  | 363 | } | 
|  | 364 | } | 
|  | 365 |  | 
|  | 366 | if( mode == MODE_DECRYPT ) | 
|  | 367 | { | 
|  | 368 | /* | 
|  | 369 | *  The encrypted file must be structured as follows: | 
|  | 370 | * | 
|  | 371 | *        00 .. 15              Initialization Vector | 
|  | 372 | *        16 .. 31              AES Encrypted Block #1 | 
|  | 373 | *           .. | 
|  | 374 | *      N*16 .. (N+1)*16 - 1    AES Encrypted Block #N | 
|  | 375 | *  (N+1)*16 .. (N+1)*16 + 32   HMAC-SHA-256(ciphertext) | 
|  | 376 | */ | 
| Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 377 | if( filesize < 16 + md_get_size( md_info ) ) | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 378 | { | 
|  | 379 | fprintf( stderr, "File too short to be encrypted.\n" ); | 
|  | 380 | goto exit; | 
|  | 381 | } | 
|  | 382 |  | 
| Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 383 | if( ( ( filesize - md_get_size( md_info ) ) % | 
|  | 384 | cipher_get_block_size( &cipher_ctx ) ) != 0 ) | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 385 | { | 
| Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 386 | fprintf( stderr, "File content not a multiple of the block size (%d).\n", | 
|  | 387 | cipher_get_block_size( &cipher_ctx )); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 388 | goto exit; | 
|  | 389 | } | 
|  | 390 |  | 
|  | 391 | /* | 
|  | 392 | * Substract the IV + HMAC length. | 
|  | 393 | */ | 
|  | 394 | filesize -= ( 16 + md_get_size( md_info ) ); | 
|  | 395 |  | 
|  | 396 | /* | 
|  | 397 | * Read the IV and original filesize modulo 16. | 
|  | 398 | */ | 
|  | 399 | if( fread( buffer, 1, 16, fin ) != 16 ) | 
|  | 400 | { | 
|  | 401 | fprintf( stderr, "fread(%d bytes) failed\n", 16 ); | 
|  | 402 | goto exit; | 
|  | 403 | } | 
|  | 404 |  | 
|  | 405 | memcpy( IV, buffer, 16 ); | 
|  | 406 | lastn = IV[15] & 0x0F; | 
|  | 407 |  | 
|  | 408 | /* | 
|  | 409 | * Hash the IV and the secret key together 8192 times | 
|  | 410 | * using the result to setup the AES context and HMAC. | 
|  | 411 | */ | 
|  | 412 | memset( digest, 0,  32 ); | 
|  | 413 | memcpy( digest, IV, 16 ); | 
|  | 414 |  | 
|  | 415 | for( i = 0; i < 8192; i++ ) | 
|  | 416 | { | 
|  | 417 | md_starts( &md_ctx ); | 
|  | 418 | md_update( &md_ctx, digest, 32 ); | 
|  | 419 | md_update( &md_ctx, key, keylen ); | 
|  | 420 | md_finish( &md_ctx, digest ); | 
|  | 421 | } | 
|  | 422 |  | 
|  | 423 | memset( key, 0, sizeof( key ) ); | 
|  | 424 |  | 
|  | 425 | cipher_setkey( &cipher_ctx, digest, cipher_info->key_length, | 
|  | 426 | POLARSSL_DECRYPT ); | 
|  | 427 | cipher_reset( &cipher_ctx, IV); | 
|  | 428 |  | 
|  | 429 | md_hmac_starts( &md_ctx, digest, 32 ); | 
|  | 430 |  | 
|  | 431 | /* | 
|  | 432 | * Decrypt and write the plaintext. | 
|  | 433 | */ | 
|  | 434 | for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) ) | 
|  | 435 | { | 
|  | 436 | if( fread( buffer, 1, cipher_get_block_size( &cipher_ctx ), fin ) != | 
|  | 437 | (size_t) cipher_get_block_size( &cipher_ctx ) ) | 
|  | 438 | { | 
|  | 439 | fprintf( stderr, "fread(%d bytes) failed\n", | 
|  | 440 | cipher_get_block_size( &cipher_ctx ) ); | 
|  | 441 | goto exit; | 
|  | 442 | } | 
|  | 443 |  | 
|  | 444 | md_hmac_update( &md_ctx, buffer, cipher_get_block_size( &cipher_ctx ) ); | 
|  | 445 | cipher_update( &cipher_ctx, buffer, cipher_get_block_size( &cipher_ctx ), | 
|  | 446 | output, &olen ); | 
|  | 447 |  | 
| Paul Bakker | 2c0994e | 2011-05-25 13:51:57 +0000 | [diff] [blame] | 448 | if( fwrite( output, 1, olen, fout ) != olen ) | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 449 | { | 
| Paul Bakker | 2c0994e | 2011-05-25 13:51:57 +0000 | [diff] [blame] | 450 | fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen ); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 451 | goto exit; | 
|  | 452 | } | 
|  | 453 | } | 
|  | 454 |  | 
|  | 455 | /* | 
|  | 456 | * Write the final block of data | 
|  | 457 | */ | 
|  | 458 | cipher_finish( &cipher_ctx, output, &olen ); | 
|  | 459 |  | 
| Paul Bakker | 2c0994e | 2011-05-25 13:51:57 +0000 | [diff] [blame] | 460 | if( fwrite( output, 1, olen, fout ) != olen ) | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 461 | { | 
| Paul Bakker | 2c0994e | 2011-05-25 13:51:57 +0000 | [diff] [blame] | 462 | fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen ); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 463 | goto exit; | 
|  | 464 | } | 
|  | 465 |  | 
|  | 466 | /* | 
|  | 467 | * Verify the message authentication code. | 
|  | 468 | */ | 
|  | 469 | md_hmac_finish( &md_ctx, digest ); | 
|  | 470 |  | 
|  | 471 | if( fread( buffer, 1, md_get_size( md_info ), fin ) != md_get_size( md_info ) ) | 
|  | 472 | { | 
|  | 473 | fprintf( stderr, "fread(%d bytes) failed\n", md_get_size( md_info ) ); | 
|  | 474 | goto exit; | 
|  | 475 | } | 
|  | 476 |  | 
|  | 477 | if( memcmp( digest, buffer, md_get_size( md_info ) ) != 0 ) | 
|  | 478 | { | 
|  | 479 | fprintf( stderr, "HMAC check failed: wrong key, " | 
|  | 480 | "or file corrupted.\n" ); | 
|  | 481 | goto exit; | 
|  | 482 | } | 
|  | 483 | } | 
|  | 484 |  | 
|  | 485 | ret = 0; | 
|  | 486 |  | 
|  | 487 | exit: | 
| Paul Bakker | 6d44032 | 2011-02-06 12:49:19 +0000 | [diff] [blame] | 488 | if( fin ) | 
|  | 489 | fclose( fin ); | 
|  | 490 | if( fout ) | 
|  | 491 | fclose( fout ); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 492 |  | 
|  | 493 | memset( buffer, 0, sizeof( buffer ) ); | 
|  | 494 | memset( digest, 0, sizeof( digest ) ); | 
|  | 495 |  | 
|  | 496 | cipher_free_ctx( &cipher_ctx ); | 
|  | 497 | md_free_ctx( &md_ctx ); | 
|  | 498 |  | 
|  | 499 | return( ret ); | 
|  | 500 | } | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 501 | #endif /* POLARSSL_CIPHER_C && POLARSSL_MD_C */ |