blob: 231849830bec0ec4629e662e466f6d1f0a662553 [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é-Gonnard085ab042015-01-23 11:06:27 +00007 * This file is part of mbed TLS (https://www.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
33#define polarssl_printf printf
34#define polarssl_fprintf fprintf
35#define polarssl_malloc malloc
36#define polarssl_free free
37#endif
38
Paul Bakker494c0b82011-04-24 15:30:07 +000039#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000040#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000041#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000042#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000043#endif
Paul Bakker20a78082011-01-21 09:32:12 +000044#else
45#include <sys/types.h>
46#include <unistd.h>
47#endif
48
49#include <string.h>
50#include <stdlib.h>
51#include <stdio.h>
52#include <time.h>
53
54#include "polarssl/cipher.h"
55#include "polarssl/md.h"
56
57#define MODE_ENCRYPT 0
58#define MODE_DECRYPT 1
59
60#define USAGE \
61 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <md> <key>\n" \
62 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
63 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
64 "\n"
65
Paul Bakker5690efc2011-05-26 13:16:06 +000066#if !defined(POLARSSL_CIPHER_C) || !defined(POLARSSL_MD_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000067int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000068{
Paul Bakkercce9d772011-11-18 14:26:47 +000069 ((void) argc);
70 ((void) argv);
71
Rich Evansf90016a2015-01-19 14:26:37 +000072 polarssl_printf("POLARSSL_CIPHER_C and/or POLARSSL_MD_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000073 return( 0 );
74}
75#else
Paul Bakker20a78082011-01-21 09:32:12 +000076int main( int argc, char *argv[] )
77{
Paul Bakker25b5fe52011-05-26 14:02:58 +000078 int ret = 1, i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000079 int mode, lastn;
Paul Bakker25b5fe52011-05-26 14:02:58 +000080 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000081 FILE *fkey, *fin = NULL, *fout = NULL;
82
83 char *p;
84 unsigned char IV[16];
85 unsigned char key[512];
86 unsigned char digest[POLARSSL_MD_MAX_SIZE];
87 unsigned char buffer[1024];
88 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +010089 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +000090
91 const cipher_info_t *cipher_info;
92 const md_info_t *md_info;
93 cipher_context_t cipher_ctx;
94 md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +000095#if defined(_WIN32_WCE)
96 long filesize, offset;
97#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000098 LARGE_INTEGER li_size;
99 __int64 filesize, offset;
100#else
101 off_t filesize, offset;
102#endif
103
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200104 cipher_init( &cipher_ctx );
105 md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000106
107 /*
108 * Parse the command-line arguments.
109 */
110 if( argc != 7 )
111 {
112 const int *list;
113
Rich Evansf90016a2015-01-19 14:26:37 +0000114 polarssl_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000115
Rich Evansf90016a2015-01-19 14:26:37 +0000116 polarssl_printf( "Available ciphers:\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000117 list = cipher_list();
118 while( *list )
119 {
120 cipher_info = cipher_info_from_type( *list );
Rich Evansf90016a2015-01-19 14:26:37 +0000121 polarssl_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000122 list++;
123 }
124
Rich Evansf90016a2015-01-19 14:26:37 +0000125 polarssl_printf( "\nAvailable message digests:\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000126 list = md_list();
127 while( *list )
128 {
129 md_info = md_info_from_type( *list );
Rich Evansf90016a2015-01-19 14:26:37 +0000130 polarssl_printf( " %s\n", md_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000131 list++;
132 }
133
Paul Bakkercce9d772011-11-18 14:26:47 +0000134#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000135 polarssl_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000136 fflush( stdout ); getchar();
137#endif
138
139 goto exit;
140 }
141
142 mode = atoi( argv[1] );
143
144 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
145 {
Rich Evansf90016a2015-01-19 14:26:37 +0000146 polarssl_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000147 goto exit;
148 }
149
150 if( strcmp( argv[2], argv[3] ) == 0 )
151 {
Rich Evansf90016a2015-01-19 14:26:37 +0000152 polarssl_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000153 goto exit;
154 }
155
156 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
157 {
Rich Evansf90016a2015-01-19 14:26:37 +0000158 polarssl_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000159 goto exit;
160 }
161
162 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
163 {
Rich Evansf90016a2015-01-19 14:26:37 +0000164 polarssl_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000165 goto exit;
166 }
167
168 /*
169 * Read the Cipher and MD from the command line
170 */
171 cipher_info = cipher_info_from_string( argv[4] );
172 if( cipher_info == NULL )
173 {
Rich Evansf90016a2015-01-19 14:26:37 +0000174 polarssl_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000175 goto exit;
176 }
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200177 if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info) ) != 0 )
178 {
Rich Evansf90016a2015-01-19 14:26:37 +0000179 polarssl_fprintf( stderr, "cipher_init_ctx failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200180 goto exit;
181 }
Paul Bakker20a78082011-01-21 09:32:12 +0000182
183 md_info = md_info_from_string( argv[5] );
184 if( md_info == NULL )
185 {
Rich Evansf90016a2015-01-19 14:26:37 +0000186 polarssl_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000187 goto exit;
188 }
189 md_init_ctx( &md_ctx, md_info);
190
191 /*
192 * Read the secret key and clean the command line.
193 */
194 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
195 {
196 keylen = fread( key, 1, sizeof( key ), fkey );
197 fclose( fkey );
198 }
199 else
200 {
201 if( memcmp( argv[6], "hex:", 4 ) == 0 )
202 {
203 p = &argv[6][4];
204 keylen = 0;
205
206 while( sscanf( p, "%02X", &n ) > 0 &&
207 keylen < (int) sizeof( key ) )
208 {
209 key[keylen++] = (unsigned char) n;
210 p += 2;
211 }
212 }
213 else
214 {
215 keylen = strlen( argv[6] );
216
217 if( keylen > (int) sizeof( key ) )
218 keylen = (int) sizeof( key );
219
220 memcpy( key, argv[6], keylen );
221 }
222 }
223
224 memset( argv[6], 0, strlen( argv[6] ) );
225
Paul Bakkercce9d772011-11-18 14:26:47 +0000226#if defined(_WIN32_WCE)
227 filesize = fseek( fin, 0L, SEEK_END );
228#else
229#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000230 /*
231 * Support large files (> 2Gb) on Win32
232 */
233 li_size.QuadPart = 0;
234 li_size.LowPart =
235 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
236 li_size.LowPart, &li_size.HighPart, FILE_END );
237
238 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
239 {
Rich Evansf90016a2015-01-19 14:26:37 +0000240 polarssl_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000241 goto exit;
242 }
243
244 filesize = li_size.QuadPart;
245#else
246 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
247 {
248 perror( "lseek" );
249 goto exit;
250 }
251#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000252#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000253
254 if( fseek( fin, 0, SEEK_SET ) < 0 )
255 {
Rich Evansf90016a2015-01-19 14:26:37 +0000256 polarssl_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000257 goto exit;
258 }
259
260 if( mode == MODE_ENCRYPT )
261 {
262 /*
263 * Generate the initialization vector as:
264 * IV = SHA-256( filesize || filename )[0..15]
265 */
266 for( i = 0; i < 8; i++ )
267 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
268
269 p = argv[2];
270
271 md_starts( &md_ctx );
272 md_update( &md_ctx, buffer, 8 );
273 md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
274 md_finish( &md_ctx, digest );
275
276 memcpy( IV, digest, 16 );
277
278 /*
279 * The last four bits in the IV are actually used
280 * to store the file size modulo the AES block size.
281 */
282 lastn = (int)( filesize & 0x0F );
283
284 IV[15] = (unsigned char)
285 ( ( IV[15] & 0xF0 ) | lastn );
286
287 /*
288 * Append the IV at the beginning of the output.
289 */
290 if( fwrite( IV, 1, 16, fout ) != 16 )
291 {
Rich Evansf90016a2015-01-19 14:26:37 +0000292 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000293 goto exit;
294 }
295
296 /*
297 * Hash the IV and the secret key together 8192 times
298 * using the result to setup the AES context and HMAC.
299 */
300 memset( digest, 0, 32 );
301 memcpy( digest, IV, 16 );
302
303 for( i = 0; i < 8192; i++ )
304 {
305 md_starts( &md_ctx );
306 md_update( &md_ctx, digest, 32 );
307 md_update( &md_ctx, key, keylen );
308 md_finish( &md_ctx, digest );
309
310 }
311
312 memset( key, 0, sizeof( key ) );
313
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000314 if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
315 POLARSSL_ENCRYPT ) != 0 )
316 {
Rich Evansf90016a2015-01-19 14:26:37 +0000317 polarssl_fprintf( stderr, "cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000318 goto exit;
319 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200320 if( cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
321 {
Rich Evansf90016a2015-01-19 14:26:37 +0000322 polarssl_fprintf( stderr, "cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200323 goto exit;
324 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200325 if( cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000326 {
Rich Evansf90016a2015-01-19 14:26:37 +0000327 polarssl_fprintf( stderr, "cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000328 goto exit;
329 }
Paul Bakker20a78082011-01-21 09:32:12 +0000330
331 md_hmac_starts( &md_ctx, digest, 32 );
332
333 /*
334 * Encrypt and write the ciphertext.
335 */
336 for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) )
337 {
Paul Bakker25b5fe52011-05-26 14:02:58 +0000338 ilen = ( (unsigned int) filesize - offset > cipher_get_block_size( &cipher_ctx ) ) ?
Paul Bakker23986e52011-04-24 08:57:21 +0000339 cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000340
Paul Bakker25b5fe52011-05-26 14:02:58 +0000341 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000342 {
Rich Evansf90016a2015-01-19 14:26:37 +0000343 polarssl_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000344 goto exit;
345 }
346
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200347 if( cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
348 {
Rich Evansf90016a2015-01-19 14:26:37 +0000349 polarssl_fprintf( stderr, "cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200350 goto exit;
351 }
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 }
360 }
361
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200362 if( cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000363 {
Rich Evansf90016a2015-01-19 14:26:37 +0000364 polarssl_fprintf( stderr, "cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000365 goto exit;
366 }
Paul Bakker20a78082011-01-21 09:32:12 +0000367 md_hmac_update( &md_ctx, output, olen );
368
Paul Bakker2c0994e2011-05-25 13:51:57 +0000369 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000370 {
Rich Evansf90016a2015-01-19 14:26:37 +0000371 polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000372 goto exit;
373 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000374
Paul Bakker20a78082011-01-21 09:32:12 +0000375 /*
376 * Finally write the HMAC.
377 */
378 md_hmac_finish( &md_ctx, digest );
379
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000380 if( fwrite( digest, 1, md_get_size( md_info ), fout ) != md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000381 {
Rich Evansf90016a2015-01-19 14:26:37 +0000382 polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000383 goto exit;
384 }
385 }
386
387 if( mode == MODE_DECRYPT )
388 {
389 /*
390 * The encrypted file must be structured as follows:
391 *
392 * 00 .. 15 Initialization Vector
393 * 16 .. 31 AES Encrypted Block #1
394 * ..
395 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
396 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
397 */
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000398 if( filesize < 16 + md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000399 {
Rich Evansf90016a2015-01-19 14:26:37 +0000400 polarssl_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000401 goto exit;
402 }
403
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000404 if( ( ( filesize - md_get_size( md_info ) ) %
405 cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000406 {
Rich Evansf90016a2015-01-19 14:26:37 +0000407 polarssl_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000408 cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000409 goto exit;
410 }
411
412 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100413 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000414 */
415 filesize -= ( 16 + md_get_size( md_info ) );
416
417 /*
418 * Read the IV and original filesize modulo 16.
419 */
420 if( fread( buffer, 1, 16, fin ) != 16 )
421 {
Rich Evansf90016a2015-01-19 14:26:37 +0000422 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000423 goto exit;
424 }
425
426 memcpy( IV, buffer, 16 );
427 lastn = IV[15] & 0x0F;
428
429 /*
430 * Hash the IV and the secret key together 8192 times
431 * using the result to setup the AES context and HMAC.
432 */
433 memset( digest, 0, 32 );
434 memcpy( digest, IV, 16 );
435
436 for( i = 0; i < 8192; i++ )
437 {
438 md_starts( &md_ctx );
439 md_update( &md_ctx, digest, 32 );
440 md_update( &md_ctx, key, keylen );
441 md_finish( &md_ctx, digest );
442 }
443
444 memset( key, 0, sizeof( key ) );
445
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200446 if( cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
447 POLARSSL_DECRYPT ) != 0 )
448 {
Rich Evansf90016a2015-01-19 14:26:37 +0000449 polarssl_fprintf( stderr, "cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200450 goto exit;
451 }
452
453 if( cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
454 {
Rich Evansf90016a2015-01-19 14:26:37 +0000455 polarssl_fprintf( stderr, "cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200456 goto exit;
457 }
458
459 if( cipher_reset( &cipher_ctx ) != 0 )
460 {
Rich Evansf90016a2015-01-19 14:26:37 +0000461 polarssl_fprintf( stderr, "cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200462 goto exit;
463 }
Paul Bakker20a78082011-01-21 09:32:12 +0000464
465 md_hmac_starts( &md_ctx, digest, 32 );
466
467 /*
468 * Decrypt and write the plaintext.
469 */
470 for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) )
471 {
472 if( fread( buffer, 1, cipher_get_block_size( &cipher_ctx ), fin ) !=
473 (size_t) cipher_get_block_size( &cipher_ctx ) )
474 {
Rich Evansf90016a2015-01-19 14:26:37 +0000475 polarssl_fprintf( stderr, "fread(%d bytes) failed\n",
Paul Bakker20a78082011-01-21 09:32:12 +0000476 cipher_get_block_size( &cipher_ctx ) );
477 goto exit;
478 }
479
480 md_hmac_update( &md_ctx, buffer, cipher_get_block_size( &cipher_ctx ) );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200481 if( cipher_update( &cipher_ctx, buffer,
482 cipher_get_block_size( &cipher_ctx ),
483 output, &olen ) != 0 )
484 {
Rich Evansf90016a2015-01-19 14:26:37 +0000485 polarssl_fprintf( stderr, "cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200486 goto exit;
487 }
Paul Bakker20a78082011-01-21 09:32:12 +0000488
Paul Bakker2c0994e2011-05-25 13:51:57 +0000489 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000490 {
Rich Evansf90016a2015-01-19 14:26:37 +0000491 polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000492 goto exit;
493 }
494 }
495
496 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000497 * Verify the message authentication code.
498 */
499 md_hmac_finish( &md_ctx, digest );
500
501 if( fread( buffer, 1, md_get_size( md_info ), fin ) != md_get_size( md_info ) )
502 {
Rich Evansf90016a2015-01-19 14:26:37 +0000503 polarssl_fprintf( stderr, "fread(%d bytes) failed\n", md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000504 goto exit;
505 }
506
Paul Bakker424cd692013-10-31 14:22:08 +0100507 /* Use constant-time buffer comparison */
508 diff = 0;
509 for( i = 0; i < md_get_size( md_info ); i++ )
510 diff |= digest[i] ^ buffer[i];
511
512 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000513 {
Rich Evansf90016a2015-01-19 14:26:37 +0000514 polarssl_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000515 "or file corrupted.\n" );
516 goto exit;
517 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100518
519 /*
520 * Write the final block of data
521 */
522 cipher_finish( &cipher_ctx, output, &olen );
523
524 if( fwrite( output, 1, olen, fout ) != olen )
525 {
Rich Evansf90016a2015-01-19 14:26:37 +0000526 polarssl_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100527 goto exit;
528 }
Paul Bakker20a78082011-01-21 09:32:12 +0000529 }
530
531 ret = 0;
532
533exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000534 if( fin )
535 fclose( fin );
536 if( fout )
537 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000538
539 memset( buffer, 0, sizeof( buffer ) );
540 memset( digest, 0, sizeof( digest ) );
541
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200542 cipher_free( &cipher_ctx );
543 md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000544
545 return( ret );
546}
Paul Bakker5690efc2011-05-26 13:16:06 +0000547#endif /* POLARSSL_CIPHER_C && POLARSSL_MD_C */