blob: 95d64d911b31611202344ec30e2ea09a471e1f0a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * AES-256 file encryption program
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Nicholas Wilson2682edf2017-12-05 12:08:15 +000020/* Enable definition of fileno() even when compiling with -std=c99. Must be
21 * set before config.h, which pulls in glibc's features.h indirectly.
22 * Harmless on other platforms. */
nia1c0c8372020-06-11 12:03:45 +010023#define _POSIX_C_SOURCE 200112L
Nicholas Wilson2682edf2017-12-05 12:08:15 +000024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000033#else
Rich Evans18b78c72015-02-11 14:06:19 +000034#include <stdio.h>
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +010035#include <stdlib.h>
36#define mbedtls_fprintf fprintf
37#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010038#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010039#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +010040#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
41#endif /* MBEDTLS_PLATFORM_C */
Rich Evans18b78c72015-02-11 14:06:19 +000042
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +010044#include "mbedtls/md.h"
Hanno Becker0b44d5c2018-10-12 16:46:37 +010045#include "mbedtls/platform_util.h"
Rich Evans18b78c72015-02-11 14:06:19 +000046
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000050
Paul Bakker494c0b82011-04-24 15:30:07 +000051#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000052#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000053#if !defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000054#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000055#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000056#else
57#include <sys/types.h>
58#include <unistd.h>
59#endif
60
Paul Bakker5121ce52009-01-03 21:22:43 +000061#define MODE_ENCRYPT 0
62#define MODE_DECRYPT 1
63
64#define USAGE \
65 "\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
66 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
67 "\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
68 "\n"
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_SHA256_C) || \
71 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_MD_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000072int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000073{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_SHA256_C "
75 "and/or MBEDTLS_FS_IO and/or MBEDTLS_MD_C "
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +010076 "not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020077 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000078}
79#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000080
Simon Butcher63cb97e2018-12-06 17:43:31 +000081
Paul Bakker5121ce52009-01-03 21:22:43 +000082int main( int argc, char *argv[] )
83{
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +010084 int ret = 0;
85 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker5690efc2011-05-26 13:16:06 +000086
Simon Butcher0e7d3872016-08-30 14:25:24 +010087 unsigned int i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000088 int mode, lastn;
89 size_t keylen;
Paul Bakker20a78082011-01-21 09:32:12 +000090 FILE *fkey, *fin = NULL, *fout = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000091
92 char *p;
Hanno Beckerce37e622017-06-27 08:24:34 +010093
Paul Bakker5121ce52009-01-03 21:22:43 +000094 unsigned char IV[16];
Hanno Beckerce37e622017-06-27 08:24:34 +010095 unsigned char tmp[16];
Paul Bakker5121ce52009-01-03 21:22:43 +000096 unsigned char key[512];
k-stachowiak20935eb2019-09-26 11:22:02 +020097 unsigned char digest[64];
Paul Bakker5121ce52009-01-03 21:22:43 +000098 unsigned char buffer[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010099 unsigned char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_aes_context aes_ctx;
102 mbedtls_md_context_t sha_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Paul Bakkercce9d772011-11-18 14:26:47 +0000104#if defined(_WIN32_WCE)
105 long filesize, offset;
106#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 LARGE_INTEGER li_size;
108 __int64 filesize, offset;
109#else
110 off_t filesize, offset;
111#endif
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_aes_init( &aes_ctx );
114 mbedtls_md_init( &sha_ctx );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 ret = mbedtls_md_setup( &sha_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 1 );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100117 if( ret != 0 )
118 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200119 mbedtls_printf( " ! mbedtls_md_setup() returned -0x%04x\n", (unsigned int) -ret );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100120 goto exit;
121 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200122
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 /*
124 * Parse the command-line arguments.
125 */
126 if( argc != 5 )
127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_printf( USAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Paul Bakkercce9d772011-11-18 14:26:47 +0000130#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 fflush( stdout ); getchar();
133#endif
134
135 goto exit;
136 }
137
138 mode = atoi( argv[1] );
Hanno Beckerce37e622017-06-27 08:24:34 +0100139 memset( IV, 0, sizeof( IV ) );
140 memset( key, 0, sizeof( key ) );
141 memset( digest, 0, sizeof( digest ) );
142 memset( buffer, 0, sizeof( buffer ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
144 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_fprintf( stderr, "invalide operation mode\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 goto exit;
148 }
149
150 if( strcmp( argv[2], argv[3] ) == 0 )
151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 goto exit;
154 }
155
156 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 goto exit;
160 }
161
162 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 goto exit;
166 }
167
168 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100169 * Read the secret key from file or command line
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 */
171 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
172 {
173 keylen = fread( key, 1, sizeof( key ), fkey );
174 fclose( fkey );
175 }
176 else
177 {
178 if( memcmp( argv[4], "hex:", 4 ) == 0 )
179 {
180 p = &argv[4][4];
181 keylen = 0;
182
183 while( sscanf( p, "%02X", &n ) > 0 &&
184 keylen < (int) sizeof( key ) )
185 {
186 key[keylen++] = (unsigned char) n;
187 p += 2;
188 }
189 }
190 else
191 {
192 keylen = strlen( argv[4] );
193
194 if( keylen > (int) sizeof( key ) )
195 keylen = (int) sizeof( key );
196
197 memcpy( key, argv[4], keylen );
198 }
199 }
200
Paul Bakkercce9d772011-11-18 14:26:47 +0000201#if defined(_WIN32_WCE)
202 filesize = fseek( fin, 0L, SEEK_END );
203#else
204#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 /*
206 * Support large files (> 2Gb) on Win32
207 */
208 li_size.QuadPart = 0;
209 li_size.LowPart =
210 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
211 li_size.LowPart, &li_size.HighPart, FILE_END );
212
213 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 goto exit;
217 }
218
219 filesize = li_size.QuadPart;
220#else
221 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
222 {
223 perror( "lseek" );
224 goto exit;
225 }
226#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000227#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
229 if( fseek( fin, 0, SEEK_SET ) < 0 )
230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 goto exit;
233 }
234
235 if( mode == MODE_ENCRYPT )
236 {
237 /*
238 * Generate the initialization vector as:
239 * IV = SHA-256( filesize || filename )[0..15]
240 */
241 for( i = 0; i < 8; i++ )
242 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
243
244 p = argv[2];
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_md_starts( &sha_ctx );
247 mbedtls_md_update( &sha_ctx, buffer, 8 );
248 mbedtls_md_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
249 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
251 memcpy( IV, digest, 16 );
252
253 /*
254 * The last four bits in the IV are actually used
255 * to store the file size modulo the AES block size.
256 */
257 lastn = (int)( filesize & 0x0F );
258
259 IV[15] = (unsigned char)
260 ( ( IV[15] & 0xF0 ) | lastn );
261
262 /*
263 * Append the IV at the beginning of the output.
264 */
265 if( fwrite( IV, 1, 16, fout ) != 16 )
266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 goto exit;
269 }
270
271 /*
272 * Hash the IV and the secret key together 8192 times
273 * using the result to setup the AES context and HMAC.
274 */
275 memset( digest, 0, 32 );
276 memcpy( digest, IV, 16 );
277
278 for( i = 0; i < 8192; i++ )
279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_md_starts( &sha_ctx );
281 mbedtls_md_update( &sha_ctx, digest, 32 );
282 mbedtls_md_update( &sha_ctx, key, keylen );
283 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 }
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_aes_setkey_enc( &aes_ctx, digest, 256 );
287 mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000288
289 /*
290 * Encrypt and write the ciphertext.
291 */
292 for( offset = 0; offset < filesize; offset += 16 )
293 {
294 n = ( filesize - offset > 16 ) ? 16 : (int)
295 ( filesize - offset );
296
297 if( fread( buffer, 1, n, fin ) != (size_t) n )
298 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200299 mbedtls_fprintf( stderr, "fread(%u bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 goto exit;
301 }
302
303 for( i = 0; i < 16; i++ )
304 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, buffer, buffer );
307 mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000308
309 if( fwrite( buffer, 1, 16, fout ) != 16 )
310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 goto exit;
313 }
314
315 memcpy( IV, buffer, 16 );
316 }
317
318 /*
319 * Finally write the HMAC.
320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
323 if( fwrite( digest, 1, 32, fout ) != 32 )
324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 goto exit;
327 }
328 }
329
330 if( mode == MODE_DECRYPT )
331 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 /*
333 * The encrypted file must be structured as follows:
334 *
335 * 00 .. 15 Initialization Vector
336 * 16 .. 31 AES Encrypted Block #1
337 * ..
338 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
339 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
340 */
341 if( filesize < 48 )
342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 goto exit;
345 }
346
347 if( ( filesize & 0x0F ) != 0 )
348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_fprintf( stderr, "File size not a multiple of 16.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 goto exit;
351 }
352
353 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100354 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 */
356 filesize -= ( 16 + 32 );
357
358 /*
359 * Read the IV and original filesize modulo 16.
360 */
361 if( fread( buffer, 1, 16, fin ) != 16 )
362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 goto exit;
365 }
366
367 memcpy( IV, buffer, 16 );
368 lastn = IV[15] & 0x0F;
369
370 /*
371 * Hash the IV and the secret key together 8192 times
372 * using the result to setup the AES context and HMAC.
373 */
374 memset( digest, 0, 32 );
375 memcpy( digest, IV, 16 );
376
377 for( i = 0; i < 8192; i++ )
378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_md_starts( &sha_ctx );
380 mbedtls_md_update( &sha_ctx, digest, 32 );
381 mbedtls_md_update( &sha_ctx, key, keylen );
382 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 }
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_aes_setkey_dec( &aes_ctx, digest, 256 );
386 mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000387
388 /*
389 * Decrypt and write the plaintext.
390 */
391 for( offset = 0; offset < filesize; offset += 16 )
392 {
393 if( fread( buffer, 1, 16, fin ) != 16 )
394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 goto exit;
397 }
398
399 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
402 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200403
Paul Bakker5121ce52009-01-03 21:22:43 +0000404 for( i = 0; i < 16; i++ )
405 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
406
407 memcpy( IV, tmp, 16 );
408
409 n = ( lastn > 0 && offset == filesize - 16 )
410 ? lastn : 16;
411
412 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
413 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200414 mbedtls_fprintf( stderr, "fwrite(%u bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 goto exit;
416 }
417 }
418
419 /*
420 * Verify the message authentication code.
421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000423
424 if( fread( buffer, 1, 32, fin ) != 32 )
425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 goto exit;
428 }
429
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100430 /* Use constant-time buffer comparison */
431 diff = 0;
432 for( i = 0; i < 32; i++ )
433 diff |= digest[i] ^ buffer[i];
434
435 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 "or file corrupted.\n" );
439 goto exit;
440 }
441 }
442
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +0100443 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000444
445exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000446 if( fin )
447 fclose( fin );
448 if( fout )
449 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000450
Hanno Beckerce37e622017-06-27 08:24:34 +0100451 /* Zeroize all command line arguments to also cover
452 the case when the user has missed or reordered some,
453 in which case the key might not be in argv[4]. */
454 for( i = 0; i < (unsigned int) argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100455 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerce37e622017-06-27 08:24:34 +0100456
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100457 mbedtls_platform_zeroize( IV, sizeof( IV ) );
458 mbedtls_platform_zeroize( key, sizeof( key ) );
459 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
460 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
461 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_aes_free( &aes_ctx );
464 mbedtls_md_free( &sha_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000465
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200466 mbedtls_exit( exit_code );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468#endif /* MBEDTLS_AES_C && MBEDTLS_SHA256_C && MBEDTLS_FS_IO */