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