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