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