blob: 67b5f2b6b25c71c5e07697e3ba1e5cc8f7db3722 [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 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00005 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakker20a78082011-01-21 09:32:12 +00006 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00007 * This file is part of mbed TLS (https://polarssl.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
38#if defined(POLARSSL_CIPHER_C) && defined(POLARSSL_MD_C) &&\
39 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
Rich Evans18b78c72015-02-11 14:06:19 +000067#if !defined(POLARSSL_CIPHER_C) || !defined(POLARSSL_MD_C) ||\
68 !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000069int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000070{
Paul Bakkercce9d772011-11-18 14:26:47 +000071 ((void) argc);
72 ((void) argv);
73
Rich Evans18b78c72015-02-11 14:06:19 +000074 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 +000075 return( 0 );
76}
77#else
Paul Bakker20a78082011-01-21 09:32:12 +000078int main( int argc, char *argv[] )
79{
Paul Bakker25b5fe52011-05-26 14:02:58 +000080 int ret = 1, i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000081 int mode, lastn;
Paul Bakker25b5fe52011-05-26 14:02:58 +000082 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000083 FILE *fkey, *fin = NULL, *fout = NULL;
84
85 char *p;
86 unsigned char IV[16];
87 unsigned char key[512];
88 unsigned char digest[POLARSSL_MD_MAX_SIZE];
89 unsigned char buffer[1024];
90 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +010091 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +000092
93 const cipher_info_t *cipher_info;
94 const md_info_t *md_info;
95 cipher_context_t cipher_ctx;
96 md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +000097#if defined(_WIN32_WCE)
98 long filesize, offset;
99#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000100 LARGE_INTEGER li_size;
101 __int64 filesize, offset;
102#else
103 off_t filesize, offset;
104#endif
105
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200106 cipher_init( &cipher_ctx );
107 md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000108
109 /*
110 * Parse the command-line arguments.
111 */
112 if( argc != 7 )
113 {
114 const int *list;
115
Rich Evansf90016a2015-01-19 14:26:37 +0000116 polarssl_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000117
Rich Evansf90016a2015-01-19 14:26:37 +0000118 polarssl_printf( "Available ciphers:\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000119 list = cipher_list();
120 while( *list )
121 {
122 cipher_info = cipher_info_from_type( *list );
Rich Evansf90016a2015-01-19 14:26:37 +0000123 polarssl_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000124 list++;
125 }
126
Rich Evansf90016a2015-01-19 14:26:37 +0000127 polarssl_printf( "\nAvailable message digests:\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000128 list = md_list();
129 while( *list )
130 {
131 md_info = md_info_from_type( *list );
Rich Evansf90016a2015-01-19 14:26:37 +0000132 polarssl_printf( " %s\n", md_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000133 list++;
134 }
135
Paul Bakkercce9d772011-11-18 14:26:47 +0000136#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000137 polarssl_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000138 fflush( stdout ); getchar();
139#endif
140
141 goto exit;
142 }
143
144 mode = atoi( argv[1] );
145
146 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
147 {
Rich Evansf90016a2015-01-19 14:26:37 +0000148 polarssl_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000149 goto exit;
150 }
151
152 if( strcmp( argv[2], argv[3] ) == 0 )
153 {
Rich Evansf90016a2015-01-19 14:26:37 +0000154 polarssl_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000155 goto exit;
156 }
157
158 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
159 {
Rich Evansf90016a2015-01-19 14:26:37 +0000160 polarssl_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000161 goto exit;
162 }
163
164 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
165 {
Rich Evansf90016a2015-01-19 14:26:37 +0000166 polarssl_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000167 goto exit;
168 }
169
170 /*
171 * Read the Cipher and MD from the command line
172 */
173 cipher_info = cipher_info_from_string( argv[4] );
174 if( cipher_info == NULL )
175 {
Rich Evansf90016a2015-01-19 14:26:37 +0000176 polarssl_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000177 goto exit;
178 }
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200179 if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info) ) != 0 )
180 {
Rich Evansf90016a2015-01-19 14:26:37 +0000181 polarssl_fprintf( stderr, "cipher_init_ctx failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200182 goto exit;
183 }
Paul Bakker20a78082011-01-21 09:32:12 +0000184
185 md_info = md_info_from_string( argv[5] );
186 if( md_info == NULL )
187 {
Rich Evansf90016a2015-01-19 14:26:37 +0000188 polarssl_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000189 goto exit;
190 }
191 md_init_ctx( &md_ctx, md_info);
192
193 /*
194 * Read the secret key and clean the command line.
195 */
196 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
197 {
198 keylen = fread( key, 1, sizeof( key ), fkey );
199 fclose( fkey );
200 }
201 else
202 {
203 if( memcmp( argv[6], "hex:", 4 ) == 0 )
204 {
205 p = &argv[6][4];
206 keylen = 0;
207
208 while( sscanf( p, "%02X", &n ) > 0 &&
209 keylen < (int) sizeof( key ) )
210 {
211 key[keylen++] = (unsigned char) n;
212 p += 2;
213 }
214 }
215 else
216 {
217 keylen = strlen( argv[6] );
218
219 if( keylen > (int) sizeof( key ) )
220 keylen = (int) sizeof( key );
221
222 memcpy( key, argv[6], keylen );
223 }
224 }
225
226 memset( argv[6], 0, strlen( argv[6] ) );
227
Paul Bakkercce9d772011-11-18 14:26:47 +0000228#if defined(_WIN32_WCE)
229 filesize = fseek( fin, 0L, SEEK_END );
230#else
231#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000232 /*
233 * Support large files (> 2Gb) on Win32
234 */
235 li_size.QuadPart = 0;
236 li_size.LowPart =
237 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
238 li_size.LowPart, &li_size.HighPart, FILE_END );
239
240 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
241 {
Rich Evansf90016a2015-01-19 14:26:37 +0000242 polarssl_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000243 goto exit;
244 }
245
246 filesize = li_size.QuadPart;
247#else
248 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
249 {
250 perror( "lseek" );
251 goto exit;
252 }
253#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000254#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000255
256 if( fseek( fin, 0, SEEK_SET ) < 0 )
257 {
Rich Evansf90016a2015-01-19 14:26:37 +0000258 polarssl_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000259 goto exit;
260 }
261
262 if( mode == MODE_ENCRYPT )
263 {
264 /*
265 * Generate the initialization vector as:
266 * IV = SHA-256( filesize || filename )[0..15]
267 */
268 for( i = 0; i < 8; i++ )
269 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
270
271 p = argv[2];
272
273 md_starts( &md_ctx );
274 md_update( &md_ctx, buffer, 8 );
275 md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
276 md_finish( &md_ctx, digest );
277
278 memcpy( IV, digest, 16 );
279
280 /*
281 * The last four bits in the IV are actually used
282 * to store the file size modulo the AES block size.
283 */
284 lastn = (int)( filesize & 0x0F );
285
286 IV[15] = (unsigned char)
287 ( ( IV[15] & 0xF0 ) | lastn );
288
289 /*
290 * Append the IV at the beginning of the output.
291 */
292 if( fwrite( IV, 1, 16, fout ) != 16 )
293 {
Rich Evansf90016a2015-01-19 14:26:37 +0000294 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000295 goto exit;
296 }
297
298 /*
299 * Hash the IV and the secret key together 8192 times
300 * using the result to setup the AES context and HMAC.
301 */
302 memset( digest, 0, 32 );
303 memcpy( digest, IV, 16 );
304
305 for( i = 0; i < 8192; i++ )
306 {
307 md_starts( &md_ctx );
308 md_update( &md_ctx, digest, 32 );
309 md_update( &md_ctx, key, keylen );
310 md_finish( &md_ctx, digest );
311
312 }
313
314 memset( key, 0, sizeof( key ) );
315
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000316 if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
317 POLARSSL_ENCRYPT ) != 0 )
318 {
Rich Evansf90016a2015-01-19 14:26:37 +0000319 polarssl_fprintf( stderr, "cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000320 goto exit;
321 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200322 if( cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
323 {
Rich Evansf90016a2015-01-19 14:26:37 +0000324 polarssl_fprintf( stderr, "cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200325 goto exit;
326 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200327 if( cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000328 {
Rich Evansf90016a2015-01-19 14:26:37 +0000329 polarssl_fprintf( stderr, "cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000330 goto exit;
331 }
Paul Bakker20a78082011-01-21 09:32:12 +0000332
333 md_hmac_starts( &md_ctx, digest, 32 );
334
335 /*
336 * Encrypt and write the ciphertext.
337 */
338 for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) )
339 {
Paul Bakker25b5fe52011-05-26 14:02:58 +0000340 ilen = ( (unsigned int) filesize - offset > cipher_get_block_size( &cipher_ctx ) ) ?
Paul Bakker23986e52011-04-24 08:57:21 +0000341 cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000342
Paul Bakker25b5fe52011-05-26 14:02:58 +0000343 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000344 {
Rich Evansf90016a2015-01-19 14:26:37 +0000345 polarssl_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000346 goto exit;
347 }
348
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200349 if( cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
350 {
Rich Evansf90016a2015-01-19 14:26:37 +0000351 polarssl_fprintf( stderr, "cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200352 goto exit;
353 }
354
Paul Bakker20a78082011-01-21 09:32:12 +0000355 md_hmac_update( &md_ctx, output, olen );
356
Paul Bakker2c0994e2011-05-25 13:51:57 +0000357 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000358 {
Rich Evansf90016a2015-01-19 14:26:37 +0000359 polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000360 goto exit;
361 }
362 }
363
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200364 if( cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000365 {
Rich Evansf90016a2015-01-19 14:26:37 +0000366 polarssl_fprintf( stderr, "cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000367 goto exit;
368 }
Paul Bakker20a78082011-01-21 09:32:12 +0000369 md_hmac_update( &md_ctx, output, olen );
370
Paul Bakker2c0994e2011-05-25 13:51:57 +0000371 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000372 {
Rich Evansf90016a2015-01-19 14:26:37 +0000373 polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000374 goto exit;
375 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000376
Paul Bakker20a78082011-01-21 09:32:12 +0000377 /*
378 * Finally write the HMAC.
379 */
380 md_hmac_finish( &md_ctx, digest );
381
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000382 if( fwrite( digest, 1, md_get_size( md_info ), fout ) != md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000383 {
Rich Evansf90016a2015-01-19 14:26:37 +0000384 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000385 goto exit;
386 }
387 }
388
389 if( mode == MODE_DECRYPT )
390 {
391 /*
392 * The encrypted file must be structured as follows:
393 *
394 * 00 .. 15 Initialization Vector
395 * 16 .. 31 AES Encrypted Block #1
396 * ..
397 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
398 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
399 */
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000400 if( filesize < 16 + md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000401 {
Rich Evansf90016a2015-01-19 14:26:37 +0000402 polarssl_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000403 goto exit;
404 }
405
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000406 if( ( ( filesize - md_get_size( md_info ) ) %
407 cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000408 {
Rich Evansf90016a2015-01-19 14:26:37 +0000409 polarssl_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000410 cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000411 goto exit;
412 }
413
414 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100415 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000416 */
417 filesize -= ( 16 + md_get_size( md_info ) );
418
419 /*
420 * Read the IV and original filesize modulo 16.
421 */
422 if( fread( buffer, 1, 16, fin ) != 16 )
423 {
Rich Evansf90016a2015-01-19 14:26:37 +0000424 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000425 goto exit;
426 }
427
428 memcpy( IV, buffer, 16 );
429 lastn = IV[15] & 0x0F;
430
431 /*
432 * Hash the IV and the secret key together 8192 times
433 * using the result to setup the AES context and HMAC.
434 */
435 memset( digest, 0, 32 );
436 memcpy( digest, IV, 16 );
437
438 for( i = 0; i < 8192; i++ )
439 {
440 md_starts( &md_ctx );
441 md_update( &md_ctx, digest, 32 );
442 md_update( &md_ctx, key, keylen );
443 md_finish( &md_ctx, digest );
444 }
445
446 memset( key, 0, sizeof( key ) );
447
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200448 if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
449 POLARSSL_DECRYPT ) != 0 )
450 {
Rich Evansf90016a2015-01-19 14:26:37 +0000451 polarssl_fprintf( stderr, "cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200452 goto exit;
453 }
454
455 if( cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
456 {
Rich Evansf90016a2015-01-19 14:26:37 +0000457 polarssl_fprintf( stderr, "cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200458 goto exit;
459 }
460
461 if( cipher_reset( &cipher_ctx ) != 0 )
462 {
Rich Evansf90016a2015-01-19 14:26:37 +0000463 polarssl_fprintf( stderr, "cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200464 goto exit;
465 }
Paul Bakker20a78082011-01-21 09:32:12 +0000466
467 md_hmac_starts( &md_ctx, digest, 32 );
468
469 /*
470 * Decrypt and write the plaintext.
471 */
472 for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) )
473 {
474 if( fread( buffer, 1, cipher_get_block_size( &cipher_ctx ), fin ) !=
475 (size_t) cipher_get_block_size( &cipher_ctx ) )
476 {
Rich Evansf90016a2015-01-19 14:26:37 +0000477 polarssl_fprintf( stderr, "fread(%d bytes) failed\n",
Paul Bakker20a78082011-01-21 09:32:12 +0000478 cipher_get_block_size( &cipher_ctx ) );
479 goto exit;
480 }
481
482 md_hmac_update( &md_ctx, buffer, cipher_get_block_size( &cipher_ctx ) );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200483 if( cipher_update( &cipher_ctx, buffer,
484 cipher_get_block_size( &cipher_ctx ),
485 output, &olen ) != 0 )
486 {
Rich Evansf90016a2015-01-19 14:26:37 +0000487 polarssl_fprintf( stderr, "cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200488 goto exit;
489 }
Paul Bakker20a78082011-01-21 09:32:12 +0000490
Paul Bakker2c0994e2011-05-25 13:51:57 +0000491 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000492 {
Rich Evansf90016a2015-01-19 14:26:37 +0000493 polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000494 goto exit;
495 }
496 }
497
498 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000499 * Verify the message authentication code.
500 */
501 md_hmac_finish( &md_ctx, digest );
502
503 if( fread( buffer, 1, md_get_size( md_info ), fin ) != md_get_size( md_info ) )
504 {
Rich Evansf90016a2015-01-19 14:26:37 +0000505 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000506 goto exit;
507 }
508
Paul Bakker424cd692013-10-31 14:22:08 +0100509 /* Use constant-time buffer comparison */
510 diff = 0;
511 for( i = 0; i < md_get_size( md_info ); i++ )
512 diff |= digest[i] ^ buffer[i];
513
514 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000515 {
Rich Evansf90016a2015-01-19 14:26:37 +0000516 polarssl_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000517 "or file corrupted.\n" );
518 goto exit;
519 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100520
521 /*
522 * Write the final block of data
523 */
524 cipher_finish( &cipher_ctx, output, &olen );
525
526 if( fwrite( output, 1, olen, fout ) != olen )
527 {
Rich Evansf90016a2015-01-19 14:26:37 +0000528 polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100529 goto exit;
530 }
Paul Bakker20a78082011-01-21 09:32:12 +0000531 }
532
533 ret = 0;
534
535exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000536 if( fin )
537 fclose( fin );
538 if( fout )
539 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000540
541 memset( buffer, 0, sizeof( buffer ) );
542 memset( digest, 0, sizeof( digest ) );
543
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200544 cipher_free( &cipher_ctx );
545 md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000546
547 return( ret );
548}
Rich Evans18b78c72015-02-11 14:06:19 +0000549#endif /* POLARSSL_CIPHER_C && POLARSSL_MD_C && POLARSSL_FS_IO */