blob: d5305c1122d44e95e137134a0a5c1a3acea3fa82 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * AES-256 file encryption program
3 *
Paul Bakker77b385e2009-07-28 17:23:11 +00004 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker77b385e2009-07-28 17:23:11 +00007 * Joined copyright on original XySSL code with: Christophe Devine
Paul Bakker5121ce52009-01-03 21:22:43 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#ifndef _CRT_SECURE_NO_DEPRECATE
25#define _CRT_SECURE_NO_DEPRECATE 1
26#endif
27
28#if defined(WIN32)
29#include <windows.h>
30#include <io.h>
31#else
32#include <sys/types.h>
33#include <unistd.h>
34#endif
35
36#include <string.h>
37#include <stdlib.h>
38#include <stdio.h>
39#include <time.h>
40
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/aes.h"
42#include "polarssl/sha2.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000043
44#define MODE_ENCRYPT 0
45#define MODE_DECRYPT 1
46
47#define USAGE \
48 "\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
49 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
50 "\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
51 "\n"
52
53int main( int argc, char *argv[] )
54{
55 int ret = 1, i, n;
56 int keylen, mode, lastn;
57 FILE *fkey, *fin, *fout;
58
59 char *p;
60 unsigned char IV[16];
61 unsigned char key[512];
62 unsigned char digest[32];
63 unsigned char buffer[1024];
64
65 aes_context aes_ctx;
66 sha2_context sha_ctx;
67
68#if defined(WIN32)
69 LARGE_INTEGER li_size;
70 __int64 filesize, offset;
71#else
72 off_t filesize, offset;
73#endif
74
75 /*
76 * Parse the command-line arguments.
77 */
78 if( argc != 5 )
79 {
80 printf( USAGE );
81
82#if defined(WIN32)
83 printf( "\n Press Enter to exit this program.\n" );
84 fflush( stdout ); getchar();
85#endif
86
87 goto exit;
88 }
89
90 mode = atoi( argv[1] );
91
92 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
93 {
94 fprintf( stderr, "invalide operation mode\n" );
95 goto exit;
96 }
97
98 if( strcmp( argv[2], argv[3] ) == 0 )
99 {
100 fprintf( stderr, "input and output filenames must differ\n" );
101 goto exit;
102 }
103
104 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
105 {
106 fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
107 goto exit;
108 }
109
110 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
111 {
112 fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
113 goto exit;
114 }
115
116 /*
117 * Read the secret key and clean the command line.
118 */
119 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
120 {
121 keylen = fread( key, 1, sizeof( key ), fkey );
122 fclose( fkey );
123 }
124 else
125 {
126 if( memcmp( argv[4], "hex:", 4 ) == 0 )
127 {
128 p = &argv[4][4];
129 keylen = 0;
130
131 while( sscanf( p, "%02X", &n ) > 0 &&
132 keylen < (int) sizeof( key ) )
133 {
134 key[keylen++] = (unsigned char) n;
135 p += 2;
136 }
137 }
138 else
139 {
140 keylen = strlen( argv[4] );
141
142 if( keylen > (int) sizeof( key ) )
143 keylen = (int) sizeof( key );
144
145 memcpy( key, argv[4], keylen );
146 }
147 }
148
149 memset( argv[4], 0, strlen( argv[4] ) );
150
151#if defined(WIN32)
152 /*
153 * Support large files (> 2Gb) on Win32
154 */
155 li_size.QuadPart = 0;
156 li_size.LowPart =
157 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
158 li_size.LowPart, &li_size.HighPart, FILE_END );
159
160 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
161 {
162 fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
163 goto exit;
164 }
165
166 filesize = li_size.QuadPart;
167#else
168 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
169 {
170 perror( "lseek" );
171 goto exit;
172 }
173#endif
174
175 if( fseek( fin, 0, SEEK_SET ) < 0 )
176 {
177 fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
178 goto exit;
179 }
180
181 if( mode == MODE_ENCRYPT )
182 {
183 /*
184 * Generate the initialization vector as:
185 * IV = SHA-256( filesize || filename )[0..15]
186 */
187 for( i = 0; i < 8; i++ )
188 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
189
190 p = argv[2];
191
192 sha2_starts( &sha_ctx, 0 );
193 sha2_update( &sha_ctx, buffer, 8 );
194 sha2_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
195 sha2_finish( &sha_ctx, digest );
196
197 memcpy( IV, digest, 16 );
198
199 /*
200 * The last four bits in the IV are actually used
201 * to store the file size modulo the AES block size.
202 */
203 lastn = (int)( filesize & 0x0F );
204
205 IV[15] = (unsigned char)
206 ( ( IV[15] & 0xF0 ) | lastn );
207
208 /*
209 * Append the IV at the beginning of the output.
210 */
211 if( fwrite( IV, 1, 16, fout ) != 16 )
212 {
213 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
214 goto exit;
215 }
216
217 /*
218 * Hash the IV and the secret key together 8192 times
219 * using the result to setup the AES context and HMAC.
220 */
221 memset( digest, 0, 32 );
222 memcpy( digest, IV, 16 );
223
224 for( i = 0; i < 8192; i++ )
225 {
226 sha2_starts( &sha_ctx, 0 );
227 sha2_update( &sha_ctx, digest, 32 );
228 sha2_update( &sha_ctx, key, keylen );
229 sha2_finish( &sha_ctx, digest );
230 }
231
232 memset( key, 0, sizeof( key ) );
233 aes_setkey_enc( &aes_ctx, digest, 256 );
234 sha2_hmac_starts( &sha_ctx, digest, 32, 0 );
235
236 /*
237 * Encrypt and write the ciphertext.
238 */
239 for( offset = 0; offset < filesize; offset += 16 )
240 {
241 n = ( filesize - offset > 16 ) ? 16 : (int)
242 ( filesize - offset );
243
244 if( fread( buffer, 1, n, fin ) != (size_t) n )
245 {
246 fprintf( stderr, "fread(%d bytes) failed\n", n );
247 goto exit;
248 }
249
250 for( i = 0; i < 16; i++ )
251 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
252
253 aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer );
254 sha2_hmac_update( &sha_ctx, buffer, 16 );
255
256 if( fwrite( buffer, 1, 16, fout ) != 16 )
257 {
258 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
259 goto exit;
260 }
261
262 memcpy( IV, buffer, 16 );
263 }
264
265 /*
266 * Finally write the HMAC.
267 */
268 sha2_hmac_finish( &sha_ctx, digest );
269
270 if( fwrite( digest, 1, 32, fout ) != 32 )
271 {
272 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
273 goto exit;
274 }
275 }
276
277 if( mode == MODE_DECRYPT )
278 {
279 unsigned char tmp[16];
280
281 /*
282 * The encrypted file must be structured as follows:
283 *
284 * 00 .. 15 Initialization Vector
285 * 16 .. 31 AES Encrypted Block #1
286 * ..
287 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
288 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
289 */
290 if( filesize < 48 )
291 {
292 fprintf( stderr, "File too short to be encrypted.\n" );
293 goto exit;
294 }
295
296 if( ( filesize & 0x0F ) != 0 )
297 {
298 fprintf( stderr, "File size not a multiple of 16.\n" );
299 goto exit;
300 }
301
302 /*
303 * Substract the IV + HMAC length.
304 */
305 filesize -= ( 16 + 32 );
306
307 /*
308 * Read the IV and original filesize modulo 16.
309 */
310 if( fread( buffer, 1, 16, fin ) != 16 )
311 {
312 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
313 goto exit;
314 }
315
316 memcpy( IV, buffer, 16 );
317 lastn = IV[15] & 0x0F;
318
319 /*
320 * Hash the IV and the secret key together 8192 times
321 * using the result to setup the AES context and HMAC.
322 */
323 memset( digest, 0, 32 );
324 memcpy( digest, IV, 16 );
325
326 for( i = 0; i < 8192; i++ )
327 {
328 sha2_starts( &sha_ctx, 0 );
329 sha2_update( &sha_ctx, digest, 32 );
330 sha2_update( &sha_ctx, key, keylen );
331 sha2_finish( &sha_ctx, digest );
332 }
333
334 memset( key, 0, sizeof( key ) );
335 aes_setkey_dec( &aes_ctx, digest, 256 );
336 sha2_hmac_starts( &sha_ctx, digest, 32, 0 );
337
338 /*
339 * Decrypt and write the plaintext.
340 */
341 for( offset = 0; offset < filesize; offset += 16 )
342 {
343 if( fread( buffer, 1, 16, fin ) != 16 )
344 {
345 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
346 goto exit;
347 }
348
349 memcpy( tmp, buffer, 16 );
350
351 sha2_hmac_update( &sha_ctx, buffer, 16 );
352 aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer );
353
354 for( i = 0; i < 16; i++ )
355 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
356
357 memcpy( IV, tmp, 16 );
358
359 n = ( lastn > 0 && offset == filesize - 16 )
360 ? lastn : 16;
361
362 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
363 {
364 fprintf( stderr, "fwrite(%d bytes) failed\n", n );
365 goto exit;
366 }
367 }
368
369 /*
370 * Verify the message authentication code.
371 */
372 sha2_hmac_finish( &sha_ctx, digest );
373
374 if( fread( buffer, 1, 32, fin ) != 32 )
375 {
376 fprintf( stderr, "fread(%d bytes) failed\n", 32 );
377 goto exit;
378 }
379
380 if( memcmp( digest, buffer, 32 ) != 0 )
381 {
382 fprintf( stderr, "HMAC check failed: wrong key, "
383 "or file corrupted.\n" );
384 goto exit;
385 }
386 }
387
388 ret = 0;
389
390exit:
391
392 memset( buffer, 0, sizeof( buffer ) );
393 memset( digest, 0, sizeof( digest ) );
394
395 memset( &aes_ctx, 0, sizeof( aes_context ) );
396 memset( &sha_ctx, 0, sizeof( sha2_context ) );
397
398 return( ret );
399}