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