blob: de8daa6a5d1c302acae8bb434e44520378e38d2d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * AES-256 file encryption program
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkútif744bd72020-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útif744bd72020-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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
48
Nicholas Wilson2682edf2017-12-05 12:08:15 +000049/* Enable definition of fileno() even when compiling with -std=c99. Must be
50 * set before config.h, which pulls in glibc's features.h indirectly.
51 * Harmless on other platforms. */
52#define _POSIX_C_SOURCE 1
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000062#else
Rich Evans18b78c72015-02-11 14:06:19 +000063#include <stdio.h>
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +010064#include <stdlib.h>
65#define mbedtls_fprintf fprintf
66#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010067#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010068#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +010069#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
70#endif /* MBEDTLS_PLATFORM_C */
Rich Evans18b78c72015-02-11 14:06:19 +000071
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000072#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +010073#include "mbedtls/md.h"
Hanno Becker0b44d5c2018-10-12 16:46:37 +010074#include "mbedtls/platform_util.h"
Rich Evans18b78c72015-02-11 14:06:19 +000075
76#include <stdio.h>
77#include <stdlib.h>
78#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000079
Paul Bakker494c0b82011-04-24 15:30:07 +000080#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000081#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000082#if !defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000083#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000084#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000085#else
86#include <sys/types.h>
87#include <unistd.h>
88#endif
89
Paul Bakker5121ce52009-01-03 21:22:43 +000090#define MODE_ENCRYPT 0
91#define MODE_DECRYPT 1
92
93#define USAGE \
94 "\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
95 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
96 "\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
97 "\n"
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_SHA256_C) || \
100 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_MD_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000101int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +0000102{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_SHA256_C "
104 "and/or MBEDTLS_FS_IO and/or MBEDTLS_MD_C "
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100105 "not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200106 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000107}
108#else
Simon Butcher63cb97e2018-12-06 17:43:31 +0000109
Simon Butcher63cb97e2018-12-06 17:43:31 +0000110
Paul Bakker5121ce52009-01-03 21:22:43 +0000111int main( int argc, char *argv[] )
112{
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +0100113 int ret = 0;
114 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000115
Simon Butcher0e7d3872016-08-30 14:25:24 +0100116 unsigned int i, n;
Paul Bakker23986e52011-04-24 08:57:21 +0000117 int mode, lastn;
118 size_t keylen;
Paul Bakker20a78082011-01-21 09:32:12 +0000119 FILE *fkey, *fin = NULL, *fout = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121 char *p;
Hanno Beckerce37e622017-06-27 08:24:34 +0100122
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 unsigned char IV[16];
Hanno Beckerce37e622017-06-27 08:24:34 +0100124 unsigned char tmp[16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 unsigned char key[512];
126 unsigned char digest[32];
127 unsigned char buffer[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100128 unsigned char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_aes_context aes_ctx;
131 mbedtls_md_context_t sha_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Paul Bakkercce9d772011-11-18 14:26:47 +0000133#if defined(_WIN32_WCE)
134 long filesize, offset;
135#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 LARGE_INTEGER li_size;
137 __int64 filesize, offset;
138#else
139 off_t filesize, offset;
140#endif
141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_aes_init( &aes_ctx );
143 mbedtls_md_init( &sha_ctx );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 ret = mbedtls_md_setup( &sha_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 1 );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100146 if( ret != 0 )
147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_printf( " ! mbedtls_md_setup() returned -0x%04x\n", -ret );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100149 goto exit;
150 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200151
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 /*
153 * Parse the command-line arguments.
154 */
155 if( argc != 5 )
156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_printf( USAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
Paul Bakkercce9d772011-11-18 14:26:47 +0000159#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 fflush( stdout ); getchar();
162#endif
163
164 goto exit;
165 }
166
167 mode = atoi( argv[1] );
Hanno Beckerce37e622017-06-27 08:24:34 +0100168 memset( IV, 0, sizeof( IV ) );
169 memset( key, 0, sizeof( key ) );
170 memset( digest, 0, sizeof( digest ) );
171 memset( buffer, 0, sizeof( buffer ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
173 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_fprintf( stderr, "invalide operation mode\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 goto exit;
177 }
178
179 if( strcmp( argv[2], argv[3] ) == 0 )
180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 goto exit;
183 }
184
185 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 goto exit;
189 }
190
191 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 goto exit;
195 }
196
197 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100198 * Read the secret key from file or command line
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 */
200 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
201 {
202 keylen = fread( key, 1, sizeof( key ), fkey );
203 fclose( fkey );
204 }
205 else
206 {
207 if( memcmp( argv[4], "hex:", 4 ) == 0 )
208 {
209 p = &argv[4][4];
210 keylen = 0;
211
212 while( sscanf( p, "%02X", &n ) > 0 &&
213 keylen < (int) sizeof( key ) )
214 {
215 key[keylen++] = (unsigned char) n;
216 p += 2;
217 }
218 }
219 else
220 {
221 keylen = strlen( argv[4] );
222
223 if( keylen > (int) sizeof( key ) )
224 keylen = (int) sizeof( key );
225
226 memcpy( key, argv[4], keylen );
227 }
228 }
229
Paul Bakkercce9d772011-11-18 14:26:47 +0000230#if defined(_WIN32_WCE)
231 filesize = fseek( fin, 0L, SEEK_END );
232#else
233#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 /*
235 * Support large files (> 2Gb) on Win32
236 */
237 li_size.QuadPart = 0;
238 li_size.LowPart =
239 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
240 li_size.LowPart, &li_size.HighPart, FILE_END );
241
242 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 goto exit;
246 }
247
248 filesize = li_size.QuadPart;
249#else
250 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
251 {
252 perror( "lseek" );
253 goto exit;
254 }
255#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000256#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000257
258 if( fseek( fin, 0, SEEK_SET ) < 0 )
259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 goto exit;
262 }
263
264 if( mode == MODE_ENCRYPT )
265 {
266 /*
267 * Generate the initialization vector as:
268 * IV = SHA-256( filesize || filename )[0..15]
269 */
270 for( i = 0; i < 8; i++ )
271 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
272
273 p = argv[2];
274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_md_starts( &sha_ctx );
276 mbedtls_md_update( &sha_ctx, buffer, 8 );
277 mbedtls_md_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
278 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000279
280 memcpy( IV, digest, 16 );
281
282 /*
283 * The last four bits in the IV are actually used
284 * to store the file size modulo the AES block size.
285 */
286 lastn = (int)( filesize & 0x0F );
287
288 IV[15] = (unsigned char)
289 ( ( IV[15] & 0xF0 ) | lastn );
290
291 /*
292 * Append the IV at the beginning of the output.
293 */
294 if( fwrite( IV, 1, 16, fout ) != 16 )
295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 goto exit;
298 }
299
300 /*
301 * Hash the IV and the secret key together 8192 times
302 * using the result to setup the AES context and HMAC.
303 */
304 memset( digest, 0, 32 );
305 memcpy( digest, IV, 16 );
306
307 for( i = 0; i < 8192; i++ )
308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_md_starts( &sha_ctx );
310 mbedtls_md_update( &sha_ctx, digest, 32 );
311 mbedtls_md_update( &sha_ctx, key, keylen );
312 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 }
314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_aes_setkey_enc( &aes_ctx, digest, 256 );
316 mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
318 /*
319 * Encrypt and write the ciphertext.
320 */
321 for( offset = 0; offset < filesize; offset += 16 )
322 {
323 n = ( filesize - offset > 16 ) ? 16 : (int)
324 ( filesize - offset );
325
326 if( fread( buffer, 1, n, fin ) != (size_t) n )
327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 goto exit;
330 }
331
332 for( i = 0; i < 16; i++ )
333 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, buffer, buffer );
336 mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337
338 if( fwrite( buffer, 1, 16, fout ) != 16 )
339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 goto exit;
342 }
343
344 memcpy( IV, buffer, 16 );
345 }
346
347 /*
348 * Finally write the HMAC.
349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 mbedtls_md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000351
352 if( fwrite( digest, 1, 32, fout ) != 32 )
353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 goto exit;
356 }
357 }
358
359 if( mode == MODE_DECRYPT )
360 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 /*
362 * The encrypted file must be structured as follows:
363 *
364 * 00 .. 15 Initialization Vector
365 * 16 .. 31 AES Encrypted Block #1
366 * ..
367 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
368 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
369 */
370 if( filesize < 48 )
371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 goto exit;
374 }
375
376 if( ( filesize & 0x0F ) != 0 )
377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_fprintf( stderr, "File size not a multiple of 16.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 goto exit;
380 }
381
382 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100383 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 */
385 filesize -= ( 16 + 32 );
386
387 /*
388 * Read the IV and original filesize modulo 16.
389 */
390 if( fread( buffer, 1, 16, fin ) != 16 )
391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000393 goto exit;
394 }
395
396 memcpy( IV, buffer, 16 );
397 lastn = IV[15] & 0x0F;
398
399 /*
400 * Hash the IV and the secret key together 8192 times
401 * using the result to setup the AES context and HMAC.
402 */
403 memset( digest, 0, 32 );
404 memcpy( digest, IV, 16 );
405
406 for( i = 0; i < 8192; i++ )
407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 mbedtls_md_starts( &sha_ctx );
409 mbedtls_md_update( &sha_ctx, digest, 32 );
410 mbedtls_md_update( &sha_ctx, key, keylen );
411 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 }
413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_aes_setkey_dec( &aes_ctx, digest, 256 );
415 mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
417 /*
418 * Decrypt and write the plaintext.
419 */
420 for( offset = 0; offset < filesize; offset += 16 )
421 {
422 if( fread( buffer, 1, 16, fin ) != 16 )
423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 goto exit;
426 }
427
428 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
431 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200432
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 for( i = 0; i < 16; i++ )
434 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
435
436 memcpy( IV, tmp, 16 );
437
438 n = ( lastn > 0 && offset == filesize - 16 )
439 ? lastn : 16;
440
441 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 goto exit;
445 }
446 }
447
448 /*
449 * Verify the message authentication code.
450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
453 if( fread( buffer, 1, 32, fin ) != 32 )
454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 goto exit;
457 }
458
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100459 /* Use constant-time buffer comparison */
460 diff = 0;
461 for( i = 0; i < 32; i++ )
462 diff |= digest[i] ^ buffer[i];
463
464 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 "or file corrupted.\n" );
468 goto exit;
469 }
470 }
471
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +0100472 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
474exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000475 if( fin )
476 fclose( fin );
477 if( fout )
478 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
Hanno Beckerce37e622017-06-27 08:24:34 +0100480 /* Zeroize all command line arguments to also cover
481 the case when the user has missed or reordered some,
482 in which case the key might not be in argv[4]. */
483 for( i = 0; i < (unsigned int) argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100484 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerce37e622017-06-27 08:24:34 +0100485
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100486 mbedtls_platform_zeroize( IV, sizeof( IV ) );
487 mbedtls_platform_zeroize( key, sizeof( key ) );
488 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
489 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
490 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_aes_free( &aes_ctx );
493 mbedtls_md_free( &sha_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200495 mbedtls_exit( exit_code );
Paul Bakker5121ce52009-01-03 21:22:43 +0000496}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497#endif /* MBEDTLS_AES_C && MBEDTLS_SHA256_C && MBEDTLS_FS_IO */