blob: 3b744d14f3d7fb73ccbffd280e45425bd738f2f0 [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é-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
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)
39#include "polarssl/aes.h"
40#include "polarssl/sha256.h"
41
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];
Hanno Beckere6ee6382017-06-27 08:45:27 +010085 unsigned char tmp[16];
Paul Bakker5121ce52009-01-03 21:22:43 +000086 unsigned char key[512];
87 unsigned char digest[32];
88 unsigned char buffer[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010089 unsigned char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +000090
91 aes_context aes_ctx;
Paul Bakker9e36f042013-06-30 14:34:05 +020092 sha256_context sha_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Paul Bakkercce9d772011-11-18 14:26:47 +000094#if defined(_WIN32_WCE)
95 long filesize, offset;
96#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000097 LARGE_INTEGER li_size;
98 __int64 filesize, offset;
99#else
100 off_t filesize, offset;
101#endif
102
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200103 aes_init( &aes_ctx );
Paul Bakker14e8be42014-06-26 12:25:06 +0200104 sha256_init( &sha_ctx );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200105
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 /*
107 * Parse the command-line arguments.
108 */
109 if( argc != 5 )
110 {
Rich Evansf90016a2015-01-19 14:26:37 +0000111 polarssl_printf( USAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Paul Bakkercce9d772011-11-18 14:26:47 +0000113#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000114 polarssl_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 fflush( stdout ); getchar();
116#endif
117
118 goto exit;
119 }
120
121 mode = atoi( argv[1] );
Hanno Beckere6ee6382017-06-27 08:45:27 +0100122 memset( IV, 0, sizeof( IV ) );
123 memset( key, 0, sizeof( key ) );
124 memset( digest, 0, sizeof( digest ) );
125 memset( buffer, 0, sizeof( buffer ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
127 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
128 {
Rich Evansf90016a2015-01-19 14:26:37 +0000129 polarssl_fprintf( stderr, "invalide operation mode\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 goto exit;
131 }
132
133 if( strcmp( argv[2], argv[3] ) == 0 )
134 {
Rich Evansf90016a2015-01-19 14:26:37 +0000135 polarssl_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 goto exit;
137 }
138
139 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
140 {
Rich Evansf90016a2015-01-19 14:26:37 +0000141 polarssl_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 goto exit;
143 }
144
145 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
146 {
Rich Evansf90016a2015-01-19 14:26:37 +0000147 polarssl_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 goto exit;
149 }
150
151 /*
Hanno Beckerdcbb0242017-06-27 11:41:45 +0100152 * Read the secret key from file or command line
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 */
154 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
155 {
156 keylen = fread( key, 1, sizeof( key ), fkey );
157 fclose( fkey );
158 }
159 else
160 {
161 if( memcmp( argv[4], "hex:", 4 ) == 0 )
162 {
163 p = &argv[4][4];
164 keylen = 0;
165
166 while( sscanf( p, "%02X", &n ) > 0 &&
167 keylen < (int) sizeof( key ) )
168 {
169 key[keylen++] = (unsigned char) n;
170 p += 2;
171 }
172 }
173 else
174 {
175 keylen = strlen( argv[4] );
176
177 if( keylen > (int) sizeof( key ) )
178 keylen = (int) sizeof( key );
179
180 memcpy( key, argv[4], keylen );
181 }
182 }
183
Paul Bakkercce9d772011-11-18 14:26:47 +0000184#if defined(_WIN32_WCE)
185 filesize = fseek( fin, 0L, SEEK_END );
186#else
187#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 /*
189 * Support large files (> 2Gb) on Win32
190 */
191 li_size.QuadPart = 0;
192 li_size.LowPart =
193 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
194 li_size.LowPart, &li_size.HighPart, FILE_END );
195
196 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
197 {
Rich Evansf90016a2015-01-19 14:26:37 +0000198 polarssl_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 goto exit;
200 }
201
202 filesize = li_size.QuadPart;
203#else
204 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
205 {
206 perror( "lseek" );
207 goto exit;
208 }
209#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000210#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
212 if( fseek( fin, 0, SEEK_SET ) < 0 )
213 {
Rich Evansf90016a2015-01-19 14:26:37 +0000214 polarssl_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 goto exit;
216 }
217
218 if( mode == MODE_ENCRYPT )
219 {
220 /*
221 * Generate the initialization vector as:
222 * IV = SHA-256( filesize || filename )[0..15]
223 */
224 for( i = 0; i < 8; i++ )
225 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
226
227 p = argv[2];
228
Paul Bakker9e36f042013-06-30 14:34:05 +0200229 sha256_starts( &sha_ctx, 0 );
230 sha256_update( &sha_ctx, buffer, 8 );
231 sha256_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
232 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
234 memcpy( IV, digest, 16 );
235
236 /*
237 * The last four bits in the IV are actually used
238 * to store the file size modulo the AES block size.
239 */
240 lastn = (int)( filesize & 0x0F );
241
242 IV[15] = (unsigned char)
243 ( ( IV[15] & 0xF0 ) | lastn );
244
245 /*
246 * Append the IV at the beginning of the output.
247 */
248 if( fwrite( IV, 1, 16, fout ) != 16 )
249 {
Rich Evansf90016a2015-01-19 14:26:37 +0000250 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 goto exit;
252 }
253
254 /*
255 * Hash the IV and the secret key together 8192 times
256 * using the result to setup the AES context and HMAC.
257 */
258 memset( digest, 0, 32 );
259 memcpy( digest, IV, 16 );
260
261 for( i = 0; i < 8192; i++ )
262 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200263 sha256_starts( &sha_ctx, 0 );
264 sha256_update( &sha_ctx, digest, 32 );
265 sha256_update( &sha_ctx, key, keylen );
266 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 }
268
Paul Bakker9e36f042013-06-30 14:34:05 +0200269 aes_setkey_enc( &aes_ctx, digest, 256 );
270 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
272 /*
273 * Encrypt and write the ciphertext.
274 */
275 for( offset = 0; offset < filesize; offset += 16 )
276 {
277 n = ( filesize - offset > 16 ) ? 16 : (int)
278 ( filesize - offset );
279
280 if( fread( buffer, 1, n, fin ) != (size_t) n )
281 {
Rich Evansf90016a2015-01-19 14:26:37 +0000282 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 goto exit;
284 }
285
286 for( i = 0; i < 16; i++ )
287 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
288
289 aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200290 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
292 if( fwrite( buffer, 1, 16, fout ) != 16 )
293 {
Rich Evansf90016a2015-01-19 14:26:37 +0000294 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 goto exit;
296 }
297
298 memcpy( IV, buffer, 16 );
299 }
300
301 /*
302 * Finally write the HMAC.
303 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200304 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000305
306 if( fwrite( digest, 1, 32, fout ) != 32 )
307 {
Rich Evansf90016a2015-01-19 14:26:37 +0000308 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 goto exit;
310 }
311 }
312
313 if( mode == MODE_DECRYPT )
314 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 /*
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
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200368 aes_setkey_dec( &aes_ctx, digest, 256 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200369 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000370
371 /*
372 * Decrypt and write the plaintext.
373 */
374 for( offset = 0; offset < filesize; offset += 16 )
375 {
376 if( fread( buffer, 1, 16, fin ) != 16 )
377 {
Rich Evansf90016a2015-01-19 14:26:37 +0000378 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 goto exit;
380 }
381
382 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200383
384 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200386
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 for( i = 0; i < 16; i++ )
388 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
389
390 memcpy( IV, tmp, 16 );
391
392 n = ( lastn > 0 && offset == filesize - 16 )
393 ? lastn : 16;
394
395 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
396 {
Rich Evansf90016a2015-01-19 14:26:37 +0000397 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 goto exit;
399 }
400 }
401
402 /*
403 * Verify the message authentication code.
404 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200405 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000406
407 if( fread( buffer, 1, 32, fin ) != 32 )
408 {
Rich Evansf90016a2015-01-19 14:26:37 +0000409 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410 goto exit;
411 }
412
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100413 /* Use constant-time buffer comparison */
414 diff = 0;
415 for( i = 0; i < 32; i++ )
416 diff |= digest[i] ^ buffer[i];
417
418 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 {
Rich Evansf90016a2015-01-19 14:26:37 +0000420 polarssl_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 "or file corrupted.\n" );
422 goto exit;
423 }
424 }
425
426 ret = 0;
427
428exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000429 if( fin )
430 fclose( fin );
431 if( fout )
432 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
Hanno Beckere6ee6382017-06-27 08:45:27 +0100434 /* Zeroize all command line arguments to also cover
435 the case when the user has missed or reordered some,
436 in which case the key might not be in argv[4]. */
437 for( i = 0; i < argc; i++ )
438 memset( argv[i], 0, strlen( argv[i] ) );
439
440 memset( IV, 0, sizeof( IV ) );
441 memset( key, 0, sizeof( key ) );
442 memset( tmp, 0, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000443 memset( buffer, 0, sizeof( buffer ) );
444 memset( digest, 0, sizeof( digest ) );
445
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200446 aes_free( &aes_ctx );
Paul Bakker14e8be42014-06-26 12:25:06 +0200447 sha256_free( &sha_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
449 return( ret );
450}
Rich Evans18b78c72015-02-11 14:06:19 +0000451#endif /* POLARSSL_AES_C && POLARSSL_SHA256_C && POLARSSL_FS_IO */