blob: be05d982cd3aa485a26dbda8066e6ac333e74797 [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 /*
191 * Read the secret key and clean the command line.
192 */
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
223 memset( argv[6], 0, strlen( argv[6] ) );
224
Paul Bakkercce9d772011-11-18 14:26:47 +0000225#if defined(_WIN32_WCE)
226 filesize = fseek( fin, 0L, SEEK_END );
227#else
228#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000229 /*
230 * Support large files (> 2Gb) on Win32
231 */
232 li_size.QuadPart = 0;
233 li_size.LowPart =
234 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
235 li_size.LowPart, &li_size.HighPart, FILE_END );
236
237 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
238 {
Rich Evansf90016a2015-01-19 14:26:37 +0000239 polarssl_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000240 goto exit;
241 }
242
243 filesize = li_size.QuadPart;
244#else
245 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
246 {
247 perror( "lseek" );
248 goto exit;
249 }
250#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000251#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000252
253 if( fseek( fin, 0, SEEK_SET ) < 0 )
254 {
Rich Evansf90016a2015-01-19 14:26:37 +0000255 polarssl_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000256 goto exit;
257 }
258
259 if( mode == MODE_ENCRYPT )
260 {
261 /*
262 * Generate the initialization vector as:
Simon Butcher31d7f5b2016-09-03 12:39:38 +0100263 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000264 */
265 for( i = 0; i < 8; i++ )
266 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
267
268 p = argv[2];
269
270 md_starts( &md_ctx );
271 md_update( &md_ctx, buffer, 8 );
272 md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
273 md_finish( &md_ctx, digest );
274
275 memcpy( IV, digest, 16 );
276
277 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000278 * Append the IV at the beginning of the output.
279 */
280 if( fwrite( IV, 1, 16, fout ) != 16 )
281 {
Rich Evansf90016a2015-01-19 14:26:37 +0000282 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000283 goto exit;
284 }
285
286 /*
287 * Hash the IV and the secret key together 8192 times
288 * using the result to setup the AES context and HMAC.
289 */
290 memset( digest, 0, 32 );
291 memcpy( digest, IV, 16 );
292
293 for( i = 0; i < 8192; i++ )
294 {
295 md_starts( &md_ctx );
296 md_update( &md_ctx, digest, 32 );
297 md_update( &md_ctx, key, keylen );
298 md_finish( &md_ctx, digest );
299
300 }
301
302 memset( key, 0, sizeof( key ) );
303
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000304 if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
305 POLARSSL_ENCRYPT ) != 0 )
306 {
Rich Evansf90016a2015-01-19 14:26:37 +0000307 polarssl_fprintf( stderr, "cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000308 goto exit;
309 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200310 if( cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
311 {
Rich Evansf90016a2015-01-19 14:26:37 +0000312 polarssl_fprintf( stderr, "cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200313 goto exit;
314 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200315 if( cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000316 {
Rich Evansf90016a2015-01-19 14:26:37 +0000317 polarssl_fprintf( stderr, "cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000318 goto exit;
319 }
Paul Bakker20a78082011-01-21 09:32:12 +0000320
321 md_hmac_starts( &md_ctx, digest, 32 );
322
323 /*
324 * Encrypt and write the ciphertext.
325 */
326 for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) )
327 {
Paul Bakker25b5fe52011-05-26 14:02:58 +0000328 ilen = ( (unsigned int) filesize - offset > cipher_get_block_size( &cipher_ctx ) ) ?
Paul Bakker23986e52011-04-24 08:57:21 +0000329 cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000330
Paul Bakker25b5fe52011-05-26 14:02:58 +0000331 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000332 {
Rich Evansf90016a2015-01-19 14:26:37 +0000333 polarssl_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000334 goto exit;
335 }
336
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200337 if( cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
338 {
Rich Evansf90016a2015-01-19 14:26:37 +0000339 polarssl_fprintf( stderr, "cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200340 goto exit;
341 }
342
Paul Bakker20a78082011-01-21 09:32:12 +0000343 md_hmac_update( &md_ctx, output, olen );
344
Paul Bakker2c0994e2011-05-25 13:51:57 +0000345 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000346 {
Rich Evansf90016a2015-01-19 14:26:37 +0000347 polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000348 goto exit;
349 }
350 }
351
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200352 if( cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000353 {
Rich Evansf90016a2015-01-19 14:26:37 +0000354 polarssl_fprintf( stderr, "cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000355 goto exit;
356 }
Paul Bakker20a78082011-01-21 09:32:12 +0000357 md_hmac_update( &md_ctx, output, olen );
358
Paul Bakker2c0994e2011-05-25 13:51:57 +0000359 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000360 {
Rich Evansf90016a2015-01-19 14:26:37 +0000361 polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000362 goto exit;
363 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000364
Paul Bakker20a78082011-01-21 09:32:12 +0000365 /*
366 * Finally write the HMAC.
367 */
368 md_hmac_finish( &md_ctx, digest );
369
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000370 if( fwrite( digest, 1, md_get_size( md_info ), fout ) != md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000371 {
Rich Evansf90016a2015-01-19 14:26:37 +0000372 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000373 goto exit;
374 }
375 }
376
377 if( mode == MODE_DECRYPT )
378 {
379 /*
380 * The encrypted file must be structured as follows:
381 *
382 * 00 .. 15 Initialization Vector
Simon Butcher31d7f5b2016-09-03 12:39:38 +0100383 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000384 * ..
Simon Butcher31d7f5b2016-09-03 12:39:38 +0100385 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
386 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000387 */
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000388 if( filesize < 16 + md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000389 {
Rich Evansf90016a2015-01-19 14:26:37 +0000390 polarssl_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000391 goto exit;
392 }
393
Simon Butcher31d7f5b2016-09-03 12:39:38 +0100394 /*
395 * Check the file size.
396 */
397 if( cipher_info->mode != POLARSSL_MODE_GCM &&
398 ( ( filesize - md_get_size( md_info ) ) %
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000399 cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000400 {
Rich Evansf90016a2015-01-19 14:26:37 +0000401 polarssl_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000402 cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000403 goto exit;
404 }
405
406 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100407 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000408 */
409 filesize -= ( 16 + md_get_size( md_info ) );
410
411 /*
412 * Read the IV and original filesize modulo 16.
413 */
414 if( fread( buffer, 1, 16, fin ) != 16 )
415 {
Rich Evansf90016a2015-01-19 14:26:37 +0000416 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000417 goto exit;
418 }
419
420 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000421
422 /*
423 * Hash the IV and the secret key together 8192 times
424 * using the result to setup the AES context and HMAC.
425 */
426 memset( digest, 0, 32 );
427 memcpy( digest, IV, 16 );
428
429 for( i = 0; i < 8192; i++ )
430 {
431 md_starts( &md_ctx );
432 md_update( &md_ctx, digest, 32 );
433 md_update( &md_ctx, key, keylen );
434 md_finish( &md_ctx, digest );
435 }
436
437 memset( key, 0, sizeof( key ) );
438
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200439 if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
440 POLARSSL_DECRYPT ) != 0 )
441 {
Rich Evansf90016a2015-01-19 14:26:37 +0000442 polarssl_fprintf( stderr, "cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200443 goto exit;
444 }
445
446 if( cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
447 {
Rich Evansf90016a2015-01-19 14:26:37 +0000448 polarssl_fprintf( stderr, "cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200449 goto exit;
450 }
451
452 if( cipher_reset( &cipher_ctx ) != 0 )
453 {
Rich Evansf90016a2015-01-19 14:26:37 +0000454 polarssl_fprintf( stderr, "cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200455 goto exit;
456 }
Paul Bakker20a78082011-01-21 09:32:12 +0000457
458 md_hmac_starts( &md_ctx, digest, 32 );
459
460 /*
461 * Decrypt and write the plaintext.
462 */
Simon Butcher31d7f5b2016-09-03 12:39:38 +0100463 for( offset = 0;
464 offset < filesize;
465 offset += cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000466 {
Simon Butcher31d7f5b2016-09-03 12:39:38 +0100467 if( (unsigned int) filesize - offset >
468 cipher_get_block_size( &cipher_ctx ) )
469 {
470 ilen = cipher_get_block_size( &cipher_ctx );
471 }
472 else
473 {
474 ilen = (unsigned int) ( filesize - offset );
475 }
476
477 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000478 {
Rich Evansf90016a2015-01-19 14:26:37 +0000479 polarssl_fprintf( stderr, "fread(%d bytes) failed\n",
Paul Bakker20a78082011-01-21 09:32:12 +0000480 cipher_get_block_size( &cipher_ctx ) );
481 goto exit;
482 }
483
Simon Butcher31d7f5b2016-09-03 12:39:38 +0100484 md_hmac_update( &md_ctx, buffer, ilen );
485 if( cipher_update( &cipher_ctx, buffer, ilen, output,
486 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200487 {
Rich Evansf90016a2015-01-19 14:26:37 +0000488 polarssl_fprintf( stderr, "cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200489 goto exit;
490 }
Paul Bakker20a78082011-01-21 09:32:12 +0000491
Paul Bakker2c0994e2011-05-25 13:51:57 +0000492 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000493 {
Rich Evansf90016a2015-01-19 14:26:37 +0000494 polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000495 goto exit;
496 }
497 }
498
499 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000500 * Verify the message authentication code.
501 */
502 md_hmac_finish( &md_ctx, digest );
503
504 if( fread( buffer, 1, md_get_size( md_info ), fin ) != md_get_size( md_info ) )
505 {
Rich Evansf90016a2015-01-19 14:26:37 +0000506 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000507 goto exit;
508 }
509
Paul Bakker424cd692013-10-31 14:22:08 +0100510 /* Use constant-time buffer comparison */
511 diff = 0;
512 for( i = 0; i < md_get_size( md_info ); i++ )
513 diff |= digest[i] ^ buffer[i];
514
515 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000516 {
Rich Evansf90016a2015-01-19 14:26:37 +0000517 polarssl_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000518 "or file corrupted.\n" );
519 goto exit;
520 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100521
522 /*
523 * Write the final block of data
524 */
525 cipher_finish( &cipher_ctx, output, &olen );
526
527 if( fwrite( output, 1, olen, fout ) != olen )
528 {
Rich Evansf90016a2015-01-19 14:26:37 +0000529 polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100530 goto exit;
531 }
Paul Bakker20a78082011-01-21 09:32:12 +0000532 }
533
534 ret = 0;
535
536exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000537 if( fin )
538 fclose( fin );
539 if( fout )
540 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000541
542 memset( buffer, 0, sizeof( buffer ) );
543 memset( digest, 0, sizeof( digest ) );
544
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200545 cipher_free( &cipher_ctx );
546 md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000547
548 return( ret );
549}
Rich Evans18b78c72015-02-11 14:06:19 +0000550#endif /* POLARSSL_CIPHER_C && POLARSSL_MD_C && POLARSSL_FS_IO */