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