blob: 8242ea7c9e1ec759f7be643d766ac8324235edf5 [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
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 Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Nicholas Wilson2682edf2017-12-05 12:08:15 +000022/* Enable definition of fileno() even when compiling with -std=c99. Must be
23 * set before config.h, which pulls in glibc's features.h indirectly.
24 * Harmless on other platforms. */
25#define _POSIX_C_SOURCE 1
26
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000035#else
Rich Evans18b78c72015-02-11 14:06:19 +000036#include <stdio.h>
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +010037#include <stdlib.h>
38#define mbedtls_fprintf fprintf
39#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010040#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010041#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +010042#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
43#endif /* MBEDTLS_PLATFORM_C */
Rich Evans18b78c72015-02-11 14:06:19 +000044
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +010046#include "mbedtls/md.h"
Hanno Becker0b44d5c2018-10-12 16:46:37 +010047#include "mbedtls/platform_util.h"
Rich Evans18b78c72015-02-11 14:06:19 +000048
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000052
Paul Bakker494c0b82011-04-24 15:30:07 +000053#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000054#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000055#if !defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000056#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000057#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000058#else
59#include <sys/types.h>
60#include <unistd.h>
61#endif
62
Paul Bakker5121ce52009-01-03 21:22:43 +000063#define MODE_ENCRYPT 0
64#define MODE_DECRYPT 1
65
66#define USAGE \
67 "\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
68 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
69 "\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
70 "\n"
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_SHA256_C) || \
73 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_MD_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000074int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_SHA256_C "
77 "and/or MBEDTLS_FS_IO and/or MBEDTLS_MD_C "
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +010078 "not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000079 return( 0 );
80}
81#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000082
Simon Butcher63cb97e2018-12-06 17:43:31 +000083
Paul Bakker5121ce52009-01-03 21:22:43 +000084int main( int argc, char *argv[] )
85{
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +010086 int ret = 0;
87 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker5690efc2011-05-26 13:16:06 +000088
Simon Butcher0e7d3872016-08-30 14:25:24 +010089 unsigned int i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000090 int mode, lastn;
91 size_t keylen;
Paul Bakker20a78082011-01-21 09:32:12 +000092 FILE *fkey, *fin = NULL, *fout = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000093
94 char *p;
Hanno Beckerce37e622017-06-27 08:24:34 +010095
Paul Bakker5121ce52009-01-03 21:22:43 +000096 unsigned char IV[16];
Hanno Beckerce37e622017-06-27 08:24:34 +010097 unsigned char tmp[16];
Paul Bakker5121ce52009-01-03 21:22:43 +000098 unsigned char key[512];
99 unsigned char digest[32];
100 unsigned char buffer[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100101 unsigned char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_aes_context aes_ctx;
104 mbedtls_md_context_t sha_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Paul Bakkercce9d772011-11-18 14:26:47 +0000106#if defined(_WIN32_WCE)
107 long filesize, offset;
108#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 LARGE_INTEGER li_size;
110 __int64 filesize, offset;
111#else
112 off_t filesize, offset;
113#endif
114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_aes_init( &aes_ctx );
116 mbedtls_md_init( &sha_ctx );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 ret = mbedtls_md_setup( &sha_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 1 );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100119 if( ret != 0 )
120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_printf( " ! mbedtls_md_setup() returned -0x%04x\n", -ret );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100122 goto exit;
123 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200124
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 /*
126 * Parse the command-line arguments.
127 */
128 if( argc != 5 )
129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_printf( USAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Paul Bakkercce9d772011-11-18 14:26:47 +0000132#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 fflush( stdout ); getchar();
135#endif
136
137 goto exit;
138 }
139
140 mode = atoi( argv[1] );
Hanno Beckerce37e622017-06-27 08:24:34 +0100141 memset( IV, 0, sizeof( IV ) );
142 memset( key, 0, sizeof( key ) );
143 memset( digest, 0, sizeof( digest ) );
144 memset( buffer, 0, sizeof( buffer ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_fprintf( stderr, "invalide operation mode\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 goto exit;
150 }
151
152 if( strcmp( argv[2], argv[3] ) == 0 )
153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 goto exit;
156 }
157
158 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 goto exit;
162 }
163
164 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 goto exit;
168 }
169
170 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100171 * Read the secret key from file or command line
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 */
173 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
174 {
175 keylen = fread( key, 1, sizeof( key ), fkey );
176 fclose( fkey );
177 }
178 else
179 {
180 if( memcmp( argv[4], "hex:", 4 ) == 0 )
181 {
182 p = &argv[4][4];
183 keylen = 0;
184
185 while( sscanf( p, "%02X", &n ) > 0 &&
186 keylen < (int) sizeof( key ) )
187 {
188 key[keylen++] = (unsigned char) n;
189 p += 2;
190 }
191 }
192 else
193 {
194 keylen = strlen( argv[4] );
195
196 if( keylen > (int) sizeof( key ) )
197 keylen = (int) sizeof( key );
198
199 memcpy( key, argv[4], keylen );
200 }
201 }
202
Paul Bakkercce9d772011-11-18 14:26:47 +0000203#if defined(_WIN32_WCE)
204 filesize = fseek( fin, 0L, SEEK_END );
205#else
206#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 /*
208 * Support large files (> 2Gb) on Win32
209 */
210 li_size.QuadPart = 0;
211 li_size.LowPart =
212 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
213 li_size.LowPart, &li_size.HighPart, FILE_END );
214
215 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 goto exit;
219 }
220
221 filesize = li_size.QuadPart;
222#else
223 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
224 {
225 perror( "lseek" );
226 goto exit;
227 }
228#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000229#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
231 if( fseek( fin, 0, SEEK_SET ) < 0 )
232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 goto exit;
235 }
236
237 if( mode == MODE_ENCRYPT )
238 {
239 /*
240 * Generate the initialization vector as:
241 * IV = SHA-256( filesize || filename )[0..15]
242 */
243 for( i = 0; i < 8; i++ )
244 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
245
246 p = argv[2];
247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_md_starts( &sha_ctx );
249 mbedtls_md_update( &sha_ctx, buffer, 8 );
250 mbedtls_md_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
251 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000252
253 memcpy( IV, digest, 16 );
254
255 /*
256 * The last four bits in the IV are actually used
257 * to store the file size modulo the AES block size.
258 */
259 lastn = (int)( filesize & 0x0F );
260
261 IV[15] = (unsigned char)
262 ( ( IV[15] & 0xF0 ) | lastn );
263
264 /*
265 * Append the IV at the beginning of the output.
266 */
267 if( fwrite( IV, 1, 16, fout ) != 16 )
268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 goto exit;
271 }
272
273 /*
274 * Hash the IV and the secret key together 8192 times
275 * using the result to setup the AES context and HMAC.
276 */
277 memset( digest, 0, 32 );
278 memcpy( digest, IV, 16 );
279
280 for( i = 0; i < 8192; i++ )
281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_md_starts( &sha_ctx );
283 mbedtls_md_update( &sha_ctx, digest, 32 );
284 mbedtls_md_update( &sha_ctx, key, keylen );
285 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 }
287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_aes_setkey_enc( &aes_ctx, digest, 256 );
289 mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
291 /*
292 * Encrypt and write the ciphertext.
293 */
294 for( offset = 0; offset < filesize; offset += 16 )
295 {
296 n = ( filesize - offset > 16 ) ? 16 : (int)
297 ( filesize - offset );
298
299 if( fread( buffer, 1, n, fin ) != (size_t) n )
300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 goto exit;
303 }
304
305 for( i = 0; i < 16; i++ )
306 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, buffer, buffer );
309 mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
311 if( fwrite( buffer, 1, 16, fout ) != 16 )
312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 goto exit;
315 }
316
317 memcpy( IV, buffer, 16 );
318 }
319
320 /*
321 * Finally write the HMAC.
322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
325 if( fwrite( digest, 1, 32, fout ) != 32 )
326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000328 goto exit;
329 }
330 }
331
332 if( mode == MODE_DECRYPT )
333 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 /*
335 * The encrypted file must be structured as follows:
336 *
337 * 00 .. 15 Initialization Vector
338 * 16 .. 31 AES Encrypted Block #1
339 * ..
340 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
341 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
342 */
343 if( filesize < 48 )
344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 goto exit;
347 }
348
349 if( ( filesize & 0x0F ) != 0 )
350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_fprintf( stderr, "File size not a multiple of 16.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 goto exit;
353 }
354
355 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100356 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000357 */
358 filesize -= ( 16 + 32 );
359
360 /*
361 * Read the IV and original filesize modulo 16.
362 */
363 if( fread( buffer, 1, 16, fin ) != 16 )
364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000366 goto exit;
367 }
368
369 memcpy( IV, buffer, 16 );
370 lastn = IV[15] & 0x0F;
371
372 /*
373 * Hash the IV and the secret key together 8192 times
374 * using the result to setup the AES context and HMAC.
375 */
376 memset( digest, 0, 32 );
377 memcpy( digest, IV, 16 );
378
379 for( i = 0; i < 8192; i++ )
380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_md_starts( &sha_ctx );
382 mbedtls_md_update( &sha_ctx, digest, 32 );
383 mbedtls_md_update( &sha_ctx, key, keylen );
384 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 }
386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_aes_setkey_dec( &aes_ctx, digest, 256 );
388 mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
390 /*
391 * Decrypt and write the plaintext.
392 */
393 for( offset = 0; offset < filesize; offset += 16 )
394 {
395 if( fread( buffer, 1, 16, fin ) != 16 )
396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 goto exit;
399 }
400
401 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
404 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200405
Paul Bakker5121ce52009-01-03 21:22:43 +0000406 for( i = 0; i < 16; i++ )
407 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
408
409 memcpy( IV, tmp, 16 );
410
411 n = ( lastn > 0 && offset == filesize - 16 )
412 ? lastn : 16;
413
414 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417 goto exit;
418 }
419 }
420
421 /*
422 * Verify the message authentication code.
423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426 if( fread( buffer, 1, 32, fin ) != 32 )
427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 goto exit;
430 }
431
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100432 /* Use constant-time buffer comparison */
433 diff = 0;
434 for( i = 0; i < 32; i++ )
435 diff |= digest[i] ^ buffer[i];
436
437 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker5121ce52009-01-03 21:22:43 +0000440 "or file corrupted.\n" );
441 goto exit;
442 }
443 }
444
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +0100445 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
447exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000448 if( fin )
449 fclose( fin );
450 if( fout )
451 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
Hanno Beckerce37e622017-06-27 08:24:34 +0100453 /* Zeroize all command line arguments to also cover
454 the case when the user has missed or reordered some,
455 in which case the key might not be in argv[4]. */
456 for( i = 0; i < (unsigned int) argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100457 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerce37e622017-06-27 08:24:34 +0100458
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100459 mbedtls_platform_zeroize( IV, sizeof( IV ) );
460 mbedtls_platform_zeroize( key, sizeof( key ) );
461 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
462 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
463 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_aes_free( &aes_ctx );
466 mbedtls_md_free( &sha_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +0100468 return( exit_code );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470#endif /* MBEDTLS_AES_C && MBEDTLS_SHA256_C && MBEDTLS_FS_IO */