blob: a904bdeae34652f7cd1c06a0a12cc282d1c44b82 [file] [log] [blame]
Paul Bakker20a78082011-01-21 09:32:12 +00001/*
2 * \brief Generic file encryption program using generic wrappers for configured
3 * security.
4 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00005 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakker20a78082011-01-21 09:32:12 +00006 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00007 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakker20a78082011-01-21 09:32:12 +00008 *
Paul Bakker20a78082011-01-21 09:32:12 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020025#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#else
27#include POLARSSL_CONFIG_FILE
28#endif
Paul Bakker20a78082011-01-21 09:32:12 +000029
Paul Bakker494c0b82011-04-24 15:30:07 +000030#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000031#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000032#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000033#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000034#endif
Paul Bakker20a78082011-01-21 09:32:12 +000035#else
36#include <sys/types.h>
37#include <unistd.h>
38#endif
39
40#include <string.h>
41#include <stdlib.h>
42#include <stdio.h>
43#include <time.h>
44
45#include "polarssl/cipher.h"
46#include "polarssl/md.h"
47
48#define MODE_ENCRYPT 0
49#define MODE_DECRYPT 1
50
51#define USAGE \
52 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <md> <key>\n" \
53 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
54 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
55 "\n"
56
Paul Bakker5690efc2011-05-26 13:16:06 +000057#if !defined(POLARSSL_CIPHER_C) || !defined(POLARSSL_MD_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000058int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000059{
Paul Bakkercce9d772011-11-18 14:26:47 +000060 ((void) argc);
61 ((void) argv);
62
Paul Bakker5690efc2011-05-26 13:16:06 +000063 printf("POLARSSL_CIPHER_C and/or POLARSSL_MD_C not defined.\n");
64 return( 0 );
65}
66#else
Paul Bakker20a78082011-01-21 09:32:12 +000067int main( int argc, char *argv[] )
68{
Paul Bakker25b5fe52011-05-26 14:02:58 +000069 int ret = 1, i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000070 int mode, lastn;
Paul Bakker25b5fe52011-05-26 14:02:58 +000071 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000072 FILE *fkey, *fin = NULL, *fout = NULL;
73
74 char *p;
75 unsigned char IV[16];
76 unsigned char key[512];
77 unsigned char digest[POLARSSL_MD_MAX_SIZE];
78 unsigned char buffer[1024];
79 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +010080 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +000081
82 const cipher_info_t *cipher_info;
83 const md_info_t *md_info;
84 cipher_context_t cipher_ctx;
85 md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +000086#if defined(_WIN32_WCE)
87 long filesize, offset;
88#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000089 LARGE_INTEGER li_size;
90 __int64 filesize, offset;
91#else
92 off_t filesize, offset;
93#endif
94
Paul Bakkerd2a2d612014-07-01 15:45:49 +020095 cipher_init( &cipher_ctx );
96 md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +000097
98 /*
99 * Parse the command-line arguments.
100 */
101 if( argc != 7 )
102 {
103 const int *list;
104
105 printf( USAGE );
106
107 printf( "Available ciphers:\n" );
108 list = cipher_list();
109 while( *list )
110 {
111 cipher_info = cipher_info_from_type( *list );
112 printf( " %s\n", cipher_info->name );
113 list++;
114 }
115
116 printf( "\nAvailable message digests:\n" );
117 list = md_list();
118 while( *list )
119 {
120 md_info = md_info_from_type( *list );
121 printf( " %s\n", md_info->name );
122 list++;
123 }
124
Paul Bakkercce9d772011-11-18 14:26:47 +0000125#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000126 printf( "\n Press Enter to exit this program.\n" );
127 fflush( stdout ); getchar();
128#endif
129
130 goto exit;
131 }
132
133 mode = atoi( argv[1] );
134
135 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
136 {
137 fprintf( stderr, "invalid operation mode\n" );
138 goto exit;
139 }
140
141 if( strcmp( argv[2], argv[3] ) == 0 )
142 {
143 fprintf( stderr, "input and output filenames must differ\n" );
144 goto exit;
145 }
146
147 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
148 {
149 fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
150 goto exit;
151 }
152
153 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
154 {
155 fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
156 goto exit;
157 }
158
159 /*
160 * Read the Cipher and MD from the command line
161 */
162 cipher_info = cipher_info_from_string( argv[4] );
163 if( cipher_info == NULL )
164 {
165 fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
166 goto exit;
167 }
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200168 if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info) ) != 0 )
169 {
170 fprintf( stderr, "cipher_init_ctx failed\n" );
171 goto exit;
172 }
Paul Bakker20a78082011-01-21 09:32:12 +0000173
174 md_info = md_info_from_string( argv[5] );
175 if( md_info == NULL )
176 {
177 fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
178 goto exit;
179 }
180 md_init_ctx( &md_ctx, md_info);
181
182 /*
183 * Read the secret key and clean the command line.
184 */
185 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
186 {
187 keylen = fread( key, 1, sizeof( key ), fkey );
188 fclose( fkey );
189 }
190 else
191 {
192 if( memcmp( argv[6], "hex:", 4 ) == 0 )
193 {
194 p = &argv[6][4];
195 keylen = 0;
196
197 while( sscanf( p, "%02X", &n ) > 0 &&
198 keylen < (int) sizeof( key ) )
199 {
200 key[keylen++] = (unsigned char) n;
201 p += 2;
202 }
203 }
204 else
205 {
206 keylen = strlen( argv[6] );
207
208 if( keylen > (int) sizeof( key ) )
209 keylen = (int) sizeof( key );
210
211 memcpy( key, argv[6], keylen );
212 }
213 }
214
215 memset( argv[6], 0, strlen( argv[6] ) );
216
Paul Bakkercce9d772011-11-18 14:26:47 +0000217#if defined(_WIN32_WCE)
218 filesize = fseek( fin, 0L, SEEK_END );
219#else
220#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000221 /*
222 * Support large files (> 2Gb) on Win32
223 */
224 li_size.QuadPart = 0;
225 li_size.LowPart =
226 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
227 li_size.LowPart, &li_size.HighPart, FILE_END );
228
229 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
230 {
231 fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
232 goto exit;
233 }
234
235 filesize = li_size.QuadPart;
236#else
237 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
238 {
239 perror( "lseek" );
240 goto exit;
241 }
242#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000243#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000244
245 if( fseek( fin, 0, SEEK_SET ) < 0 )
246 {
247 fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
248 goto exit;
249 }
250
251 if( mode == MODE_ENCRYPT )
252 {
253 /*
254 * Generate the initialization vector as:
255 * IV = SHA-256( filesize || filename )[0..15]
256 */
257 for( i = 0; i < 8; i++ )
258 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
259
260 p = argv[2];
261
262 md_starts( &md_ctx );
263 md_update( &md_ctx, buffer, 8 );
264 md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
265 md_finish( &md_ctx, digest );
266
267 memcpy( IV, digest, 16 );
268
269 /*
270 * The last four bits in the IV are actually used
271 * to store the file size modulo the AES block size.
272 */
273 lastn = (int)( filesize & 0x0F );
274
275 IV[15] = (unsigned char)
276 ( ( IV[15] & 0xF0 ) | lastn );
277
278 /*
279 * Append the IV at the beginning of the output.
280 */
281 if( fwrite( IV, 1, 16, fout ) != 16 )
282 {
283 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
284 goto exit;
285 }
286
287 /*
288 * Hash the IV and the secret key together 8192 times
289 * using the result to setup the AES context and HMAC.
290 */
291 memset( digest, 0, 32 );
292 memcpy( digest, IV, 16 );
293
294 for( i = 0; i < 8192; i++ )
295 {
296 md_starts( &md_ctx );
297 md_update( &md_ctx, digest, 32 );
298 md_update( &md_ctx, key, keylen );
299 md_finish( &md_ctx, digest );
300
301 }
302
303 memset( key, 0, sizeof( key ) );
304
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000305 if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
306 POLARSSL_ENCRYPT ) != 0 )
307 {
308 fprintf( stderr, "cipher_setkey() returned error\n");
309 goto exit;
310 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200311 if( cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
312 {
313 fprintf( stderr, "cipher_set_iv() returned error\n");
314 goto exit;
315 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200316 if( cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000317 {
318 fprintf( stderr, "cipher_reset() returned error\n");
319 goto exit;
320 }
Paul Bakker20a78082011-01-21 09:32:12 +0000321
322 md_hmac_starts( &md_ctx, digest, 32 );
323
324 /*
325 * Encrypt and write the ciphertext.
326 */
327 for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) )
328 {
Paul Bakker25b5fe52011-05-26 14:02:58 +0000329 ilen = ( (unsigned int) filesize - offset > cipher_get_block_size( &cipher_ctx ) ) ?
Paul Bakker23986e52011-04-24 08:57:21 +0000330 cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000331
Paul Bakker25b5fe52011-05-26 14:02:58 +0000332 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000333 {
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200334 fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000335 goto exit;
336 }
337
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200338 if( cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
339 {
340 fprintf( stderr, "cipher_update() returned error\n");
341 goto exit;
342 }
343
Paul Bakker20a78082011-01-21 09:32:12 +0000344 md_hmac_update( &md_ctx, output, olen );
345
Paul Bakker2c0994e2011-05-25 13:51:57 +0000346 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000347 {
Paul Bakker2c0994e2011-05-25 13:51:57 +0000348 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000349 goto exit;
350 }
351 }
352
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200353 if( cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000354 {
355 fprintf( stderr, "cipher_finish() returned error\n" );
356 goto exit;
357 }
Paul Bakker20a78082011-01-21 09:32:12 +0000358 md_hmac_update( &md_ctx, output, olen );
359
Paul Bakker2c0994e2011-05-25 13:51:57 +0000360 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000361 {
Paul Bakker25b5fe52011-05-26 14:02:58 +0000362 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000363 goto exit;
364 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000365
Paul Bakker20a78082011-01-21 09:32:12 +0000366 /*
367 * Finally write the HMAC.
368 */
369 md_hmac_finish( &md_ctx, digest );
370
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000371 if( fwrite( digest, 1, md_get_size( md_info ), fout ) != md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000372 {
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000373 fprintf( stderr, "fwrite(%d bytes) failed\n", md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000374 goto exit;
375 }
376 }
377
378 if( mode == MODE_DECRYPT )
379 {
380 /*
381 * The encrypted file must be structured as follows:
382 *
383 * 00 .. 15 Initialization Vector
384 * 16 .. 31 AES Encrypted Block #1
385 * ..
386 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
387 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
388 */
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000389 if( filesize < 16 + md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000390 {
391 fprintf( stderr, "File too short to be encrypted.\n" );
392 goto exit;
393 }
394
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000395 if( ( ( filesize - md_get_size( md_info ) ) %
396 cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000397 {
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000398 fprintf( stderr, "File content not a multiple of the block size (%d).\n",
399 cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000400 goto exit;
401 }
402
403 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100404 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000405 */
406 filesize -= ( 16 + md_get_size( md_info ) );
407
408 /*
409 * Read the IV and original filesize modulo 16.
410 */
411 if( fread( buffer, 1, 16, fin ) != 16 )
412 {
413 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
414 goto exit;
415 }
416
417 memcpy( IV, buffer, 16 );
418 lastn = IV[15] & 0x0F;
419
420 /*
421 * Hash the IV and the secret key together 8192 times
422 * using the result to setup the AES context and HMAC.
423 */
424 memset( digest, 0, 32 );
425 memcpy( digest, IV, 16 );
426
427 for( i = 0; i < 8192; i++ )
428 {
429 md_starts( &md_ctx );
430 md_update( &md_ctx, digest, 32 );
431 md_update( &md_ctx, key, keylen );
432 md_finish( &md_ctx, digest );
433 }
434
435 memset( key, 0, sizeof( key ) );
436
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200437 if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
438 POLARSSL_DECRYPT ) != 0 )
439 {
440 fprintf( stderr, "cipher_setkey() returned error\n" );
441 goto exit;
442 }
443
444 if( cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
445 {
446 fprintf( stderr, "cipher_set_iv() returned error\n" );
447 goto exit;
448 }
449
450 if( cipher_reset( &cipher_ctx ) != 0 )
451 {
452 fprintf( stderr, "cipher_reset() returned error\n" );
453 goto exit;
454 }
Paul Bakker20a78082011-01-21 09:32:12 +0000455
456 md_hmac_starts( &md_ctx, digest, 32 );
457
458 /*
459 * Decrypt and write the plaintext.
460 */
461 for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) )
462 {
463 if( fread( buffer, 1, cipher_get_block_size( &cipher_ctx ), fin ) !=
464 (size_t) cipher_get_block_size( &cipher_ctx ) )
465 {
466 fprintf( stderr, "fread(%d bytes) failed\n",
467 cipher_get_block_size( &cipher_ctx ) );
468 goto exit;
469 }
470
471 md_hmac_update( &md_ctx, buffer, cipher_get_block_size( &cipher_ctx ) );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200472 if( cipher_update( &cipher_ctx, buffer,
473 cipher_get_block_size( &cipher_ctx ),
474 output, &olen ) != 0 )
475 {
476 fprintf( stderr, "cipher_update() returned error\n" );
477 goto exit;
478 }
Paul Bakker20a78082011-01-21 09:32:12 +0000479
Paul Bakker2c0994e2011-05-25 13:51:57 +0000480 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000481 {
Paul Bakker2c0994e2011-05-25 13:51:57 +0000482 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000483 goto exit;
484 }
485 }
486
487 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000488 * Verify the message authentication code.
489 */
490 md_hmac_finish( &md_ctx, digest );
491
492 if( fread( buffer, 1, md_get_size( md_info ), fin ) != md_get_size( md_info ) )
493 {
494 fprintf( stderr, "fread(%d bytes) failed\n", md_get_size( md_info ) );
495 goto exit;
496 }
497
Paul Bakker424cd692013-10-31 14:22:08 +0100498 /* Use constant-time buffer comparison */
499 diff = 0;
500 for( i = 0; i < md_get_size( md_info ); i++ )
501 diff |= digest[i] ^ buffer[i];
502
503 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000504 {
505 fprintf( stderr, "HMAC check failed: wrong key, "
506 "or file corrupted.\n" );
507 goto exit;
508 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100509
510 /*
511 * Write the final block of data
512 */
513 cipher_finish( &cipher_ctx, output, &olen );
514
515 if( fwrite( output, 1, olen, fout ) != olen )
516 {
517 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
518 goto exit;
519 }
Paul Bakker20a78082011-01-21 09:32:12 +0000520 }
521
522 ret = 0;
523
524exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000525 if( fin )
526 fclose( fin );
527 if( fout )
528 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000529
530 memset( buffer, 0, sizeof( buffer ) );
531 memset( digest, 0, sizeof( digest ) );
532
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200533 cipher_free( &cipher_ctx );
534 md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000535
536 return( ret );
537}
Paul Bakker5690efc2011-05-26 13:16:06 +0000538#endif /* POLARSSL_CIPHER_C && POLARSSL_MD_C */