blob: 40b7fec382a5e35761f4e7a5adadee8f2737e978 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * AES-256 file encryption program
3 *
Paul Bakker9e36f042013-06-30 14:34:05 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker494c0b82011-04-24 15:30:07 +000032#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000033#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000034#if !defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000035#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000036#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000037#else
38#include <sys/types.h>
39#include <unistd.h>
40#endif
41
42#include <string.h>
43#include <stdlib.h>
44#include <stdio.h>
45#include <time.h>
46
Paul Bakker40e46942009-01-03 21:51:57 +000047#include "polarssl/aes.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020048#include "polarssl/sha256.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000049
50#define MODE_ENCRYPT 0
51#define MODE_DECRYPT 1
52
53#define USAGE \
54 "\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
55 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
56 "\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
57 "\n"
58
Paul Bakker9e36f042013-06-30 14:34:05 +020059#if !defined(POLARSSL_AES_C) || !defined(POLARSSL_SHA256_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000060int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000061{
Paul Bakkercce9d772011-11-18 14:26:47 +000062 ((void) argc);
63 ((void) argv);
Paul Bakker9e36f042013-06-30 14:34:05 +020064 printf("POLARSSL_AES_C and/or POLARSSL_SHA256_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000065 return( 0 );
66}
67#else
Paul Bakker5121ce52009-01-03 21:22:43 +000068int main( int argc, char *argv[] )
69{
Paul Bakker5690efc2011-05-26 13:16:06 +000070 int ret = 1;
71
72 int i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000073 int mode, lastn;
74 size_t keylen;
Paul Bakker20a78082011-01-21 09:32:12 +000075 FILE *fkey, *fin = NULL, *fout = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000076
77 char *p;
78 unsigned char IV[16];
79 unsigned char key[512];
80 unsigned char digest[32];
81 unsigned char buffer[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010082 unsigned char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +000083
84 aes_context aes_ctx;
Paul Bakker9e36f042013-06-30 14:34:05 +020085 sha256_context sha_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Paul Bakkercce9d772011-11-18 14:26:47 +000087#if defined(_WIN32_WCE)
88 long filesize, offset;
89#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000090 LARGE_INTEGER li_size;
91 __int64 filesize, offset;
92#else
93 off_t filesize, offset;
94#endif
95
Paul Bakker8cfd9d82014-06-18 11:16:11 +020096 aes_init( &aes_ctx );
Paul Bakker14e8be42014-06-26 12:25:06 +020097 sha256_init( &sha_ctx );
Paul Bakker8cfd9d82014-06-18 11:16:11 +020098
Paul Bakker5121ce52009-01-03 21:22:43 +000099 /*
100 * Parse the command-line arguments.
101 */
102 if( argc != 5 )
103 {
104 printf( USAGE );
105
Paul Bakkercce9d772011-11-18 14:26:47 +0000106#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 printf( "\n Press Enter to exit this program.\n" );
108 fflush( stdout ); getchar();
109#endif
110
111 goto exit;
112 }
113
114 mode = atoi( argv[1] );
115
116 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
117 {
118 fprintf( stderr, "invalide operation mode\n" );
119 goto exit;
120 }
121
122 if( strcmp( argv[2], argv[3] ) == 0 )
123 {
124 fprintf( stderr, "input and output filenames must differ\n" );
125 goto exit;
126 }
127
128 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
129 {
130 fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
131 goto exit;
132 }
133
134 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
135 {
136 fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
137 goto exit;
138 }
139
140 /*
141 * Read the secret key and clean the command line.
142 */
143 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
144 {
145 keylen = fread( key, 1, sizeof( key ), fkey );
146 fclose( fkey );
147 }
148 else
149 {
150 if( memcmp( argv[4], "hex:", 4 ) == 0 )
151 {
152 p = &argv[4][4];
153 keylen = 0;
154
155 while( sscanf( p, "%02X", &n ) > 0 &&
156 keylen < (int) sizeof( key ) )
157 {
158 key[keylen++] = (unsigned char) n;
159 p += 2;
160 }
161 }
162 else
163 {
164 keylen = strlen( argv[4] );
165
166 if( keylen > (int) sizeof( key ) )
167 keylen = (int) sizeof( key );
168
169 memcpy( key, argv[4], keylen );
170 }
171 }
172
173 memset( argv[4], 0, strlen( argv[4] ) );
174
Paul Bakkercce9d772011-11-18 14:26:47 +0000175#if defined(_WIN32_WCE)
176 filesize = fseek( fin, 0L, SEEK_END );
177#else
178#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 /*
180 * Support large files (> 2Gb) on Win32
181 */
182 li_size.QuadPart = 0;
183 li_size.LowPart =
184 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
185 li_size.LowPart, &li_size.HighPart, FILE_END );
186
187 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
188 {
189 fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
190 goto exit;
191 }
192
193 filesize = li_size.QuadPart;
194#else
195 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
196 {
197 perror( "lseek" );
198 goto exit;
199 }
200#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000201#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203 if( fseek( fin, 0, SEEK_SET ) < 0 )
204 {
205 fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
206 goto exit;
207 }
208
209 if( mode == MODE_ENCRYPT )
210 {
211 /*
212 * Generate the initialization vector as:
213 * IV = SHA-256( filesize || filename )[0..15]
214 */
215 for( i = 0; i < 8; i++ )
216 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
217
218 p = argv[2];
219
Paul Bakker9e36f042013-06-30 14:34:05 +0200220 sha256_starts( &sha_ctx, 0 );
221 sha256_update( &sha_ctx, buffer, 8 );
222 sha256_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
223 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
225 memcpy( IV, digest, 16 );
226
227 /*
228 * The last four bits in the IV are actually used
229 * to store the file size modulo the AES block size.
230 */
231 lastn = (int)( filesize & 0x0F );
232
233 IV[15] = (unsigned char)
234 ( ( IV[15] & 0xF0 ) | lastn );
235
236 /*
237 * Append the IV at the beginning of the output.
238 */
239 if( fwrite( IV, 1, 16, fout ) != 16 )
240 {
241 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
242 goto exit;
243 }
244
245 /*
246 * Hash the IV and the secret key together 8192 times
247 * using the result to setup the AES context and HMAC.
248 */
249 memset( digest, 0, 32 );
250 memcpy( digest, IV, 16 );
251
252 for( i = 0; i < 8192; i++ )
253 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200254 sha256_starts( &sha_ctx, 0 );
255 sha256_update( &sha_ctx, digest, 32 );
256 sha256_update( &sha_ctx, key, keylen );
257 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 }
259
260 memset( key, 0, sizeof( key ) );
Paul Bakker9e36f042013-06-30 14:34:05 +0200261 aes_setkey_enc( &aes_ctx, digest, 256 );
262 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000263
264 /*
265 * Encrypt and write the ciphertext.
266 */
267 for( offset = 0; offset < filesize; offset += 16 )
268 {
269 n = ( filesize - offset > 16 ) ? 16 : (int)
270 ( filesize - offset );
271
272 if( fread( buffer, 1, n, fin ) != (size_t) n )
273 {
274 fprintf( stderr, "fread(%d bytes) failed\n", n );
275 goto exit;
276 }
277
278 for( i = 0; i < 16; i++ )
279 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
280
281 aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200282 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
284 if( fwrite( buffer, 1, 16, fout ) != 16 )
285 {
286 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
287 goto exit;
288 }
289
290 memcpy( IV, buffer, 16 );
291 }
292
293 /*
294 * Finally write the HMAC.
295 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200296 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
298 if( fwrite( digest, 1, 32, fout ) != 32 )
299 {
300 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
301 goto exit;
302 }
303 }
304
305 if( mode == MODE_DECRYPT )
306 {
307 unsigned char tmp[16];
308
309 /*
310 * The encrypted file must be structured as follows:
311 *
312 * 00 .. 15 Initialization Vector
313 * 16 .. 31 AES Encrypted Block #1
314 * ..
315 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
316 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
317 */
318 if( filesize < 48 )
319 {
320 fprintf( stderr, "File too short to be encrypted.\n" );
321 goto exit;
322 }
323
324 if( ( filesize & 0x0F ) != 0 )
325 {
326 fprintf( stderr, "File size not a multiple of 16.\n" );
327 goto exit;
328 }
329
330 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100331 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 */
333 filesize -= ( 16 + 32 );
334
335 /*
336 * Read the IV and original filesize modulo 16.
337 */
338 if( fread( buffer, 1, 16, fin ) != 16 )
339 {
340 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
341 goto exit;
342 }
343
344 memcpy( IV, buffer, 16 );
345 lastn = IV[15] & 0x0F;
346
347 /*
348 * Hash the IV and the secret key together 8192 times
349 * using the result to setup the AES context and HMAC.
350 */
351 memset( digest, 0, 32 );
352 memcpy( digest, IV, 16 );
353
354 for( i = 0; i < 8192; i++ )
355 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200356 sha256_starts( &sha_ctx, 0 );
357 sha256_update( &sha_ctx, digest, 32 );
358 sha256_update( &sha_ctx, key, keylen );
359 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 }
361
362 memset( key, 0, sizeof( key ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200363 aes_setkey_dec( &aes_ctx, digest, 256 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200364 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000365
366 /*
367 * Decrypt and write the plaintext.
368 */
369 for( offset = 0; offset < filesize; offset += 16 )
370 {
371 if( fread( buffer, 1, 16, fin ) != 16 )
372 {
373 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
374 goto exit;
375 }
376
377 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200378
379 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200381
Paul Bakker5121ce52009-01-03 21:22:43 +0000382 for( i = 0; i < 16; i++ )
383 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
384
385 memcpy( IV, tmp, 16 );
386
387 n = ( lastn > 0 && offset == filesize - 16 )
388 ? lastn : 16;
389
390 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
391 {
392 fprintf( stderr, "fwrite(%d bytes) failed\n", n );
393 goto exit;
394 }
395 }
396
397 /*
398 * Verify the message authentication code.
399 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200400 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000401
402 if( fread( buffer, 1, 32, fin ) != 32 )
403 {
404 fprintf( stderr, "fread(%d bytes) failed\n", 32 );
405 goto exit;
406 }
407
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100408 /* Use constant-time buffer comparison */
409 diff = 0;
410 for( i = 0; i < 32; i++ )
411 diff |= digest[i] ^ buffer[i];
412
413 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000414 {
415 fprintf( stderr, "HMAC check failed: wrong key, "
416 "or file corrupted.\n" );
417 goto exit;
418 }
419 }
420
421 ret = 0;
422
423exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000424 if( fin )
425 fclose( fin );
426 if( fout )
427 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000428
429 memset( buffer, 0, sizeof( buffer ) );
430 memset( digest, 0, sizeof( digest ) );
431
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200432 aes_free( &aes_ctx );
Paul Bakker14e8be42014-06-26 12:25:06 +0200433 sha256_free( &sha_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000434
435 return( ret );
436}
Paul Bakker9e36f042013-06-30 14:34:05 +0200437#endif /* POLARSSL_AES_C && POLARSSL_SHA256_C */