blob: db20db456f1397c204e6ca0029301622d4d18c8e [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
34#define polarssl_malloc malloc
35#define polarssl_free free
36#endif
37
Paul Bakker494c0b82011-04-24 15:30:07 +000038#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000039#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000040#if !defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000041#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000043#else
44#include <sys/types.h>
45#include <unistd.h>
46#endif
47
48#include <string.h>
49#include <stdlib.h>
50#include <stdio.h>
51#include <time.h>
52
Paul Bakker40e46942009-01-03 21:51:57 +000053#include "polarssl/aes.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020054#include "polarssl/sha256.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000055
56#define MODE_ENCRYPT 0
57#define MODE_DECRYPT 1
58
59#define USAGE \
60 "\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
61 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
62 "\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
63 "\n"
64
Paul Bakker9e36f042013-06-30 14:34:05 +020065#if !defined(POLARSSL_AES_C) || !defined(POLARSSL_SHA256_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000066int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000067{
Paul Bakkercce9d772011-11-18 14:26:47 +000068 ((void) argc);
69 ((void) argv);
Rich Evansf90016a2015-01-19 14:26:37 +000070 polarssl_printf("POLARSSL_AES_C and/or POLARSSL_SHA256_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000071 return( 0 );
72}
73#else
Paul Bakker5121ce52009-01-03 21:22:43 +000074int main( int argc, char *argv[] )
75{
Paul Bakker5690efc2011-05-26 13:16:06 +000076 int ret = 1;
77
78 int i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000079 int mode, lastn;
80 size_t keylen;
Paul Bakker20a78082011-01-21 09:32:12 +000081 FILE *fkey, *fin = NULL, *fout = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000082
83 char *p;
84 unsigned char IV[16];
85 unsigned char key[512];
86 unsigned char digest[32];
87 unsigned char buffer[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010088 unsigned char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +000089
90 aes_context aes_ctx;
Paul Bakker9e36f042013-06-30 14:34:05 +020091 sha256_context sha_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Paul Bakkercce9d772011-11-18 14:26:47 +000093#if defined(_WIN32_WCE)
94 long filesize, offset;
95#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000096 LARGE_INTEGER li_size;
97 __int64 filesize, offset;
98#else
99 off_t filesize, offset;
100#endif
101
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200102 aes_init( &aes_ctx );
Paul Bakker14e8be42014-06-26 12:25:06 +0200103 sha256_init( &sha_ctx );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200104
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 /*
106 * Parse the command-line arguments.
107 */
108 if( argc != 5 )
109 {
Rich Evansf90016a2015-01-19 14:26:37 +0000110 polarssl_printf( USAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Paul Bakkercce9d772011-11-18 14:26:47 +0000112#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000113 polarssl_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 fflush( stdout ); getchar();
115#endif
116
117 goto exit;
118 }
119
120 mode = atoi( argv[1] );
121
122 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
123 {
Rich Evansf90016a2015-01-19 14:26:37 +0000124 polarssl_fprintf( stderr, "invalide operation mode\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 goto exit;
126 }
127
128 if( strcmp( argv[2], argv[3] ) == 0 )
129 {
Rich Evansf90016a2015-01-19 14:26:37 +0000130 polarssl_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 goto exit;
132 }
133
134 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
135 {
Rich Evansf90016a2015-01-19 14:26:37 +0000136 polarssl_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 goto exit;
138 }
139
140 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
141 {
Rich Evansf90016a2015-01-19 14:26:37 +0000142 polarssl_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 goto exit;
144 }
145
146 /*
147 * Read the secret key and clean the command line.
148 */
149 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
150 {
151 keylen = fread( key, 1, sizeof( key ), fkey );
152 fclose( fkey );
153 }
154 else
155 {
156 if( memcmp( argv[4], "hex:", 4 ) == 0 )
157 {
158 p = &argv[4][4];
159 keylen = 0;
160
161 while( sscanf( p, "%02X", &n ) > 0 &&
162 keylen < (int) sizeof( key ) )
163 {
164 key[keylen++] = (unsigned char) n;
165 p += 2;
166 }
167 }
168 else
169 {
170 keylen = strlen( argv[4] );
171
172 if( keylen > (int) sizeof( key ) )
173 keylen = (int) sizeof( key );
174
175 memcpy( key, argv[4], keylen );
176 }
177 }
178
179 memset( argv[4], 0, strlen( argv[4] ) );
180
Paul Bakkercce9d772011-11-18 14:26:47 +0000181#if defined(_WIN32_WCE)
182 filesize = fseek( fin, 0L, SEEK_END );
183#else
184#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 /*
186 * Support large files (> 2Gb) on Win32
187 */
188 li_size.QuadPart = 0;
189 li_size.LowPart =
190 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
191 li_size.LowPart, &li_size.HighPart, FILE_END );
192
193 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
194 {
Rich Evansf90016a2015-01-19 14:26:37 +0000195 polarssl_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 goto exit;
197 }
198
199 filesize = li_size.QuadPart;
200#else
201 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
202 {
203 perror( "lseek" );
204 goto exit;
205 }
206#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000207#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
209 if( fseek( fin, 0, SEEK_SET ) < 0 )
210 {
Rich Evansf90016a2015-01-19 14:26:37 +0000211 polarssl_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 goto exit;
213 }
214
215 if( mode == MODE_ENCRYPT )
216 {
217 /*
218 * Generate the initialization vector as:
219 * IV = SHA-256( filesize || filename )[0..15]
220 */
221 for( i = 0; i < 8; i++ )
222 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
223
224 p = argv[2];
225
Paul Bakker9e36f042013-06-30 14:34:05 +0200226 sha256_starts( &sha_ctx, 0 );
227 sha256_update( &sha_ctx, buffer, 8 );
228 sha256_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
229 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
231 memcpy( IV, digest, 16 );
232
233 /*
234 * The last four bits in the IV are actually used
235 * to store the file size modulo the AES block size.
236 */
237 lastn = (int)( filesize & 0x0F );
238
239 IV[15] = (unsigned char)
240 ( ( IV[15] & 0xF0 ) | lastn );
241
242 /*
243 * Append the IV at the beginning of the output.
244 */
245 if( fwrite( IV, 1, 16, fout ) != 16 )
246 {
Rich Evansf90016a2015-01-19 14:26:37 +0000247 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 goto exit;
249 }
250
251 /*
252 * Hash the IV and the secret key together 8192 times
253 * using the result to setup the AES context and HMAC.
254 */
255 memset( digest, 0, 32 );
256 memcpy( digest, IV, 16 );
257
258 for( i = 0; i < 8192; i++ )
259 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200260 sha256_starts( &sha_ctx, 0 );
261 sha256_update( &sha_ctx, digest, 32 );
262 sha256_update( &sha_ctx, key, keylen );
263 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 }
265
266 memset( key, 0, sizeof( key ) );
Paul Bakker9e36f042013-06-30 14:34:05 +0200267 aes_setkey_enc( &aes_ctx, digest, 256 );
268 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000269
270 /*
271 * Encrypt and write the ciphertext.
272 */
273 for( offset = 0; offset < filesize; offset += 16 )
274 {
275 n = ( filesize - offset > 16 ) ? 16 : (int)
276 ( filesize - offset );
277
278 if( fread( buffer, 1, n, fin ) != (size_t) n )
279 {
Rich Evansf90016a2015-01-19 14:26:37 +0000280 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 goto exit;
282 }
283
284 for( i = 0; i < 16; i++ )
285 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
286
287 aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200288 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
290 if( fwrite( buffer, 1, 16, fout ) != 16 )
291 {
Rich Evansf90016a2015-01-19 14:26:37 +0000292 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 goto exit;
294 }
295
296 memcpy( IV, buffer, 16 );
297 }
298
299 /*
300 * Finally write the HMAC.
301 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200302 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000303
304 if( fwrite( digest, 1, 32, fout ) != 32 )
305 {
Rich Evansf90016a2015-01-19 14:26:37 +0000306 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 goto exit;
308 }
309 }
310
311 if( mode == MODE_DECRYPT )
312 {
313 unsigned char tmp[16];
314
315 /*
316 * The encrypted file must be structured as follows:
317 *
318 * 00 .. 15 Initialization Vector
319 * 16 .. 31 AES Encrypted Block #1
320 * ..
321 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
322 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
323 */
324 if( filesize < 48 )
325 {
Rich Evansf90016a2015-01-19 14:26:37 +0000326 polarssl_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 goto exit;
328 }
329
330 if( ( filesize & 0x0F ) != 0 )
331 {
Rich Evansf90016a2015-01-19 14:26:37 +0000332 polarssl_fprintf( stderr, "File size not a multiple of 16.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 goto exit;
334 }
335
336 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100337 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 */
339 filesize -= ( 16 + 32 );
340
341 /*
342 * Read the IV and original filesize modulo 16.
343 */
344 if( fread( buffer, 1, 16, fin ) != 16 )
345 {
Rich Evansf90016a2015-01-19 14:26:37 +0000346 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 goto exit;
348 }
349
350 memcpy( IV, buffer, 16 );
351 lastn = IV[15] & 0x0F;
352
353 /*
354 * Hash the IV and the secret key together 8192 times
355 * using the result to setup the AES context and HMAC.
356 */
357 memset( digest, 0, 32 );
358 memcpy( digest, IV, 16 );
359
360 for( i = 0; i < 8192; i++ )
361 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200362 sha256_starts( &sha_ctx, 0 );
363 sha256_update( &sha_ctx, digest, 32 );
364 sha256_update( &sha_ctx, key, keylen );
365 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000366 }
367
368 memset( key, 0, sizeof( key ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200369 aes_setkey_dec( &aes_ctx, digest, 256 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200370 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000371
372 /*
373 * Decrypt and write the plaintext.
374 */
375 for( offset = 0; offset < filesize; offset += 16 )
376 {
377 if( fread( buffer, 1, 16, fin ) != 16 )
378 {
Rich Evansf90016a2015-01-19 14:26:37 +0000379 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 goto exit;
381 }
382
383 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200384
385 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200387
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 for( i = 0; i < 16; i++ )
389 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
390
391 memcpy( IV, tmp, 16 );
392
393 n = ( lastn > 0 && offset == filesize - 16 )
394 ? lastn : 16;
395
396 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
397 {
Rich Evansf90016a2015-01-19 14:26:37 +0000398 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000399 goto exit;
400 }
401 }
402
403 /*
404 * Verify the message authentication code.
405 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200406 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000407
408 if( fread( buffer, 1, 32, fin ) != 32 )
409 {
Rich Evansf90016a2015-01-19 14:26:37 +0000410 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 goto exit;
412 }
413
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100414 /* Use constant-time buffer comparison */
415 diff = 0;
416 for( i = 0; i < 32; i++ )
417 diff |= digest[i] ^ buffer[i];
418
419 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 {
Rich Evansf90016a2015-01-19 14:26:37 +0000421 polarssl_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 "or file corrupted.\n" );
423 goto exit;
424 }
425 }
426
427 ret = 0;
428
429exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000430 if( fin )
431 fclose( fin );
432 if( fout )
433 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000434
435 memset( buffer, 0, sizeof( buffer ) );
436 memset( digest, 0, sizeof( digest ) );
437
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200438 aes_free( &aes_ctx );
Paul Bakker14e8be42014-06-26 12:25:06 +0200439 sha256_free( &sha_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000440
441 return( ret );
442}
Paul Bakker9e36f042013-06-30 14:34:05 +0200443#endif /* POLARSSL_AES_C && POLARSSL_SHA256_C */