Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * AES-256 file encryption program |
| 3 | * |
Paul Bakker | fc8c436 | 2010-03-21 17:37:16 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 5 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 6 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 23 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 24 | #endif |
| 25 | |
| 26 | #if defined(WIN32) |
| 27 | #include <windows.h> |
| 28 | #include <io.h> |
| 29 | #else |
| 30 | #include <sys/types.h> |
| 31 | #include <unistd.h> |
| 32 | #endif |
| 33 | |
| 34 | #include <string.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <stdio.h> |
| 37 | #include <time.h> |
| 38 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 39 | #include "polarssl/aes.h" |
| 40 | #include "polarssl/sha2.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 41 | |
| 42 | #define MODE_ENCRYPT 0 |
| 43 | #define MODE_DECRYPT 1 |
| 44 | |
| 45 | #define USAGE \ |
| 46 | "\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \ |
| 47 | "\n <mode>: 0 = encrypt, 1 = decrypt\n" \ |
| 48 | "\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \ |
| 49 | "\n" |
| 50 | |
| 51 | int main( int argc, char *argv[] ) |
| 52 | { |
| 53 | int ret = 1, i, n; |
| 54 | int keylen, mode, lastn; |
| 55 | FILE *fkey, *fin, *fout; |
| 56 | |
| 57 | char *p; |
| 58 | unsigned char IV[16]; |
| 59 | unsigned char key[512]; |
| 60 | unsigned char digest[32]; |
| 61 | unsigned char buffer[1024]; |
| 62 | |
| 63 | aes_context aes_ctx; |
| 64 | sha2_context sha_ctx; |
| 65 | |
| 66 | #if defined(WIN32) |
| 67 | LARGE_INTEGER li_size; |
| 68 | __int64 filesize, offset; |
| 69 | #else |
| 70 | off_t filesize, offset; |
| 71 | #endif |
| 72 | |
| 73 | /* |
| 74 | * Parse the command-line arguments. |
| 75 | */ |
| 76 | if( argc != 5 ) |
| 77 | { |
| 78 | printf( USAGE ); |
| 79 | |
| 80 | #if defined(WIN32) |
| 81 | printf( "\n Press Enter to exit this program.\n" ); |
| 82 | fflush( stdout ); getchar(); |
| 83 | #endif |
| 84 | |
| 85 | goto exit; |
| 86 | } |
| 87 | |
| 88 | mode = atoi( argv[1] ); |
| 89 | |
| 90 | if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT ) |
| 91 | { |
| 92 | fprintf( stderr, "invalide operation mode\n" ); |
| 93 | goto exit; |
| 94 | } |
| 95 | |
| 96 | if( strcmp( argv[2], argv[3] ) == 0 ) |
| 97 | { |
| 98 | fprintf( stderr, "input and output filenames must differ\n" ); |
| 99 | goto exit; |
| 100 | } |
| 101 | |
| 102 | if( ( fin = fopen( argv[2], "rb" ) ) == NULL ) |
| 103 | { |
| 104 | fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] ); |
| 105 | goto exit; |
| 106 | } |
| 107 | |
| 108 | if( ( fout = fopen( argv[3], "wb+" ) ) == NULL ) |
| 109 | { |
| 110 | fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] ); |
| 111 | goto exit; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Read the secret key and clean the command line. |
| 116 | */ |
| 117 | if( ( fkey = fopen( argv[4], "rb" ) ) != NULL ) |
| 118 | { |
| 119 | keylen = fread( key, 1, sizeof( key ), fkey ); |
| 120 | fclose( fkey ); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | if( memcmp( argv[4], "hex:", 4 ) == 0 ) |
| 125 | { |
| 126 | p = &argv[4][4]; |
| 127 | keylen = 0; |
| 128 | |
| 129 | while( sscanf( p, "%02X", &n ) > 0 && |
| 130 | keylen < (int) sizeof( key ) ) |
| 131 | { |
| 132 | key[keylen++] = (unsigned char) n; |
| 133 | p += 2; |
| 134 | } |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | keylen = strlen( argv[4] ); |
| 139 | |
| 140 | if( keylen > (int) sizeof( key ) ) |
| 141 | keylen = (int) sizeof( key ); |
| 142 | |
| 143 | memcpy( key, argv[4], keylen ); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | memset( argv[4], 0, strlen( argv[4] ) ); |
| 148 | |
| 149 | #if defined(WIN32) |
| 150 | /* |
| 151 | * Support large files (> 2Gb) on Win32 |
| 152 | */ |
| 153 | li_size.QuadPart = 0; |
| 154 | li_size.LowPart = |
| 155 | SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ), |
| 156 | li_size.LowPart, &li_size.HighPart, FILE_END ); |
| 157 | |
| 158 | if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR ) |
| 159 | { |
| 160 | fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" ); |
| 161 | goto exit; |
| 162 | } |
| 163 | |
| 164 | filesize = li_size.QuadPart; |
| 165 | #else |
| 166 | if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 ) |
| 167 | { |
| 168 | perror( "lseek" ); |
| 169 | goto exit; |
| 170 | } |
| 171 | #endif |
| 172 | |
| 173 | if( fseek( fin, 0, SEEK_SET ) < 0 ) |
| 174 | { |
| 175 | fprintf( stderr, "fseek(0,SEEK_SET) failed\n" ); |
| 176 | goto exit; |
| 177 | } |
| 178 | |
| 179 | if( mode == MODE_ENCRYPT ) |
| 180 | { |
| 181 | /* |
| 182 | * Generate the initialization vector as: |
| 183 | * IV = SHA-256( filesize || filename )[0..15] |
| 184 | */ |
| 185 | for( i = 0; i < 8; i++ ) |
| 186 | buffer[i] = (unsigned char)( filesize >> ( i << 3 ) ); |
| 187 | |
| 188 | p = argv[2]; |
| 189 | |
| 190 | sha2_starts( &sha_ctx, 0 ); |
| 191 | sha2_update( &sha_ctx, buffer, 8 ); |
| 192 | sha2_update( &sha_ctx, (unsigned char *) p, strlen( p ) ); |
| 193 | sha2_finish( &sha_ctx, digest ); |
| 194 | |
| 195 | memcpy( IV, digest, 16 ); |
| 196 | |
| 197 | /* |
| 198 | * The last four bits in the IV are actually used |
| 199 | * to store the file size modulo the AES block size. |
| 200 | */ |
| 201 | lastn = (int)( filesize & 0x0F ); |
| 202 | |
| 203 | IV[15] = (unsigned char) |
| 204 | ( ( IV[15] & 0xF0 ) | lastn ); |
| 205 | |
| 206 | /* |
| 207 | * Append the IV at the beginning of the output. |
| 208 | */ |
| 209 | if( fwrite( IV, 1, 16, fout ) != 16 ) |
| 210 | { |
| 211 | fprintf( stderr, "fwrite(%d bytes) failed\n", 16 ); |
| 212 | goto exit; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Hash the IV and the secret key together 8192 times |
| 217 | * using the result to setup the AES context and HMAC. |
| 218 | */ |
| 219 | memset( digest, 0, 32 ); |
| 220 | memcpy( digest, IV, 16 ); |
| 221 | |
| 222 | for( i = 0; i < 8192; i++ ) |
| 223 | { |
| 224 | sha2_starts( &sha_ctx, 0 ); |
| 225 | sha2_update( &sha_ctx, digest, 32 ); |
| 226 | sha2_update( &sha_ctx, key, keylen ); |
| 227 | sha2_finish( &sha_ctx, digest ); |
| 228 | } |
| 229 | |
| 230 | memset( key, 0, sizeof( key ) ); |
| 231 | aes_setkey_enc( &aes_ctx, digest, 256 ); |
| 232 | sha2_hmac_starts( &sha_ctx, digest, 32, 0 ); |
| 233 | |
| 234 | /* |
| 235 | * Encrypt and write the ciphertext. |
| 236 | */ |
| 237 | for( offset = 0; offset < filesize; offset += 16 ) |
| 238 | { |
| 239 | n = ( filesize - offset > 16 ) ? 16 : (int) |
| 240 | ( filesize - offset ); |
| 241 | |
| 242 | if( fread( buffer, 1, n, fin ) != (size_t) n ) |
| 243 | { |
| 244 | fprintf( stderr, "fread(%d bytes) failed\n", n ); |
| 245 | goto exit; |
| 246 | } |
| 247 | |
| 248 | for( i = 0; i < 16; i++ ) |
| 249 | buffer[i] = (unsigned char)( buffer[i] ^ IV[i] ); |
| 250 | |
| 251 | aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer ); |
| 252 | sha2_hmac_update( &sha_ctx, buffer, 16 ); |
| 253 | |
| 254 | if( fwrite( buffer, 1, 16, fout ) != 16 ) |
| 255 | { |
| 256 | fprintf( stderr, "fwrite(%d bytes) failed\n", 16 ); |
| 257 | goto exit; |
| 258 | } |
| 259 | |
| 260 | memcpy( IV, buffer, 16 ); |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Finally write the HMAC. |
| 265 | */ |
| 266 | sha2_hmac_finish( &sha_ctx, digest ); |
| 267 | |
| 268 | if( fwrite( digest, 1, 32, fout ) != 32 ) |
| 269 | { |
| 270 | fprintf( stderr, "fwrite(%d bytes) failed\n", 16 ); |
| 271 | goto exit; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if( mode == MODE_DECRYPT ) |
| 276 | { |
| 277 | unsigned char tmp[16]; |
| 278 | |
| 279 | /* |
| 280 | * The encrypted file must be structured as follows: |
| 281 | * |
| 282 | * 00 .. 15 Initialization Vector |
| 283 | * 16 .. 31 AES Encrypted Block #1 |
| 284 | * .. |
| 285 | * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N |
| 286 | * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext) |
| 287 | */ |
| 288 | if( filesize < 48 ) |
| 289 | { |
| 290 | fprintf( stderr, "File too short to be encrypted.\n" ); |
| 291 | goto exit; |
| 292 | } |
| 293 | |
| 294 | if( ( filesize & 0x0F ) != 0 ) |
| 295 | { |
| 296 | fprintf( stderr, "File size not a multiple of 16.\n" ); |
| 297 | goto exit; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Substract the IV + HMAC length. |
| 302 | */ |
| 303 | filesize -= ( 16 + 32 ); |
| 304 | |
| 305 | /* |
| 306 | * Read the IV and original filesize modulo 16. |
| 307 | */ |
| 308 | if( fread( buffer, 1, 16, fin ) != 16 ) |
| 309 | { |
| 310 | fprintf( stderr, "fread(%d bytes) failed\n", 16 ); |
| 311 | goto exit; |
| 312 | } |
| 313 | |
| 314 | memcpy( IV, buffer, 16 ); |
| 315 | lastn = IV[15] & 0x0F; |
| 316 | |
| 317 | /* |
| 318 | * Hash the IV and the secret key together 8192 times |
| 319 | * using the result to setup the AES context and HMAC. |
| 320 | */ |
| 321 | memset( digest, 0, 32 ); |
| 322 | memcpy( digest, IV, 16 ); |
| 323 | |
| 324 | for( i = 0; i < 8192; i++ ) |
| 325 | { |
| 326 | sha2_starts( &sha_ctx, 0 ); |
| 327 | sha2_update( &sha_ctx, digest, 32 ); |
| 328 | sha2_update( &sha_ctx, key, keylen ); |
| 329 | sha2_finish( &sha_ctx, digest ); |
| 330 | } |
| 331 | |
| 332 | memset( key, 0, sizeof( key ) ); |
| 333 | aes_setkey_dec( &aes_ctx, digest, 256 ); |
| 334 | sha2_hmac_starts( &sha_ctx, digest, 32, 0 ); |
| 335 | |
| 336 | /* |
| 337 | * Decrypt and write the plaintext. |
| 338 | */ |
| 339 | for( offset = 0; offset < filesize; offset += 16 ) |
| 340 | { |
| 341 | if( fread( buffer, 1, 16, fin ) != 16 ) |
| 342 | { |
| 343 | fprintf( stderr, "fread(%d bytes) failed\n", 16 ); |
| 344 | goto exit; |
| 345 | } |
| 346 | |
| 347 | memcpy( tmp, buffer, 16 ); |
| 348 | |
| 349 | sha2_hmac_update( &sha_ctx, buffer, 16 ); |
| 350 | aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer ); |
| 351 | |
| 352 | for( i = 0; i < 16; i++ ) |
| 353 | buffer[i] = (unsigned char)( buffer[i] ^ IV[i] ); |
| 354 | |
| 355 | memcpy( IV, tmp, 16 ); |
| 356 | |
| 357 | n = ( lastn > 0 && offset == filesize - 16 ) |
| 358 | ? lastn : 16; |
| 359 | |
| 360 | if( fwrite( buffer, 1, n, fout ) != (size_t) n ) |
| 361 | { |
| 362 | fprintf( stderr, "fwrite(%d bytes) failed\n", n ); |
| 363 | goto exit; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * Verify the message authentication code. |
| 369 | */ |
| 370 | sha2_hmac_finish( &sha_ctx, digest ); |
| 371 | |
| 372 | if( fread( buffer, 1, 32, fin ) != 32 ) |
| 373 | { |
| 374 | fprintf( stderr, "fread(%d bytes) failed\n", 32 ); |
| 375 | goto exit; |
| 376 | } |
| 377 | |
| 378 | if( memcmp( digest, buffer, 32 ) != 0 ) |
| 379 | { |
| 380 | fprintf( stderr, "HMAC check failed: wrong key, " |
| 381 | "or file corrupted.\n" ); |
| 382 | goto exit; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | ret = 0; |
| 387 | |
| 388 | exit: |
| 389 | |
| 390 | memset( buffer, 0, sizeof( buffer ) ); |
| 391 | memset( digest, 0, sizeof( digest ) ); |
| 392 | |
| 393 | memset( &aes_ctx, 0, sizeof( aes_context ) ); |
| 394 | memset( &sha_ctx, 0, sizeof( sha2_context ) ); |
| 395 | |
| 396 | return( ret ); |
| 397 | } |