blob: 5d733b44f56bf0205f1362e3b2bb69e38a0387b7 [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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é-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_fprintf fprintf
Rich Evans18b78c72015-02-11 14:06:19 +000034#define polarssl_printf printf
35#endif
36
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +000037#if defined(POLARSSL_AES_C) && defined(POLARSSL_SHA256_C) && \
Rich Evans18b78c72015-02-11 14:06:19 +000038 defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/aes.h"
40#include "mbedtls/sha256.h"
Rich Evans18b78c72015-02-11 14:06:19 +000041
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000045#endif
46
Paul Bakker494c0b82011-04-24 15:30:07 +000047#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000048#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000049#if !defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000050#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000051#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052#else
53#include <sys/types.h>
54#include <unistd.h>
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#define MODE_ENCRYPT 0
58#define MODE_DECRYPT 1
59
60#define USAGE \
61 "\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
62 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
63 "\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
64 "\n"
65
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +000066#if !defined(POLARSSL_AES_C) || !defined(POLARSSL_SHA256_C) || \
Rich Evans18b78c72015-02-11 14:06:19 +000067 !defined(POLARSSL_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000068int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000069{
Rich Evans18b78c72015-02-11 14:06:19 +000070 polarssl_printf("POLARSSL_AES_C and/or POLARSSL_SHA256_C and/or POLARSSL_FS_IO 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] );
wslfacc334ef2014-12-28 00:55:41 +0800121 memset(IV, 0, sizeof(IV));
122 memset(key, 0, sizeof(key));
123 memset(digest, 0, sizeof(digest));
124 memset(buffer, 0, sizeof(buffer));
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
126 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
127 {
Rich Evansf90016a2015-01-19 14:26:37 +0000128 polarssl_fprintf( stderr, "invalide operation mode\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 goto exit;
130 }
131
132 if( strcmp( argv[2], argv[3] ) == 0 )
133 {
Rich Evansf90016a2015-01-19 14:26:37 +0000134 polarssl_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 goto exit;
136 }
137
138 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
139 {
Rich Evansf90016a2015-01-19 14:26:37 +0000140 polarssl_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 goto exit;
142 }
143
144 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
145 {
Rich Evansf90016a2015-01-19 14:26:37 +0000146 polarssl_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 goto exit;
148 }
149
150 /*
151 * Read the secret key and clean the command line.
152 */
153 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
154 {
155 keylen = fread( key, 1, sizeof( key ), fkey );
156 fclose( fkey );
157 }
158 else
159 {
160 if( memcmp( argv[4], "hex:", 4 ) == 0 )
161 {
162 p = &argv[4][4];
163 keylen = 0;
164
165 while( sscanf( p, "%02X", &n ) > 0 &&
166 keylen < (int) sizeof( key ) )
167 {
168 key[keylen++] = (unsigned char) n;
169 p += 2;
170 }
171 }
172 else
173 {
174 keylen = strlen( argv[4] );
175
176 if( keylen > (int) sizeof( key ) )
177 keylen = (int) sizeof( key );
178
179 memcpy( key, argv[4], keylen );
180 }
181 }
182
183 memset( argv[4], 0, strlen( argv[4] ) );
184
Paul Bakkercce9d772011-11-18 14:26:47 +0000185#if defined(_WIN32_WCE)
186 filesize = fseek( fin, 0L, SEEK_END );
187#else
188#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 /*
190 * Support large files (> 2Gb) on Win32
191 */
192 li_size.QuadPart = 0;
193 li_size.LowPart =
194 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
195 li_size.LowPart, &li_size.HighPart, FILE_END );
196
197 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
198 {
Rich Evansf90016a2015-01-19 14:26:37 +0000199 polarssl_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 goto exit;
201 }
202
203 filesize = li_size.QuadPart;
204#else
205 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
206 {
207 perror( "lseek" );
208 goto exit;
209 }
210#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000211#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
213 if( fseek( fin, 0, SEEK_SET ) < 0 )
214 {
Rich Evansf90016a2015-01-19 14:26:37 +0000215 polarssl_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 goto exit;
217 }
218
219 if( mode == MODE_ENCRYPT )
220 {
221 /*
222 * Generate the initialization vector as:
223 * IV = SHA-256( filesize || filename )[0..15]
224 */
225 for( i = 0; i < 8; i++ )
226 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
227
228 p = argv[2];
229
Paul Bakker9e36f042013-06-30 14:34:05 +0200230 sha256_starts( &sha_ctx, 0 );
231 sha256_update( &sha_ctx, buffer, 8 );
232 sha256_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
233 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
235 memcpy( IV, digest, 16 );
236
237 /*
238 * The last four bits in the IV are actually used
239 * to store the file size modulo the AES block size.
240 */
241 lastn = (int)( filesize & 0x0F );
242
243 IV[15] = (unsigned char)
244 ( ( IV[15] & 0xF0 ) | lastn );
245
246 /*
247 * Append the IV at the beginning of the output.
248 */
249 if( fwrite( IV, 1, 16, fout ) != 16 )
250 {
Rich Evansf90016a2015-01-19 14:26:37 +0000251 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 goto exit;
253 }
254
255 /*
256 * Hash the IV and the secret key together 8192 times
257 * using the result to setup the AES context and HMAC.
258 */
259 memset( digest, 0, 32 );
260 memcpy( digest, IV, 16 );
261
262 for( i = 0; i < 8192; i++ )
263 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200264 sha256_starts( &sha_ctx, 0 );
265 sha256_update( &sha_ctx, digest, 32 );
266 sha256_update( &sha_ctx, key, keylen );
267 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 }
269
270 memset( key, 0, sizeof( key ) );
Paul Bakker9e36f042013-06-30 14:34:05 +0200271 aes_setkey_enc( &aes_ctx, digest, 256 );
272 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000273
274 /*
275 * Encrypt and write the ciphertext.
276 */
277 for( offset = 0; offset < filesize; offset += 16 )
278 {
279 n = ( filesize - offset > 16 ) ? 16 : (int)
280 ( filesize - offset );
281
282 if( fread( buffer, 1, n, fin ) != (size_t) n )
283 {
Rich Evansf90016a2015-01-19 14:26:37 +0000284 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 goto exit;
286 }
287
288 for( i = 0; i < 16; i++ )
289 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
290
291 aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200292 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000293
294 if( fwrite( buffer, 1, 16, fout ) != 16 )
295 {
Rich Evansf90016a2015-01-19 14:26:37 +0000296 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 goto exit;
298 }
299
300 memcpy( IV, buffer, 16 );
301 }
302
303 /*
304 * Finally write the HMAC.
305 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200306 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
308 if( fwrite( digest, 1, 32, fout ) != 32 )
309 {
Rich Evansf90016a2015-01-19 14:26:37 +0000310 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 goto exit;
312 }
313 }
314
315 if( mode == MODE_DECRYPT )
316 {
317 unsigned char tmp[16];
318
319 /*
320 * The encrypted file must be structured as follows:
321 *
322 * 00 .. 15 Initialization Vector
323 * 16 .. 31 AES Encrypted Block #1
324 * ..
325 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
326 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
327 */
328 if( filesize < 48 )
329 {
Rich Evansf90016a2015-01-19 14:26:37 +0000330 polarssl_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 goto exit;
332 }
333
334 if( ( filesize & 0x0F ) != 0 )
335 {
Rich Evansf90016a2015-01-19 14:26:37 +0000336 polarssl_fprintf( stderr, "File size not a multiple of 16.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 goto exit;
338 }
339
340 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100341 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 */
343 filesize -= ( 16 + 32 );
344
345 /*
346 * Read the IV and original filesize modulo 16.
347 */
348 if( fread( buffer, 1, 16, fin ) != 16 )
349 {
Rich Evansf90016a2015-01-19 14:26:37 +0000350 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 goto exit;
352 }
353
354 memcpy( IV, buffer, 16 );
355 lastn = IV[15] & 0x0F;
356
357 /*
358 * Hash the IV and the secret key together 8192 times
359 * using the result to setup the AES context and HMAC.
360 */
361 memset( digest, 0, 32 );
362 memcpy( digest, IV, 16 );
363
364 for( i = 0; i < 8192; i++ )
365 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200366 sha256_starts( &sha_ctx, 0 );
367 sha256_update( &sha_ctx, digest, 32 );
368 sha256_update( &sha_ctx, key, keylen );
369 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 }
371
372 memset( key, 0, sizeof( key ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200373 aes_setkey_dec( &aes_ctx, digest, 256 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200374 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
376 /*
377 * Decrypt and write the plaintext.
378 */
379 for( offset = 0; offset < filesize; offset += 16 )
380 {
381 if( fread( buffer, 1, 16, fin ) != 16 )
382 {
Rich Evansf90016a2015-01-19 14:26:37 +0000383 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 goto exit;
385 }
386
387 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200388
389 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200391
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 for( i = 0; i < 16; i++ )
393 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
394
395 memcpy( IV, tmp, 16 );
396
397 n = ( lastn > 0 && offset == filesize - 16 )
398 ? lastn : 16;
399
400 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
401 {
Rich Evansf90016a2015-01-19 14:26:37 +0000402 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 goto exit;
404 }
405 }
406
407 /*
408 * Verify the message authentication code.
409 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200410 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
412 if( fread( buffer, 1, 32, fin ) != 32 )
413 {
Rich Evansf90016a2015-01-19 14:26:37 +0000414 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 goto exit;
416 }
417
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100418 /* Use constant-time buffer comparison */
419 diff = 0;
420 for( i = 0; i < 32; i++ )
421 diff |= digest[i] ^ buffer[i];
422
423 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 {
Rich Evansf90016a2015-01-19 14:26:37 +0000425 polarssl_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker5121ce52009-01-03 21:22:43 +0000426 "or file corrupted.\n" );
427 goto exit;
428 }
429 }
430
431 ret = 0;
432
433exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000434 if( fin )
435 fclose( fin );
436 if( fout )
437 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
439 memset( buffer, 0, sizeof( buffer ) );
440 memset( digest, 0, sizeof( digest ) );
441
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200442 aes_free( &aes_ctx );
Paul Bakker14e8be42014-06-26 12:25:06 +0200443 sha256_free( &sha_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000444
445 return( ret );
446}
Rich Evans18b78c72015-02-11 14:06:19 +0000447#endif /* POLARSSL_AES_C && POLARSSL_SHA256_C && POLARSSL_FS_IO */