blob: 7a0e4549725d1c7407c850bfb3511fb3e4bf1ac1 [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 *
5 * Copyright (C) 2006-2010, Brainspark B.V.
6 *
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
27#ifndef _CRT_SECURE_NO_DEPRECATE
28#define _CRT_SECURE_NO_DEPRECATE 1
29#endif
30
Paul Bakker494c0b82011-04-24 15:30:07 +000031#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000032#include <windows.h>
33#include <io.h>
34#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
Paul Bakker5690efc2011-05-26 13:16:06 +000044#include "polarssl/config.h"
45
Paul Bakker20a78082011-01-21 09:32:12 +000046#include "polarssl/cipher.h"
47#include "polarssl/md.h"
48
49#define MODE_ENCRYPT 0
50#define MODE_DECRYPT 1
51
52#define USAGE \
53 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <md> <key>\n" \
54 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
55 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
56 "\n"
57
Paul Bakker5690efc2011-05-26 13:16:06 +000058#if !defined(POLARSSL_CIPHER_C) || !defined(POLARSSL_MD_C)
59int main( void )
60{
61 printf("POLARSSL_CIPHER_C and/or POLARSSL_MD_C not defined.\n");
62 return( 0 );
63}
64#else
Paul Bakker20a78082011-01-21 09:32:12 +000065int main( int argc, char *argv[] )
66{
Paul Bakker25b5fe52011-05-26 14:02:58 +000067 int ret = 1, i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000068 int mode, lastn;
Paul Bakker25b5fe52011-05-26 14:02:58 +000069 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000070 FILE *fkey, *fin = NULL, *fout = NULL;
71
72 char *p;
73 unsigned char IV[16];
74 unsigned char key[512];
75 unsigned char digest[POLARSSL_MD_MAX_SIZE];
76 unsigned char buffer[1024];
77 unsigned char output[1024];
78
79 const cipher_info_t *cipher_info;
80 const md_info_t *md_info;
81 cipher_context_t cipher_ctx;
82 md_context_t md_ctx;
83#if defined(WIN32)
84 LARGE_INTEGER li_size;
85 __int64 filesize, offset;
86#else
87 off_t filesize, offset;
88#endif
89
90 memset( &cipher_ctx, 0, sizeof( cipher_context_t ));
91 memset( &md_ctx, 0, sizeof( md_context_t ));
92
93 /*
94 * Parse the command-line arguments.
95 */
96 if( argc != 7 )
97 {
98 const int *list;
99
100 printf( USAGE );
101
102 printf( "Available ciphers:\n" );
103 list = cipher_list();
104 while( *list )
105 {
106 cipher_info = cipher_info_from_type( *list );
107 printf( " %s\n", cipher_info->name );
108 list++;
109 }
110
111 printf( "\nAvailable message digests:\n" );
112 list = md_list();
113 while( *list )
114 {
115 md_info = md_info_from_type( *list );
116 printf( " %s\n", md_info->name );
117 list++;
118 }
119
120#if defined(WIN32)
121 printf( "\n Press Enter to exit this program.\n" );
122 fflush( stdout ); getchar();
123#endif
124
125 goto exit;
126 }
127
128 mode = atoi( argv[1] );
129
130 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
131 {
132 fprintf( stderr, "invalid operation mode\n" );
133 goto exit;
134 }
135
136 if( strcmp( argv[2], argv[3] ) == 0 )
137 {
138 fprintf( stderr, "input and output filenames must differ\n" );
139 goto exit;
140 }
141
142 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
143 {
144 fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
145 goto exit;
146 }
147
148 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
149 {
150 fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
151 goto exit;
152 }
153
154 /*
155 * Read the Cipher and MD from the command line
156 */
157 cipher_info = cipher_info_from_string( argv[4] );
158 if( cipher_info == NULL )
159 {
160 fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
161 goto exit;
162 }
163 cipher_init_ctx( &cipher_ctx, cipher_info);
164
165 md_info = md_info_from_string( argv[5] );
166 if( md_info == NULL )
167 {
168 fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
169 goto exit;
170 }
171 md_init_ctx( &md_ctx, md_info);
172
173 /*
174 * Read the secret key and clean the command line.
175 */
176 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
177 {
178 keylen = fread( key, 1, sizeof( key ), fkey );
179 fclose( fkey );
180 }
181 else
182 {
183 if( memcmp( argv[6], "hex:", 4 ) == 0 )
184 {
185 p = &argv[6][4];
186 keylen = 0;
187
188 while( sscanf( p, "%02X", &n ) > 0 &&
189 keylen < (int) sizeof( key ) )
190 {
191 key[keylen++] = (unsigned char) n;
192 p += 2;
193 }
194 }
195 else
196 {
197 keylen = strlen( argv[6] );
198
199 if( keylen > (int) sizeof( key ) )
200 keylen = (int) sizeof( key );
201
202 memcpy( key, argv[6], keylen );
203 }
204 }
205
206 memset( argv[6], 0, strlen( argv[6] ) );
207
208#if defined(WIN32)
209 /*
210 * Support large files (> 2Gb) on Win32
211 */
212 li_size.QuadPart = 0;
213 li_size.LowPart =
214 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
215 li_size.LowPart, &li_size.HighPart, FILE_END );
216
217 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
218 {
219 fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
220 goto exit;
221 }
222
223 filesize = li_size.QuadPart;
224#else
225 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
226 {
227 perror( "lseek" );
228 goto exit;
229 }
230#endif
231
232 if( fseek( fin, 0, SEEK_SET ) < 0 )
233 {
234 fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
235 goto exit;
236 }
237
238 if( mode == MODE_ENCRYPT )
239 {
240 /*
241 * Generate the initialization vector as:
242 * IV = SHA-256( filesize || filename )[0..15]
243 */
244 for( i = 0; i < 8; i++ )
245 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
246
247 p = argv[2];
248
249 md_starts( &md_ctx );
250 md_update( &md_ctx, buffer, 8 );
251 md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
252 md_finish( &md_ctx, digest );
253
254 memcpy( IV, digest, 16 );
255
256 /*
257 * The last four bits in the IV are actually used
258 * to store the file size modulo the AES block size.
259 */
260 lastn = (int)( filesize & 0x0F );
261
262 IV[15] = (unsigned char)
263 ( ( IV[15] & 0xF0 ) | lastn );
264
265 /*
266 * Append the IV at the beginning of the output.
267 */
268 if( fwrite( IV, 1, 16, fout ) != 16 )
269 {
270 fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
271 goto exit;
272 }
273
274 /*
275 * Hash the IV and the secret key together 8192 times
276 * using the result to setup the AES context and HMAC.
277 */
278 memset( digest, 0, 32 );
279 memcpy( digest, IV, 16 );
280
281 for( i = 0; i < 8192; i++ )
282 {
283 md_starts( &md_ctx );
284 md_update( &md_ctx, digest, 32 );
285 md_update( &md_ctx, key, keylen );
286 md_finish( &md_ctx, digest );
287
288 }
289
290 memset( key, 0, sizeof( key ) );
291
292 cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
293 POLARSSL_ENCRYPT );
294 cipher_reset( &cipher_ctx, IV);
295
296 md_hmac_starts( &md_ctx, digest, 32 );
297
298 /*
299 * Encrypt and write the ciphertext.
300 */
301 for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) )
302 {
Paul Bakker25b5fe52011-05-26 14:02:58 +0000303 ilen = ( (unsigned int) filesize - offset > cipher_get_block_size( &cipher_ctx ) ) ?
Paul Bakker23986e52011-04-24 08:57:21 +0000304 cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000305
Paul Bakker25b5fe52011-05-26 14:02:58 +0000306 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000307 {
Paul Bakker2c0994e2011-05-25 13:51:57 +0000308 fprintf( stderr, "fread(%ld bytes) failed\n", (long) n );
Paul Bakker20a78082011-01-21 09:32:12 +0000309 goto exit;
310 }
311
Paul Bakker25b5fe52011-05-26 14:02:58 +0000312 cipher_update( &cipher_ctx, buffer, ilen, output, &olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000313 md_hmac_update( &md_ctx, output, olen );
314
Paul Bakker2c0994e2011-05-25 13:51:57 +0000315 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000316 {
Paul Bakker2c0994e2011-05-25 13:51:57 +0000317 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000318 goto exit;
319 }
320 }
321
322 cipher_finish( &cipher_ctx, output, &olen );
323 md_hmac_update( &md_ctx, output, olen );
324
Paul Bakker2c0994e2011-05-25 13:51:57 +0000325 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000326 {
Paul Bakker25b5fe52011-05-26 14:02:58 +0000327 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000328 goto exit;
329 }
330 /*
331 * Finally write the HMAC.
332 */
333 md_hmac_finish( &md_ctx, digest );
334
335 if( fwrite( digest, 1, md_get_size( md_info), fout ) != md_get_size( md_info) )
336 {
337 fprintf( stderr, "fwrite(%d bytes) failed\n", md_get_size( md_info) );
338 goto exit;
339 }
340 }
341
342 if( mode == MODE_DECRYPT )
343 {
344 /*
345 * The encrypted file must be structured as follows:
346 *
347 * 00 .. 15 Initialization Vector
348 * 16 .. 31 AES Encrypted Block #1
349 * ..
350 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
351 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
352 */
353 if( filesize < 16 + md_get_size( md_info) )
354 {
355 fprintf( stderr, "File too short to be encrypted.\n" );
356 goto exit;
357 }
358
359 if( ( filesize & 0x0F ) != 0 )
360 {
361 fprintf( stderr, "File size not a multiple of 16.\n" );
362 goto exit;
363 }
364
365 /*
366 * Substract the IV + HMAC length.
367 */
368 filesize -= ( 16 + md_get_size( md_info ) );
369
370 /*
371 * Read the IV and original filesize modulo 16.
372 */
373 if( fread( buffer, 1, 16, fin ) != 16 )
374 {
375 fprintf( stderr, "fread(%d bytes) failed\n", 16 );
376 goto exit;
377 }
378
379 memcpy( IV, buffer, 16 );
380 lastn = IV[15] & 0x0F;
381
382 /*
383 * Hash the IV and the secret key together 8192 times
384 * using the result to setup the AES context and HMAC.
385 */
386 memset( digest, 0, 32 );
387 memcpy( digest, IV, 16 );
388
389 for( i = 0; i < 8192; i++ )
390 {
391 md_starts( &md_ctx );
392 md_update( &md_ctx, digest, 32 );
393 md_update( &md_ctx, key, keylen );
394 md_finish( &md_ctx, digest );
395 }
396
397 memset( key, 0, sizeof( key ) );
398
399 cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
400 POLARSSL_DECRYPT );
401 cipher_reset( &cipher_ctx, IV);
402
403 md_hmac_starts( &md_ctx, digest, 32 );
404
405 /*
406 * Decrypt and write the plaintext.
407 */
408 for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) )
409 {
410 if( fread( buffer, 1, cipher_get_block_size( &cipher_ctx ), fin ) !=
411 (size_t) cipher_get_block_size( &cipher_ctx ) )
412 {
413 fprintf( stderr, "fread(%d bytes) failed\n",
414 cipher_get_block_size( &cipher_ctx ) );
415 goto exit;
416 }
417
418 md_hmac_update( &md_ctx, buffer, cipher_get_block_size( &cipher_ctx ) );
419 cipher_update( &cipher_ctx, buffer, cipher_get_block_size( &cipher_ctx ),
420 output, &olen );
421
Paul Bakker2c0994e2011-05-25 13:51:57 +0000422 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000423 {
Paul Bakker2c0994e2011-05-25 13:51:57 +0000424 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000425 goto exit;
426 }
427 }
428
429 /*
430 * Write the final block of data
431 */
432 cipher_finish( &cipher_ctx, output, &olen );
433
Paul Bakker2c0994e2011-05-25 13:51:57 +0000434 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000435 {
Paul Bakker2c0994e2011-05-25 13:51:57 +0000436 fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000437 goto exit;
438 }
439
440 /*
441 * Verify the message authentication code.
442 */
443 md_hmac_finish( &md_ctx, digest );
444
445 if( fread( buffer, 1, md_get_size( md_info ), fin ) != md_get_size( md_info ) )
446 {
447 fprintf( stderr, "fread(%d bytes) failed\n", md_get_size( md_info ) );
448 goto exit;
449 }
450
451 if( memcmp( digest, buffer, md_get_size( md_info ) ) != 0 )
452 {
453 fprintf( stderr, "HMAC check failed: wrong key, "
454 "or file corrupted.\n" );
455 goto exit;
456 }
457 }
458
459 ret = 0;
460
461exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000462 if( fin )
463 fclose( fin );
464 if( fout )
465 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000466
467 memset( buffer, 0, sizeof( buffer ) );
468 memset( digest, 0, sizeof( digest ) );
469
470 cipher_free_ctx( &cipher_ctx );
471 md_free_ctx( &md_ctx );
472
473 return( ret );
474}
Paul Bakker5690efc2011-05-26 13:16:06 +0000475#endif /* POLARSSL_CIPHER_C && POLARSSL_MD_C */