blob: be832e012177bf426e942a65e905c5372bd66b42 [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://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
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
37#if defined(POLARSSL_AES_C) && defined(POLARSSL_SHA256_C) &&\
38 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
Rich Evans18b78c72015-02-11 14:06:19 +000066#if !defined(POLARSSL_AES_C) || !defined(POLARSSL_SHA256_C) ||\
67 !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000068int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000069{
Paul Bakkercce9d772011-11-18 14:26:47 +000070 ((void) argc);
71 ((void) argv);
Rich Evans18b78c72015-02-11 14:06:19 +000072 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 +000073 return( 0 );
74}
75#else
Paul Bakker5121ce52009-01-03 21:22:43 +000076int main( int argc, char *argv[] )
77{
Paul Bakker5690efc2011-05-26 13:16:06 +000078 int ret = 1;
79
80 int i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000081 int mode, lastn;
82 size_t keylen;
Paul Bakker20a78082011-01-21 09:32:12 +000083 FILE *fkey, *fin = NULL, *fout = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000084
85 char *p;
86 unsigned char IV[16];
87 unsigned char key[512];
88 unsigned char digest[32];
89 unsigned char buffer[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010090 unsigned char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +000091
92 aes_context aes_ctx;
Paul Bakker9e36f042013-06-30 14:34:05 +020093 sha256_context sha_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +000094
Paul Bakkercce9d772011-11-18 14:26:47 +000095#if defined(_WIN32_WCE)
96 long filesize, offset;
97#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000098 LARGE_INTEGER li_size;
99 __int64 filesize, offset;
100#else
101 off_t filesize, offset;
102#endif
103
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200104 aes_init( &aes_ctx );
Paul Bakker14e8be42014-06-26 12:25:06 +0200105 sha256_init( &sha_ctx );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200106
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 /*
108 * Parse the command-line arguments.
109 */
110 if( argc != 5 )
111 {
Rich Evansf90016a2015-01-19 14:26:37 +0000112 polarssl_printf( USAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
Paul Bakkercce9d772011-11-18 14:26:47 +0000114#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000115 polarssl_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 fflush( stdout ); getchar();
117#endif
118
119 goto exit;
120 }
121
122 mode = atoi( argv[1] );
wslfacc334ef2014-12-28 00:55:41 +0800123 memset(IV, 0, sizeof(IV));
124 memset(key, 0, sizeof(key));
125 memset(digest, 0, sizeof(digest));
126 memset(buffer, 0, sizeof(buffer));
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
128 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
129 {
Rich Evansf90016a2015-01-19 14:26:37 +0000130 polarssl_fprintf( stderr, "invalide operation mode\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 goto exit;
132 }
133
134 if( strcmp( argv[2], argv[3] ) == 0 )
135 {
Rich Evansf90016a2015-01-19 14:26:37 +0000136 polarssl_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 goto exit;
138 }
139
140 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
141 {
Rich Evansf90016a2015-01-19 14:26:37 +0000142 polarssl_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 goto exit;
144 }
145
146 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
147 {
Rich Evansf90016a2015-01-19 14:26:37 +0000148 polarssl_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 goto exit;
150 }
151
152 /*
153 * Read the secret key and clean the command line.
154 */
155 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
156 {
157 keylen = fread( key, 1, sizeof( key ), fkey );
158 fclose( fkey );
159 }
160 else
161 {
162 if( memcmp( argv[4], "hex:", 4 ) == 0 )
163 {
164 p = &argv[4][4];
165 keylen = 0;
166
167 while( sscanf( p, "%02X", &n ) > 0 &&
168 keylen < (int) sizeof( key ) )
169 {
170 key[keylen++] = (unsigned char) n;
171 p += 2;
172 }
173 }
174 else
175 {
176 keylen = strlen( argv[4] );
177
178 if( keylen > (int) sizeof( key ) )
179 keylen = (int) sizeof( key );
180
181 memcpy( key, argv[4], keylen );
182 }
183 }
184
185 memset( argv[4], 0, strlen( argv[4] ) );
186
Paul Bakkercce9d772011-11-18 14:26:47 +0000187#if defined(_WIN32_WCE)
188 filesize = fseek( fin, 0L, SEEK_END );
189#else
190#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 /*
192 * Support large files (> 2Gb) on Win32
193 */
194 li_size.QuadPart = 0;
195 li_size.LowPart =
196 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
197 li_size.LowPart, &li_size.HighPart, FILE_END );
198
199 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
200 {
Rich Evansf90016a2015-01-19 14:26:37 +0000201 polarssl_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 goto exit;
203 }
204
205 filesize = li_size.QuadPart;
206#else
207 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
208 {
209 perror( "lseek" );
210 goto exit;
211 }
212#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000213#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
215 if( fseek( fin, 0, SEEK_SET ) < 0 )
216 {
Rich Evansf90016a2015-01-19 14:26:37 +0000217 polarssl_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 goto exit;
219 }
220
221 if( mode == MODE_ENCRYPT )
222 {
223 /*
224 * Generate the initialization vector as:
225 * IV = SHA-256( filesize || filename )[0..15]
226 */
227 for( i = 0; i < 8; i++ )
228 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
229
230 p = argv[2];
231
Paul Bakker9e36f042013-06-30 14:34:05 +0200232 sha256_starts( &sha_ctx, 0 );
233 sha256_update( &sha_ctx, buffer, 8 );
234 sha256_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
235 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
237 memcpy( IV, digest, 16 );
238
239 /*
240 * The last four bits in the IV are actually used
241 * to store the file size modulo the AES block size.
242 */
243 lastn = (int)( filesize & 0x0F );
244
245 IV[15] = (unsigned char)
246 ( ( IV[15] & 0xF0 ) | lastn );
247
248 /*
249 * Append the IV at the beginning of the output.
250 */
251 if( fwrite( IV, 1, 16, fout ) != 16 )
252 {
Rich Evansf90016a2015-01-19 14:26:37 +0000253 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 goto exit;
255 }
256
257 /*
258 * Hash the IV and the secret key together 8192 times
259 * using the result to setup the AES context and HMAC.
260 */
261 memset( digest, 0, 32 );
262 memcpy( digest, IV, 16 );
263
264 for( i = 0; i < 8192; i++ )
265 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200266 sha256_starts( &sha_ctx, 0 );
267 sha256_update( &sha_ctx, digest, 32 );
268 sha256_update( &sha_ctx, key, keylen );
269 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 }
271
272 memset( key, 0, sizeof( key ) );
Paul Bakker9e36f042013-06-30 14:34:05 +0200273 aes_setkey_enc( &aes_ctx, digest, 256 );
274 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
276 /*
277 * Encrypt and write the ciphertext.
278 */
279 for( offset = 0; offset < filesize; offset += 16 )
280 {
281 n = ( filesize - offset > 16 ) ? 16 : (int)
282 ( filesize - offset );
283
284 if( fread( buffer, 1, n, fin ) != (size_t) n )
285 {
Rich Evansf90016a2015-01-19 14:26:37 +0000286 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 goto exit;
288 }
289
290 for( i = 0; i < 16; i++ )
291 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
292
293 aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200294 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
296 if( fwrite( buffer, 1, 16, fout ) != 16 )
297 {
Rich Evansf90016a2015-01-19 14:26:37 +0000298 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 goto exit;
300 }
301
302 memcpy( IV, buffer, 16 );
303 }
304
305 /*
306 * Finally write the HMAC.
307 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200308 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
310 if( fwrite( digest, 1, 32, fout ) != 32 )
311 {
Rich Evansf90016a2015-01-19 14:26:37 +0000312 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 goto exit;
314 }
315 }
316
317 if( mode == MODE_DECRYPT )
318 {
319 unsigned char tmp[16];
320
321 /*
322 * The encrypted file must be structured as follows:
323 *
324 * 00 .. 15 Initialization Vector
325 * 16 .. 31 AES Encrypted Block #1
326 * ..
327 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
328 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
329 */
330 if( filesize < 48 )
331 {
Rich Evansf90016a2015-01-19 14:26:37 +0000332 polarssl_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 goto exit;
334 }
335
336 if( ( filesize & 0x0F ) != 0 )
337 {
Rich Evansf90016a2015-01-19 14:26:37 +0000338 polarssl_fprintf( stderr, "File size not a multiple of 16.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 goto exit;
340 }
341
342 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100343 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 */
345 filesize -= ( 16 + 32 );
346
347 /*
348 * Read the IV and original filesize modulo 16.
349 */
350 if( fread( buffer, 1, 16, fin ) != 16 )
351 {
Rich Evansf90016a2015-01-19 14:26:37 +0000352 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353 goto exit;
354 }
355
356 memcpy( IV, buffer, 16 );
357 lastn = IV[15] & 0x0F;
358
359 /*
360 * Hash the IV and the secret key together 8192 times
361 * using the result to setup the AES context and HMAC.
362 */
363 memset( digest, 0, 32 );
364 memcpy( digest, IV, 16 );
365
366 for( i = 0; i < 8192; i++ )
367 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200368 sha256_starts( &sha_ctx, 0 );
369 sha256_update( &sha_ctx, digest, 32 );
370 sha256_update( &sha_ctx, key, keylen );
371 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000372 }
373
374 memset( key, 0, sizeof( key ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200375 aes_setkey_dec( &aes_ctx, digest, 256 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200376 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
378 /*
379 * Decrypt and write the plaintext.
380 */
381 for( offset = 0; offset < filesize; offset += 16 )
382 {
383 if( fread( buffer, 1, 16, fin ) != 16 )
384 {
Rich Evansf90016a2015-01-19 14:26:37 +0000385 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 goto exit;
387 }
388
389 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200390
391 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200393
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 for( i = 0; i < 16; i++ )
395 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
396
397 memcpy( IV, tmp, 16 );
398
399 n = ( lastn > 0 && offset == filesize - 16 )
400 ? lastn : 16;
401
402 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
403 {
Rich Evansf90016a2015-01-19 14:26:37 +0000404 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 goto exit;
406 }
407 }
408
409 /*
410 * Verify the message authentication code.
411 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200412 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000413
414 if( fread( buffer, 1, 32, fin ) != 32 )
415 {
Rich Evansf90016a2015-01-19 14:26:37 +0000416 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417 goto exit;
418 }
419
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100420 /* Use constant-time buffer comparison */
421 diff = 0;
422 for( i = 0; i < 32; i++ )
423 diff |= digest[i] ^ buffer[i];
424
425 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000426 {
Rich Evansf90016a2015-01-19 14:26:37 +0000427 polarssl_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 "or file corrupted.\n" );
429 goto exit;
430 }
431 }
432
433 ret = 0;
434
435exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000436 if( fin )
437 fclose( fin );
438 if( fout )
439 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000440
441 memset( buffer, 0, sizeof( buffer ) );
442 memset( digest, 0, sizeof( digest ) );
443
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200444 aes_free( &aes_ctx );
Paul Bakker14e8be42014-06-26 12:25:06 +0200445 sha256_free( &sha_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
447 return( ret );
448}
Rich Evans18b78c72015-02-11 14:06:19 +0000449#endif /* POLARSSL_AES_C && POLARSSL_SHA256_C && POLARSSL_FS_IO */