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