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 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 27 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 28 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #else |
| 30 | #include POLARSSL_CONFIG_FILE |
| 31 | #endif |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 32 | |
Paul Bakker | 494c0b8 | 2011-04-24 15:30:07 +0000 | [diff] [blame] | 33 | #if defined(_WIN32) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 34 | #include <windows.h> |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 35 | #if !defined(_WIN32_WCE) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 36 | #include <io.h> |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 37 | #endif |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 38 | #else |
| 39 | #include <sys/types.h> |
| 40 | #include <unistd.h> |
| 41 | #endif |
| 42 | |
| 43 | #include <string.h> |
| 44 | #include <stdlib.h> |
| 45 | #include <stdio.h> |
| 46 | #include <time.h> |
| 47 | |
| 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]; |
Paul Bakker | 424cd69 | 2013-10-31 14:22:08 +0100 | [diff] [blame] | 83 | unsigned char diff; |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 84 | |
| 85 | const cipher_info_t *cipher_info; |
| 86 | const md_info_t *md_info; |
| 87 | cipher_context_t cipher_ctx; |
| 88 | md_context_t md_ctx; |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 89 | #if defined(_WIN32_WCE) |
| 90 | long filesize, offset; |
| 91 | #elif defined(_WIN32) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 92 | LARGE_INTEGER li_size; |
| 93 | __int64 filesize, offset; |
| 94 | #else |
| 95 | off_t filesize, offset; |
| 96 | #endif |
| 97 | |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame^] | 98 | cipher_init( &cipher_ctx ); |
| 99 | md_init( &md_ctx ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 100 | |
| 101 | /* |
| 102 | * Parse the command-line arguments. |
| 103 | */ |
| 104 | if( argc != 7 ) |
| 105 | { |
| 106 | const int *list; |
| 107 | |
| 108 | printf( USAGE ); |
| 109 | |
| 110 | printf( "Available ciphers:\n" ); |
| 111 | list = cipher_list(); |
| 112 | while( *list ) |
| 113 | { |
| 114 | cipher_info = cipher_info_from_type( *list ); |
| 115 | printf( " %s\n", cipher_info->name ); |
| 116 | list++; |
| 117 | } |
| 118 | |
| 119 | printf( "\nAvailable message digests:\n" ); |
| 120 | list = md_list(); |
| 121 | while( *list ) |
| 122 | { |
| 123 | md_info = md_info_from_type( *list ); |
| 124 | printf( " %s\n", md_info->name ); |
| 125 | list++; |
| 126 | } |
| 127 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 128 | #if defined(_WIN32) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 129 | printf( "\n Press Enter to exit this program.\n" ); |
| 130 | fflush( stdout ); getchar(); |
| 131 | #endif |
| 132 | |
| 133 | goto exit; |
| 134 | } |
| 135 | |
| 136 | mode = atoi( argv[1] ); |
| 137 | |
| 138 | if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT ) |
| 139 | { |
| 140 | fprintf( stderr, "invalid operation mode\n" ); |
| 141 | goto exit; |
| 142 | } |
| 143 | |
| 144 | if( strcmp( argv[2], argv[3] ) == 0 ) |
| 145 | { |
| 146 | fprintf( stderr, "input and output filenames must differ\n" ); |
| 147 | goto exit; |
| 148 | } |
| 149 | |
| 150 | if( ( fin = fopen( argv[2], "rb" ) ) == NULL ) |
| 151 | { |
| 152 | fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] ); |
| 153 | goto exit; |
| 154 | } |
| 155 | |
| 156 | if( ( fout = fopen( argv[3], "wb+" ) ) == NULL ) |
| 157 | { |
| 158 | fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] ); |
| 159 | goto exit; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Read the Cipher and MD from the command line |
| 164 | */ |
| 165 | cipher_info = cipher_info_from_string( argv[4] ); |
| 166 | if( cipher_info == NULL ) |
| 167 | { |
| 168 | fprintf( stderr, "Cipher '%s' not found\n", argv[4] ); |
| 169 | goto exit; |
| 170 | } |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 171 | if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info) ) != 0 ) |
| 172 | { |
| 173 | fprintf( stderr, "cipher_init_ctx failed\n" ); |
| 174 | goto exit; |
| 175 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 176 | |
| 177 | md_info = md_info_from_string( argv[5] ); |
| 178 | if( md_info == NULL ) |
| 179 | { |
| 180 | fprintf( stderr, "Message Digest '%s' not found\n", argv[5] ); |
| 181 | goto exit; |
| 182 | } |
| 183 | md_init_ctx( &md_ctx, md_info); |
| 184 | |
| 185 | /* |
| 186 | * Read the secret key and clean the command line. |
| 187 | */ |
| 188 | if( ( fkey = fopen( argv[6], "rb" ) ) != NULL ) |
| 189 | { |
| 190 | keylen = fread( key, 1, sizeof( key ), fkey ); |
| 191 | fclose( fkey ); |
| 192 | } |
| 193 | else |
| 194 | { |
| 195 | if( memcmp( argv[6], "hex:", 4 ) == 0 ) |
| 196 | { |
| 197 | p = &argv[6][4]; |
| 198 | keylen = 0; |
| 199 | |
| 200 | while( sscanf( p, "%02X", &n ) > 0 && |
| 201 | keylen < (int) sizeof( key ) ) |
| 202 | { |
| 203 | key[keylen++] = (unsigned char) n; |
| 204 | p += 2; |
| 205 | } |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | keylen = strlen( argv[6] ); |
| 210 | |
| 211 | if( keylen > (int) sizeof( key ) ) |
| 212 | keylen = (int) sizeof( key ); |
| 213 | |
| 214 | memcpy( key, argv[6], keylen ); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | memset( argv[6], 0, strlen( argv[6] ) ); |
| 219 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 220 | #if defined(_WIN32_WCE) |
| 221 | filesize = fseek( fin, 0L, SEEK_END ); |
| 222 | #else |
| 223 | #if defined(_WIN32) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 224 | /* |
| 225 | * Support large files (> 2Gb) on Win32 |
| 226 | */ |
| 227 | li_size.QuadPart = 0; |
| 228 | li_size.LowPart = |
| 229 | SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ), |
| 230 | li_size.LowPart, &li_size.HighPart, FILE_END ); |
| 231 | |
| 232 | if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR ) |
| 233 | { |
| 234 | fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" ); |
| 235 | goto exit; |
| 236 | } |
| 237 | |
| 238 | filesize = li_size.QuadPart; |
| 239 | #else |
| 240 | if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 ) |
| 241 | { |
| 242 | perror( "lseek" ); |
| 243 | goto exit; |
| 244 | } |
| 245 | #endif |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 246 | #endif |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 247 | |
| 248 | if( fseek( fin, 0, SEEK_SET ) < 0 ) |
| 249 | { |
| 250 | fprintf( stderr, "fseek(0,SEEK_SET) failed\n" ); |
| 251 | goto exit; |
| 252 | } |
| 253 | |
| 254 | if( mode == MODE_ENCRYPT ) |
| 255 | { |
| 256 | /* |
| 257 | * Generate the initialization vector as: |
| 258 | * IV = SHA-256( filesize || filename )[0..15] |
| 259 | */ |
| 260 | for( i = 0; i < 8; i++ ) |
| 261 | buffer[i] = (unsigned char)( filesize >> ( i << 3 ) ); |
| 262 | |
| 263 | p = argv[2]; |
| 264 | |
| 265 | md_starts( &md_ctx ); |
| 266 | md_update( &md_ctx, buffer, 8 ); |
| 267 | md_update( &md_ctx, (unsigned char *) p, strlen( p ) ); |
| 268 | md_finish( &md_ctx, digest ); |
| 269 | |
| 270 | memcpy( IV, digest, 16 ); |
| 271 | |
| 272 | /* |
| 273 | * The last four bits in the IV are actually used |
| 274 | * to store the file size modulo the AES block size. |
| 275 | */ |
| 276 | lastn = (int)( filesize & 0x0F ); |
| 277 | |
| 278 | IV[15] = (unsigned char) |
| 279 | ( ( IV[15] & 0xF0 ) | lastn ); |
| 280 | |
| 281 | /* |
| 282 | * Append the IV at the beginning of the output. |
| 283 | */ |
| 284 | if( fwrite( IV, 1, 16, fout ) != 16 ) |
| 285 | { |
| 286 | fprintf( stderr, "fwrite(%d bytes) failed\n", 16 ); |
| 287 | goto exit; |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Hash the IV and the secret key together 8192 times |
| 292 | * using the result to setup the AES context and HMAC. |
| 293 | */ |
| 294 | memset( digest, 0, 32 ); |
| 295 | memcpy( digest, IV, 16 ); |
| 296 | |
| 297 | for( i = 0; i < 8192; i++ ) |
| 298 | { |
| 299 | md_starts( &md_ctx ); |
| 300 | md_update( &md_ctx, digest, 32 ); |
| 301 | md_update( &md_ctx, key, keylen ); |
| 302 | md_finish( &md_ctx, digest ); |
| 303 | |
| 304 | } |
| 305 | |
| 306 | memset( key, 0, sizeof( key ) ); |
| 307 | |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 308 | if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length, |
| 309 | POLARSSL_ENCRYPT ) != 0 ) |
| 310 | { |
| 311 | fprintf( stderr, "cipher_setkey() returned error\n"); |
| 312 | goto exit; |
| 313 | } |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 314 | if( cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 ) |
| 315 | { |
| 316 | fprintf( stderr, "cipher_set_iv() returned error\n"); |
| 317 | goto exit; |
| 318 | } |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 319 | if( cipher_reset( &cipher_ctx ) != 0 ) |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 320 | { |
| 321 | fprintf( stderr, "cipher_reset() returned error\n"); |
| 322 | goto exit; |
| 323 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 324 | |
| 325 | md_hmac_starts( &md_ctx, digest, 32 ); |
| 326 | |
| 327 | /* |
| 328 | * Encrypt and write the ciphertext. |
| 329 | */ |
| 330 | for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) ) |
| 331 | { |
Paul Bakker | 25b5fe5 | 2011-05-26 14:02:58 +0000 | [diff] [blame] | 332 | ilen = ( (unsigned int) filesize - offset > cipher_get_block_size( &cipher_ctx ) ) ? |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 333 | cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 334 | |
Paul Bakker | 25b5fe5 | 2011-05-26 14:02:58 +0000 | [diff] [blame] | 335 | if( fread( buffer, 1, ilen, fin ) != ilen ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 336 | { |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 337 | fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 338 | goto exit; |
| 339 | } |
| 340 | |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 341 | if( cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 ) |
| 342 | { |
| 343 | fprintf( stderr, "cipher_update() returned error\n"); |
| 344 | goto exit; |
| 345 | } |
| 346 | |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 347 | md_hmac_update( &md_ctx, output, olen ); |
| 348 | |
Paul Bakker | 2c0994e | 2011-05-25 13:51:57 +0000 | [diff] [blame] | 349 | if( fwrite( output, 1, olen, fout ) != olen ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 350 | { |
Paul Bakker | 2c0994e | 2011-05-25 13:51:57 +0000 | [diff] [blame] | 351 | fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 352 | goto exit; |
| 353 | } |
| 354 | } |
| 355 | |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 356 | if( cipher_finish( &cipher_ctx, output, &olen ) != 0 ) |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 357 | { |
| 358 | fprintf( stderr, "cipher_finish() returned error\n" ); |
| 359 | goto exit; |
| 360 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 361 | md_hmac_update( &md_ctx, output, olen ); |
| 362 | |
Paul Bakker | 2c0994e | 2011-05-25 13:51:57 +0000 | [diff] [blame] | 363 | if( fwrite( output, 1, olen, fout ) != olen ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 364 | { |
Paul Bakker | 25b5fe5 | 2011-05-26 14:02:58 +0000 | [diff] [blame] | 365 | fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 366 | goto exit; |
| 367 | } |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 368 | |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 369 | /* |
| 370 | * Finally write the HMAC. |
| 371 | */ |
| 372 | md_hmac_finish( &md_ctx, digest ); |
| 373 | |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 374 | 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] | 375 | { |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 376 | fprintf( stderr, "fwrite(%d bytes) failed\n", md_get_size( md_info ) ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 377 | goto exit; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | if( mode == MODE_DECRYPT ) |
| 382 | { |
| 383 | /* |
| 384 | * The encrypted file must be structured as follows: |
| 385 | * |
| 386 | * 00 .. 15 Initialization Vector |
| 387 | * 16 .. 31 AES Encrypted Block #1 |
| 388 | * .. |
| 389 | * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N |
| 390 | * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext) |
| 391 | */ |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 392 | if( filesize < 16 + md_get_size( md_info ) ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 393 | { |
| 394 | fprintf( stderr, "File too short to be encrypted.\n" ); |
| 395 | goto exit; |
| 396 | } |
| 397 | |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 398 | if( ( ( filesize - md_get_size( md_info ) ) % |
| 399 | cipher_get_block_size( &cipher_ctx ) ) != 0 ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 400 | { |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 401 | fprintf( stderr, "File content not a multiple of the block size (%d).\n", |
| 402 | cipher_get_block_size( &cipher_ctx )); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 403 | goto exit; |
| 404 | } |
| 405 | |
| 406 | /* |
Paul Bakker | 60b1d10 | 2013-10-29 10:02:51 +0100 | [diff] [blame] | 407 | * Subtract the IV + HMAC length. |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 408 | */ |
| 409 | filesize -= ( 16 + md_get_size( md_info ) ); |
| 410 | |
| 411 | /* |
| 412 | * Read the IV and original filesize modulo 16. |
| 413 | */ |
| 414 | if( fread( buffer, 1, 16, fin ) != 16 ) |
| 415 | { |
| 416 | fprintf( stderr, "fread(%d bytes) failed\n", 16 ); |
| 417 | goto exit; |
| 418 | } |
| 419 | |
| 420 | memcpy( IV, buffer, 16 ); |
| 421 | lastn = IV[15] & 0x0F; |
| 422 | |
| 423 | /* |
| 424 | * Hash the IV and the secret key together 8192 times |
| 425 | * using the result to setup the AES context and HMAC. |
| 426 | */ |
| 427 | memset( digest, 0, 32 ); |
| 428 | memcpy( digest, IV, 16 ); |
| 429 | |
| 430 | for( i = 0; i < 8192; i++ ) |
| 431 | { |
| 432 | md_starts( &md_ctx ); |
| 433 | md_update( &md_ctx, digest, 32 ); |
| 434 | md_update( &md_ctx, key, keylen ); |
| 435 | md_finish( &md_ctx, digest ); |
| 436 | } |
| 437 | |
| 438 | memset( key, 0, sizeof( key ) ); |
| 439 | |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 440 | if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length, |
| 441 | POLARSSL_DECRYPT ) != 0 ) |
| 442 | { |
| 443 | fprintf( stderr, "cipher_setkey() returned error\n" ); |
| 444 | goto exit; |
| 445 | } |
| 446 | |
| 447 | if( cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 ) |
| 448 | { |
| 449 | fprintf( stderr, "cipher_set_iv() returned error\n" ); |
| 450 | goto exit; |
| 451 | } |
| 452 | |
| 453 | if( cipher_reset( &cipher_ctx ) != 0 ) |
| 454 | { |
| 455 | fprintf( stderr, "cipher_reset() returned error\n" ); |
| 456 | goto exit; |
| 457 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 458 | |
| 459 | md_hmac_starts( &md_ctx, digest, 32 ); |
| 460 | |
| 461 | /* |
| 462 | * Decrypt and write the plaintext. |
| 463 | */ |
| 464 | for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) ) |
| 465 | { |
| 466 | if( fread( buffer, 1, cipher_get_block_size( &cipher_ctx ), fin ) != |
| 467 | (size_t) cipher_get_block_size( &cipher_ctx ) ) |
| 468 | { |
| 469 | fprintf( stderr, "fread(%d bytes) failed\n", |
| 470 | cipher_get_block_size( &cipher_ctx ) ); |
| 471 | goto exit; |
| 472 | } |
| 473 | |
| 474 | md_hmac_update( &md_ctx, buffer, cipher_get_block_size( &cipher_ctx ) ); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 475 | if( cipher_update( &cipher_ctx, buffer, |
| 476 | cipher_get_block_size( &cipher_ctx ), |
| 477 | output, &olen ) != 0 ) |
| 478 | { |
| 479 | fprintf( stderr, "cipher_update() returned error\n" ); |
| 480 | goto exit; |
| 481 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 482 | |
Paul Bakker | 2c0994e | 2011-05-25 13:51:57 +0000 | [diff] [blame] | 483 | if( fwrite( output, 1, olen, fout ) != olen ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 484 | { |
Paul Bakker | 2c0994e | 2011-05-25 13:51:57 +0000 | [diff] [blame] | 485 | fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 486 | goto exit; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | /* |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 491 | * Verify the message authentication code. |
| 492 | */ |
| 493 | md_hmac_finish( &md_ctx, digest ); |
| 494 | |
| 495 | if( fread( buffer, 1, md_get_size( md_info ), fin ) != md_get_size( md_info ) ) |
| 496 | { |
| 497 | fprintf( stderr, "fread(%d bytes) failed\n", md_get_size( md_info ) ); |
| 498 | goto exit; |
| 499 | } |
| 500 | |
Paul Bakker | 424cd69 | 2013-10-31 14:22:08 +0100 | [diff] [blame] | 501 | /* Use constant-time buffer comparison */ |
| 502 | diff = 0; |
| 503 | for( i = 0; i < md_get_size( md_info ); i++ ) |
| 504 | diff |= digest[i] ^ buffer[i]; |
| 505 | |
| 506 | if( diff != 0 ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 507 | { |
| 508 | fprintf( stderr, "HMAC check failed: wrong key, " |
| 509 | "or file corrupted.\n" ); |
| 510 | goto exit; |
| 511 | } |
Manuel Pégourié-Gonnard | 0f2eacb | 2013-11-25 17:55:17 +0100 | [diff] [blame] | 512 | |
| 513 | /* |
| 514 | * Write the final block of data |
| 515 | */ |
| 516 | cipher_finish( &cipher_ctx, output, &olen ); |
| 517 | |
| 518 | if( fwrite( output, 1, olen, fout ) != olen ) |
| 519 | { |
| 520 | fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen ); |
| 521 | goto exit; |
| 522 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | ret = 0; |
| 526 | |
| 527 | exit: |
Paul Bakker | 6d44032 | 2011-02-06 12:49:19 +0000 | [diff] [blame] | 528 | if( fin ) |
| 529 | fclose( fin ); |
| 530 | if( fout ) |
| 531 | fclose( fout ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 532 | |
| 533 | memset( buffer, 0, sizeof( buffer ) ); |
| 534 | memset( digest, 0, sizeof( digest ) ); |
| 535 | |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame^] | 536 | cipher_free( &cipher_ctx ); |
| 537 | md_free( &md_ctx ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 538 | |
| 539 | return( ret ); |
| 540 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 541 | #endif /* POLARSSL_CIPHER_C && POLARSSL_MD_C */ |