blob: c727f936e1fb31924b9d408cf58121139a89284f [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +010032#include <stdlib.h>
33#define mbedtls_fprintf fprintf
34#define mbedtls_printf printf
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010035#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +010036#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
37#endif /* MBEDTLS_PLATFORM_C */
Rich Evans18b78c72015-02-11 14:06:19 +000038
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +010040#include "mbedtls/md.h"
Rich Evans18b78c72015-02-11 14:06:19 +000041
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000045
Paul Bakker494c0b82011-04-24 15:30:07 +000046#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000047#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000048#if !defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000049#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000050#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000051#else
52#include <sys/types.h>
53#include <unistd.h>
54#endif
55
Paul Bakker5121ce52009-01-03 21:22:43 +000056#define MODE_ENCRYPT 0
57#define MODE_DECRYPT 1
58
59#define USAGE \
60 "\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
61 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
62 "\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
63 "\n"
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_SHA256_C) || \
66 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_MD_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000067int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000068{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_SHA256_C "
70 "and/or MBEDTLS_FS_IO and/or MBEDTLS_MD_C "
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +010071 "not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000072 return( 0 );
73}
74#else
Paul Bakker5121ce52009-01-03 21:22:43 +000075int main( int argc, char *argv[] )
76{
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +010077 int ret = 0;
78 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker5690efc2011-05-26 13:16:06 +000079
Simon Butcher0e7d3872016-08-30 14:25:24 +010080 unsigned int i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000081 int mode, lastn;
82 size_t keylen;
Paul Bakker20a78082011-01-21 09:32:12 +000083 FILE *fkey, *fin = NULL, *fout = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000084
85 char *p;
Hanno Beckerce37e622017-06-27 08:24:34 +010086
Paul Bakker5121ce52009-01-03 21:22:43 +000087 unsigned char IV[16];
Hanno Beckerce37e622017-06-27 08:24:34 +010088 unsigned char tmp[16];
Paul Bakker5121ce52009-01-03 21:22:43 +000089 unsigned char key[512];
90 unsigned char digest[32];
91 unsigned char buffer[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010092 unsigned char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_aes_context aes_ctx;
95 mbedtls_md_context_t sha_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Paul Bakkercce9d772011-11-18 14:26:47 +000097#if defined(_WIN32_WCE)
98 long filesize, offset;
99#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 LARGE_INTEGER li_size;
101 __int64 filesize, offset;
102#else
103 off_t filesize, offset;
104#endif
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 mbedtls_aes_init( &aes_ctx );
107 mbedtls_md_init( &sha_ctx );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 ret = mbedtls_md_setup( &sha_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 1 );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100110 if( ret != 0 )
111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_printf( " ! mbedtls_md_setup() returned -0x%04x\n", -ret );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100113 goto exit;
114 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200115
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 /*
117 * Parse the command-line arguments.
118 */
119 if( argc != 5 )
120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_printf( USAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
Paul Bakkercce9d772011-11-18 14:26:47 +0000123#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 fflush( stdout ); getchar();
126#endif
127
128 goto exit;
129 }
130
131 mode = atoi( argv[1] );
Hanno Beckerce37e622017-06-27 08:24:34 +0100132 memset( IV, 0, sizeof( IV ) );
133 memset( key, 0, sizeof( key ) );
134 memset( digest, 0, sizeof( digest ) );
135 memset( buffer, 0, sizeof( buffer ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
137 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_fprintf( stderr, "invalide operation mode\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 goto exit;
141 }
142
143 if( strcmp( argv[2], argv[3] ) == 0 )
144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 goto exit;
147 }
148
149 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 goto exit;
153 }
154
155 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 goto exit;
159 }
160
161 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100162 * Read the secret key from file or command line
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 */
164 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
165 {
166 keylen = fread( key, 1, sizeof( key ), fkey );
167 fclose( fkey );
168 }
169 else
170 {
171 if( memcmp( argv[4], "hex:", 4 ) == 0 )
172 {
173 p = &argv[4][4];
174 keylen = 0;
175
176 while( sscanf( p, "%02X", &n ) > 0 &&
177 keylen < (int) sizeof( key ) )
178 {
179 key[keylen++] = (unsigned char) n;
180 p += 2;
181 }
182 }
183 else
184 {
185 keylen = strlen( argv[4] );
186
187 if( keylen > (int) sizeof( key ) )
188 keylen = (int) sizeof( key );
189
190 memcpy( key, argv[4], keylen );
191 }
192 }
193
Paul Bakkercce9d772011-11-18 14:26:47 +0000194#if defined(_WIN32_WCE)
195 filesize = fseek( fin, 0L, SEEK_END );
196#else
197#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 /*
199 * Support large files (> 2Gb) on Win32
200 */
201 li_size.QuadPart = 0;
202 li_size.LowPart =
203 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
204 li_size.LowPart, &li_size.HighPart, FILE_END );
205
206 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 goto exit;
210 }
211
212 filesize = li_size.QuadPart;
213#else
214 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
215 {
216 perror( "lseek" );
217 goto exit;
218 }
219#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000220#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
222 if( fseek( fin, 0, SEEK_SET ) < 0 )
223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 goto exit;
226 }
227
228 if( mode == MODE_ENCRYPT )
229 {
230 /*
231 * Generate the initialization vector as:
232 * IV = SHA-256( filesize || filename )[0..15]
233 */
234 for( i = 0; i < 8; i++ )
235 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
236
237 p = argv[2];
238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 mbedtls_md_starts( &sha_ctx );
240 mbedtls_md_update( &sha_ctx, buffer, 8 );
241 mbedtls_md_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
242 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
244 memcpy( IV, digest, 16 );
245
246 /*
247 * The last four bits in the IV are actually used
248 * to store the file size modulo the AES block size.
249 */
250 lastn = (int)( filesize & 0x0F );
251
252 IV[15] = (unsigned char)
253 ( ( IV[15] & 0xF0 ) | lastn );
254
255 /*
256 * Append the IV at the beginning of the output.
257 */
258 if( fwrite( IV, 1, 16, fout ) != 16 )
259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 goto exit;
262 }
263
264 /*
265 * Hash the IV and the secret key together 8192 times
266 * using the result to setup the AES context and HMAC.
267 */
268 memset( digest, 0, 32 );
269 memcpy( digest, IV, 16 );
270
271 for( i = 0; i < 8192; i++ )
272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_md_starts( &sha_ctx );
274 mbedtls_md_update( &sha_ctx, digest, 32 );
275 mbedtls_md_update( &sha_ctx, key, keylen );
276 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 }
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_aes_setkey_enc( &aes_ctx, digest, 256 );
280 mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000281
282 /*
283 * Encrypt and write the ciphertext.
284 */
285 for( offset = 0; offset < filesize; offset += 16 )
286 {
287 n = ( filesize - offset > 16 ) ? 16 : (int)
288 ( filesize - offset );
289
290 if( fread( buffer, 1, n, fin ) != (size_t) n )
291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 goto exit;
294 }
295
296 for( i = 0; i < 16; i++ )
297 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, buffer, buffer );
300 mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000301
302 if( fwrite( buffer, 1, 16, fout ) != 16 )
303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 goto exit;
306 }
307
308 memcpy( IV, buffer, 16 );
309 }
310
311 /*
312 * Finally write the HMAC.
313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
316 if( fwrite( digest, 1, 32, fout ) != 32 )
317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 goto exit;
320 }
321 }
322
323 if( mode == MODE_DECRYPT )
324 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 /*
326 * The encrypted file must be structured as follows:
327 *
328 * 00 .. 15 Initialization Vector
329 * 16 .. 31 AES Encrypted Block #1
330 * ..
331 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
332 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
333 */
334 if( filesize < 48 )
335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 goto exit;
338 }
339
340 if( ( filesize & 0x0F ) != 0 )
341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 mbedtls_fprintf( stderr, "File size not a multiple of 16.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 goto exit;
344 }
345
346 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100347 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 */
349 filesize -= ( 16 + 32 );
350
351 /*
352 * Read the IV and original filesize modulo 16.
353 */
354 if( fread( buffer, 1, 16, fin ) != 16 )
355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000357 goto exit;
358 }
359
360 memcpy( IV, buffer, 16 );
361 lastn = IV[15] & 0x0F;
362
363 /*
364 * Hash the IV and the secret key together 8192 times
365 * using the result to setup the AES context and HMAC.
366 */
367 memset( digest, 0, 32 );
368 memcpy( digest, IV, 16 );
369
370 for( i = 0; i < 8192; i++ )
371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_md_starts( &sha_ctx );
373 mbedtls_md_update( &sha_ctx, digest, 32 );
374 mbedtls_md_update( &sha_ctx, key, keylen );
375 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 }
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_aes_setkey_dec( &aes_ctx, digest, 256 );
379 mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000380
381 /*
382 * Decrypt and write the plaintext.
383 */
384 for( offset = 0; offset < filesize; offset += 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( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
395 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200396
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 for( i = 0; i < 16; i++ )
398 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
399
400 memcpy( IV, tmp, 16 );
401
402 n = ( lastn > 0 && offset == filesize - 16 )
403 ? lastn : 16;
404
405 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 goto exit;
409 }
410 }
411
412 /*
413 * Verify the message authentication code.
414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
417 if( fread( buffer, 1, 32, fin ) != 32 )
418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 goto exit;
421 }
422
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100423 /* Use constant-time buffer comparison */
424 diff = 0;
425 for( i = 0; i < 32; i++ )
426 diff |= digest[i] ^ buffer[i];
427
428 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 "or file corrupted.\n" );
432 goto exit;
433 }
434 }
435
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +0100436 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
438exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000439 if( fin )
440 fclose( fin );
441 if( fout )
442 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000443
Hanno Beckerce37e622017-06-27 08:24:34 +0100444 /* Zeroize all command line arguments to also cover
445 the case when the user has missed or reordered some,
446 in which case the key might not be in argv[4]. */
447 for( i = 0; i < (unsigned int) argc; i++ )
448 memset( argv[i], 0, strlen( argv[i] ) );
449
450 memset( IV, 0, sizeof( IV ) );
451 memset( key, 0, sizeof( key ) );
452 memset( tmp, 0, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 memset( buffer, 0, sizeof( buffer ) );
454 memset( digest, 0, sizeof( digest ) );
455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_aes_free( &aes_ctx );
457 mbedtls_md_free( &sha_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +0100459 return( exit_code );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#endif /* MBEDTLS_AES_C && MBEDTLS_SHA256_C && MBEDTLS_FS_IO */