blob: db7ed325e7c40cf182fbef1babb4baf72a1f8060 [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 );
97
Paul Bakker5121ce52009-01-03 21:22:43 +000098 /*
99 * Parse the command-line arguments.
100 */
101 if( argc != 5 )
102 {
103 printf( USAGE );
104
Paul Bakkercce9d772011-11-18 14:26:47 +0000105#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 printf( "\n Press Enter to exit this program.\n" );
107 fflush( stdout ); getchar();
108#endif
109
110 goto exit;
111 }
112
113 mode = atoi( argv[1] );
114
115 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
116 {
117 fprintf( stderr, "invalide operation mode\n" );
118 goto exit;
119 }
120
121 if( strcmp( argv[2], argv[3] ) == 0 )
122 {
123 fprintf( stderr, "input and output filenames must differ\n" );
124 goto exit;
125 }
126
127 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
128 {
129 fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
130 goto exit;
131 }
132
133 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
134 {
135 fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
136 goto exit;
137 }
138
139 /*
140 * Read the secret key and clean the command line.
141 */
142 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
143 {
144 keylen = fread( key, 1, sizeof( key ), fkey );
145 fclose( fkey );
146 }
147 else
148 {
149 if( memcmp( argv[4], "hex:", 4 ) == 0 )
150 {
151 p = &argv[4][4];
152 keylen = 0;
153
154 while( sscanf( p, "%02X", &n ) > 0 &&
155 keylen < (int) sizeof( key ) )
156 {
157 key[keylen++] = (unsigned char) n;
158 p += 2;
159 }
160 }
161 else
162 {
163 keylen = strlen( argv[4] );
164
165 if( keylen > (int) sizeof( key ) )
166 keylen = (int) sizeof( key );
167
168 memcpy( key, argv[4], keylen );
169 }
170 }
171
172 memset( argv[4], 0, strlen( argv[4] ) );
173
Paul Bakkercce9d772011-11-18 14:26:47 +0000174#if defined(_WIN32_WCE)
175 filesize = fseek( fin, 0L, SEEK_END );
176#else
177#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 /*
179 * Support large files (> 2Gb) on Win32
180 */
181 li_size.QuadPart = 0;
182 li_size.LowPart =
183 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
184 li_size.LowPart, &li_size.HighPart, FILE_END );
185
186 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
187 {
188 fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
189 goto exit;
190 }
191
192 filesize = li_size.QuadPart;
193#else
194 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
195 {
196 perror( "lseek" );
197 goto exit;
198 }
199#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000200#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
202 if( fseek( fin, 0, SEEK_SET ) < 0 )
203 {
204 fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
205 goto exit;
206 }
207
208 if( mode == MODE_ENCRYPT )
209 {
210 /*
211 * Generate the initialization vector as:
212 * IV = SHA-256( filesize || filename )[0..15]
213 */
214 for( i = 0; i < 8; i++ )
215 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
216
217 p = argv[2];
218
Paul Bakker9e36f042013-06-30 14:34:05 +0200219 sha256_starts( &sha_ctx, 0 );
220 sha256_update( &sha_ctx, buffer, 8 );
221 sha256_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
222 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
224 memcpy( IV, digest, 16 );
225
226 /*
227 * The last four bits in the IV are actually used
228 * to store the file size modulo the AES block size.
229 */
230 lastn = (int)( filesize & 0x0F );
231
232 IV[15] = (unsigned char)
233 ( ( IV[15] & 0xF0 ) | lastn );
234
235 /*
236 * Append the IV at the beginning of the output.
237 */
238 if( fwrite( IV, 1, 16, fout ) != 16 )
239 {
240 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
241 goto exit;
242 }
243
244 /*
245 * Hash the IV and the secret key together 8192 times
246 * using the result to setup the AES context and HMAC.
247 */
248 memset( digest, 0, 32 );
249 memcpy( digest, IV, 16 );
250
251 for( i = 0; i < 8192; i++ )
252 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200253 sha256_starts( &sha_ctx, 0 );
254 sha256_update( &sha_ctx, digest, 32 );
255 sha256_update( &sha_ctx, key, keylen );
256 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 }
258
259 memset( key, 0, sizeof( key ) );
Paul Bakker9e36f042013-06-30 14:34:05 +0200260 aes_setkey_enc( &aes_ctx, digest, 256 );
261 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000262
263 /*
264 * Encrypt and write the ciphertext.
265 */
266 for( offset = 0; offset < filesize; offset += 16 )
267 {
268 n = ( filesize - offset > 16 ) ? 16 : (int)
269 ( filesize - offset );
270
271 if( fread( buffer, 1, n, fin ) != (size_t) n )
272 {
273 fprintf( stderr, "fread(%d bytes) failed\n", n );
274 goto exit;
275 }
276
277 for( i = 0; i < 16; i++ )
278 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
279
280 aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200281 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000282
283 if( fwrite( buffer, 1, 16, fout ) != 16 )
284 {
285 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
286 goto exit;
287 }
288
289 memcpy( IV, buffer, 16 );
290 }
291
292 /*
293 * Finally write the HMAC.
294 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200295 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
297 if( fwrite( digest, 1, 32, fout ) != 32 )
298 {
299 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
300 goto exit;
301 }
302 }
303
304 if( mode == MODE_DECRYPT )
305 {
306 unsigned char tmp[16];
307
308 /*
309 * The encrypted file must be structured as follows:
310 *
311 * 00 .. 15 Initialization Vector
312 * 16 .. 31 AES Encrypted Block #1
313 * ..
314 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
315 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
316 */
317 if( filesize < 48 )
318 {
319 fprintf( stderr, "File too short to be encrypted.\n" );
320 goto exit;
321 }
322
323 if( ( filesize & 0x0F ) != 0 )
324 {
325 fprintf( stderr, "File size not a multiple of 16.\n" );
326 goto exit;
327 }
328
329 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100330 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 */
332 filesize -= ( 16 + 32 );
333
334 /*
335 * Read the IV and original filesize modulo 16.
336 */
337 if( fread( buffer, 1, 16, fin ) != 16 )
338 {
339 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
340 goto exit;
341 }
342
343 memcpy( IV, buffer, 16 );
344 lastn = IV[15] & 0x0F;
345
346 /*
347 * Hash the IV and the secret key together 8192 times
348 * using the result to setup the AES context and HMAC.
349 */
350 memset( digest, 0, 32 );
351 memcpy( digest, IV, 16 );
352
353 for( i = 0; i < 8192; i++ )
354 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200355 sha256_starts( &sha_ctx, 0 );
356 sha256_update( &sha_ctx, digest, 32 );
357 sha256_update( &sha_ctx, key, keylen );
358 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000359 }
360
361 memset( key, 0, sizeof( key ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200362 aes_setkey_dec( &aes_ctx, digest, 256 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200363 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000364
365 /*
366 * Decrypt and write the plaintext.
367 */
368 for( offset = 0; offset < filesize; offset += 16 )
369 {
370 if( fread( buffer, 1, 16, fin ) != 16 )
371 {
372 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
373 goto exit;
374 }
375
376 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200377
378 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200380
Paul Bakker5121ce52009-01-03 21:22:43 +0000381 for( i = 0; i < 16; i++ )
382 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
383
384 memcpy( IV, tmp, 16 );
385
386 n = ( lastn > 0 && offset == filesize - 16 )
387 ? lastn : 16;
388
389 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
390 {
391 fprintf( stderr, "fwrite(%d bytes) failed\n", n );
392 goto exit;
393 }
394 }
395
396 /*
397 * Verify the message authentication code.
398 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200399 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000400
401 if( fread( buffer, 1, 32, fin ) != 32 )
402 {
403 fprintf( stderr, "fread(%d bytes) failed\n", 32 );
404 goto exit;
405 }
406
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100407 /* Use constant-time buffer comparison */
408 diff = 0;
409 for( i = 0; i < 32; i++ )
410 diff |= digest[i] ^ buffer[i];
411
412 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 {
414 fprintf( stderr, "HMAC check failed: wrong key, "
415 "or file corrupted.\n" );
416 goto exit;
417 }
418 }
419
420 ret = 0;
421
422exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000423 if( fin )
424 fclose( fin );
425 if( fout )
426 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000427
428 memset( buffer, 0, sizeof( buffer ) );
429 memset( digest, 0, sizeof( digest ) );
430
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200431 aes_free( &aes_ctx );
Paul Bakker9e36f042013-06-30 14:34:05 +0200432 memset( &sha_ctx, 0, sizeof( sha256_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
434 return( ret );
435}
Paul Bakker9e36f042013-06-30 14:34:05 +0200436#endif /* POLARSSL_AES_C && POLARSSL_SHA256_C */