blob: 69c4060009dd934a6a9ddde5eab01fd478cff477 [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
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010040#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +010041#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
42#endif /* MBEDTLS_PLATFORM_C */
Rich Evans18b78c72015-02-11 14:06:19 +000043
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +010045#include "mbedtls/md.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");
Paul Bakker5690efc2011-05-26 13:16:06 +000077 return( 0 );
78}
79#else
Paul Bakker5121ce52009-01-03 21:22:43 +000080int main( int argc, char *argv[] )
81{
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +010082 int ret = 0;
83 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker5690efc2011-05-26 13:16:06 +000084
Simon Butcher0e7d3872016-08-30 14:25:24 +010085 unsigned int i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000086 int mode, lastn;
87 size_t keylen;
Paul Bakker20a78082011-01-21 09:32:12 +000088 FILE *fkey, *fin = NULL, *fout = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000089
90 char *p;
Hanno Beckerce37e622017-06-27 08:24:34 +010091
Paul Bakker5121ce52009-01-03 21:22:43 +000092 unsigned char IV[16];
Hanno Beckerce37e622017-06-27 08:24:34 +010093 unsigned char tmp[16];
Paul Bakker5121ce52009-01-03 21:22:43 +000094 unsigned char key[512];
95 unsigned char digest[32];
96 unsigned char buffer[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010097 unsigned char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +000098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_aes_context aes_ctx;
100 mbedtls_md_context_t sha_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Paul Bakkercce9d772011-11-18 14:26:47 +0000102#if defined(_WIN32_WCE)
103 long filesize, offset;
104#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 LARGE_INTEGER li_size;
106 __int64 filesize, offset;
107#else
108 off_t filesize, offset;
109#endif
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_aes_init( &aes_ctx );
112 mbedtls_md_init( &sha_ctx );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 ret = mbedtls_md_setup( &sha_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 1 );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100115 if( ret != 0 )
116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_printf( " ! mbedtls_md_setup() returned -0x%04x\n", -ret );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100118 goto exit;
119 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200120
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 /*
122 * Parse the command-line arguments.
123 */
124 if( argc != 5 )
125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_printf( USAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
Paul Bakkercce9d772011-11-18 14:26:47 +0000128#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 fflush( stdout ); getchar();
131#endif
132
133 goto exit;
134 }
135
136 mode = atoi( argv[1] );
Hanno Beckerce37e622017-06-27 08:24:34 +0100137 memset( IV, 0, sizeof( IV ) );
138 memset( key, 0, sizeof( key ) );
139 memset( digest, 0, sizeof( digest ) );
140 memset( buffer, 0, sizeof( buffer ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
142 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_fprintf( stderr, "invalide operation mode\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 goto exit;
146 }
147
148 if( strcmp( argv[2], argv[3] ) == 0 )
149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 goto exit;
152 }
153
154 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 goto exit;
158 }
159
160 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 goto exit;
164 }
165
166 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100167 * Read the secret key from file or command line
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 */
169 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
170 {
171 keylen = fread( key, 1, sizeof( key ), fkey );
172 fclose( fkey );
173 }
174 else
175 {
176 if( memcmp( argv[4], "hex:", 4 ) == 0 )
177 {
178 p = &argv[4][4];
179 keylen = 0;
180
181 while( sscanf( p, "%02X", &n ) > 0 &&
182 keylen < (int) sizeof( key ) )
183 {
184 key[keylen++] = (unsigned char) n;
185 p += 2;
186 }
187 }
188 else
189 {
190 keylen = strlen( argv[4] );
191
192 if( keylen > (int) sizeof( key ) )
193 keylen = (int) sizeof( key );
194
195 memcpy( key, argv[4], keylen );
196 }
197 }
198
Paul Bakkercce9d772011-11-18 14:26:47 +0000199#if defined(_WIN32_WCE)
200 filesize = fseek( fin, 0L, SEEK_END );
201#else
202#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 /*
204 * Support large files (> 2Gb) on Win32
205 */
206 li_size.QuadPart = 0;
207 li_size.LowPart =
208 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
209 li_size.LowPart, &li_size.HighPart, FILE_END );
210
211 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 goto exit;
215 }
216
217 filesize = li_size.QuadPart;
218#else
219 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
220 {
221 perror( "lseek" );
222 goto exit;
223 }
224#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000225#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
227 if( fseek( fin, 0, SEEK_SET ) < 0 )
228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 goto exit;
231 }
232
233 if( mode == MODE_ENCRYPT )
234 {
235 /*
236 * Generate the initialization vector as:
237 * IV = SHA-256( filesize || filename )[0..15]
238 */
239 for( i = 0; i < 8; i++ )
240 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
241
242 p = argv[2];
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_md_starts( &sha_ctx );
245 mbedtls_md_update( &sha_ctx, buffer, 8 );
246 mbedtls_md_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
247 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
249 memcpy( IV, digest, 16 );
250
251 /*
252 * The last four bits in the IV are actually used
253 * to store the file size modulo the AES block size.
254 */
255 lastn = (int)( filesize & 0x0F );
256
257 IV[15] = (unsigned char)
258 ( ( IV[15] & 0xF0 ) | lastn );
259
260 /*
261 * Append the IV at the beginning of the output.
262 */
263 if( fwrite( IV, 1, 16, fout ) != 16 )
264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 goto exit;
267 }
268
269 /*
270 * Hash the IV and the secret key together 8192 times
271 * using the result to setup the AES context and HMAC.
272 */
273 memset( digest, 0, 32 );
274 memcpy( digest, IV, 16 );
275
276 for( i = 0; i < 8192; i++ )
277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_md_starts( &sha_ctx );
279 mbedtls_md_update( &sha_ctx, digest, 32 );
280 mbedtls_md_update( &sha_ctx, key, keylen );
281 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 }
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_aes_setkey_enc( &aes_ctx, digest, 256 );
285 mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
287 /*
288 * Encrypt and write the ciphertext.
289 */
290 for( offset = 0; offset < filesize; offset += 16 )
291 {
292 n = ( filesize - offset > 16 ) ? 16 : (int)
293 ( filesize - offset );
294
295 if( fread( buffer, 1, n, fin ) != (size_t) n )
296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 goto exit;
299 }
300
301 for( i = 0; i < 16; i++ )
302 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, buffer, buffer );
305 mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
307 if( fwrite( buffer, 1, 16, fout ) != 16 )
308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 goto exit;
311 }
312
313 memcpy( IV, buffer, 16 );
314 }
315
316 /*
317 * Finally write the HMAC.
318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
321 if( fwrite( digest, 1, 32, fout ) != 32 )
322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 goto exit;
325 }
326 }
327
328 if( mode == MODE_DECRYPT )
329 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 /*
331 * The encrypted file must be structured as follows:
332 *
333 * 00 .. 15 Initialization Vector
334 * 16 .. 31 AES Encrypted Block #1
335 * ..
336 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
337 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
338 */
339 if( filesize < 48 )
340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 goto exit;
343 }
344
345 if( ( filesize & 0x0F ) != 0 )
346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 mbedtls_fprintf( stderr, "File size not a multiple of 16.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 goto exit;
349 }
350
351 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100352 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000353 */
354 filesize -= ( 16 + 32 );
355
356 /*
357 * Read the IV and original filesize modulo 16.
358 */
359 if( fread( buffer, 1, 16, fin ) != 16 )
360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 goto exit;
363 }
364
365 memcpy( IV, buffer, 16 );
366 lastn = IV[15] & 0x0F;
367
368 /*
369 * Hash the IV and the secret key together 8192 times
370 * using the result to setup the AES context and HMAC.
371 */
372 memset( digest, 0, 32 );
373 memcpy( digest, IV, 16 );
374
375 for( i = 0; i < 8192; i++ )
376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_md_starts( &sha_ctx );
378 mbedtls_md_update( &sha_ctx, digest, 32 );
379 mbedtls_md_update( &sha_ctx, key, keylen );
380 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000381 }
382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_aes_setkey_dec( &aes_ctx, digest, 256 );
384 mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000385
386 /*
387 * Decrypt and write the plaintext.
388 */
389 for( offset = 0; offset < filesize; offset += 16 )
390 {
391 if( fread( buffer, 1, 16, fin ) != 16 )
392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 goto exit;
395 }
396
397 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
400 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200401
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 for( i = 0; i < 16; i++ )
403 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
404
405 memcpy( IV, tmp, 16 );
406
407 n = ( lastn > 0 && offset == filesize - 16 )
408 ? lastn : 16;
409
410 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 goto exit;
414 }
415 }
416
417 /*
418 * Verify the message authentication code.
419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000421
422 if( fread( buffer, 1, 32, fin ) != 32 )
423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 goto exit;
426 }
427
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100428 /* Use constant-time buffer comparison */
429 diff = 0;
430 for( i = 0; i < 32; i++ )
431 diff |= digest[i] ^ buffer[i];
432
433 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker5121ce52009-01-03 21:22:43 +0000436 "or file corrupted.\n" );
437 goto exit;
438 }
439 }
440
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +0100441 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000442
443exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000444 if( fin )
445 fclose( fin );
446 if( fout )
447 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
Hanno Beckerce37e622017-06-27 08:24:34 +0100449 /* Zeroize all command line arguments to also cover
450 the case when the user has missed or reordered some,
451 in which case the key might not be in argv[4]. */
452 for( i = 0; i < (unsigned int) argc; i++ )
453 memset( argv[i], 0, strlen( argv[i] ) );
454
455 memset( IV, 0, sizeof( IV ) );
456 memset( key, 0, sizeof( key ) );
457 memset( tmp, 0, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 memset( buffer, 0, sizeof( buffer ) );
459 memset( digest, 0, sizeof( digest ) );
460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_aes_free( &aes_ctx );
462 mbedtls_md_free( &sha_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +0100464 return( exit_code );
Paul Bakker5121ce52009-01-03 21:22:43 +0000465}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466#endif /* MBEDTLS_AES_C && MBEDTLS_SHA256_C && MBEDTLS_FS_IO */