blob: c8747698e1ae2f808dce52cd59d2107c3f6d726c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * AES-256 file encryption program
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000055#else
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
Andres Amaya Garcia15cbf612018-04-29 19:01:34 +010057#include <stdlib.h>
58#define mbedtls_fprintf fprintf
59#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020060#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010061#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia15cbf612018-04-29 19:01:34 +010062#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
63#endif /* MBEDTLS_PLATFORM_C */
Rich Evans18b78c72015-02-11 14:06:19 +000064
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +010066#include "mbedtls/md.h"
Rich Evans18b78c72015-02-11 14:06:19 +000067
68#include <stdio.h>
69#include <stdlib.h>
70#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000071
Paul Bakker494c0b82011-04-24 15:30:07 +000072#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000073#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000074#if !defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000075#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000076#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000077#else
78#include <sys/types.h>
79#include <unistd.h>
80#endif
81
Paul Bakker5121ce52009-01-03 21:22:43 +000082#define MODE_ENCRYPT 0
83#define MODE_DECRYPT 1
84
85#define USAGE \
86 "\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
87 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
88 "\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
89 "\n"
90
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_SHA256_C) || \
92 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_MD_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000093int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000094{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_SHA256_C "
96 "and/or MBEDTLS_FS_IO and/or MBEDTLS_MD_C "
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +010097 "not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020098 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000099}
100#else
Hanno Becker9a1a1512018-10-12 16:46:37 +0100101
102/* Implementation that should never be optimized out by the compiler */
103static void mbedtls_zeroize( void *v, size_t n ) {
104 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
105}
106
Paul Bakker5121ce52009-01-03 21:22:43 +0000107int main( int argc, char *argv[] )
108{
Andres Amaya Garcia15cbf612018-04-29 19:01:34 +0100109 int ret = 0;
110 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000111
Simon Butcher0e7d3872016-08-30 14:25:24 +0100112 unsigned int i, n;
Paul Bakker23986e52011-04-24 08:57:21 +0000113 int mode, lastn;
114 size_t keylen;
Paul Bakker20a78082011-01-21 09:32:12 +0000115 FILE *fkey, *fin = NULL, *fout = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
117 char *p;
Hanno Beckerce37e622017-06-27 08:24:34 +0100118
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 unsigned char IV[16];
Hanno Beckerce37e622017-06-27 08:24:34 +0100120 unsigned char tmp[16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 unsigned char key[512];
122 unsigned char digest[32];
123 unsigned char buffer[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100124 unsigned char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_aes_context aes_ctx;
127 mbedtls_md_context_t sha_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
Paul Bakkercce9d772011-11-18 14:26:47 +0000129#if defined(_WIN32_WCE)
130 long filesize, offset;
131#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 LARGE_INTEGER li_size;
133 __int64 filesize, offset;
134#else
135 off_t filesize, offset;
136#endif
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_aes_init( &aes_ctx );
139 mbedtls_md_init( &sha_ctx );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 ret = mbedtls_md_setup( &sha_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 1 );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100142 if( ret != 0 )
143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_printf( " ! mbedtls_md_setup() returned -0x%04x\n", -ret );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100145 goto exit;
146 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200147
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 /*
149 * Parse the command-line arguments.
150 */
151 if( argc != 5 )
152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_printf( USAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Paul Bakkercce9d772011-11-18 14:26:47 +0000155#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 fflush( stdout ); getchar();
158#endif
159
160 goto exit;
161 }
162
163 mode = atoi( argv[1] );
Hanno Beckerce37e622017-06-27 08:24:34 +0100164 memset( IV, 0, sizeof( IV ) );
165 memset( key, 0, sizeof( key ) );
166 memset( digest, 0, sizeof( digest ) );
167 memset( buffer, 0, sizeof( buffer ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_fprintf( stderr, "invalide operation mode\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 goto exit;
173 }
174
175 if( strcmp( argv[2], argv[3] ) == 0 )
176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 goto exit;
179 }
180
181 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 goto exit;
185 }
186
187 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 goto exit;
191 }
192
193 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100194 * Read the secret key from file or command line
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 */
196 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
197 {
198 keylen = fread( key, 1, sizeof( key ), fkey );
199 fclose( fkey );
200 }
201 else
202 {
203 if( memcmp( argv[4], "hex:", 4 ) == 0 )
204 {
205 p = &argv[4][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[4] );
218
219 if( keylen > (int) sizeof( key ) )
220 keylen = (int) sizeof( key );
221
222 memcpy( key, argv[4], keylen );
223 }
224 }
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 Bakker5121ce52009-01-03 21:22:43 +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 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +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 Bakker5121ce52009-01-03 21:22:43 +0000253
254 if( fseek( fin, 0, SEEK_SET ) < 0 )
255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_md_starts( &sha_ctx );
272 mbedtls_md_update( &sha_ctx, buffer, 8 );
273 mbedtls_md_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
274 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
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 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +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 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 mbedtls_md_starts( &sha_ctx );
306 mbedtls_md_update( &sha_ctx, digest, 32 );
307 mbedtls_md_update( &sha_ctx, key, keylen );
308 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 }
310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_aes_setkey_enc( &aes_ctx, digest, 256 );
312 mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000313
314 /*
315 * Encrypt and write the ciphertext.
316 */
317 for( offset = 0; offset < filesize; offset += 16 )
318 {
319 n = ( filesize - offset > 16 ) ? 16 : (int)
320 ( filesize - offset );
321
322 if( fread( buffer, 1, n, fin ) != (size_t) n )
323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 goto exit;
326 }
327
328 for( i = 0; i < 16; i++ )
329 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, buffer, buffer );
332 mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000333
334 if( fwrite( buffer, 1, 16, fout ) != 16 )
335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 goto exit;
338 }
339
340 memcpy( IV, buffer, 16 );
341 }
342
343 /*
344 * Finally write the HMAC.
345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 mbedtls_md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000347
348 if( fwrite( digest, 1, 32, fout ) != 32 )
349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 goto exit;
352 }
353 }
354
355 if( mode == MODE_DECRYPT )
356 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000357 /*
358 * The encrypted file must be structured as follows:
359 *
360 * 00 .. 15 Initialization Vector
361 * 16 .. 31 AES Encrypted Block #1
362 * ..
363 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
364 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
365 */
366 if( filesize < 48 )
367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000369 goto exit;
370 }
371
372 if( ( filesize & 0x0F ) != 0 )
373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_fprintf( stderr, "File size not a multiple of 16.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000375 goto exit;
376 }
377
378 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100379 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 */
381 filesize -= ( 16 + 32 );
382
383 /*
384 * Read the IV and original filesize modulo 16.
385 */
386 if( fread( buffer, 1, 16, fin ) != 16 )
387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000389 goto exit;
390 }
391
392 memcpy( IV, buffer, 16 );
393 lastn = IV[15] & 0x0F;
394
395 /*
396 * Hash the IV and the secret key together 8192 times
397 * using the result to setup the AES context and HMAC.
398 */
399 memset( digest, 0, 32 );
400 memcpy( digest, IV, 16 );
401
402 for( i = 0; i < 8192; i++ )
403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 mbedtls_md_starts( &sha_ctx );
405 mbedtls_md_update( &sha_ctx, digest, 32 );
406 mbedtls_md_update( &sha_ctx, key, keylen );
407 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 }
409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_aes_setkey_dec( &aes_ctx, digest, 256 );
411 mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
413 /*
414 * Decrypt and write the plaintext.
415 */
416 for( offset = 0; offset < filesize; offset += 16 )
417 {
418 if( fread( buffer, 1, 16, fin ) != 16 )
419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 goto exit;
422 }
423
424 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
427 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200428
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 for( i = 0; i < 16; i++ )
430 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
431
432 memcpy( IV, tmp, 16 );
433
434 n = ( lastn > 0 && offset == filesize - 16 )
435 ? lastn : 16;
436
437 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000440 goto exit;
441 }
442 }
443
444 /*
445 * Verify the message authentication code.
446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
449 if( fread( buffer, 1, 32, fin ) != 32 )
450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 goto exit;
453 }
454
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100455 /* Use constant-time buffer comparison */
456 diff = 0;
457 for( i = 0; i < 32; i++ )
458 diff |= digest[i] ^ buffer[i];
459
460 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 "or file corrupted.\n" );
464 goto exit;
465 }
466 }
467
Andres Amaya Garcia15cbf612018-04-29 19:01:34 +0100468 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
470exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000471 if( fin )
472 fclose( fin );
473 if( fout )
474 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
Hanno Beckerce37e622017-06-27 08:24:34 +0100476 /* Zeroize all command line arguments to also cover
477 the case when the user has missed or reordered some,
478 in which case the key might not be in argv[4]. */
479 for( i = 0; i < (unsigned int) argc; i++ )
Hanno Becker9a1a1512018-10-12 16:46:37 +0100480 mbedtls_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerce37e622017-06-27 08:24:34 +0100481
Hanno Becker9a1a1512018-10-12 16:46:37 +0100482 mbedtls_zeroize( IV, sizeof( IV ) );
483 mbedtls_zeroize( key, sizeof( key ) );
484 mbedtls_zeroize( tmp, sizeof( tmp ) );
485 mbedtls_zeroize( buffer, sizeof( buffer ) );
486 mbedtls_zeroize( digest, sizeof( digest ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_aes_free( &aes_ctx );
489 mbedtls_md_free( &sha_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200491 mbedtls_exit( exit_code );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493#endif /* MBEDTLS_AES_C && MBEDTLS_SHA256_C && MBEDTLS_FS_IO */