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