blob: 5725eb0f326081f4fa22633ea528b378d9c74cee [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"
Hanno Becker0b44d5c2018-10-12 16:46:37 +010046#include "mbedtls/platform_util.h"
Rich Evans18b78c72015-02-11 14:06:19 +000047
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000051
Paul Bakker494c0b82011-04-24 15:30:07 +000052#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000053#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000054#if !defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000055#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057#else
58#include <sys/types.h>
59#include <unistd.h>
60#endif
61
Paul Bakker5121ce52009-01-03 21:22:43 +000062#define MODE_ENCRYPT 0
63#define MODE_DECRYPT 1
64
65#define USAGE \
66 "\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
67 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
68 "\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
69 "\n"
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_SHA256_C) || \
72 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_MD_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000073int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000074{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_SHA256_C "
76 "and/or MBEDTLS_FS_IO and/or MBEDTLS_MD_C "
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +010077 "not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000078 return( 0 );
79}
80#else
Paul Bakker5121ce52009-01-03 21:22:43 +000081int main( int argc, char *argv[] )
82{
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +010083 int ret = 0;
84 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker5690efc2011-05-26 13:16:06 +000085
Simon Butcher0e7d3872016-08-30 14:25:24 +010086 unsigned int i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000087 int mode, lastn;
88 size_t keylen;
Paul Bakker20a78082011-01-21 09:32:12 +000089 FILE *fkey, *fin = NULL, *fout = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000090
91 char *p;
Hanno Beckerce37e622017-06-27 08:24:34 +010092
Paul Bakker5121ce52009-01-03 21:22:43 +000093 unsigned char IV[16];
Hanno Beckerce37e622017-06-27 08:24:34 +010094 unsigned char tmp[16];
Paul Bakker5121ce52009-01-03 21:22:43 +000095 unsigned char key[512];
96 unsigned char digest[32];
97 unsigned char buffer[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +010098 unsigned char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_aes_context aes_ctx;
101 mbedtls_md_context_t sha_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Paul Bakkercce9d772011-11-18 14:26:47 +0000103#if defined(_WIN32_WCE)
104 long filesize, offset;
105#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 LARGE_INTEGER li_size;
107 __int64 filesize, offset;
108#else
109 off_t filesize, offset;
110#endif
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_aes_init( &aes_ctx );
113 mbedtls_md_init( &sha_ctx );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 ret = mbedtls_md_setup( &sha_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 1 );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100116 if( ret != 0 )
117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_printf( " ! mbedtls_md_setup() returned -0x%04x\n", -ret );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100119 goto exit;
120 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200121
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 /*
123 * Parse the command-line arguments.
124 */
125 if( argc != 5 )
126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_printf( USAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
Paul Bakkercce9d772011-11-18 14:26:47 +0000129#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 fflush( stdout ); getchar();
132#endif
133
134 goto exit;
135 }
136
137 mode = atoi( argv[1] );
Hanno Beckerce37e622017-06-27 08:24:34 +0100138 memset( IV, 0, sizeof( IV ) );
139 memset( key, 0, sizeof( key ) );
140 memset( digest, 0, sizeof( digest ) );
141 memset( buffer, 0, sizeof( buffer ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
143 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_fprintf( stderr, "invalide operation mode\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 goto exit;
147 }
148
149 if( strcmp( argv[2], argv[3] ) == 0 )
150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 goto exit;
153 }
154
155 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 goto exit;
159 }
160
161 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 goto exit;
165 }
166
167 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100168 * Read the secret key from file or command line
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 */
170 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
171 {
172 keylen = fread( key, 1, sizeof( key ), fkey );
173 fclose( fkey );
174 }
175 else
176 {
177 if( memcmp( argv[4], "hex:", 4 ) == 0 )
178 {
179 p = &argv[4][4];
180 keylen = 0;
181
182 while( sscanf( p, "%02X", &n ) > 0 &&
183 keylen < (int) sizeof( key ) )
184 {
185 key[keylen++] = (unsigned char) n;
186 p += 2;
187 }
188 }
189 else
190 {
191 keylen = strlen( argv[4] );
192
193 if( keylen > (int) sizeof( key ) )
194 keylen = (int) sizeof( key );
195
196 memcpy( key, argv[4], keylen );
197 }
198 }
199
Paul Bakkercce9d772011-11-18 14:26:47 +0000200#if defined(_WIN32_WCE)
201 filesize = fseek( fin, 0L, SEEK_END );
202#else
203#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 /*
205 * Support large files (> 2Gb) on Win32
206 */
207 li_size.QuadPart = 0;
208 li_size.LowPart =
209 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
210 li_size.LowPart, &li_size.HighPart, FILE_END );
211
212 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 goto exit;
216 }
217
218 filesize = li_size.QuadPart;
219#else
220 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
221 {
222 perror( "lseek" );
223 goto exit;
224 }
225#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000226#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000227
228 if( fseek( fin, 0, SEEK_SET ) < 0 )
229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 goto exit;
232 }
233
234 if( mode == MODE_ENCRYPT )
235 {
236 /*
237 * Generate the initialization vector as:
238 * IV = SHA-256( filesize || filename )[0..15]
239 */
240 for( i = 0; i < 8; i++ )
241 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
242
243 p = argv[2];
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_md_starts( &sha_ctx );
246 mbedtls_md_update( &sha_ctx, buffer, 8 );
247 mbedtls_md_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
248 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
250 memcpy( IV, digest, 16 );
251
252 /*
253 * The last four bits in the IV are actually used
254 * to store the file size modulo the AES block size.
255 */
256 lastn = (int)( filesize & 0x0F );
257
258 IV[15] = (unsigned char)
259 ( ( IV[15] & 0xF0 ) | lastn );
260
261 /*
262 * Append the IV at the beginning of the output.
263 */
264 if( fwrite( IV, 1, 16, fout ) != 16 )
265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 goto exit;
268 }
269
270 /*
271 * Hash the IV and the secret key together 8192 times
272 * using the result to setup the AES context and HMAC.
273 */
274 memset( digest, 0, 32 );
275 memcpy( digest, IV, 16 );
276
277 for( i = 0; i < 8192; i++ )
278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_md_starts( &sha_ctx );
280 mbedtls_md_update( &sha_ctx, digest, 32 );
281 mbedtls_md_update( &sha_ctx, key, keylen );
282 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 }
284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_aes_setkey_enc( &aes_ctx, digest, 256 );
286 mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
288 /*
289 * Encrypt and write the ciphertext.
290 */
291 for( offset = 0; offset < filesize; offset += 16 )
292 {
293 n = ( filesize - offset > 16 ) ? 16 : (int)
294 ( filesize - offset );
295
296 if( fread( buffer, 1, n, fin ) != (size_t) n )
297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 goto exit;
300 }
301
302 for( i = 0; i < 16; i++ )
303 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, buffer, buffer );
306 mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
308 if( fwrite( buffer, 1, 16, fout ) != 16 )
309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 goto exit;
312 }
313
314 memcpy( IV, buffer, 16 );
315 }
316
317 /*
318 * Finally write the HMAC.
319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 mbedtls_md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
322 if( fwrite( digest, 1, 32, fout ) != 32 )
323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 goto exit;
326 }
327 }
328
329 if( mode == MODE_DECRYPT )
330 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 /*
332 * The encrypted file must be structured as follows:
333 *
334 * 00 .. 15 Initialization Vector
335 * 16 .. 31 AES Encrypted Block #1
336 * ..
337 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
338 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
339 */
340 if( filesize < 48 )
341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 goto exit;
344 }
345
346 if( ( filesize & 0x0F ) != 0 )
347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_fprintf( stderr, "File size not a multiple of 16.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000349 goto exit;
350 }
351
352 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100353 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 */
355 filesize -= ( 16 + 32 );
356
357 /*
358 * Read the IV and original filesize modulo 16.
359 */
360 if( fread( buffer, 1, 16, fin ) != 16 )
361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 goto exit;
364 }
365
366 memcpy( IV, buffer, 16 );
367 lastn = IV[15] & 0x0F;
368
369 /*
370 * Hash the IV and the secret key together 8192 times
371 * using the result to setup the AES context and HMAC.
372 */
373 memset( digest, 0, 32 );
374 memcpy( digest, IV, 16 );
375
376 for( i = 0; i < 8192; i++ )
377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_md_starts( &sha_ctx );
379 mbedtls_md_update( &sha_ctx, digest, 32 );
380 mbedtls_md_update( &sha_ctx, key, keylen );
381 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000382 }
383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_aes_setkey_dec( &aes_ctx, digest, 256 );
385 mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000386
387 /*
388 * Decrypt and write the plaintext.
389 */
390 for( offset = 0; offset < filesize; offset += 16 )
391 {
392 if( fread( buffer, 1, 16, fin ) != 16 )
393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 goto exit;
396 }
397
398 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
401 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200402
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 for( i = 0; i < 16; i++ )
404 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
405
406 memcpy( IV, tmp, 16 );
407
408 n = ( lastn > 0 && offset == filesize - 16 )
409 ? lastn : 16;
410
411 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000414 goto exit;
415 }
416 }
417
418 /*
419 * Verify the message authentication code.
420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
423 if( fread( buffer, 1, 32, fin ) != 32 )
424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426 goto exit;
427 }
428
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100429 /* Use constant-time buffer comparison */
430 diff = 0;
431 for( i = 0; i < 32; i++ )
432 diff |= digest[i] ^ buffer[i];
433
434 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker5121ce52009-01-03 21:22:43 +0000437 "or file corrupted.\n" );
438 goto exit;
439 }
440 }
441
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +0100442 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000443
444exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000445 if( fin )
446 fclose( fin );
447 if( fout )
448 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000449
Hanno Beckerce37e622017-06-27 08:24:34 +0100450 /* Zeroize all command line arguments to also cover
451 the case when the user has missed or reordered some,
452 in which case the key might not be in argv[4]. */
453 for( i = 0; i < (unsigned int) argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100454 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerce37e622017-06-27 08:24:34 +0100455
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100456 mbedtls_platform_zeroize( IV, sizeof( IV ) );
457 mbedtls_platform_zeroize( key, sizeof( key ) );
458 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
459 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
460 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_aes_free( &aes_ctx );
463 mbedtls_md_free( &sha_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +0100465 return( exit_code );
Paul Bakker5121ce52009-01-03 21:22:43 +0000466}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#endif /* MBEDTLS_AES_C && MBEDTLS_SHA256_C && MBEDTLS_FS_IO */