blob: 859dc3382304b3d59d779fe0d529721742ed8086 [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 *
Paul Bakkercce9d772011-11-18 14:26:47 +00005 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakker20a78082011-01-21 09:32:12 +00006 *
7 * This file is part of PolarSSL (http://www.polarssl.org)
8 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Paul Bakker20a78082011-01-21 09:32:12 +000028
Paul Bakker494c0b82011-04-24 15:30:07 +000029#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000030#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000031#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000032#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000033#endif
Paul Bakker20a78082011-01-21 09:32:12 +000034#else
35#include <sys/types.h>
36#include <unistd.h>
37#endif
38
39#include <string.h>
40#include <stdlib.h>
41#include <stdio.h>
42#include <time.h>
43
44#include "polarssl/cipher.h"
45#include "polarssl/md.h"
46
47#define MODE_ENCRYPT 0
48#define MODE_DECRYPT 1
49
50#define USAGE \
51 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <md> <key>\n" \
52 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
53 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
54 "\n"
55
Paul Bakker5690efc2011-05-26 13:16:06 +000056#if !defined(POLARSSL_CIPHER_C) || !defined(POLARSSL_MD_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);
61
Paul Bakker5690efc2011-05-26 13:16:06 +000062 printf("POLARSSL_CIPHER_C and/or POLARSSL_MD_C not defined.\n");
63 return( 0 );
64}
65#else
Paul Bakker20a78082011-01-21 09:32:12 +000066int main( int argc, char *argv[] )
67{
Paul Bakker25b5fe52011-05-26 14:02:58 +000068 int ret = 1, i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000069 int mode, lastn;
Paul Bakker25b5fe52011-05-26 14:02:58 +000070 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000071 FILE *fkey, *fin = NULL, *fout = NULL;
72
73 char *p;
74 unsigned char IV[16];
75 unsigned char key[512];
76 unsigned char digest[POLARSSL_MD_MAX_SIZE];
77 unsigned char buffer[1024];
78 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +010079 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +000080
81 const cipher_info_t *cipher_info;
82 const md_info_t *md_info;
83 cipher_context_t cipher_ctx;
84 md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +000085#if defined(_WIN32_WCE)
86 long filesize, offset;
87#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000088 LARGE_INTEGER li_size;
89 __int64 filesize, offset;
90#else
91 off_t filesize, offset;
92#endif
93
94 memset( &cipher_ctx, 0, sizeof( cipher_context_t ));
95 memset( &md_ctx, 0, sizeof( md_context_t ));
96
97 /*
98 * Parse the command-line arguments.
99 */
100 if( argc != 7 )
101 {
102 const int *list;
103
104 printf( USAGE );
105
106 printf( "Available ciphers:\n" );
107 list = cipher_list();
108 while( *list )
109 {
110 cipher_info = cipher_info_from_type( *list );
111 printf( " %s\n", cipher_info->name );
112 list++;
113 }
114
115 printf( "\nAvailable message digests:\n" );
116 list = md_list();
117 while( *list )
118 {
119 md_info = md_info_from_type( *list );
120 printf( " %s\n", md_info->name );
121 list++;
122 }
123
Paul Bakkercce9d772011-11-18 14:26:47 +0000124#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000125 printf( "\n Press Enter to exit this program.\n" );
126 fflush( stdout ); getchar();
127#endif
128
129 goto exit;
130 }
131
132 mode = atoi( argv[1] );
133
134 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
135 {
136 fprintf( stderr, "invalid operation mode\n" );
137 goto exit;
138 }
139
140 if( strcmp( argv[2], argv[3] ) == 0 )
141 {
142 fprintf( stderr, "input and output filenames must differ\n" );
143 goto exit;
144 }
145
146 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
147 {
148 fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
149 goto exit;
150 }
151
152 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
153 {
154 fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
155 goto exit;
156 }
157
158 /*
159 * Read the Cipher and MD from the command line
160 */
161 cipher_info = cipher_info_from_string( argv[4] );
162 if( cipher_info == NULL )
163 {
164 fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
165 goto exit;
166 }
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200167 if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info) ) != 0 )
168 {
169 fprintf( stderr, "cipher_init_ctx failed\n" );
170 goto exit;
171 }
Paul Bakker20a78082011-01-21 09:32:12 +0000172
173 md_info = md_info_from_string( argv[5] );
174 if( md_info == NULL )
175 {
176 fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
177 goto exit;
178 }
179 md_init_ctx( &md_ctx, md_info);
180
181 /*
182 * Read the secret key and clean the command line.
183 */
184 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
185 {
186 keylen = fread( key, 1, sizeof( key ), fkey );
187 fclose( fkey );
188 }
189 else
190 {
191 if( memcmp( argv[6], "hex:", 4 ) == 0 )
192 {
193 p = &argv[6][4];
194 keylen = 0;
195
196 while( sscanf( p, "%02X", &n ) > 0 &&
197 keylen < (int) sizeof( key ) )
198 {
199 key[keylen++] = (unsigned char) n;
200 p += 2;
201 }
202 }
203 else
204 {
205 keylen = strlen( argv[6] );
206
207 if( keylen > (int) sizeof( key ) )
208 keylen = (int) sizeof( key );
209
210 memcpy( key, argv[6], keylen );
211 }
212 }
213
214 memset( argv[6], 0, strlen( argv[6] ) );
215
Paul Bakkercce9d772011-11-18 14:26:47 +0000216#if defined(_WIN32_WCE)
217 filesize = fseek( fin, 0L, SEEK_END );
218#else
219#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000220 /*
221 * Support large files (> 2Gb) on Win32
222 */
223 li_size.QuadPart = 0;
224 li_size.LowPart =
225 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
226 li_size.LowPart, &li_size.HighPart, FILE_END );
227
228 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
229 {
230 fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
231 goto exit;
232 }
233
234 filesize = li_size.QuadPart;
235#else
236 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
237 {
238 perror( "lseek" );
239 goto exit;
240 }
241#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000242#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000243
244 if( fseek( fin, 0, SEEK_SET ) < 0 )
245 {
246 fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
247 goto exit;
248 }
249
250 if( mode == MODE_ENCRYPT )
251 {
252 /*
253 * Generate the initialization vector as:
254 * IV = SHA-256( filesize || filename )[0..15]
255 */
256 for( i = 0; i < 8; i++ )
257 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
258
259 p = argv[2];
260
261 md_starts( &md_ctx );
262 md_update( &md_ctx, buffer, 8 );
263 md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
264 md_finish( &md_ctx, digest );
265
266 memcpy( IV, digest, 16 );
267
268 /*
269 * The last four bits in the IV are actually used
270 * to store the file size modulo the AES block size.
271 */
272 lastn = (int)( filesize & 0x0F );
273
274 IV[15] = (unsigned char)
275 ( ( IV[15] & 0xF0 ) | lastn );
276
277 /*
278 * Append the IV at the beginning of the output.
279 */
280 if( fwrite( IV, 1, 16, fout ) != 16 )
281 {
282 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
283 goto exit;
284 }
285
286 /*
287 * Hash the IV and the secret key together 8192 times
288 * using the result to setup the AES context and HMAC.
289 */
290 memset( digest, 0, 32 );
291 memcpy( digest, IV, 16 );
292
293 for( i = 0; i < 8192; i++ )
294 {
295 md_starts( &md_ctx );
296 md_update( &md_ctx, digest, 32 );
297 md_update( &md_ctx, key, keylen );
298 md_finish( &md_ctx, digest );
299
300 }
301
302 memset( key, 0, sizeof( key ) );
303
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000304 if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
305 POLARSSL_ENCRYPT ) != 0 )
306 {
307 fprintf( stderr, "cipher_setkey() returned error\n");
308 goto exit;
309 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200310 if( cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
311 {
312 fprintf( stderr, "cipher_set_iv() returned error\n");
313 goto exit;
314 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200315 if( cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000316 {
317 fprintf( stderr, "cipher_reset() returned error\n");
318 goto exit;
319 }
Paul Bakker20a78082011-01-21 09:32:12 +0000320
321 md_hmac_starts( &md_ctx, digest, 32 );
322
323 /*
324 * Encrypt and write the ciphertext.
325 */
326 for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) )
327 {
Paul Bakker25b5fe52011-05-26 14:02:58 +0000328 ilen = ( (unsigned int) filesize - offset > cipher_get_block_size( &cipher_ctx ) ) ?
Paul Bakker23986e52011-04-24 08:57:21 +0000329 cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000330
Paul Bakker25b5fe52011-05-26 14:02:58 +0000331 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000332 {
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200333 fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000334 goto exit;
335 }
336
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200337 if( cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
338 {
339 fprintf( stderr, "cipher_update() returned error\n");
340 goto exit;
341 }
342
Paul Bakker20a78082011-01-21 09:32:12 +0000343 md_hmac_update( &md_ctx, output, olen );
344
Paul Bakker2c0994e2011-05-25 13:51:57 +0000345 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000346 {
Paul Bakker2c0994e2011-05-25 13:51:57 +0000347 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000348 goto exit;
349 }
350 }
351
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200352 if( cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000353 {
354 fprintf( stderr, "cipher_finish() returned error\n" );
355 goto exit;
356 }
Paul Bakker20a78082011-01-21 09:32:12 +0000357 md_hmac_update( &md_ctx, output, olen );
358
Paul Bakker2c0994e2011-05-25 13:51:57 +0000359 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000360 {
Paul Bakker25b5fe52011-05-26 14:02:58 +0000361 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000362 goto exit;
363 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000364
Paul Bakker20a78082011-01-21 09:32:12 +0000365 /*
366 * Finally write the HMAC.
367 */
368 md_hmac_finish( &md_ctx, digest );
369
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000370 if( fwrite( digest, 1, md_get_size( md_info ), fout ) != md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000371 {
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000372 fprintf( stderr, "fwrite(%d bytes) failed\n", md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000373 goto exit;
374 }
375 }
376
377 if( mode == MODE_DECRYPT )
378 {
379 /*
380 * The encrypted file must be structured as follows:
381 *
382 * 00 .. 15 Initialization Vector
383 * 16 .. 31 AES Encrypted Block #1
384 * ..
385 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
386 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
387 */
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000388 if( filesize < 16 + md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000389 {
390 fprintf( stderr, "File too short to be encrypted.\n" );
391 goto exit;
392 }
393
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000394 if( ( ( filesize - md_get_size( md_info ) ) %
395 cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000396 {
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000397 fprintf( stderr, "File content not a multiple of the block size (%d).\n",
398 cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000399 goto exit;
400 }
401
402 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100403 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000404 */
405 filesize -= ( 16 + md_get_size( md_info ) );
406
407 /*
408 * Read the IV and original filesize modulo 16.
409 */
410 if( fread( buffer, 1, 16, fin ) != 16 )
411 {
412 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
413 goto exit;
414 }
415
416 memcpy( IV, buffer, 16 );
417 lastn = IV[15] & 0x0F;
418
419 /*
420 * Hash the IV and the secret key together 8192 times
421 * using the result to setup the AES context and HMAC.
422 */
423 memset( digest, 0, 32 );
424 memcpy( digest, IV, 16 );
425
426 for( i = 0; i < 8192; i++ )
427 {
428 md_starts( &md_ctx );
429 md_update( &md_ctx, digest, 32 );
430 md_update( &md_ctx, key, keylen );
431 md_finish( &md_ctx, digest );
432 }
433
434 memset( key, 0, sizeof( key ) );
435
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200436 if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
437 POLARSSL_DECRYPT ) != 0 )
438 {
439 fprintf( stderr, "cipher_setkey() returned error\n" );
440 goto exit;
441 }
442
443 if( cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
444 {
445 fprintf( stderr, "cipher_set_iv() returned error\n" );
446 goto exit;
447 }
448
449 if( cipher_reset( &cipher_ctx ) != 0 )
450 {
451 fprintf( stderr, "cipher_reset() returned error\n" );
452 goto exit;
453 }
Paul Bakker20a78082011-01-21 09:32:12 +0000454
455 md_hmac_starts( &md_ctx, digest, 32 );
456
457 /*
458 * Decrypt and write the plaintext.
459 */
460 for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) )
461 {
462 if( fread( buffer, 1, cipher_get_block_size( &cipher_ctx ), fin ) !=
463 (size_t) cipher_get_block_size( &cipher_ctx ) )
464 {
465 fprintf( stderr, "fread(%d bytes) failed\n",
466 cipher_get_block_size( &cipher_ctx ) );
467 goto exit;
468 }
469
470 md_hmac_update( &md_ctx, buffer, cipher_get_block_size( &cipher_ctx ) );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200471 if( cipher_update( &cipher_ctx, buffer,
472 cipher_get_block_size( &cipher_ctx ),
473 output, &olen ) != 0 )
474 {
475 fprintf( stderr, "cipher_update() returned error\n" );
476 goto exit;
477 }
Paul Bakker20a78082011-01-21 09:32:12 +0000478
Paul Bakker2c0994e2011-05-25 13:51:57 +0000479 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000480 {
Paul Bakker2c0994e2011-05-25 13:51:57 +0000481 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000482 goto exit;
483 }
484 }
485
486 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000487 * Verify the message authentication code.
488 */
489 md_hmac_finish( &md_ctx, digest );
490
491 if( fread( buffer, 1, md_get_size( md_info ), fin ) != md_get_size( md_info ) )
492 {
493 fprintf( stderr, "fread(%d bytes) failed\n", md_get_size( md_info ) );
494 goto exit;
495 }
496
Paul Bakker424cd692013-10-31 14:22:08 +0100497 /* Use constant-time buffer comparison */
498 diff = 0;
499 for( i = 0; i < md_get_size( md_info ); i++ )
500 diff |= digest[i] ^ buffer[i];
501
502 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000503 {
504 fprintf( stderr, "HMAC check failed: wrong key, "
505 "or file corrupted.\n" );
506 goto exit;
507 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100508
509 /*
510 * Write the final block of data
511 */
512 cipher_finish( &cipher_ctx, output, &olen );
513
514 if( fwrite( output, 1, olen, fout ) != olen )
515 {
516 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
517 goto exit;
518 }
Paul Bakker20a78082011-01-21 09:32:12 +0000519 }
520
521 ret = 0;
522
523exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000524 if( fin )
525 fclose( fin );
526 if( fout )
527 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000528
529 memset( buffer, 0, sizeof( buffer ) );
530 memset( digest, 0, sizeof( digest ) );
531
532 cipher_free_ctx( &cipher_ctx );
533 md_free_ctx( &md_ctx );
534
535 return( ret );
536}
Paul Bakker5690efc2011-05-26 13:16:06 +0000537#endif /* POLARSSL_CIPHER_C && POLARSSL_MD_C */