blob: 4c1f8ea4b75d02beabe7f10aec803ba4cd821e08 [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];
78
79 aes_context aes_ctx;
Paul Bakker9e36f042013-06-30 14:34:05 +020080 sha256_context sha_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +000081
Paul Bakkercce9d772011-11-18 14:26:47 +000082#if defined(_WIN32_WCE)
83 long filesize, offset;
84#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000085 LARGE_INTEGER li_size;
86 __int64 filesize, offset;
87#else
88 off_t filesize, offset;
89#endif
90
91 /*
92 * Parse the command-line arguments.
93 */
94 if( argc != 5 )
95 {
96 printf( USAGE );
97
Paul Bakkercce9d772011-11-18 14:26:47 +000098#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000099 printf( "\n Press Enter to exit this program.\n" );
100 fflush( stdout ); getchar();
101#endif
102
103 goto exit;
104 }
105
106 mode = atoi( argv[1] );
107
108 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
109 {
110 fprintf( stderr, "invalide operation mode\n" );
111 goto exit;
112 }
113
114 if( strcmp( argv[2], argv[3] ) == 0 )
115 {
116 fprintf( stderr, "input and output filenames must differ\n" );
117 goto exit;
118 }
119
120 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
121 {
122 fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
123 goto exit;
124 }
125
126 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
127 {
128 fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
129 goto exit;
130 }
131
132 /*
133 * Read the secret key and clean the command line.
134 */
135 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
136 {
137 keylen = fread( key, 1, sizeof( key ), fkey );
138 fclose( fkey );
139 }
140 else
141 {
142 if( memcmp( argv[4], "hex:", 4 ) == 0 )
143 {
144 p = &argv[4][4];
145 keylen = 0;
146
147 while( sscanf( p, "%02X", &n ) > 0 &&
148 keylen < (int) sizeof( key ) )
149 {
150 key[keylen++] = (unsigned char) n;
151 p += 2;
152 }
153 }
154 else
155 {
156 keylen = strlen( argv[4] );
157
158 if( keylen > (int) sizeof( key ) )
159 keylen = (int) sizeof( key );
160
161 memcpy( key, argv[4], keylen );
162 }
163 }
164
165 memset( argv[4], 0, strlen( argv[4] ) );
166
Paul Bakkercce9d772011-11-18 14:26:47 +0000167#if defined(_WIN32_WCE)
168 filesize = fseek( fin, 0L, SEEK_END );
169#else
170#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 /*
172 * Support large files (> 2Gb) on Win32
173 */
174 li_size.QuadPart = 0;
175 li_size.LowPart =
176 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
177 li_size.LowPart, &li_size.HighPart, FILE_END );
178
179 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
180 {
181 fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
182 goto exit;
183 }
184
185 filesize = li_size.QuadPart;
186#else
187 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
188 {
189 perror( "lseek" );
190 goto exit;
191 }
192#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000193#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
195 if( fseek( fin, 0, SEEK_SET ) < 0 )
196 {
197 fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
198 goto exit;
199 }
200
201 if( mode == MODE_ENCRYPT )
202 {
203 /*
204 * Generate the initialization vector as:
205 * IV = SHA-256( filesize || filename )[0..15]
206 */
207 for( i = 0; i < 8; i++ )
208 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
209
210 p = argv[2];
211
Paul Bakker9e36f042013-06-30 14:34:05 +0200212 sha256_starts( &sha_ctx, 0 );
213 sha256_update( &sha_ctx, buffer, 8 );
214 sha256_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
215 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
217 memcpy( IV, digest, 16 );
218
219 /*
220 * The last four bits in the IV are actually used
221 * to store the file size modulo the AES block size.
222 */
223 lastn = (int)( filesize & 0x0F );
224
225 IV[15] = (unsigned char)
226 ( ( IV[15] & 0xF0 ) | lastn );
227
228 /*
229 * Append the IV at the beginning of the output.
230 */
231 if( fwrite( IV, 1, 16, fout ) != 16 )
232 {
233 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
234 goto exit;
235 }
236
237 /*
238 * Hash the IV and the secret key together 8192 times
239 * using the result to setup the AES context and HMAC.
240 */
241 memset( digest, 0, 32 );
242 memcpy( digest, IV, 16 );
243
244 for( i = 0; i < 8192; i++ )
245 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200246 sha256_starts( &sha_ctx, 0 );
247 sha256_update( &sha_ctx, digest, 32 );
248 sha256_update( &sha_ctx, key, keylen );
249 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 }
251
252 memset( key, 0, sizeof( key ) );
Paul Bakker9e36f042013-06-30 14:34:05 +0200253 aes_setkey_enc( &aes_ctx, digest, 256 );
254 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
256 /*
257 * Encrypt and write the ciphertext.
258 */
259 for( offset = 0; offset < filesize; offset += 16 )
260 {
261 n = ( filesize - offset > 16 ) ? 16 : (int)
262 ( filesize - offset );
263
264 if( fread( buffer, 1, n, fin ) != (size_t) n )
265 {
266 fprintf( stderr, "fread(%d bytes) failed\n", n );
267 goto exit;
268 }
269
270 for( i = 0; i < 16; i++ )
271 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
272
273 aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200274 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
276 if( fwrite( buffer, 1, 16, fout ) != 16 )
277 {
278 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
279 goto exit;
280 }
281
282 memcpy( IV, buffer, 16 );
283 }
284
285 /*
286 * Finally write the HMAC.
287 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200288 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
290 if( fwrite( digest, 1, 32, fout ) != 32 )
291 {
292 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
293 goto exit;
294 }
295 }
296
297 if( mode == MODE_DECRYPT )
298 {
299 unsigned char tmp[16];
300
301 /*
302 * The encrypted file must be structured as follows:
303 *
304 * 00 .. 15 Initialization Vector
305 * 16 .. 31 AES Encrypted Block #1
306 * ..
307 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
308 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
309 */
310 if( filesize < 48 )
311 {
312 fprintf( stderr, "File too short to be encrypted.\n" );
313 goto exit;
314 }
315
316 if( ( filesize & 0x0F ) != 0 )
317 {
318 fprintf( stderr, "File size not a multiple of 16.\n" );
319 goto exit;
320 }
321
322 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100323 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 */
325 filesize -= ( 16 + 32 );
326
327 /*
328 * Read the IV and original filesize modulo 16.
329 */
330 if( fread( buffer, 1, 16, fin ) != 16 )
331 {
332 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
333 goto exit;
334 }
335
336 memcpy( IV, buffer, 16 );
337 lastn = IV[15] & 0x0F;
338
339 /*
340 * Hash the IV and the secret key together 8192 times
341 * using the result to setup the AES context and HMAC.
342 */
343 memset( digest, 0, 32 );
344 memcpy( digest, IV, 16 );
345
346 for( i = 0; i < 8192; i++ )
347 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200348 sha256_starts( &sha_ctx, 0 );
349 sha256_update( &sha_ctx, digest, 32 );
350 sha256_update( &sha_ctx, key, keylen );
351 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 }
353
354 memset( key, 0, sizeof( key ) );
355 aes_setkey_dec( &aes_ctx, digest, 256 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200356 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000357
358 /*
359 * Decrypt and write the plaintext.
360 */
361 for( offset = 0; offset < filesize; offset += 16 )
362 {
363 if( fread( buffer, 1, 16, fin ) != 16 )
364 {
365 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
366 goto exit;
367 }
368
369 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200370
371 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000372 aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200373
Paul Bakker5121ce52009-01-03 21:22:43 +0000374 for( i = 0; i < 16; i++ )
375 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
376
377 memcpy( IV, tmp, 16 );
378
379 n = ( lastn > 0 && offset == filesize - 16 )
380 ? lastn : 16;
381
382 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
383 {
384 fprintf( stderr, "fwrite(%d bytes) failed\n", n );
385 goto exit;
386 }
387 }
388
389 /*
390 * Verify the message authentication code.
391 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200392 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000393
394 if( fread( buffer, 1, 32, fin ) != 32 )
395 {
396 fprintf( stderr, "fread(%d bytes) failed\n", 32 );
397 goto exit;
398 }
399
400 if( memcmp( digest, buffer, 32 ) != 0 )
401 {
402 fprintf( stderr, "HMAC check failed: wrong key, "
403 "or file corrupted.\n" );
404 goto exit;
405 }
406 }
407
408 ret = 0;
409
410exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000411 if( fin )
412 fclose( fin );
413 if( fout )
414 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000415
416 memset( buffer, 0, sizeof( buffer ) );
417 memset( digest, 0, sizeof( digest ) );
418
419 memset( &aes_ctx, 0, sizeof( aes_context ) );
Paul Bakker9e36f042013-06-30 14:34:05 +0200420 memset( &sha_ctx, 0, sizeof( sha256_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000421
422 return( ret );
423}
Paul Bakker9e36f042013-06-30 14:34:05 +0200424#endif /* POLARSSL_AES_C && POLARSSL_SHA256_C */