blob: 20f4a20d26751376c247498b0bc6d8463cfcda4d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * AES-256 file encryption program
3 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * 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
Paul Bakker494c0b82011-04-24 15:30:07 +000027#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000028#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000029#if !defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000030#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000031#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000032#else
33#include <sys/types.h>
34#include <unistd.h>
35#endif
36
37#include <string.h>
38#include <stdlib.h>
39#include <stdio.h>
40#include <time.h>
41
Paul Bakker5690efc2011-05-26 13:16:06 +000042#include "polarssl/config.h"
43
Paul Bakker40e46942009-01-03 21:51:57 +000044#include "polarssl/aes.h"
45#include "polarssl/sha2.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000046
47#define MODE_ENCRYPT 0
48#define MODE_DECRYPT 1
49
50#define USAGE \
51 "\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
52 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
53 "\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
54 "\n"
55
Paul Bakker5690efc2011-05-26 13:16:06 +000056#if !defined(POLARSSL_AES_C) || !defined(POLARSSL_SHA2_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000057int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000058{
Paul Bakkercce9d772011-11-18 14:26:47 +000059 ((void) argc);
60 ((void) argv);
Paul Bakker5690efc2011-05-26 13:16:06 +000061 printf("POLARSSL_AES_C and/or POLARSSL_SHA2_C not defined.\n");
62 return( 0 );
63}
64#else
Paul Bakker5121ce52009-01-03 21:22:43 +000065int main( int argc, char *argv[] )
66{
Paul Bakker5690efc2011-05-26 13:16:06 +000067 int ret = 1;
68
69 int i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000070 int mode, lastn;
71 size_t keylen;
Paul Bakker20a78082011-01-21 09:32:12 +000072 FILE *fkey, *fin = NULL, *fout = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000073
74 char *p;
75 unsigned char IV[16];
76 unsigned char key[512];
77 unsigned char digest[32];
78 unsigned char buffer[1024];
Manuel Pégourié-Gonnard79f1ff82013-10-28 12:51:32 +010079 unsigned char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81 aes_context aes_ctx;
82 sha2_context sha_ctx;
83
Paul Bakkercce9d772011-11-18 14:26:47 +000084#if defined(_WIN32_WCE)
85 long filesize, offset;
86#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000087 LARGE_INTEGER li_size;
88 __int64 filesize, offset;
89#else
90 off_t filesize, offset;
91#endif
92
93 /*
94 * Parse the command-line arguments.
95 */
96 if( argc != 5 )
97 {
98 printf( USAGE );
99
Paul Bakkercce9d772011-11-18 14:26:47 +0000100#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 printf( "\n Press Enter to exit this program.\n" );
102 fflush( stdout ); getchar();
103#endif
104
105 goto exit;
106 }
107
108 mode = atoi( argv[1] );
109
110 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
111 {
112 fprintf( stderr, "invalide operation mode\n" );
113 goto exit;
114 }
115
116 if( strcmp( argv[2], argv[3] ) == 0 )
117 {
118 fprintf( stderr, "input and output filenames must differ\n" );
119 goto exit;
120 }
121
122 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
123 {
124 fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
125 goto exit;
126 }
127
128 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
129 {
130 fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
131 goto exit;
132 }
133
134 /*
135 * Read the secret key and clean the command line.
136 */
137 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
138 {
139 keylen = fread( key, 1, sizeof( key ), fkey );
140 fclose( fkey );
141 }
142 else
143 {
144 if( memcmp( argv[4], "hex:", 4 ) == 0 )
145 {
146 p = &argv[4][4];
147 keylen = 0;
148
149 while( sscanf( p, "%02X", &n ) > 0 &&
150 keylen < (int) sizeof( key ) )
151 {
152 key[keylen++] = (unsigned char) n;
153 p += 2;
154 }
155 }
156 else
157 {
158 keylen = strlen( argv[4] );
159
160 if( keylen > (int) sizeof( key ) )
161 keylen = (int) sizeof( key );
162
163 memcpy( key, argv[4], keylen );
164 }
165 }
166
167 memset( argv[4], 0, strlen( argv[4] ) );
168
Paul Bakkercce9d772011-11-18 14:26:47 +0000169#if defined(_WIN32_WCE)
170 filesize = fseek( fin, 0L, SEEK_END );
171#else
172#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 /*
174 * Support large files (> 2Gb) on Win32
175 */
176 li_size.QuadPart = 0;
177 li_size.LowPart =
178 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
179 li_size.LowPart, &li_size.HighPart, FILE_END );
180
181 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
182 {
183 fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
184 goto exit;
185 }
186
187 filesize = li_size.QuadPart;
188#else
189 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
190 {
191 perror( "lseek" );
192 goto exit;
193 }
194#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000195#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
197 if( fseek( fin, 0, SEEK_SET ) < 0 )
198 {
199 fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
200 goto exit;
201 }
202
203 if( mode == MODE_ENCRYPT )
204 {
205 /*
206 * Generate the initialization vector as:
207 * IV = SHA-256( filesize || filename )[0..15]
208 */
209 for( i = 0; i < 8; i++ )
210 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
211
212 p = argv[2];
213
214 sha2_starts( &sha_ctx, 0 );
215 sha2_update( &sha_ctx, buffer, 8 );
216 sha2_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
217 sha2_finish( &sha_ctx, digest );
218
219 memcpy( IV, digest, 16 );
220
221 /*
222 * The last four bits in the IV are actually used
223 * to store the file size modulo the AES block size.
224 */
225 lastn = (int)( filesize & 0x0F );
226
227 IV[15] = (unsigned char)
228 ( ( IV[15] & 0xF0 ) | lastn );
229
230 /*
231 * Append the IV at the beginning of the output.
232 */
233 if( fwrite( IV, 1, 16, fout ) != 16 )
234 {
235 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
236 goto exit;
237 }
238
239 /*
240 * Hash the IV and the secret key together 8192 times
241 * using the result to setup the AES context and HMAC.
242 */
243 memset( digest, 0, 32 );
244 memcpy( digest, IV, 16 );
245
246 for( i = 0; i < 8192; i++ )
247 {
248 sha2_starts( &sha_ctx, 0 );
249 sha2_update( &sha_ctx, digest, 32 );
250 sha2_update( &sha_ctx, key, keylen );
251 sha2_finish( &sha_ctx, digest );
252 }
253
254 memset( key, 0, sizeof( key ) );
255 aes_setkey_enc( &aes_ctx, digest, 256 );
256 sha2_hmac_starts( &sha_ctx, digest, 32, 0 );
257
258 /*
259 * Encrypt and write the ciphertext.
260 */
261 for( offset = 0; offset < filesize; offset += 16 )
262 {
263 n = ( filesize - offset > 16 ) ? 16 : (int)
264 ( filesize - offset );
265
266 if( fread( buffer, 1, n, fin ) != (size_t) n )
267 {
268 fprintf( stderr, "fread(%d bytes) failed\n", n );
269 goto exit;
270 }
271
272 for( i = 0; i < 16; i++ )
273 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
274
275 aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer );
276 sha2_hmac_update( &sha_ctx, buffer, 16 );
277
278 if( fwrite( buffer, 1, 16, fout ) != 16 )
279 {
280 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
281 goto exit;
282 }
283
284 memcpy( IV, buffer, 16 );
285 }
286
287 /*
288 * Finally write the HMAC.
289 */
290 sha2_hmac_finish( &sha_ctx, digest );
291
292 if( fwrite( digest, 1, 32, fout ) != 32 )
293 {
294 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
295 goto exit;
296 }
297 }
298
299 if( mode == MODE_DECRYPT )
300 {
301 unsigned char tmp[16];
302
303 /*
304 * The encrypted file must be structured as follows:
305 *
306 * 00 .. 15 Initialization Vector
307 * 16 .. 31 AES Encrypted Block #1
308 * ..
309 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
310 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
311 */
312 if( filesize < 48 )
313 {
314 fprintf( stderr, "File too short to be encrypted.\n" );
315 goto exit;
316 }
317
318 if( ( filesize & 0x0F ) != 0 )
319 {
320 fprintf( stderr, "File size not a multiple of 16.\n" );
321 goto exit;
322 }
323
324 /*
325 * Substract the IV + HMAC length.
326 */
327 filesize -= ( 16 + 32 );
328
329 /*
330 * Read the IV and original filesize modulo 16.
331 */
332 if( fread( buffer, 1, 16, fin ) != 16 )
333 {
334 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
335 goto exit;
336 }
337
338 memcpy( IV, buffer, 16 );
339 lastn = IV[15] & 0x0F;
340
341 /*
342 * Hash the IV and the secret key together 8192 times
343 * using the result to setup the AES context and HMAC.
344 */
345 memset( digest, 0, 32 );
346 memcpy( digest, IV, 16 );
347
348 for( i = 0; i < 8192; i++ )
349 {
350 sha2_starts( &sha_ctx, 0 );
351 sha2_update( &sha_ctx, digest, 32 );
352 sha2_update( &sha_ctx, key, keylen );
353 sha2_finish( &sha_ctx, digest );
354 }
355
356 memset( key, 0, sizeof( key ) );
357 aes_setkey_dec( &aes_ctx, digest, 256 );
358 sha2_hmac_starts( &sha_ctx, digest, 32, 0 );
359
360 /*
361 * Decrypt and write the plaintext.
362 */
363 for( offset = 0; offset < filesize; offset += 16 )
364 {
365 if( fread( buffer, 1, 16, fin ) != 16 )
366 {
367 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
368 goto exit;
369 }
370
371 memcpy( tmp, buffer, 16 );
372
373 sha2_hmac_update( &sha_ctx, buffer, 16 );
374 aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer );
375
376 for( i = 0; i < 16; i++ )
377 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
378
379 memcpy( IV, tmp, 16 );
380
381 n = ( lastn > 0 && offset == filesize - 16 )
382 ? lastn : 16;
383
384 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
385 {
386 fprintf( stderr, "fwrite(%d bytes) failed\n", n );
387 goto exit;
388 }
389 }
390
391 /*
392 * Verify the message authentication code.
393 */
394 sha2_hmac_finish( &sha_ctx, digest );
395
396 if( fread( buffer, 1, 32, fin ) != 32 )
397 {
398 fprintf( stderr, "fread(%d bytes) failed\n", 32 );
399 goto exit;
400 }
401
Manuel Pégourié-Gonnard79f1ff82013-10-28 12:51:32 +0100402 /* Use constant-time buffer comparison */
403 diff = 0;
404 for( i = 0; i < 32; i++ )
405 diff |= digest[i] ^ buffer[i];
406
407 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 {
409 fprintf( stderr, "HMAC check failed: wrong key, "
410 "or file corrupted.\n" );
411 goto exit;
412 }
413 }
414
415 ret = 0;
416
417exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000418 if( fin )
419 fclose( fin );
420 if( fout )
421 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
423 memset( buffer, 0, sizeof( buffer ) );
424 memset( digest, 0, sizeof( digest ) );
425
426 memset( &aes_ctx, 0, sizeof( aes_context ) );
427 memset( &sha_ctx, 0, sizeof( sha2_context ) );
428
429 return( ret );
430}
Paul Bakker5690efc2011-05-26 13:16:06 +0000431#endif /* POLARSSL_AES_C && POLARSSL_SHA2_C */