blob: 55f92518089bc9e130cb6bf2483458026587b4bd [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * AES-256 file encryption program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Paul Bakker494c0b82011-04-24 15:30:07 +000036#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000037#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000038#if !defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000039#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000040#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000041#else
42#include <sys/types.h>
43#include <unistd.h>
44#endif
45
46#include <string.h>
47#include <stdlib.h>
48#include <stdio.h>
49#include <time.h>
50
Paul Bakker40e46942009-01-03 21:51:57 +000051#include "polarssl/aes.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020052#include "polarssl/sha256.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000053
54#define MODE_ENCRYPT 0
55#define MODE_DECRYPT 1
56
57#define USAGE \
58 "\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
59 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
60 "\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
61 "\n"
62
Paul Bakker9e36f042013-06-30 14:34:05 +020063#if !defined(POLARSSL_AES_C) || !defined(POLARSSL_SHA256_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000064int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000065{
Paul Bakkercce9d772011-11-18 14:26:47 +000066 ((void) argc);
67 ((void) argv);
Rich Evansf90016a2015-01-19 14:26:37 +000068 polarssl_printf("POLARSSL_AES_C and/or POLARSSL_SHA256_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000069 return( 0 );
70}
71#else
Paul Bakker5121ce52009-01-03 21:22:43 +000072int main( int argc, char *argv[] )
73{
Paul Bakker5690efc2011-05-26 13:16:06 +000074 int ret = 1;
75
76 int i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000077 int mode, lastn;
78 size_t keylen;
Paul Bakker20a78082011-01-21 09:32:12 +000079 FILE *fkey, *fin = NULL, *fout = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81 char *p;
82 unsigned char IV[16];
83 unsigned char key[512];
84 unsigned char digest[32];
85 unsigned char buffer[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010086 unsigned char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +000087
88 aes_context aes_ctx;
Paul Bakker9e36f042013-06-30 14:34:05 +020089 sha256_context sha_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +000090
Paul Bakkercce9d772011-11-18 14:26:47 +000091#if defined(_WIN32_WCE)
92 long filesize, offset;
93#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000094 LARGE_INTEGER li_size;
95 __int64 filesize, offset;
96#else
97 off_t filesize, offset;
98#endif
99
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200100 aes_init( &aes_ctx );
Paul Bakker14e8be42014-06-26 12:25:06 +0200101 sha256_init( &sha_ctx );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200102
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 /*
104 * Parse the command-line arguments.
105 */
106 if( argc != 5 )
107 {
Rich Evansf90016a2015-01-19 14:26:37 +0000108 polarssl_printf( USAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Paul Bakkercce9d772011-11-18 14:26:47 +0000110#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000111 polarssl_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 fflush( stdout ); getchar();
113#endif
114
115 goto exit;
116 }
117
118 mode = atoi( argv[1] );
119
120 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
121 {
Rich Evansf90016a2015-01-19 14:26:37 +0000122 polarssl_fprintf( stderr, "invalide operation mode\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 goto exit;
124 }
125
126 if( strcmp( argv[2], argv[3] ) == 0 )
127 {
Rich Evansf90016a2015-01-19 14:26:37 +0000128 polarssl_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 goto exit;
130 }
131
132 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
133 {
Rich Evansf90016a2015-01-19 14:26:37 +0000134 polarssl_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 goto exit;
136 }
137
138 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
139 {
Rich Evansf90016a2015-01-19 14:26:37 +0000140 polarssl_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 goto exit;
142 }
143
144 /*
145 * Read the secret key and clean the command line.
146 */
147 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
148 {
149 keylen = fread( key, 1, sizeof( key ), fkey );
150 fclose( fkey );
151 }
152 else
153 {
154 if( memcmp( argv[4], "hex:", 4 ) == 0 )
155 {
156 p = &argv[4][4];
157 keylen = 0;
158
159 while( sscanf( p, "%02X", &n ) > 0 &&
160 keylen < (int) sizeof( key ) )
161 {
162 key[keylen++] = (unsigned char) n;
163 p += 2;
164 }
165 }
166 else
167 {
168 keylen = strlen( argv[4] );
169
170 if( keylen > (int) sizeof( key ) )
171 keylen = (int) sizeof( key );
172
173 memcpy( key, argv[4], keylen );
174 }
175 }
176
177 memset( argv[4], 0, strlen( argv[4] ) );
178
Paul Bakkercce9d772011-11-18 14:26:47 +0000179#if defined(_WIN32_WCE)
180 filesize = fseek( fin, 0L, SEEK_END );
181#else
182#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 /*
184 * Support large files (> 2Gb) on Win32
185 */
186 li_size.QuadPart = 0;
187 li_size.LowPart =
188 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
189 li_size.LowPart, &li_size.HighPart, FILE_END );
190
191 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
192 {
Rich Evansf90016a2015-01-19 14:26:37 +0000193 polarssl_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 goto exit;
195 }
196
197 filesize = li_size.QuadPart;
198#else
199 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
200 {
201 perror( "lseek" );
202 goto exit;
203 }
204#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000205#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000206
207 if( fseek( fin, 0, SEEK_SET ) < 0 )
208 {
Rich Evansf90016a2015-01-19 14:26:37 +0000209 polarssl_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 goto exit;
211 }
212
213 if( mode == MODE_ENCRYPT )
214 {
215 /*
216 * Generate the initialization vector as:
217 * IV = SHA-256( filesize || filename )[0..15]
218 */
219 for( i = 0; i < 8; i++ )
220 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
221
222 p = argv[2];
223
Paul Bakker9e36f042013-06-30 14:34:05 +0200224 sha256_starts( &sha_ctx, 0 );
225 sha256_update( &sha_ctx, buffer, 8 );
226 sha256_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
227 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
229 memcpy( IV, digest, 16 );
230
231 /*
232 * The last four bits in the IV are actually used
233 * to store the file size modulo the AES block size.
234 */
235 lastn = (int)( filesize & 0x0F );
236
237 IV[15] = (unsigned char)
238 ( ( IV[15] & 0xF0 ) | lastn );
239
240 /*
241 * Append the IV at the beginning of the output.
242 */
243 if( fwrite( IV, 1, 16, fout ) != 16 )
244 {
Rich Evansf90016a2015-01-19 14:26:37 +0000245 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 goto exit;
247 }
248
249 /*
250 * Hash the IV and the secret key together 8192 times
251 * using the result to setup the AES context and HMAC.
252 */
253 memset( digest, 0, 32 );
254 memcpy( digest, IV, 16 );
255
256 for( i = 0; i < 8192; i++ )
257 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200258 sha256_starts( &sha_ctx, 0 );
259 sha256_update( &sha_ctx, digest, 32 );
260 sha256_update( &sha_ctx, key, keylen );
261 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 }
263
264 memset( key, 0, sizeof( key ) );
Paul Bakker9e36f042013-06-30 14:34:05 +0200265 aes_setkey_enc( &aes_ctx, digest, 256 );
266 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000267
268 /*
269 * Encrypt and write the ciphertext.
270 */
271 for( offset = 0; offset < filesize; offset += 16 )
272 {
273 n = ( filesize - offset > 16 ) ? 16 : (int)
274 ( filesize - offset );
275
276 if( fread( buffer, 1, n, fin ) != (size_t) n )
277 {
Rich Evansf90016a2015-01-19 14:26:37 +0000278 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 goto exit;
280 }
281
282 for( i = 0; i < 16; i++ )
283 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
284
285 aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200286 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
288 if( fwrite( buffer, 1, 16, fout ) != 16 )
289 {
Rich Evansf90016a2015-01-19 14:26:37 +0000290 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 goto exit;
292 }
293
294 memcpy( IV, buffer, 16 );
295 }
296
297 /*
298 * Finally write the HMAC.
299 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200300 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000301
302 if( fwrite( digest, 1, 32, fout ) != 32 )
303 {
Rich Evansf90016a2015-01-19 14:26:37 +0000304 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 goto exit;
306 }
307 }
308
309 if( mode == MODE_DECRYPT )
310 {
311 unsigned char tmp[16];
312
313 /*
314 * The encrypted file must be structured as follows:
315 *
316 * 00 .. 15 Initialization Vector
317 * 16 .. 31 AES Encrypted Block #1
318 * ..
319 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
320 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
321 */
322 if( filesize < 48 )
323 {
Rich Evansf90016a2015-01-19 14:26:37 +0000324 polarssl_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 goto exit;
326 }
327
328 if( ( filesize & 0x0F ) != 0 )
329 {
Rich Evansf90016a2015-01-19 14:26:37 +0000330 polarssl_fprintf( stderr, "File size not a multiple of 16.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 goto exit;
332 }
333
334 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100335 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 */
337 filesize -= ( 16 + 32 );
338
339 /*
340 * Read the IV and original filesize modulo 16.
341 */
342 if( fread( buffer, 1, 16, fin ) != 16 )
343 {
Rich Evansf90016a2015-01-19 14:26:37 +0000344 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 goto exit;
346 }
347
348 memcpy( IV, buffer, 16 );
349 lastn = IV[15] & 0x0F;
350
351 /*
352 * Hash the IV and the secret key together 8192 times
353 * using the result to setup the AES context and HMAC.
354 */
355 memset( digest, 0, 32 );
356 memcpy( digest, IV, 16 );
357
358 for( i = 0; i < 8192; i++ )
359 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200360 sha256_starts( &sha_ctx, 0 );
361 sha256_update( &sha_ctx, digest, 32 );
362 sha256_update( &sha_ctx, key, keylen );
363 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 }
365
366 memset( key, 0, sizeof( key ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200367 aes_setkey_dec( &aes_ctx, digest, 256 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200368 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000369
370 /*
371 * Decrypt and write the plaintext.
372 */
373 for( offset = 0; offset < filesize; offset += 16 )
374 {
375 if( fread( buffer, 1, 16, fin ) != 16 )
376 {
Rich Evansf90016a2015-01-19 14:26:37 +0000377 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 goto exit;
379 }
380
381 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200382
383 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200385
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 for( i = 0; i < 16; i++ )
387 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
388
389 memcpy( IV, tmp, 16 );
390
391 n = ( lastn > 0 && offset == filesize - 16 )
392 ? lastn : 16;
393
394 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
395 {
Rich Evansf90016a2015-01-19 14:26:37 +0000396 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 goto exit;
398 }
399 }
400
401 /*
402 * Verify the message authentication code.
403 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200404 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
406 if( fread( buffer, 1, 32, fin ) != 32 )
407 {
Rich Evansf90016a2015-01-19 14:26:37 +0000408 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 goto exit;
410 }
411
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100412 /* Use constant-time buffer comparison */
413 diff = 0;
414 for( i = 0; i < 32; i++ )
415 diff |= digest[i] ^ buffer[i];
416
417 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 {
Rich Evansf90016a2015-01-19 14:26:37 +0000419 polarssl_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 "or file corrupted.\n" );
421 goto exit;
422 }
423 }
424
425 ret = 0;
426
427exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000428 if( fin )
429 fclose( fin );
430 if( fout )
431 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000432
433 memset( buffer, 0, sizeof( buffer ) );
434 memset( digest, 0, sizeof( digest ) );
435
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200436 aes_free( &aes_ctx );
Paul Bakker14e8be42014-06-26 12:25:06 +0200437 sha256_free( &sha_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
439 return( ret );
440}
Paul Bakker9e36f042013-06-30 14:34:05 +0200441#endif /* POLARSSL_AES_C && POLARSSL_SHA256_C */