blob: 63ea51c618eb873c88b24963500aed9aa54fb706 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * AES-256 file encryption program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Paul Bakker494c0b82011-04-24 15:30:07 +000036#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000037#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000038#if !defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000039#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000040#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000041#else
42#include <sys/types.h>
43#include <unistd.h>
44#endif
45
46#include <string.h>
47#include <stdlib.h>
48#include <stdio.h>
49#include <time.h>
50
Paul Bakker40e46942009-01-03 21:51:57 +000051#include "polarssl/aes.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020052#include "polarssl/sha256.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000053
54#define MODE_ENCRYPT 0
55#define MODE_DECRYPT 1
56
57#define USAGE \
58 "\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
59 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
60 "\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
61 "\n"
62
Paul Bakker9e36f042013-06-30 14:34:05 +020063#if !defined(POLARSSL_AES_C) || !defined(POLARSSL_SHA256_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000064int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000065{
Paul Bakkercce9d772011-11-18 14:26:47 +000066 ((void) argc);
67 ((void) argv);
Rich Evansf90016a2015-01-19 14:26:37 +000068 polarssl_printf("POLARSSL_AES_C and/or POLARSSL_SHA256_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000069 return( 0 );
70}
71#else
Paul Bakker5121ce52009-01-03 21:22:43 +000072int main( int argc, char *argv[] )
73{
Paul Bakker5690efc2011-05-26 13:16:06 +000074 int ret = 1;
75
76 int i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000077 int mode, lastn;
78 size_t keylen;
Paul Bakker20a78082011-01-21 09:32:12 +000079 FILE *fkey, *fin = NULL, *fout = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81 char *p;
82 unsigned char IV[16];
83 unsigned char key[512];
84 unsigned char digest[32];
85 unsigned char buffer[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010086 unsigned char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +000087
88 aes_context aes_ctx;
Paul Bakker9e36f042013-06-30 14:34:05 +020089 sha256_context sha_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +000090
Paul Bakkercce9d772011-11-18 14:26:47 +000091#if defined(_WIN32_WCE)
92 long filesize, offset;
93#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000094 LARGE_INTEGER li_size;
95 __int64 filesize, offset;
96#else
97 off_t filesize, offset;
98#endif
99
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200100 aes_init( &aes_ctx );
Paul Bakker14e8be42014-06-26 12:25:06 +0200101 sha256_init( &sha_ctx );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200102
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 /*
104 * Parse the command-line arguments.
105 */
106 if( argc != 5 )
107 {
Rich Evansf90016a2015-01-19 14:26:37 +0000108 polarssl_printf( USAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Paul Bakkercce9d772011-11-18 14:26:47 +0000110#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000111 polarssl_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 fflush( stdout ); getchar();
113#endif
114
115 goto exit;
116 }
117
118 mode = atoi( argv[1] );
wslfacc334ef2014-12-28 00:55:41 +0800119 memset(IV, 0, sizeof(IV));
120 memset(key, 0, sizeof(key));
121 memset(digest, 0, sizeof(digest));
122 memset(buffer, 0, sizeof(buffer));
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
124 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
125 {
Rich Evansf90016a2015-01-19 14:26:37 +0000126 polarssl_fprintf( stderr, "invalide operation mode\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 goto exit;
128 }
129
130 if( strcmp( argv[2], argv[3] ) == 0 )
131 {
Rich Evansf90016a2015-01-19 14:26:37 +0000132 polarssl_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 goto exit;
134 }
135
136 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
137 {
Rich Evansf90016a2015-01-19 14:26:37 +0000138 polarssl_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 goto exit;
140 }
141
142 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
143 {
Rich Evansf90016a2015-01-19 14:26:37 +0000144 polarssl_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 goto exit;
146 }
147
148 /*
149 * Read the secret key and clean the command line.
150 */
151 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
152 {
153 keylen = fread( key, 1, sizeof( key ), fkey );
154 fclose( fkey );
155 }
156 else
157 {
158 if( memcmp( argv[4], "hex:", 4 ) == 0 )
159 {
160 p = &argv[4][4];
161 keylen = 0;
162
163 while( sscanf( p, "%02X", &n ) > 0 &&
164 keylen < (int) sizeof( key ) )
165 {
166 key[keylen++] = (unsigned char) n;
167 p += 2;
168 }
169 }
170 else
171 {
172 keylen = strlen( argv[4] );
173
174 if( keylen > (int) sizeof( key ) )
175 keylen = (int) sizeof( key );
176
177 memcpy( key, argv[4], keylen );
178 }
179 }
180
181 memset( argv[4], 0, strlen( argv[4] ) );
182
Paul Bakkercce9d772011-11-18 14:26:47 +0000183#if defined(_WIN32_WCE)
184 filesize = fseek( fin, 0L, SEEK_END );
185#else
186#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 /*
188 * Support large files (> 2Gb) on Win32
189 */
190 li_size.QuadPart = 0;
191 li_size.LowPart =
192 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
193 li_size.LowPart, &li_size.HighPart, FILE_END );
194
195 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
196 {
Rich Evansf90016a2015-01-19 14:26:37 +0000197 polarssl_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 goto exit;
199 }
200
201 filesize = li_size.QuadPart;
202#else
203 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
204 {
205 perror( "lseek" );
206 goto exit;
207 }
208#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000209#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 if( fseek( fin, 0, SEEK_SET ) < 0 )
212 {
Rich Evansf90016a2015-01-19 14:26:37 +0000213 polarssl_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 goto exit;
215 }
216
217 if( mode == MODE_ENCRYPT )
218 {
219 /*
220 * Generate the initialization vector as:
221 * IV = SHA-256( filesize || filename )[0..15]
222 */
223 for( i = 0; i < 8; i++ )
224 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
225
226 p = argv[2];
227
Paul Bakker9e36f042013-06-30 14:34:05 +0200228 sha256_starts( &sha_ctx, 0 );
229 sha256_update( &sha_ctx, buffer, 8 );
230 sha256_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
231 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
233 memcpy( IV, digest, 16 );
234
235 /*
236 * The last four bits in the IV are actually used
237 * to store the file size modulo the AES block size.
238 */
239 lastn = (int)( filesize & 0x0F );
240
241 IV[15] = (unsigned char)
242 ( ( IV[15] & 0xF0 ) | lastn );
243
244 /*
245 * Append the IV at the beginning of the output.
246 */
247 if( fwrite( IV, 1, 16, fout ) != 16 )
248 {
Rich Evansf90016a2015-01-19 14:26:37 +0000249 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 goto exit;
251 }
252
253 /*
254 * Hash the IV and the secret key together 8192 times
255 * using the result to setup the AES context and HMAC.
256 */
257 memset( digest, 0, 32 );
258 memcpy( digest, IV, 16 );
259
260 for( i = 0; i < 8192; i++ )
261 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200262 sha256_starts( &sha_ctx, 0 );
263 sha256_update( &sha_ctx, digest, 32 );
264 sha256_update( &sha_ctx, key, keylen );
265 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 }
267
268 memset( key, 0, sizeof( key ) );
Paul Bakker9e36f042013-06-30 14:34:05 +0200269 aes_setkey_enc( &aes_ctx, digest, 256 );
270 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
272 /*
273 * Encrypt and write the ciphertext.
274 */
275 for( offset = 0; offset < filesize; offset += 16 )
276 {
277 n = ( filesize - offset > 16 ) ? 16 : (int)
278 ( filesize - offset );
279
280 if( fread( buffer, 1, n, fin ) != (size_t) n )
281 {
Rich Evansf90016a2015-01-19 14:26:37 +0000282 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 goto exit;
284 }
285
286 for( i = 0; i < 16; i++ )
287 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
288
289 aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200290 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
292 if( fwrite( buffer, 1, 16, fout ) != 16 )
293 {
Rich Evansf90016a2015-01-19 14:26:37 +0000294 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 goto exit;
296 }
297
298 memcpy( IV, buffer, 16 );
299 }
300
301 /*
302 * Finally write the HMAC.
303 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200304 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000305
306 if( fwrite( digest, 1, 32, fout ) != 32 )
307 {
Rich Evansf90016a2015-01-19 14:26:37 +0000308 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 goto exit;
310 }
311 }
312
313 if( mode == MODE_DECRYPT )
314 {
315 unsigned char tmp[16];
316
317 /*
318 * The encrypted file must be structured as follows:
319 *
320 * 00 .. 15 Initialization Vector
321 * 16 .. 31 AES Encrypted Block #1
322 * ..
323 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
324 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
325 */
326 if( filesize < 48 )
327 {
Rich Evansf90016a2015-01-19 14:26:37 +0000328 polarssl_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 goto exit;
330 }
331
332 if( ( filesize & 0x0F ) != 0 )
333 {
Rich Evansf90016a2015-01-19 14:26:37 +0000334 polarssl_fprintf( stderr, "File size not a multiple of 16.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335 goto exit;
336 }
337
338 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100339 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 */
341 filesize -= ( 16 + 32 );
342
343 /*
344 * Read the IV and original filesize modulo 16.
345 */
346 if( fread( buffer, 1, 16, fin ) != 16 )
347 {
Rich Evansf90016a2015-01-19 14:26:37 +0000348 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000349 goto exit;
350 }
351
352 memcpy( IV, buffer, 16 );
353 lastn = IV[15] & 0x0F;
354
355 /*
356 * Hash the IV and the secret key together 8192 times
357 * using the result to setup the AES context and HMAC.
358 */
359 memset( digest, 0, 32 );
360 memcpy( digest, IV, 16 );
361
362 for( i = 0; i < 8192; i++ )
363 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200364 sha256_starts( &sha_ctx, 0 );
365 sha256_update( &sha_ctx, digest, 32 );
366 sha256_update( &sha_ctx, key, keylen );
367 sha256_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 }
369
370 memset( key, 0, sizeof( key ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200371 aes_setkey_dec( &aes_ctx, digest, 256 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200372 sha256_hmac_starts( &sha_ctx, digest, 32, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
374 /*
375 * Decrypt and write the plaintext.
376 */
377 for( offset = 0; offset < filesize; offset += 16 )
378 {
379 if( fread( buffer, 1, 16, fin ) != 16 )
380 {
Rich Evansf90016a2015-01-19 14:26:37 +0000381 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000382 goto exit;
383 }
384
385 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200386
387 sha256_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200389
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 for( i = 0; i < 16; i++ )
391 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
392
393 memcpy( IV, tmp, 16 );
394
395 n = ( lastn > 0 && offset == filesize - 16 )
396 ? lastn : 16;
397
398 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
399 {
Rich Evansf90016a2015-01-19 14:26:37 +0000400 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000401 goto exit;
402 }
403 }
404
405 /*
406 * Verify the message authentication code.
407 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200408 sha256_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000409
410 if( fread( buffer, 1, 32, fin ) != 32 )
411 {
Rich Evansf90016a2015-01-19 14:26:37 +0000412 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 goto exit;
414 }
415
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100416 /* Use constant-time buffer comparison */
417 diff = 0;
418 for( i = 0; i < 32; i++ )
419 diff |= digest[i] ^ buffer[i];
420
421 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 {
Rich Evansf90016a2015-01-19 14:26:37 +0000423 polarssl_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 "or file corrupted.\n" );
425 goto exit;
426 }
427 }
428
429 ret = 0;
430
431exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000432 if( fin )
433 fclose( fin );
434 if( fout )
435 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000436
437 memset( buffer, 0, sizeof( buffer ) );
438 memset( digest, 0, sizeof( digest ) );
439
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200440 aes_free( &aes_ctx );
Paul Bakker14e8be42014-06-26 12:25:06 +0200441 sha256_free( &sha_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000442
443 return( ret );
444}
Paul Bakker9e36f042013-06-30 14:34:05 +0200445#endif /* POLARSSL_AES_C && POLARSSL_SHA256_C */