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