blob: 22f283443ab41ce539967c4688f17e34f62d5053 [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 *
Simon Butcher31d7f5b2016-09-03 12:39:38 +01005 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
Paul Bakker20a78082011-01-21 09:32:12 +00006 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00007 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker20a78082011-01-21 09:32:12 +00008 *
Paul Bakker20a78082011-01-21 09:32:12 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020025#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#else
27#include POLARSSL_CONFIG_FILE
28#endif
Paul Bakker20a78082011-01-21 09:32:12 +000029
Rich Evansf90016a2015-01-19 14:26:37 +000030#if defined(POLARSSL_PLATFORM_C)
31#include "polarssl/platform.h"
32#else
Rich Evans18b78c72015-02-11 14:06:19 +000033#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000034#define polarssl_fprintf fprintf
Rich Evans18b78c72015-02-11 14:06:19 +000035#define polarssl_printf printf
36#endif
37
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +000038#if defined(POLARSSL_CIPHER_C) && defined(POLARSSL_MD_C) && \
Rich Evans18b78c72015-02-11 14:06:19 +000039 defined(POLARSSL_FS_IO)
40#include "polarssl/cipher.h"
41#include "polarssl/md.h"
42
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000046#endif
47
Paul Bakker494c0b82011-04-24 15:30:07 +000048#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000049#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000050#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000051#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000052#endif
Paul Bakker20a78082011-01-21 09:32:12 +000053#else
54#include <sys/types.h>
55#include <unistd.h>
56#endif
57
Paul Bakker20a78082011-01-21 09:32:12 +000058#define MODE_ENCRYPT 0
59#define MODE_DECRYPT 1
60
61#define USAGE \
62 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <md> <key>\n" \
63 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
64 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
65 "\n"
66
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +000067#if !defined(POLARSSL_CIPHER_C) || !defined(POLARSSL_MD_C) || \
Rich Evans18b78c72015-02-11 14:06:19 +000068 !defined(POLARSSL_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000069int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000070{
Rich Evans18b78c72015-02-11 14:06:19 +000071 polarssl_printf("POLARSSL_CIPHER_C and/or POLARSSL_MD_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000072 return( 0 );
73}
74#else
Paul Bakker20a78082011-01-21 09:32:12 +000075int main( int argc, char *argv[] )
76{
Paul Bakker25b5fe52011-05-26 14:02:58 +000077 int ret = 1, i, n;
Simon Butcher31d7f5b2016-09-03 12:39:38 +010078 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +000079 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000080 FILE *fkey, *fin = NULL, *fout = NULL;
81
82 char *p;
83 unsigned char IV[16];
84 unsigned char key[512];
85 unsigned char digest[POLARSSL_MD_MAX_SIZE];
86 unsigned char buffer[1024];
87 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +010088 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +000089
90 const cipher_info_t *cipher_info;
91 const md_info_t *md_info;
92 cipher_context_t cipher_ctx;
93 md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +000094#if defined(_WIN32_WCE)
95 long filesize, offset;
96#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000097 LARGE_INTEGER li_size;
98 __int64 filesize, offset;
99#else
100 off_t filesize, offset;
101#endif
102
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200103 cipher_init( &cipher_ctx );
104 md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000105
106 /*
107 * Parse the command-line arguments.
108 */
109 if( argc != 7 )
110 {
111 const int *list;
112
Rich Evansf90016a2015-01-19 14:26:37 +0000113 polarssl_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000114
Rich Evansf90016a2015-01-19 14:26:37 +0000115 polarssl_printf( "Available ciphers:\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000116 list = cipher_list();
117 while( *list )
118 {
119 cipher_info = cipher_info_from_type( *list );
Rich Evansf90016a2015-01-19 14:26:37 +0000120 polarssl_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000121 list++;
122 }
123
Rich Evansf90016a2015-01-19 14:26:37 +0000124 polarssl_printf( "\nAvailable message digests:\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000125 list = md_list();
126 while( *list )
127 {
128 md_info = md_info_from_type( *list );
Rich Evansf90016a2015-01-19 14:26:37 +0000129 polarssl_printf( " %s\n", md_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000130 list++;
131 }
132
Paul Bakkercce9d772011-11-18 14:26:47 +0000133#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000134 polarssl_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000135 fflush( stdout ); getchar();
136#endif
137
138 goto exit;
139 }
140
141 mode = atoi( argv[1] );
142
143 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
144 {
Rich Evansf90016a2015-01-19 14:26:37 +0000145 polarssl_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000146 goto exit;
147 }
148
149 if( strcmp( argv[2], argv[3] ) == 0 )
150 {
Rich Evansf90016a2015-01-19 14:26:37 +0000151 polarssl_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000152 goto exit;
153 }
154
155 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
156 {
Rich Evansf90016a2015-01-19 14:26:37 +0000157 polarssl_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000158 goto exit;
159 }
160
161 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
162 {
Rich Evansf90016a2015-01-19 14:26:37 +0000163 polarssl_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000164 goto exit;
165 }
166
167 /*
168 * Read the Cipher and MD from the command line
169 */
170 cipher_info = cipher_info_from_string( argv[4] );
171 if( cipher_info == NULL )
172 {
Rich Evansf90016a2015-01-19 14:26:37 +0000173 polarssl_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000174 goto exit;
175 }
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200176 if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info) ) != 0 )
177 {
Rich Evansf90016a2015-01-19 14:26:37 +0000178 polarssl_fprintf( stderr, "cipher_init_ctx failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200179 goto exit;
180 }
Paul Bakker20a78082011-01-21 09:32:12 +0000181
182 md_info = md_info_from_string( argv[5] );
183 if( md_info == NULL )
184 {
Rich Evansf90016a2015-01-19 14:26:37 +0000185 polarssl_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000186 goto exit;
187 }
188 md_init_ctx( &md_ctx, md_info);
189
190 /*
Hanno Beckerdcbb0242017-06-27 11:41:45 +0100191 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000192 */
193 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
194 {
195 keylen = fread( key, 1, sizeof( key ), fkey );
196 fclose( fkey );
197 }
198 else
199 {
200 if( memcmp( argv[6], "hex:", 4 ) == 0 )
201 {
202 p = &argv[6][4];
203 keylen = 0;
204
205 while( sscanf( p, "%02X", &n ) > 0 &&
206 keylen < (int) sizeof( key ) )
207 {
208 key[keylen++] = (unsigned char) n;
209 p += 2;
210 }
211 }
212 else
213 {
214 keylen = strlen( argv[6] );
215
216 if( keylen > (int) sizeof( key ) )
217 keylen = (int) sizeof( key );
218
219 memcpy( key, argv[6], keylen );
220 }
221 }
222
Paul Bakkercce9d772011-11-18 14:26:47 +0000223#if defined(_WIN32_WCE)
224 filesize = fseek( fin, 0L, SEEK_END );
225#else
226#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000227 /*
228 * Support large files (> 2Gb) on Win32
229 */
230 li_size.QuadPart = 0;
231 li_size.LowPart =
232 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
233 li_size.LowPart, &li_size.HighPart, FILE_END );
234
235 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
236 {
Rich Evansf90016a2015-01-19 14:26:37 +0000237 polarssl_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000238 goto exit;
239 }
240
241 filesize = li_size.QuadPart;
242#else
243 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
244 {
245 perror( "lseek" );
246 goto exit;
247 }
248#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000249#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000250
251 if( fseek( fin, 0, SEEK_SET ) < 0 )
252 {
Rich Evansf90016a2015-01-19 14:26:37 +0000253 polarssl_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000254 goto exit;
255 }
256
257 if( mode == MODE_ENCRYPT )
258 {
259 /*
260 * Generate the initialization vector as:
Simon Butcher31d7f5b2016-09-03 12:39:38 +0100261 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000262 */
263 for( i = 0; i < 8; i++ )
264 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
265
266 p = argv[2];
267
268 md_starts( &md_ctx );
269 md_update( &md_ctx, buffer, 8 );
270 md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
271 md_finish( &md_ctx, digest );
272
273 memcpy( IV, digest, 16 );
274
275 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000276 * Append the IV at the beginning of the output.
277 */
278 if( fwrite( IV, 1, 16, fout ) != 16 )
279 {
Rich Evansf90016a2015-01-19 14:26:37 +0000280 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000281 goto exit;
282 }
283
284 /*
285 * Hash the IV and the secret key together 8192 times
286 * using the result to setup the AES context and HMAC.
287 */
288 memset( digest, 0, 32 );
289 memcpy( digest, IV, 16 );
290
291 for( i = 0; i < 8192; i++ )
292 {
293 md_starts( &md_ctx );
294 md_update( &md_ctx, digest, 32 );
295 md_update( &md_ctx, key, keylen );
296 md_finish( &md_ctx, digest );
297
298 }
299
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000300 if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
301 POLARSSL_ENCRYPT ) != 0 )
302 {
Rich Evansf90016a2015-01-19 14:26:37 +0000303 polarssl_fprintf( stderr, "cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000304 goto exit;
305 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200306 if( cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
307 {
Rich Evansf90016a2015-01-19 14:26:37 +0000308 polarssl_fprintf( stderr, "cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200309 goto exit;
310 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200311 if( cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000312 {
Rich Evansf90016a2015-01-19 14:26:37 +0000313 polarssl_fprintf( stderr, "cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000314 goto exit;
315 }
Paul Bakker20a78082011-01-21 09:32:12 +0000316
317 md_hmac_starts( &md_ctx, digest, 32 );
318
319 /*
320 * Encrypt and write the ciphertext.
321 */
322 for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) )
323 {
Paul Bakker25b5fe52011-05-26 14:02:58 +0000324 ilen = ( (unsigned int) filesize - offset > cipher_get_block_size( &cipher_ctx ) ) ?
Paul Bakker23986e52011-04-24 08:57:21 +0000325 cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000326
Paul Bakker25b5fe52011-05-26 14:02:58 +0000327 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000328 {
Rich Evansf90016a2015-01-19 14:26:37 +0000329 polarssl_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000330 goto exit;
331 }
332
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200333 if( cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
334 {
Rich Evansf90016a2015-01-19 14:26:37 +0000335 polarssl_fprintf( stderr, "cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200336 goto exit;
337 }
338
Paul Bakker20a78082011-01-21 09:32:12 +0000339 md_hmac_update( &md_ctx, output, olen );
340
Paul Bakker2c0994e2011-05-25 13:51:57 +0000341 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000342 {
Rich Evansf90016a2015-01-19 14:26:37 +0000343 polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000344 goto exit;
345 }
346 }
347
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200348 if( cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000349 {
Rich Evansf90016a2015-01-19 14:26:37 +0000350 polarssl_fprintf( stderr, "cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000351 goto exit;
352 }
Paul Bakker20a78082011-01-21 09:32:12 +0000353 md_hmac_update( &md_ctx, output, olen );
354
Paul Bakker2c0994e2011-05-25 13:51:57 +0000355 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000356 {
Rich Evansf90016a2015-01-19 14:26:37 +0000357 polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000358 goto exit;
359 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000360
Paul Bakker20a78082011-01-21 09:32:12 +0000361 /*
362 * Finally write the HMAC.
363 */
364 md_hmac_finish( &md_ctx, digest );
365
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000366 if( fwrite( digest, 1, md_get_size( md_info ), fout ) != md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000367 {
Rich Evansf90016a2015-01-19 14:26:37 +0000368 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000369 goto exit;
370 }
371 }
372
373 if( mode == MODE_DECRYPT )
374 {
375 /*
376 * The encrypted file must be structured as follows:
377 *
378 * 00 .. 15 Initialization Vector
Simon Butcher31d7f5b2016-09-03 12:39:38 +0100379 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000380 * ..
Simon Butcher31d7f5b2016-09-03 12:39:38 +0100381 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
382 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000383 */
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000384 if( filesize < 16 + md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000385 {
Rich Evansf90016a2015-01-19 14:26:37 +0000386 polarssl_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000387 goto exit;
388 }
389
Simon Butcher31d7f5b2016-09-03 12:39:38 +0100390 /*
391 * Check the file size.
392 */
393 if( cipher_info->mode != POLARSSL_MODE_GCM &&
394 ( ( filesize - md_get_size( md_info ) ) %
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000395 cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000396 {
Rich Evansf90016a2015-01-19 14:26:37 +0000397 polarssl_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000398 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 {
Rich Evansf90016a2015-01-19 14:26:37 +0000412 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000413 goto exit;
414 }
415
416 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000417
418 /*
419 * Hash the IV and the secret key together 8192 times
420 * using the result to setup the AES context and HMAC.
421 */
422 memset( digest, 0, 32 );
423 memcpy( digest, IV, 16 );
424
425 for( i = 0; i < 8192; i++ )
426 {
427 md_starts( &md_ctx );
428 md_update( &md_ctx, digest, 32 );
429 md_update( &md_ctx, key, keylen );
430 md_finish( &md_ctx, digest );
431 }
432
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200433 if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
434 POLARSSL_DECRYPT ) != 0 )
435 {
Rich Evansf90016a2015-01-19 14:26:37 +0000436 polarssl_fprintf( stderr, "cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200437 goto exit;
438 }
439
440 if( cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
441 {
Rich Evansf90016a2015-01-19 14:26:37 +0000442 polarssl_fprintf( stderr, "cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200443 goto exit;
444 }
445
446 if( cipher_reset( &cipher_ctx ) != 0 )
447 {
Rich Evansf90016a2015-01-19 14:26:37 +0000448 polarssl_fprintf( stderr, "cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200449 goto exit;
450 }
Paul Bakker20a78082011-01-21 09:32:12 +0000451
452 md_hmac_starts( &md_ctx, digest, 32 );
453
454 /*
455 * Decrypt and write the plaintext.
456 */
Simon Butcher31d7f5b2016-09-03 12:39:38 +0100457 for( offset = 0;
458 offset < filesize;
459 offset += cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000460 {
Simon Butcher31d7f5b2016-09-03 12:39:38 +0100461 if( (unsigned int) filesize - offset >
462 cipher_get_block_size( &cipher_ctx ) )
463 {
464 ilen = cipher_get_block_size( &cipher_ctx );
465 }
466 else
467 {
468 ilen = (unsigned int) ( filesize - offset );
469 }
470
471 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000472 {
Rich Evansf90016a2015-01-19 14:26:37 +0000473 polarssl_fprintf( stderr, "fread(%d bytes) failed\n",
Paul Bakker20a78082011-01-21 09:32:12 +0000474 cipher_get_block_size( &cipher_ctx ) );
475 goto exit;
476 }
477
Simon Butcher31d7f5b2016-09-03 12:39:38 +0100478 md_hmac_update( &md_ctx, buffer, ilen );
479 if( cipher_update( &cipher_ctx, buffer, ilen, output,
480 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200481 {
Rich Evansf90016a2015-01-19 14:26:37 +0000482 polarssl_fprintf( stderr, "cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200483 goto exit;
484 }
Paul Bakker20a78082011-01-21 09:32:12 +0000485
Paul Bakker2c0994e2011-05-25 13:51:57 +0000486 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000487 {
Rich Evansf90016a2015-01-19 14:26:37 +0000488 polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000489 goto exit;
490 }
491 }
492
493 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000494 * Verify the message authentication code.
495 */
496 md_hmac_finish( &md_ctx, digest );
497
498 if( fread( buffer, 1, md_get_size( md_info ), fin ) != md_get_size( md_info ) )
499 {
Rich Evansf90016a2015-01-19 14:26:37 +0000500 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000501 goto exit;
502 }
503
Paul Bakker424cd692013-10-31 14:22:08 +0100504 /* Use constant-time buffer comparison */
505 diff = 0;
506 for( i = 0; i < md_get_size( md_info ); i++ )
507 diff |= digest[i] ^ buffer[i];
508
509 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000510 {
Rich Evansf90016a2015-01-19 14:26:37 +0000511 polarssl_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000512 "or file corrupted.\n" );
513 goto exit;
514 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100515
516 /*
517 * Write the final block of data
518 */
519 cipher_finish( &cipher_ctx, output, &olen );
520
521 if( fwrite( output, 1, olen, fout ) != olen )
522 {
Rich Evansf90016a2015-01-19 14:26:37 +0000523 polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100524 goto exit;
525 }
Paul Bakker20a78082011-01-21 09:32:12 +0000526 }
527
528 ret = 0;
529
530exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000531 if( fin )
532 fclose( fin );
533 if( fout )
534 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000535
Hanno Becker8188d392017-06-27 08:46:50 +0100536 /* Zeroize all command line arguments to also cover
537 the case when the user has missed or reordered some,
538 in which case the key might not be in argv[6]. */
539 for( i = 0; i < argc; i++ )
540 memset( argv[i], 0, strlen( argv[i] ) );
541
542 memset( IV, 0, sizeof( IV ) );
543 memset( key, 0, sizeof( key ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000544 memset( buffer, 0, sizeof( buffer ) );
Hanno Becker8188d392017-06-27 08:46:50 +0100545 memset( output, 0, sizeof( output ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000546 memset( digest, 0, sizeof( digest ) );
547
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200548 cipher_free( &cipher_ctx );
549 md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000550
551 return( ret );
552}
Rich Evans18b78c72015-02-11 14:06:19 +0000553#endif /* POLARSSL_CIPHER_C && POLARSSL_MD_C && POLARSSL_FS_IO */