blob: c10ed71a7182e29d2555940bb346a05317a45bf8 [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é-Gonnard0edee5e2015-01-26 15:29:40 +00005 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakker20a78082011-01-21 09:32:12 +00006 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00007 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker20a78082011-01-21 09:32:12 +00008 *
9 * 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
24#ifndef _CRT_SECURE_NO_DEPRECATE
25#define _CRT_SECURE_NO_DEPRECATE 1
26#endif
27
Paul Bakker494c0b82011-04-24 15:30:07 +000028#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000029#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000030#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000031#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000032#endif
Paul Bakker20a78082011-01-21 09:32:12 +000033#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 Bakker20a78082011-01-21 09:32:12 +000045#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 Bakker2a8c2882013-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
95 memset( &cipher_ctx, 0, sizeof( cipher_context_t ));
96 memset( &md_ctx, 0, sizeof( md_context_t ));
97
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 Bakker993f02c2014-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 }
311 if( cipher_reset( &cipher_ctx, IV ) != 0 )
312 {
313 fprintf( stderr, "cipher_reset() returned error\n");
314 goto exit;
315 }
Paul Bakker20a78082011-01-21 09:32:12 +0000316
317 md_hmac_starts( &md_ctx, digest, 32 );
318
319 /*
320 * Encrypt and write the ciphertext.
321 */
322 for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) )
323 {
Paul Bakker25b5fe52011-05-26 14:02:58 +0000324 ilen = ( (unsigned int) filesize - offset > cipher_get_block_size( &cipher_ctx ) ) ?
Paul Bakker23986e52011-04-24 08:57:21 +0000325 cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000326
Paul Bakker25b5fe52011-05-26 14:02:58 +0000327 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000328 {
Paul Bakker993f02c2014-04-17 16:00:59 +0200329 fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000330 goto exit;
331 }
332
Paul Bakker993f02c2014-04-17 16:00:59 +0200333 if( cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
334 {
335 fprintf( stderr, "cipher_update() returned error\n");
336 goto exit;
337 }
338
Paul Bakker20a78082011-01-21 09:32:12 +0000339 md_hmac_update( &md_ctx, output, olen );
340
Paul Bakker2c0994e2011-05-25 13:51:57 +0000341 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000342 {
Paul Bakker2c0994e2011-05-25 13:51:57 +0000343 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000344 goto exit;
345 }
346 }
347
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000348 if( cipher_finish( &cipher_ctx, output, &olen ) != 0 )
349 {
350 fprintf( stderr, "cipher_finish() returned error\n" );
351 goto exit;
352 }
Paul Bakker20a78082011-01-21 09:32:12 +0000353 md_hmac_update( &md_ctx, output, olen );
354
Paul Bakker2c0994e2011-05-25 13:51:57 +0000355 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000356 {
Paul Bakker25b5fe52011-05-26 14:02:58 +0000357 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000358 goto exit;
359 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000360
Paul Bakker20a78082011-01-21 09:32:12 +0000361 /*
362 * Finally write the HMAC.
363 */
364 md_hmac_finish( &md_ctx, digest );
365
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000366 if( fwrite( digest, 1, md_get_size( md_info ), fout ) != md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000367 {
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000368 fprintf( stderr, "fwrite(%d bytes) failed\n", md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000369 goto exit;
370 }
371 }
372
373 if( mode == MODE_DECRYPT )
374 {
375 /*
376 * The encrypted file must be structured as follows:
377 *
378 * 00 .. 15 Initialization Vector
379 * 16 .. 31 AES Encrypted Block #1
380 * ..
381 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
382 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
383 */
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000384 if( filesize < 16 + md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000385 {
386 fprintf( stderr, "File too short to be encrypted.\n" );
387 goto exit;
388 }
389
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000390 if( ( ( filesize - md_get_size( md_info ) ) %
391 cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000392 {
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000393 fprintf( stderr, "File content not a multiple of the block size (%d).\n",
394 cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000395 goto exit;
396 }
397
398 /*
399 * Substract the IV + HMAC length.
400 */
401 filesize -= ( 16 + md_get_size( md_info ) );
402
403 /*
404 * Read the IV and original filesize modulo 16.
405 */
406 if( fread( buffer, 1, 16, fin ) != 16 )
407 {
408 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
409 goto exit;
410 }
411
412 memcpy( IV, buffer, 16 );
413 lastn = IV[15] & 0x0F;
414
415 /*
416 * Hash the IV and the secret key together 8192 times
417 * using the result to setup the AES context and HMAC.
418 */
419 memset( digest, 0, 32 );
420 memcpy( digest, IV, 16 );
421
422 for( i = 0; i < 8192; i++ )
423 {
424 md_starts( &md_ctx );
425 md_update( &md_ctx, digest, 32 );
426 md_update( &md_ctx, key, keylen );
427 md_finish( &md_ctx, digest );
428 }
429
430 memset( key, 0, sizeof( key ) );
431
Paul Bakker993f02c2014-04-17 16:00:59 +0200432 if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
433 POLARSSL_DECRYPT ) != 0 )
434 {
435 fprintf( stderr, "cipher_setkey() returned error\n" );
436 goto exit;
437 }
438
439 if( cipher_reset( &cipher_ctx, IV ) != 0 )
440 {
441 fprintf( stderr, "cipher_reset() returned error\n" );
442 goto exit;
443 }
Paul Bakker20a78082011-01-21 09:32:12 +0000444
445 md_hmac_starts( &md_ctx, digest, 32 );
446
447 /*
448 * Decrypt and write the plaintext.
449 */
450 for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) )
451 {
452 if( fread( buffer, 1, cipher_get_block_size( &cipher_ctx ), fin ) !=
453 (size_t) cipher_get_block_size( &cipher_ctx ) )
454 {
455 fprintf( stderr, "fread(%d bytes) failed\n",
456 cipher_get_block_size( &cipher_ctx ) );
457 goto exit;
458 }
459
460 md_hmac_update( &md_ctx, buffer, cipher_get_block_size( &cipher_ctx ) );
Paul Bakker993f02c2014-04-17 16:00:59 +0200461 if( cipher_update( &cipher_ctx, buffer,
462 cipher_get_block_size( &cipher_ctx ),
463 output, &olen ) != 0 )
464 {
465 fprintf( stderr, "cipher_update() returned error\n" );
466 goto exit;
467 }
Paul Bakker20a78082011-01-21 09:32:12 +0000468
Paul Bakker2c0994e2011-05-25 13:51:57 +0000469 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000470 {
Paul Bakker2c0994e2011-05-25 13:51:57 +0000471 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000472 goto exit;
473 }
474 }
475
476 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000477 * Verify the message authentication code.
478 */
479 md_hmac_finish( &md_ctx, digest );
480
481 if( fread( buffer, 1, md_get_size( md_info ), fin ) != md_get_size( md_info ) )
482 {
483 fprintf( stderr, "fread(%d bytes) failed\n", md_get_size( md_info ) );
484 goto exit;
485 }
486
Paul Bakker2a8c2882013-10-31 14:22:08 +0100487 /* Use constant-time buffer comparison */
488 diff = 0;
489 for( i = 0; i < md_get_size( md_info ); i++ )
490 diff |= digest[i] ^ buffer[i];
491
492 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000493 {
494 fprintf( stderr, "HMAC check failed: wrong key, "
495 "or file corrupted.\n" );
496 goto exit;
497 }
Manuel Pégourié-Gonnardb9f6d502013-11-25 17:55:17 +0100498
499 /*
500 * Write the final block of data
501 */
Paul Bakkera16e7f22014-07-09 14:58:11 +0200502 if( cipher_finish( &cipher_ctx, output, &olen ) != 0 )
503 {
504 fprintf( stderr, "cipher_finish() returned error\n" );
505 goto exit;
506 }
Manuel Pégourié-Gonnardb9f6d502013-11-25 17:55:17 +0100507
508 if( fwrite( output, 1, olen, fout ) != olen )
509 {
510 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
511 goto exit;
512 }
Paul Bakker20a78082011-01-21 09:32:12 +0000513 }
514
515 ret = 0;
516
517exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000518 if( fin )
519 fclose( fin );
520 if( fout )
521 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000522
523 memset( buffer, 0, sizeof( buffer ) );
524 memset( digest, 0, sizeof( digest ) );
525
526 cipher_free_ctx( &cipher_ctx );
527 md_free_ctx( &md_ctx );
528
529 return( ret );
530}
Paul Bakker5690efc2011-05-26 13:16:06 +0000531#endif /* POLARSSL_CIPHER_C && POLARSSL_MD_C */