blob: 1239ca202e3f6628677dad839d301d8bf2d56400 [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é-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakker494c0b82011-04-24 15:30:07 +000028#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000029#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000030#if !defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000031#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000032#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000033#else
34#include <sys/types.h>
35#include <unistd.h>
36#endif
37
38#include <string.h>
39#include <stdlib.h>
40#include <stdio.h>
41#include <time.h>
42
Paul Bakker40e46942009-01-03 21:51:57 +000043#include "polarssl/aes.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020044#include "polarssl/sha256.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000045
46#define MODE_ENCRYPT 0
47#define MODE_DECRYPT 1
48
49#define USAGE \
50 "\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
51 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
52 "\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
53 "\n"
54
Paul Bakker9e36f042013-06-30 14:34:05 +020055#if !defined(POLARSSL_AES_C) || !defined(POLARSSL_SHA256_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000056int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000057{
Paul Bakkercce9d772011-11-18 14:26:47 +000058 ((void) argc);
59 ((void) argv);
Paul Bakker9e36f042013-06-30 14:34:05 +020060 printf("POLARSSL_AES_C and/or POLARSSL_SHA256_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000061 return( 0 );
62}
63#else
Paul Bakker5121ce52009-01-03 21:22:43 +000064int main( int argc, char *argv[] )
65{
Paul Bakker5690efc2011-05-26 13:16:06 +000066 int ret = 1;
67
68 int i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000069 int mode, lastn;
70 size_t keylen;
Paul Bakker20a78082011-01-21 09:32:12 +000071 FILE *fkey, *fin = NULL, *fout = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000072
73 char *p;
74 unsigned char IV[16];
75 unsigned char key[512];
76 unsigned char digest[32];
77 unsigned char buffer[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010078 unsigned char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +000079
80 aes_context aes_ctx;
Paul Bakker9e36f042013-06-30 14:34:05 +020081 sha256_context sha_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Paul Bakkercce9d772011-11-18 14:26:47 +000083#if defined(_WIN32_WCE)
84 long filesize, offset;
85#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000086 LARGE_INTEGER li_size;
87 __int64 filesize, offset;
88#else
89 off_t filesize, offset;
90#endif
91
92 /*
93 * Parse the command-line arguments.
94 */
95 if( argc != 5 )
96 {
97 printf( USAGE );
98
Paul Bakkercce9d772011-11-18 14:26:47 +000099#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 printf( "\n Press Enter to exit this program.\n" );
101 fflush( stdout ); getchar();
102#endif
103
104 goto exit;
105 }
106
107 mode = atoi( argv[1] );
108
109 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
110 {
111 fprintf( stderr, "invalide operation mode\n" );
112 goto exit;
113 }
114
115 if( strcmp( argv[2], argv[3] ) == 0 )
116 {
117 fprintf( stderr, "input and output filenames must differ\n" );
118 goto exit;
119 }
120
121 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
122 {
123 fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
124 goto exit;
125 }
126
127 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
128 {
129 fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
130 goto exit;
131 }
132
133 /*
134 * Read the secret key and clean the command line.
135 */
136 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
137 {
138 keylen = fread( key, 1, sizeof( key ), fkey );
139 fclose( fkey );
140 }
141 else
142 {
143 if( memcmp( argv[4], "hex:", 4 ) == 0 )
144 {
145 p = &argv[4][4];
146 keylen = 0;
147
148 while( sscanf( p, "%02X", &n ) > 0 &&
149 keylen < (int) sizeof( key ) )
150 {
151 key[keylen++] = (unsigned char) n;
152 p += 2;
153 }
154 }
155 else
156 {
157 keylen = strlen( argv[4] );
158
159 if( keylen > (int) sizeof( key ) )
160 keylen = (int) sizeof( key );
161
162 memcpy( key, argv[4], keylen );
163 }
164 }
165
166 memset( argv[4], 0, strlen( argv[4] ) );
167
Paul Bakkercce9d772011-11-18 14:26:47 +0000168#if defined(_WIN32_WCE)
169 filesize = fseek( fin, 0L, SEEK_END );
170#else
171#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 /*
173 * Support large files (> 2Gb) on Win32
174 */
175 li_size.QuadPart = 0;
176 li_size.LowPart =
177 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
178 li_size.LowPart, &li_size.HighPart, FILE_END );
179
180 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
181 {
182 fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
183 goto exit;
184 }
185
186 filesize = li_size.QuadPart;
187#else
188 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
189 {
190 perror( "lseek" );
191 goto exit;
192 }
193#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000194#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
196 if( fseek( fin, 0, SEEK_SET ) < 0 )
197 {
198 fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
199 goto exit;
200 }
201
202 if( mode == MODE_ENCRYPT )
203 {
204 /*
205 * Generate the initialization vector as:
206 * IV = SHA-256( filesize || filename )[0..15]
207 */
208 for( i = 0; i < 8; i++ )
209 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
210
211 p = argv[2];
212
Paul Bakker9e36f042013-06-30 14:34:05 +0200213 sha256_starts( &sha_ctx, 0 );
214 sha256_update( &sha_ctx, buffer, 8 );
215 sha256_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
216 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
218 memcpy( IV, digest, 16 );
219
220 /*
221 * The last four bits in the IV are actually used
222 * to store the file size modulo the AES block size.
223 */
224 lastn = (int)( filesize & 0x0F );
225
226 IV[15] = (unsigned char)
227 ( ( IV[15] & 0xF0 ) | lastn );
228
229 /*
230 * Append the IV at the beginning of the output.
231 */
232 if( fwrite( IV, 1, 16, fout ) != 16 )
233 {
234 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
235 goto exit;
236 }
237
238 /*
239 * Hash the IV and the secret key together 8192 times
240 * using the result to setup the AES context and HMAC.
241 */
242 memset( digest, 0, 32 );
243 memcpy( digest, IV, 16 );
244
245 for( i = 0; i < 8192; i++ )
246 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200247 sha256_starts( &sha_ctx, 0 );
248 sha256_update( &sha_ctx, digest, 32 );
249 sha256_update( &sha_ctx, key, keylen );
250 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 }
252
253 memset( key, 0, sizeof( key ) );
Paul Bakker9e36f042013-06-30 14:34:05 +0200254 aes_setkey_enc( &aes_ctx, digest, 256 );
255 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
257 /*
258 * Encrypt and write the ciphertext.
259 */
260 for( offset = 0; offset < filesize; offset += 16 )
261 {
262 n = ( filesize - offset > 16 ) ? 16 : (int)
263 ( filesize - offset );
264
265 if( fread( buffer, 1, n, fin ) != (size_t) n )
266 {
267 fprintf( stderr, "fread(%d bytes) failed\n", n );
268 goto exit;
269 }
270
271 for( i = 0; i < 16; i++ )
272 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
273
274 aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200275 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000276
277 if( fwrite( buffer, 1, 16, fout ) != 16 )
278 {
279 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
280 goto exit;
281 }
282
283 memcpy( IV, buffer, 16 );
284 }
285
286 /*
287 * Finally write the HMAC.
288 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200289 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
291 if( fwrite( digest, 1, 32, fout ) != 32 )
292 {
293 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
294 goto exit;
295 }
296 }
297
298 if( mode == MODE_DECRYPT )
299 {
300 unsigned char tmp[16];
301
302 /*
303 * The encrypted file must be structured as follows:
304 *
305 * 00 .. 15 Initialization Vector
306 * 16 .. 31 AES Encrypted Block #1
307 * ..
308 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
309 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
310 */
311 if( filesize < 48 )
312 {
313 fprintf( stderr, "File too short to be encrypted.\n" );
314 goto exit;
315 }
316
317 if( ( filesize & 0x0F ) != 0 )
318 {
319 fprintf( stderr, "File size not a multiple of 16.\n" );
320 goto exit;
321 }
322
323 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100324 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 */
326 filesize -= ( 16 + 32 );
327
328 /*
329 * Read the IV and original filesize modulo 16.
330 */
331 if( fread( buffer, 1, 16, fin ) != 16 )
332 {
333 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
334 goto exit;
335 }
336
337 memcpy( IV, buffer, 16 );
338 lastn = IV[15] & 0x0F;
339
340 /*
341 * Hash the IV and the secret key together 8192 times
342 * using the result to setup the AES context and HMAC.
343 */
344 memset( digest, 0, 32 );
345 memcpy( digest, IV, 16 );
346
347 for( i = 0; i < 8192; i++ )
348 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200349 sha256_starts( &sha_ctx, 0 );
350 sha256_update( &sha_ctx, digest, 32 );
351 sha256_update( &sha_ctx, key, keylen );
352 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353 }
354
355 memset( key, 0, sizeof( key ) );
356 aes_setkey_dec( &aes_ctx, digest, 256 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200357 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000358
359 /*
360 * Decrypt and write the plaintext.
361 */
362 for( offset = 0; offset < filesize; offset += 16 )
363 {
364 if( fread( buffer, 1, 16, fin ) != 16 )
365 {
366 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
367 goto exit;
368 }
369
370 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200371
372 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200374
Paul Bakker5121ce52009-01-03 21:22:43 +0000375 for( i = 0; i < 16; i++ )
376 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
377
378 memcpy( IV, tmp, 16 );
379
380 n = ( lastn > 0 && offset == filesize - 16 )
381 ? lastn : 16;
382
383 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
384 {
385 fprintf( stderr, "fwrite(%d bytes) failed\n", n );
386 goto exit;
387 }
388 }
389
390 /*
391 * Verify the message authentication code.
392 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200393 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000394
395 if( fread( buffer, 1, 32, fin ) != 32 )
396 {
397 fprintf( stderr, "fread(%d bytes) failed\n", 32 );
398 goto exit;
399 }
400
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100401 /* Use constant-time buffer comparison */
402 diff = 0;
403 for( i = 0; i < 32; i++ )
404 diff |= digest[i] ^ buffer[i];
405
406 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 {
408 fprintf( stderr, "HMAC check failed: wrong key, "
409 "or file corrupted.\n" );
410 goto exit;
411 }
412 }
413
414 ret = 0;
415
416exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000417 if( fin )
418 fclose( fin );
419 if( fout )
420 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000421
422 memset( buffer, 0, sizeof( buffer ) );
423 memset( digest, 0, sizeof( digest ) );
424
425 memset( &aes_ctx, 0, sizeof( aes_context ) );
Paul Bakker9e36f042013-06-30 14:34:05 +0200426 memset( &sha_ctx, 0, sizeof( sha256_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000427
428 return( ret );
429}
Paul Bakker9e36f042013-06-30 14:34:05 +0200430#endif /* POLARSSL_AES_C && POLARSSL_SHA256_C */