blob: e61ba7d4620e71d607a84a98c0cbc2098ebb41fd [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é-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +010038#include "mbedtls/md.h"
Rich Evans18b78c72015-02-11 14:06:19 +000039
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000043
Paul Bakker494c0b82011-04-24 15:30:07 +000044#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000045#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000046#if !defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000047#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000048#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000049#else
50#include <sys/types.h>
51#include <unistd.h>
52#endif
53
Paul Bakker5121ce52009-01-03 21:22:43 +000054#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
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +000063#if !defined(POLARSSL_AES_C) || !defined(POLARSSL_SHA256_C) || \
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +010064 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_MD_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000065int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000066{
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +010067 polarssl_printf("POLARSSL_AES_C and/or POLARSSL_SHA256_C "
68 "and/or POLARSSL_FS_IO and/or POLARSSL_MD_C "
69 "not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000070 return( 0 );
71}
72#else
Paul Bakker5121ce52009-01-03 21:22:43 +000073int main( int argc, char *argv[] )
74{
Paul Bakker5690efc2011-05-26 13:16:06 +000075 int ret = 1;
76
77 int i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000078 int mode, lastn;
79 size_t keylen;
Paul Bakker20a78082011-01-21 09:32:12 +000080 FILE *fkey, *fin = NULL, *fout = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000081
82 char *p;
83 unsigned char IV[16];
84 unsigned char key[512];
85 unsigned char digest[32];
86 unsigned char buffer[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010087 unsigned char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +000088
89 aes_context aes_ctx;
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +010090 md_context_t sha_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +000091
Paul Bakkercce9d772011-11-18 14:26:47 +000092#if defined(_WIN32_WCE)
93 long filesize, offset;
94#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000095 LARGE_INTEGER li_size;
96 __int64 filesize, offset;
97#else
98 off_t filesize, offset;
99#endif
100
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200101 aes_init( &aes_ctx );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100102 md_init( &sha_ctx );
103
104 ret = md_init_ctx( &sha_ctx, md_info_from_type( POLARSSL_MD_SHA256 ) );
105 if( ret != 0 )
106 {
107 polarssl_printf( " ! md_init_ctx() returned -0x%04x\n", -ret );
108 goto exit;
109 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200110
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 /*
112 * Parse the command-line arguments.
113 */
114 if( argc != 5 )
115 {
Rich Evansf90016a2015-01-19 14:26:37 +0000116 polarssl_printf( USAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
Paul Bakkercce9d772011-11-18 14:26:47 +0000118#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000119 polarssl_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 fflush( stdout ); getchar();
121#endif
122
123 goto exit;
124 }
125
126 mode = atoi( argv[1] );
wslfacc334ef2014-12-28 00:55:41 +0800127 memset(IV, 0, sizeof(IV));
128 memset(key, 0, sizeof(key));
129 memset(digest, 0, sizeof(digest));
130 memset(buffer, 0, sizeof(buffer));
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
132 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
133 {
Rich Evansf90016a2015-01-19 14:26:37 +0000134 polarssl_fprintf( stderr, "invalide operation mode\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 goto exit;
136 }
137
138 if( strcmp( argv[2], argv[3] ) == 0 )
139 {
Rich Evansf90016a2015-01-19 14:26:37 +0000140 polarssl_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 goto exit;
142 }
143
144 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
145 {
Rich Evansf90016a2015-01-19 14:26:37 +0000146 polarssl_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 goto exit;
148 }
149
150 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
151 {
Rich Evansf90016a2015-01-19 14:26:37 +0000152 polarssl_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 goto exit;
154 }
155
156 /*
157 * Read the secret key and clean the command line.
158 */
159 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
160 {
161 keylen = fread( key, 1, sizeof( key ), fkey );
162 fclose( fkey );
163 }
164 else
165 {
166 if( memcmp( argv[4], "hex:", 4 ) == 0 )
167 {
168 p = &argv[4][4];
169 keylen = 0;
170
171 while( sscanf( p, "%02X", &n ) > 0 &&
172 keylen < (int) sizeof( key ) )
173 {
174 key[keylen++] = (unsigned char) n;
175 p += 2;
176 }
177 }
178 else
179 {
180 keylen = strlen( argv[4] );
181
182 if( keylen > (int) sizeof( key ) )
183 keylen = (int) sizeof( key );
184
185 memcpy( key, argv[4], keylen );
186 }
187 }
188
189 memset( argv[4], 0, strlen( argv[4] ) );
190
Paul Bakkercce9d772011-11-18 14:26:47 +0000191#if defined(_WIN32_WCE)
192 filesize = fseek( fin, 0L, SEEK_END );
193#else
194#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 /*
196 * Support large files (> 2Gb) on Win32
197 */
198 li_size.QuadPart = 0;
199 li_size.LowPart =
200 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
201 li_size.LowPart, &li_size.HighPart, FILE_END );
202
203 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
204 {
Rich Evansf90016a2015-01-19 14:26:37 +0000205 polarssl_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 goto exit;
207 }
208
209 filesize = li_size.QuadPart;
210#else
211 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
212 {
213 perror( "lseek" );
214 goto exit;
215 }
216#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000217#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000218
219 if( fseek( fin, 0, SEEK_SET ) < 0 )
220 {
Rich Evansf90016a2015-01-19 14:26:37 +0000221 polarssl_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 goto exit;
223 }
224
225 if( mode == MODE_ENCRYPT )
226 {
227 /*
228 * Generate the initialization vector as:
229 * IV = SHA-256( filesize || filename )[0..15]
230 */
231 for( i = 0; i < 8; i++ )
232 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
233
234 p = argv[2];
235
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100236 md_starts( &sha_ctx );
237 md_update( &sha_ctx, buffer, 8 );
238 md_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
239 md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
241 memcpy( IV, digest, 16 );
242
243 /*
244 * The last four bits in the IV are actually used
245 * to store the file size modulo the AES block size.
246 */
247 lastn = (int)( filesize & 0x0F );
248
249 IV[15] = (unsigned char)
250 ( ( IV[15] & 0xF0 ) | lastn );
251
252 /*
253 * Append the IV at the beginning of the output.
254 */
255 if( fwrite( IV, 1, 16, fout ) != 16 )
256 {
Rich Evansf90016a2015-01-19 14:26:37 +0000257 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 goto exit;
259 }
260
261 /*
262 * Hash the IV and the secret key together 8192 times
263 * using the result to setup the AES context and HMAC.
264 */
265 memset( digest, 0, 32 );
266 memcpy( digest, IV, 16 );
267
268 for( i = 0; i < 8192; i++ )
269 {
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100270 md_starts( &sha_ctx );
271 md_update( &sha_ctx, digest, 32 );
272 md_update( &sha_ctx, key, keylen );
273 md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 }
275
276 memset( key, 0, sizeof( key ) );
Paul Bakker9e36f042013-06-30 14:34:05 +0200277 aes_setkey_enc( &aes_ctx, digest, 256 );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100278 md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000279
280 /*
281 * Encrypt and write the ciphertext.
282 */
283 for( offset = 0; offset < filesize; offset += 16 )
284 {
285 n = ( filesize - offset > 16 ) ? 16 : (int)
286 ( filesize - offset );
287
288 if( fread( buffer, 1, n, fin ) != (size_t) n )
289 {
Rich Evansf90016a2015-01-19 14:26:37 +0000290 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 goto exit;
292 }
293
294 for( i = 0; i < 16; i++ )
295 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
296
297 aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100298 md_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300 if( fwrite( buffer, 1, 16, fout ) != 16 )
301 {
Rich Evansf90016a2015-01-19 14:26:37 +0000302 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 goto exit;
304 }
305
306 memcpy( IV, buffer, 16 );
307 }
308
309 /*
310 * Finally write the HMAC.
311 */
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100312 md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000313
314 if( fwrite( digest, 1, 32, fout ) != 32 )
315 {
Rich Evansf90016a2015-01-19 14:26:37 +0000316 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 goto exit;
318 }
319 }
320
321 if( mode == MODE_DECRYPT )
322 {
323 unsigned char tmp[16];
324
325 /*
326 * The encrypted file must be structured as follows:
327 *
328 * 00 .. 15 Initialization Vector
329 * 16 .. 31 AES Encrypted Block #1
330 * ..
331 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
332 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
333 */
334 if( filesize < 48 )
335 {
Rich Evansf90016a2015-01-19 14:26:37 +0000336 polarssl_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 goto exit;
338 }
339
340 if( ( filesize & 0x0F ) != 0 )
341 {
Rich Evansf90016a2015-01-19 14:26:37 +0000342 polarssl_fprintf( stderr, "File size not a multiple of 16.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 goto exit;
344 }
345
346 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100347 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 */
349 filesize -= ( 16 + 32 );
350
351 /*
352 * Read the IV and original filesize modulo 16.
353 */
354 if( fread( buffer, 1, 16, fin ) != 16 )
355 {
Rich Evansf90016a2015-01-19 14:26:37 +0000356 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000357 goto exit;
358 }
359
360 memcpy( IV, buffer, 16 );
361 lastn = IV[15] & 0x0F;
362
363 /*
364 * Hash the IV and the secret key together 8192 times
365 * using the result to setup the AES context and HMAC.
366 */
367 memset( digest, 0, 32 );
368 memcpy( digest, IV, 16 );
369
370 for( i = 0; i < 8192; i++ )
371 {
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100372 md_starts( &sha_ctx );
373 md_update( &sha_ctx, digest, 32 );
374 md_update( &sha_ctx, key, keylen );
375 md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 }
377
378 memset( key, 0, sizeof( key ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200379 aes_setkey_dec( &aes_ctx, digest, 256 );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100380 md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000381
382 /*
383 * Decrypt and write the plaintext.
384 */
385 for( offset = 0; offset < filesize; offset += 16 )
386 {
387 if( fread( buffer, 1, 16, fin ) != 16 )
388 {
Rich Evansf90016a2015-01-19 14:26:37 +0000389 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 goto exit;
391 }
392
393 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200394
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100395 md_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200397
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 for( i = 0; i < 16; i++ )
399 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
400
401 memcpy( IV, tmp, 16 );
402
403 n = ( lastn > 0 && offset == filesize - 16 )
404 ? lastn : 16;
405
406 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
407 {
Rich Evansf90016a2015-01-19 14:26:37 +0000408 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 goto exit;
410 }
411 }
412
413 /*
414 * Verify the message authentication code.
415 */
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100416 md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
418 if( fread( buffer, 1, 32, fin ) != 32 )
419 {
Rich Evansf90016a2015-01-19 14:26:37 +0000420 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 goto exit;
422 }
423
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100424 /* Use constant-time buffer comparison */
425 diff = 0;
426 for( i = 0; i < 32; i++ )
427 diff |= digest[i] ^ buffer[i];
428
429 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 {
Rich Evansf90016a2015-01-19 14:26:37 +0000431 polarssl_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker5121ce52009-01-03 21:22:43 +0000432 "or file corrupted.\n" );
433 goto exit;
434 }
435 }
436
437 ret = 0;
438
439exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000440 if( fin )
441 fclose( fin );
442 if( fout )
443 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000444
445 memset( buffer, 0, sizeof( buffer ) );
446 memset( digest, 0, sizeof( digest ) );
447
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200448 aes_free( &aes_ctx );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100449 md_free( &sha_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000450
451 return( ret );
452}
Rich Evans18b78c72015-02-11 14:06:19 +0000453#endif /* POLARSSL_AES_C && POLARSSL_SHA256_C && POLARSSL_FS_IO */