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