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