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