blob: d2845de8866468e18c72e1eee2d4f053bc317e33 [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];
79
80 const cipher_info_t *cipher_info;
81 const md_info_t *md_info;
82 cipher_context_t cipher_ctx;
83 md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +000084#if defined(_WIN32_WCE)
85 long filesize, offset;
86#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000087 LARGE_INTEGER li_size;
88 __int64 filesize, offset;
89#else
90 off_t filesize, offset;
91#endif
92
93 memset( &cipher_ctx, 0, sizeof( cipher_context_t ));
94 memset( &md_ctx, 0, sizeof( md_context_t ));
95
96 /*
97 * Parse the command-line arguments.
98 */
99 if( argc != 7 )
100 {
101 const int *list;
102
103 printf( USAGE );
104
105 printf( "Available ciphers:\n" );
106 list = cipher_list();
107 while( *list )
108 {
109 cipher_info = cipher_info_from_type( *list );
110 printf( " %s\n", cipher_info->name );
111 list++;
112 }
113
114 printf( "\nAvailable message digests:\n" );
115 list = md_list();
116 while( *list )
117 {
118 md_info = md_info_from_type( *list );
119 printf( " %s\n", md_info->name );
120 list++;
121 }
122
Paul Bakkercce9d772011-11-18 14:26:47 +0000123#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000124 printf( "\n Press Enter to exit this program.\n" );
125 fflush( stdout ); getchar();
126#endif
127
128 goto exit;
129 }
130
131 mode = atoi( argv[1] );
132
133 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
134 {
135 fprintf( stderr, "invalid operation mode\n" );
136 goto exit;
137 }
138
139 if( strcmp( argv[2], argv[3] ) == 0 )
140 {
141 fprintf( stderr, "input and output filenames must differ\n" );
142 goto exit;
143 }
144
145 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
146 {
147 fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
148 goto exit;
149 }
150
151 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
152 {
153 fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
154 goto exit;
155 }
156
157 /*
158 * Read the Cipher and MD from the command line
159 */
160 cipher_info = cipher_info_from_string( argv[4] );
161 if( cipher_info == NULL )
162 {
163 fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
164 goto exit;
165 }
166 cipher_init_ctx( &cipher_ctx, cipher_info);
167
168 md_info = md_info_from_string( argv[5] );
169 if( md_info == NULL )
170 {
171 fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
172 goto exit;
173 }
174 md_init_ctx( &md_ctx, md_info);
175
176 /*
177 * Read the secret key and clean the command line.
178 */
179 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
180 {
181 keylen = fread( key, 1, sizeof( key ), fkey );
182 fclose( fkey );
183 }
184 else
185 {
186 if( memcmp( argv[6], "hex:", 4 ) == 0 )
187 {
188 p = &argv[6][4];
189 keylen = 0;
190
191 while( sscanf( p, "%02X", &n ) > 0 &&
192 keylen < (int) sizeof( key ) )
193 {
194 key[keylen++] = (unsigned char) n;
195 p += 2;
196 }
197 }
198 else
199 {
200 keylen = strlen( argv[6] );
201
202 if( keylen > (int) sizeof( key ) )
203 keylen = (int) sizeof( key );
204
205 memcpy( key, argv[6], keylen );
206 }
207 }
208
209 memset( argv[6], 0, strlen( argv[6] ) );
210
Paul Bakkercce9d772011-11-18 14:26:47 +0000211#if defined(_WIN32_WCE)
212 filesize = fseek( fin, 0L, SEEK_END );
213#else
214#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000215 /*
216 * Support large files (> 2Gb) on Win32
217 */
218 li_size.QuadPart = 0;
219 li_size.LowPart =
220 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
221 li_size.LowPart, &li_size.HighPart, FILE_END );
222
223 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
224 {
225 fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
226 goto exit;
227 }
228
229 filesize = li_size.QuadPart;
230#else
231 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
232 {
233 perror( "lseek" );
234 goto exit;
235 }
236#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000237#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000238
239 if( fseek( fin, 0, SEEK_SET ) < 0 )
240 {
241 fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
242 goto exit;
243 }
244
245 if( mode == MODE_ENCRYPT )
246 {
247 /*
248 * Generate the initialization vector as:
249 * IV = SHA-256( filesize || filename )[0..15]
250 */
251 for( i = 0; i < 8; i++ )
252 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
253
254 p = argv[2];
255
256 md_starts( &md_ctx );
257 md_update( &md_ctx, buffer, 8 );
258 md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
259 md_finish( &md_ctx, digest );
260
261 memcpy( IV, digest, 16 );
262
263 /*
264 * The last four bits in the IV are actually used
265 * to store the file size modulo the AES block size.
266 */
267 lastn = (int)( filesize & 0x0F );
268
269 IV[15] = (unsigned char)
270 ( ( IV[15] & 0xF0 ) | lastn );
271
272 /*
273 * Append the IV at the beginning of the output.
274 */
275 if( fwrite( IV, 1, 16, fout ) != 16 )
276 {
277 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
278 goto exit;
279 }
280
281 /*
282 * Hash the IV and the secret key together 8192 times
283 * using the result to setup the AES context and HMAC.
284 */
285 memset( digest, 0, 32 );
286 memcpy( digest, IV, 16 );
287
288 for( i = 0; i < 8192; i++ )
289 {
290 md_starts( &md_ctx );
291 md_update( &md_ctx, digest, 32 );
292 md_update( &md_ctx, key, keylen );
293 md_finish( &md_ctx, digest );
294
295 }
296
297 memset( key, 0, sizeof( key ) );
298
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000299 if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
300 POLARSSL_ENCRYPT ) != 0 )
301 {
302 fprintf( stderr, "cipher_setkey() returned error\n");
303 goto exit;
304 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200305 if( cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
306 {
307 fprintf( stderr, "cipher_set_iv() returned error\n");
308 goto exit;
309 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200310 if( cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000311 {
312 fprintf( stderr, "cipher_reset() returned error\n");
313 goto exit;
314 }
Paul Bakker20a78082011-01-21 09:32:12 +0000315
316 md_hmac_starts( &md_ctx, digest, 32 );
317
318 /*
319 * Encrypt and write the ciphertext.
320 */
321 for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) )
322 {
Paul Bakker25b5fe52011-05-26 14:02:58 +0000323 ilen = ( (unsigned int) filesize - offset > cipher_get_block_size( &cipher_ctx ) ) ?
Paul Bakker23986e52011-04-24 08:57:21 +0000324 cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000325
Paul Bakker25b5fe52011-05-26 14:02:58 +0000326 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000327 {
Paul Bakker2c0994e2011-05-25 13:51:57 +0000328 fprintf( stderr, "fread(%ld bytes) failed\n", (long) n );
Paul Bakker20a78082011-01-21 09:32:12 +0000329 goto exit;
330 }
331
Paul Bakker25b5fe52011-05-26 14:02:58 +0000332 cipher_update( &cipher_ctx, buffer, ilen, output, &olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000333 md_hmac_update( &md_ctx, output, olen );
334
Paul Bakker2c0994e2011-05-25 13:51:57 +0000335 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000336 {
Paul Bakker2c0994e2011-05-25 13:51:57 +0000337 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000338 goto exit;
339 }
340 }
341
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200342 if( cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000343 {
344 fprintf( stderr, "cipher_finish() returned error\n" );
345 goto exit;
346 }
Paul Bakker20a78082011-01-21 09:32:12 +0000347 md_hmac_update( &md_ctx, output, olen );
348
Paul Bakker2c0994e2011-05-25 13:51:57 +0000349 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000350 {
Paul Bakker25b5fe52011-05-26 14:02:58 +0000351 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000352 goto exit;
353 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000354
Paul Bakker20a78082011-01-21 09:32:12 +0000355 /*
356 * Finally write the HMAC.
357 */
358 md_hmac_finish( &md_ctx, digest );
359
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000360 if( fwrite( digest, 1, md_get_size( md_info ), fout ) != md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000361 {
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000362 fprintf( stderr, "fwrite(%d bytes) failed\n", md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000363 goto exit;
364 }
365 }
366
367 if( mode == MODE_DECRYPT )
368 {
369 /*
370 * The encrypted file must be structured as follows:
371 *
372 * 00 .. 15 Initialization Vector
373 * 16 .. 31 AES Encrypted Block #1
374 * ..
375 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
376 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
377 */
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000378 if( filesize < 16 + md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000379 {
380 fprintf( stderr, "File too short to be encrypted.\n" );
381 goto exit;
382 }
383
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000384 if( ( ( filesize - md_get_size( md_info ) ) %
385 cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000386 {
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000387 fprintf( stderr, "File content not a multiple of the block size (%d).\n",
388 cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000389 goto exit;
390 }
391
392 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100393 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000394 */
395 filesize -= ( 16 + md_get_size( md_info ) );
396
397 /*
398 * Read the IV and original filesize modulo 16.
399 */
400 if( fread( buffer, 1, 16, fin ) != 16 )
401 {
402 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
403 goto exit;
404 }
405
406 memcpy( IV, buffer, 16 );
407 lastn = IV[15] & 0x0F;
408
409 /*
410 * Hash the IV and the secret key together 8192 times
411 * using the result to setup the AES context and HMAC.
412 */
413 memset( digest, 0, 32 );
414 memcpy( digest, IV, 16 );
415
416 for( i = 0; i < 8192; i++ )
417 {
418 md_starts( &md_ctx );
419 md_update( &md_ctx, digest, 32 );
420 md_update( &md_ctx, key, keylen );
421 md_finish( &md_ctx, digest );
422 }
423
424 memset( key, 0, sizeof( key ) );
425
426 cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
427 POLARSSL_DECRYPT );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200428 cipher_set_iv( &cipher_ctx, IV, 16 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200429 cipher_reset( &cipher_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000430
431 md_hmac_starts( &md_ctx, digest, 32 );
432
433 /*
434 * Decrypt and write the plaintext.
435 */
436 for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) )
437 {
438 if( fread( buffer, 1, cipher_get_block_size( &cipher_ctx ), fin ) !=
439 (size_t) cipher_get_block_size( &cipher_ctx ) )
440 {
441 fprintf( stderr, "fread(%d bytes) failed\n",
442 cipher_get_block_size( &cipher_ctx ) );
443 goto exit;
444 }
445
446 md_hmac_update( &md_ctx, buffer, cipher_get_block_size( &cipher_ctx ) );
447 cipher_update( &cipher_ctx, buffer, cipher_get_block_size( &cipher_ctx ),
448 output, &olen );
449
Paul Bakker2c0994e2011-05-25 13:51:57 +0000450 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000451 {
Paul Bakker2c0994e2011-05-25 13:51:57 +0000452 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000453 goto exit;
454 }
455 }
456
457 /*
458 * Write the final block of data
459 */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200460 cipher_finish( &cipher_ctx, output, &olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000461
Paul Bakker2c0994e2011-05-25 13:51:57 +0000462 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000463 {
Paul Bakker2c0994e2011-05-25 13:51:57 +0000464 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000465 goto exit;
466 }
467
468 /*
469 * Verify the message authentication code.
470 */
471 md_hmac_finish( &md_ctx, digest );
472
473 if( fread( buffer, 1, md_get_size( md_info ), fin ) != md_get_size( md_info ) )
474 {
475 fprintf( stderr, "fread(%d bytes) failed\n", md_get_size( md_info ) );
476 goto exit;
477 }
478
479 if( memcmp( digest, buffer, md_get_size( md_info ) ) != 0 )
480 {
481 fprintf( stderr, "HMAC check failed: wrong key, "
482 "or file corrupted.\n" );
483 goto exit;
484 }
485 }
486
487 ret = 0;
488
489exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000490 if( fin )
491 fclose( fin );
492 if( fout )
493 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000494
495 memset( buffer, 0, sizeof( buffer ) );
496 memset( digest, 0, sizeof( digest ) );
497
498 cipher_free_ctx( &cipher_ctx );
499 md_free_ctx( &md_ctx );
500
501 return( ret );
502}
Paul Bakker5690efc2011-05-26 13:16:06 +0000503#endif /* POLARSSL_CIPHER_C && POLARSSL_MD_C */