blob: 28f74d1a416089370d682ee033e9e7ab5306e611 [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
96 /*
97 * Parse the command-line arguments.
98 */
99 if( argc != 5 )
100 {
101 printf( USAGE );
102
Paul Bakkercce9d772011-11-18 14:26:47 +0000103#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 printf( "\n Press Enter to exit this program.\n" );
105 fflush( stdout ); getchar();
106#endif
107
108 goto exit;
109 }
110
111 mode = atoi( argv[1] );
112
113 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
114 {
115 fprintf( stderr, "invalide operation mode\n" );
116 goto exit;
117 }
118
119 if( strcmp( argv[2], argv[3] ) == 0 )
120 {
121 fprintf( stderr, "input and output filenames must differ\n" );
122 goto exit;
123 }
124
125 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
126 {
127 fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
128 goto exit;
129 }
130
131 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
132 {
133 fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
134 goto exit;
135 }
136
137 /*
138 * Read the secret key and clean the command line.
139 */
140 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
141 {
142 keylen = fread( key, 1, sizeof( key ), fkey );
143 fclose( fkey );
144 }
145 else
146 {
147 if( memcmp( argv[4], "hex:", 4 ) == 0 )
148 {
149 p = &argv[4][4];
150 keylen = 0;
151
152 while( sscanf( p, "%02X", &n ) > 0 &&
153 keylen < (int) sizeof( key ) )
154 {
155 key[keylen++] = (unsigned char) n;
156 p += 2;
157 }
158 }
159 else
160 {
161 keylen = strlen( argv[4] );
162
163 if( keylen > (int) sizeof( key ) )
164 keylen = (int) sizeof( key );
165
166 memcpy( key, argv[4], keylen );
167 }
168 }
169
170 memset( argv[4], 0, strlen( argv[4] ) );
171
Paul Bakkercce9d772011-11-18 14:26:47 +0000172#if defined(_WIN32_WCE)
173 filesize = fseek( fin, 0L, SEEK_END );
174#else
175#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 /*
177 * Support large files (> 2Gb) on Win32
178 */
179 li_size.QuadPart = 0;
180 li_size.LowPart =
181 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
182 li_size.LowPart, &li_size.HighPart, FILE_END );
183
184 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
185 {
186 fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
187 goto exit;
188 }
189
190 filesize = li_size.QuadPart;
191#else
192 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
193 {
194 perror( "lseek" );
195 goto exit;
196 }
197#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000198#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
200 if( fseek( fin, 0, SEEK_SET ) < 0 )
201 {
202 fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
203 goto exit;
204 }
205
206 if( mode == MODE_ENCRYPT )
207 {
208 /*
209 * Generate the initialization vector as:
210 * IV = SHA-256( filesize || filename )[0..15]
211 */
212 for( i = 0; i < 8; i++ )
213 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
214
215 p = argv[2];
216
Paul Bakker9e36f042013-06-30 14:34:05 +0200217 sha256_starts( &sha_ctx, 0 );
218 sha256_update( &sha_ctx, buffer, 8 );
219 sha256_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
220 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
222 memcpy( IV, digest, 16 );
223
224 /*
225 * The last four bits in the IV are actually used
226 * to store the file size modulo the AES block size.
227 */
228 lastn = (int)( filesize & 0x0F );
229
230 IV[15] = (unsigned char)
231 ( ( IV[15] & 0xF0 ) | lastn );
232
233 /*
234 * Append the IV at the beginning of the output.
235 */
236 if( fwrite( IV, 1, 16, fout ) != 16 )
237 {
238 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
239 goto exit;
240 }
241
242 /*
243 * Hash the IV and the secret key together 8192 times
244 * using the result to setup the AES context and HMAC.
245 */
246 memset( digest, 0, 32 );
247 memcpy( digest, IV, 16 );
248
249 for( i = 0; i < 8192; i++ )
250 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200251 sha256_starts( &sha_ctx, 0 );
252 sha256_update( &sha_ctx, digest, 32 );
253 sha256_update( &sha_ctx, key, keylen );
254 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 }
256
257 memset( key, 0, sizeof( key ) );
Paul Bakker9e36f042013-06-30 14:34:05 +0200258 aes_setkey_enc( &aes_ctx, digest, 256 );
259 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
261 /*
262 * Encrypt and write the ciphertext.
263 */
264 for( offset = 0; offset < filesize; offset += 16 )
265 {
266 n = ( filesize - offset > 16 ) ? 16 : (int)
267 ( filesize - offset );
268
269 if( fread( buffer, 1, n, fin ) != (size_t) n )
270 {
271 fprintf( stderr, "fread(%d bytes) failed\n", n );
272 goto exit;
273 }
274
275 for( i = 0; i < 16; i++ )
276 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
277
278 aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200279 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000280
281 if( fwrite( buffer, 1, 16, fout ) != 16 )
282 {
283 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
284 goto exit;
285 }
286
287 memcpy( IV, buffer, 16 );
288 }
289
290 /*
291 * Finally write the HMAC.
292 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200293 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000294
295 if( fwrite( digest, 1, 32, fout ) != 32 )
296 {
297 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
298 goto exit;
299 }
300 }
301
302 if( mode == MODE_DECRYPT )
303 {
304 unsigned char tmp[16];
305
306 /*
307 * The encrypted file must be structured as follows:
308 *
309 * 00 .. 15 Initialization Vector
310 * 16 .. 31 AES Encrypted Block #1
311 * ..
312 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
313 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
314 */
315 if( filesize < 48 )
316 {
317 fprintf( stderr, "File too short to be encrypted.\n" );
318 goto exit;
319 }
320
321 if( ( filesize & 0x0F ) != 0 )
322 {
323 fprintf( stderr, "File size not a multiple of 16.\n" );
324 goto exit;
325 }
326
327 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100328 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 */
330 filesize -= ( 16 + 32 );
331
332 /*
333 * Read the IV and original filesize modulo 16.
334 */
335 if( fread( buffer, 1, 16, fin ) != 16 )
336 {
337 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
338 goto exit;
339 }
340
341 memcpy( IV, buffer, 16 );
342 lastn = IV[15] & 0x0F;
343
344 /*
345 * Hash the IV and the secret key together 8192 times
346 * using the result to setup the AES context and HMAC.
347 */
348 memset( digest, 0, 32 );
349 memcpy( digest, IV, 16 );
350
351 for( i = 0; i < 8192; i++ )
352 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200353 sha256_starts( &sha_ctx, 0 );
354 sha256_update( &sha_ctx, digest, 32 );
355 sha256_update( &sha_ctx, key, keylen );
356 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000357 }
358
359 memset( key, 0, sizeof( key ) );
360 aes_setkey_dec( &aes_ctx, digest, 256 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200361 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000362
363 /*
364 * Decrypt and write the plaintext.
365 */
366 for( offset = 0; offset < filesize; offset += 16 )
367 {
368 if( fread( buffer, 1, 16, fin ) != 16 )
369 {
370 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
371 goto exit;
372 }
373
374 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200375
376 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200378
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 for( i = 0; i < 16; i++ )
380 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
381
382 memcpy( IV, tmp, 16 );
383
384 n = ( lastn > 0 && offset == filesize - 16 )
385 ? lastn : 16;
386
387 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
388 {
389 fprintf( stderr, "fwrite(%d bytes) failed\n", n );
390 goto exit;
391 }
392 }
393
394 /*
395 * Verify the message authentication code.
396 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200397 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000398
399 if( fread( buffer, 1, 32, fin ) != 32 )
400 {
401 fprintf( stderr, "fread(%d bytes) failed\n", 32 );
402 goto exit;
403 }
404
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100405 /* Use constant-time buffer comparison */
406 diff = 0;
407 for( i = 0; i < 32; i++ )
408 diff |= digest[i] ^ buffer[i];
409
410 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 {
412 fprintf( stderr, "HMAC check failed: wrong key, "
413 "or file corrupted.\n" );
414 goto exit;
415 }
416 }
417
418 ret = 0;
419
420exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000421 if( fin )
422 fclose( fin );
423 if( fout )
424 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426 memset( buffer, 0, sizeof( buffer ) );
427 memset( digest, 0, sizeof( digest ) );
428
429 memset( &aes_ctx, 0, sizeof( aes_context ) );
Paul Bakker9e36f042013-06-30 14:34:05 +0200430 memset( &sha_ctx, 0, sizeof( sha256_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000431
432 return( ret );
433}
Paul Bakker9e36f042013-06-30 14:34:05 +0200434#endif /* POLARSSL_AES_C && POLARSSL_SHA256_C */