blob: df747c9ca226622a2f08837d4773b6d6be5127ff [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");
Andrzej Kurek825ebd42020-05-18 11:47:25 -040079 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000080}
81#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000082
Jarno Lamsa642596e2019-10-04 12:52:42 +030083#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
84int mbedtls_hardware_poll( void *data, unsigned char *output,
85 size_t len, size_t *olen )
86{
87 size_t i;
88 (void) data;
89 for( i = 0; i < len; ++i )
90 output[i] = rand();
91 *olen = len;
92 return( 0 );
93}
94#endif
Simon Butcher63cb97e2018-12-06 17:43:31 +000095
Paul Bakker5121ce52009-01-03 21:22:43 +000096int main( int argc, char *argv[] )
97{
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +010098 int ret = 0;
99 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000100
Simon Butcher0e7d3872016-08-30 14:25:24 +0100101 unsigned int i, n;
Paul Bakker23986e52011-04-24 08:57:21 +0000102 int mode, lastn;
103 size_t keylen;
Paul Bakker20a78082011-01-21 09:32:12 +0000104 FILE *fkey, *fin = NULL, *fout = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
106 char *p;
Hanno Beckerce37e622017-06-27 08:24:34 +0100107
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 unsigned char IV[16];
Hanno Beckerce37e622017-06-27 08:24:34 +0100109 unsigned char tmp[16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 unsigned char key[512];
111 unsigned char digest[32];
112 unsigned char buffer[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100113 unsigned char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_aes_context aes_ctx;
116 mbedtls_md_context_t sha_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
Paul Bakkercce9d772011-11-18 14:26:47 +0000118#if defined(_WIN32_WCE)
119 long filesize, offset;
120#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 LARGE_INTEGER li_size;
122 __int64 filesize, offset;
123#else
124 off_t filesize, offset;
125#endif
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_aes_init( &aes_ctx );
128 mbedtls_md_init( &sha_ctx );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 ret = mbedtls_md_setup( &sha_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 1 );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100131 if( ret != 0 )
132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_printf( " ! mbedtls_md_setup() returned -0x%04x\n", -ret );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100134 goto exit;
135 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200136
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 /*
138 * Parse the command-line arguments.
139 */
140 if( argc != 5 )
141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_printf( USAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
Paul Bakkercce9d772011-11-18 14:26:47 +0000144#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 fflush( stdout ); getchar();
147#endif
148
149 goto exit;
150 }
151
152 mode = atoi( argv[1] );
Hanno Beckerce37e622017-06-27 08:24:34 +0100153 memset( IV, 0, sizeof( IV ) );
154 memset( key, 0, sizeof( key ) );
155 memset( digest, 0, sizeof( digest ) );
156 memset( buffer, 0, sizeof( buffer ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
158 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_fprintf( stderr, "invalide operation mode\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 goto exit;
162 }
163
164 if( strcmp( argv[2], argv[3] ) == 0 )
165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 goto exit;
168 }
169
170 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 goto exit;
174 }
175
176 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 goto exit;
180 }
181
182 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100183 * Read the secret key from file or command line
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 */
185 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
186 {
187 keylen = fread( key, 1, sizeof( key ), fkey );
188 fclose( fkey );
189 }
190 else
191 {
192 if( memcmp( argv[4], "hex:", 4 ) == 0 )
193 {
194 p = &argv[4][4];
195 keylen = 0;
196
197 while( sscanf( p, "%02X", &n ) > 0 &&
198 keylen < (int) sizeof( key ) )
199 {
200 key[keylen++] = (unsigned char) n;
201 p += 2;
202 }
203 }
204 else
205 {
206 keylen = strlen( argv[4] );
207
208 if( keylen > (int) sizeof( key ) )
209 keylen = (int) sizeof( key );
210
211 memcpy( key, argv[4], keylen );
212 }
213 }
214
Paul Bakkercce9d772011-11-18 14:26:47 +0000215#if defined(_WIN32_WCE)
216 filesize = fseek( fin, 0L, SEEK_END );
217#else
218#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 /*
220 * Support large files (> 2Gb) on Win32
221 */
222 li_size.QuadPart = 0;
223 li_size.LowPart =
224 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
225 li_size.LowPart, &li_size.HighPart, FILE_END );
226
227 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 goto exit;
231 }
232
233 filesize = li_size.QuadPart;
234#else
235 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
236 {
237 perror( "lseek" );
238 goto exit;
239 }
240#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000241#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000242
243 if( fseek( fin, 0, SEEK_SET ) < 0 )
244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 goto exit;
247 }
248
249 if( mode == MODE_ENCRYPT )
250 {
251 /*
252 * Generate the initialization vector as:
253 * IV = SHA-256( filesize || filename )[0..15]
254 */
255 for( i = 0; i < 8; i++ )
256 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
257
258 p = argv[2];
259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_md_starts( &sha_ctx );
261 mbedtls_md_update( &sha_ctx, buffer, 8 );
262 mbedtls_md_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
263 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000264
265 memcpy( IV, digest, 16 );
266
267 /*
268 * The last four bits in the IV are actually used
269 * to store the file size modulo the AES block size.
270 */
271 lastn = (int)( filesize & 0x0F );
272
273 IV[15] = (unsigned char)
274 ( ( IV[15] & 0xF0 ) | lastn );
275
276 /*
277 * Append the IV at the beginning of the output.
278 */
279 if( fwrite( IV, 1, 16, fout ) != 16 )
280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 goto exit;
283 }
284
285 /*
286 * Hash the IV and the secret key together 8192 times
287 * using the result to setup the AES context and HMAC.
288 */
289 memset( digest, 0, 32 );
290 memcpy( digest, IV, 16 );
291
292 for( i = 0; i < 8192; i++ )
293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_md_starts( &sha_ctx );
295 mbedtls_md_update( &sha_ctx, digest, 32 );
296 mbedtls_md_update( &sha_ctx, key, keylen );
297 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 }
299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 mbedtls_aes_setkey_enc( &aes_ctx, digest, 256 );
301 mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000302
303 /*
304 * Encrypt and write the ciphertext.
305 */
306 for( offset = 0; offset < filesize; offset += 16 )
307 {
308 n = ( filesize - offset > 16 ) ? 16 : (int)
309 ( filesize - offset );
310
311 if( fread( buffer, 1, n, fin ) != (size_t) n )
312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 goto exit;
315 }
316
317 for( i = 0; i < 16; i++ )
318 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, buffer, buffer );
321 mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
323 if( fwrite( buffer, 1, 16, fout ) != 16 )
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 memcpy( IV, buffer, 16 );
330 }
331
332 /*
333 * Finally write the HMAC.
334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
337 if( fwrite( digest, 1, 32, fout ) != 32 )
338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 goto exit;
341 }
342 }
343
344 if( mode == MODE_DECRYPT )
345 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 /*
347 * The encrypted file must be structured as follows:
348 *
349 * 00 .. 15 Initialization Vector
350 * 16 .. 31 AES Encrypted Block #1
351 * ..
352 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
353 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
354 */
355 if( filesize < 48 )
356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000358 goto exit;
359 }
360
361 if( ( filesize & 0x0F ) != 0 )
362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_fprintf( stderr, "File size not a multiple of 16.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 goto exit;
365 }
366
367 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100368 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000369 */
370 filesize -= ( 16 + 32 );
371
372 /*
373 * Read the IV and original filesize modulo 16.
374 */
375 if( fread( buffer, 1, 16, fin ) != 16 )
376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 goto exit;
379 }
380
381 memcpy( IV, buffer, 16 );
382 lastn = IV[15] & 0x0F;
383
384 /*
385 * Hash the IV and the secret key together 8192 times
386 * using the result to setup the AES context and HMAC.
387 */
388 memset( digest, 0, 32 );
389 memcpy( digest, IV, 16 );
390
391 for( i = 0; i < 8192; i++ )
392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_md_starts( &sha_ctx );
394 mbedtls_md_update( &sha_ctx, digest, 32 );
395 mbedtls_md_update( &sha_ctx, key, keylen );
396 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 }
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_aes_setkey_dec( &aes_ctx, digest, 256 );
400 mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000401
402 /*
403 * Decrypt and write the plaintext.
404 */
405 for( offset = 0; offset < filesize; offset += 16 )
406 {
407 if( fread( buffer, 1, 16, fin ) != 16 )
408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410 goto exit;
411 }
412
413 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
416 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200417
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 for( i = 0; i < 16; i++ )
419 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
420
421 memcpy( IV, tmp, 16 );
422
423 n = ( lastn > 0 && offset == filesize - 16 )
424 ? lastn : 16;
425
426 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 goto exit;
430 }
431 }
432
433 /*
434 * Verify the message authentication code.
435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
438 if( fread( buffer, 1, 32, fin ) != 32 )
439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 goto exit;
442 }
443
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100444 /* Use constant-time buffer comparison */
445 diff = 0;
446 for( i = 0; i < 32; i++ )
447 diff |= digest[i] ^ buffer[i];
448
449 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 "or file corrupted.\n" );
453 goto exit;
454 }
455 }
456
Andres Amaya Garcia388c1b12018-04-29 19:01:34 +0100457 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
459exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000460 if( fin )
461 fclose( fin );
462 if( fout )
463 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
Hanno Beckerce37e622017-06-27 08:24:34 +0100465 /* Zeroize all command line arguments to also cover
466 the case when the user has missed or reordered some,
467 in which case the key might not be in argv[4]. */
468 for( i = 0; i < (unsigned int) argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100469 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerce37e622017-06-27 08:24:34 +0100470
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100471 mbedtls_platform_zeroize( IV, sizeof( IV ) );
472 mbedtls_platform_zeroize( key, sizeof( key ) );
473 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
474 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
475 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_aes_free( &aes_ctx );
478 mbedtls_md_free( &sha_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400480 mbedtls_exit( exit_code );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482#endif /* MBEDTLS_AES_C && MBEDTLS_SHA256_C && MBEDTLS_FS_IO */