blob: 0eb3a89dd81293625b104349de2bf40d95d53b3b [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
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000057#else
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
Andres Amaya Garcia15cbf612018-04-29 19:01:34 +010059#include <stdlib.h>
60#define mbedtls_fprintf fprintf
61#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020062#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010063#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia15cbf612018-04-29 19:01:34 +010064#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
65#endif /* MBEDTLS_PLATFORM_C */
Rich Evans18b78c72015-02-11 14:06:19 +000066
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +010068#include "mbedtls/md.h"
Rich Evans18b78c72015-02-11 14:06:19 +000069
70#include <stdio.h>
71#include <stdlib.h>
72#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000073
Paul Bakker494c0b82011-04-24 15:30:07 +000074#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000075#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000076#if !defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000077#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000078#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000079#else
80#include <sys/types.h>
81#include <unistd.h>
82#endif
83
Paul Bakker5121ce52009-01-03 21:22:43 +000084#define MODE_ENCRYPT 0
85#define MODE_DECRYPT 1
86
87#define USAGE \
88 "\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
89 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
90 "\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
91 "\n"
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_SHA256_C) || \
94 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_MD_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000095int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000096{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_SHA256_C "
98 "and/or MBEDTLS_FS_IO and/or MBEDTLS_MD_C "
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +010099 "not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200100 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000101}
102#else
Hanno Becker9a1a1512018-10-12 16:46:37 +0100103
104/* Implementation that should never be optimized out by the compiler */
105static void mbedtls_zeroize( void *v, size_t n ) {
106 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
107}
108
Paul Bakker5121ce52009-01-03 21:22:43 +0000109int main( int argc, char *argv[] )
110{
Andres Amaya Garcia15cbf612018-04-29 19:01:34 +0100111 int ret = 0;
112 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000113
Simon Butcher0e7d3872016-08-30 14:25:24 +0100114 unsigned int i, n;
Paul Bakker23986e52011-04-24 08:57:21 +0000115 int mode, lastn;
116 size_t keylen;
Paul Bakker20a78082011-01-21 09:32:12 +0000117 FILE *fkey, *fin = NULL, *fout = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
119 char *p;
Hanno Beckerce37e622017-06-27 08:24:34 +0100120
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 unsigned char IV[16];
Hanno Beckerce37e622017-06-27 08:24:34 +0100122 unsigned char tmp[16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 unsigned char key[512];
124 unsigned char digest[32];
125 unsigned char buffer[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100126 unsigned char diff;
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_aes_context aes_ctx;
129 mbedtls_md_context_t sha_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Paul Bakkercce9d772011-11-18 14:26:47 +0000131#if defined(_WIN32_WCE)
132 long filesize, offset;
133#elif defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 LARGE_INTEGER li_size;
135 __int64 filesize, offset;
136#else
137 off_t filesize, offset;
138#endif
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_aes_init( &aes_ctx );
141 mbedtls_md_init( &sha_ctx );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 ret = mbedtls_md_setup( &sha_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 1 );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100144 if( ret != 0 )
145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_printf( " ! mbedtls_md_setup() returned -0x%04x\n", -ret );
Manuel Pégourié-Gonnard003b3b12015-03-24 18:22:59 +0100147 goto exit;
148 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200149
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 /*
151 * Parse the command-line arguments.
152 */
153 if( argc != 5 )
154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_printf( USAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Paul Bakkercce9d772011-11-18 14:26:47 +0000157#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 fflush( stdout ); getchar();
160#endif
161
162 goto exit;
163 }
164
165 mode = atoi( argv[1] );
Hanno Beckerce37e622017-06-27 08:24:34 +0100166 memset( IV, 0, sizeof( IV ) );
167 memset( key, 0, sizeof( key ) );
168 memset( digest, 0, sizeof( digest ) );
169 memset( buffer, 0, sizeof( buffer ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
171 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_fprintf( stderr, "invalide operation mode\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 goto exit;
175 }
176
177 if( strcmp( argv[2], argv[3] ) == 0 )
178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 goto exit;
181 }
182
183 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 goto exit;
187 }
188
189 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 goto exit;
193 }
194
195 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100196 * Read the secret key from file or command line
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 */
198 if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
199 {
200 keylen = fread( key, 1, sizeof( key ), fkey );
201 fclose( fkey );
202 }
203 else
204 {
205 if( memcmp( argv[4], "hex:", 4 ) == 0 )
206 {
207 p = &argv[4][4];
208 keylen = 0;
209
210 while( sscanf( p, "%02X", &n ) > 0 &&
211 keylen < (int) sizeof( key ) )
212 {
213 key[keylen++] = (unsigned char) n;
214 p += 2;
215 }
216 }
217 else
218 {
219 keylen = strlen( argv[4] );
220
221 if( keylen > (int) sizeof( key ) )
222 keylen = (int) sizeof( key );
223
224 memcpy( key, argv[4], keylen );
225 }
226 }
227
Paul Bakkercce9d772011-11-18 14:26:47 +0000228#if defined(_WIN32_WCE)
229 filesize = fseek( fin, 0L, SEEK_END );
230#else
231#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 /*
233 * Support large files (> 2Gb) on Win32
234 */
235 li_size.QuadPart = 0;
236 li_size.LowPart =
237 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
238 li_size.LowPart, &li_size.HighPart, FILE_END );
239
240 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 goto exit;
244 }
245
246 filesize = li_size.QuadPart;
247#else
248 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
249 {
250 perror( "lseek" );
251 goto exit;
252 }
253#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000254#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
256 if( fseek( fin, 0, SEEK_SET ) < 0 )
257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 goto exit;
260 }
261
262 if( mode == MODE_ENCRYPT )
263 {
264 /*
265 * Generate the initialization vector as:
266 * IV = SHA-256( filesize || filename )[0..15]
267 */
268 for( i = 0; i < 8; i++ )
269 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
270
271 p = argv[2];
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_md_starts( &sha_ctx );
274 mbedtls_md_update( &sha_ctx, buffer, 8 );
275 mbedtls_md_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
276 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000277
278 memcpy( IV, digest, 16 );
279
280 /*
281 * The last four bits in the IV are actually used
282 * to store the file size modulo the AES block size.
283 */
284 lastn = (int)( filesize & 0x0F );
285
286 IV[15] = (unsigned char)
287 ( ( IV[15] & 0xF0 ) | lastn );
288
289 /*
290 * Append the IV at the beginning of the output.
291 */
292 if( fwrite( IV, 1, 16, fout ) != 16 )
293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 goto exit;
296 }
297
298 /*
299 * Hash the IV and the secret key together 8192 times
300 * using the result to setup the AES context and HMAC.
301 */
302 memset( digest, 0, 32 );
303 memcpy( digest, IV, 16 );
304
305 for( i = 0; i < 8192; i++ )
306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 mbedtls_md_starts( &sha_ctx );
308 mbedtls_md_update( &sha_ctx, digest, 32 );
309 mbedtls_md_update( &sha_ctx, key, keylen );
310 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 }
312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_aes_setkey_enc( &aes_ctx, digest, 256 );
314 mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
316 /*
317 * Encrypt and write the ciphertext.
318 */
319 for( offset = 0; offset < filesize; offset += 16 )
320 {
321 n = ( filesize - offset > 16 ) ? 16 : (int)
322 ( filesize - offset );
323
324 if( fread( buffer, 1, n, fin ) != (size_t) n )
325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 goto exit;
328 }
329
330 for( i = 0; i < 16; i++ )
331 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, buffer, buffer );
334 mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
336 if( fwrite( buffer, 1, 16, fout ) != 16 )
337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 goto exit;
340 }
341
342 memcpy( IV, buffer, 16 );
343 }
344
345 /*
346 * Finally write the HMAC.
347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000349
350 if( fwrite( digest, 1, 32, fout ) != 32 )
351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353 goto exit;
354 }
355 }
356
357 if( mode == MODE_DECRYPT )
358 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000359 /*
360 * The encrypted file must be structured as follows:
361 *
362 * 00 .. 15 Initialization Vector
363 * 16 .. 31 AES Encrypted Block #1
364 * ..
365 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
366 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
367 */
368 if( filesize < 48 )
369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 goto exit;
372 }
373
374 if( ( filesize & 0x0F ) != 0 )
375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_fprintf( stderr, "File size not a multiple of 16.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 goto exit;
378 }
379
380 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100381 * Subtract the IV + HMAC length.
Paul Bakker5121ce52009-01-03 21:22:43 +0000382 */
383 filesize -= ( 16 + 32 );
384
385 /*
386 * Read the IV and original filesize modulo 16.
387 */
388 if( fread( buffer, 1, 16, fin ) != 16 )
389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 goto exit;
392 }
393
394 memcpy( IV, buffer, 16 );
395 lastn = IV[15] & 0x0F;
396
397 /*
398 * Hash the IV and the secret key together 8192 times
399 * using the result to setup the AES context and HMAC.
400 */
401 memset( digest, 0, 32 );
402 memcpy( digest, IV, 16 );
403
404 for( i = 0; i < 8192; i++ )
405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 mbedtls_md_starts( &sha_ctx );
407 mbedtls_md_update( &sha_ctx, digest, 32 );
408 mbedtls_md_update( &sha_ctx, key, keylen );
409 mbedtls_md_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410 }
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_aes_setkey_dec( &aes_ctx, digest, 256 );
413 mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
415 /*
416 * Decrypt and write the plaintext.
417 */
418 for( offset = 0; offset < filesize; offset += 16 )
419 {
420 if( fread( buffer, 1, 16, fin ) != 16 )
421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000423 goto exit;
424 }
425
426 memcpy( tmp, buffer, 16 );
Paul Bakker9e36f042013-06-30 14:34:05 +0200427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
429 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, buffer, buffer );
Paul Bakker9e36f042013-06-30 14:34:05 +0200430
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 for( i = 0; i < 16; i++ )
432 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
433
434 memcpy( IV, tmp, 16 );
435
436 n = ( lastn > 0 && offset == filesize - 16 )
437 ? lastn : 16;
438
439 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000442 goto exit;
443 }
444 }
445
446 /*
447 * Verify the message authentication code.
448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_md_hmac_finish( &sha_ctx, digest );
Paul Bakker5121ce52009-01-03 21:22:43 +0000450
451 if( fread( buffer, 1, 32, fin ) != 32 )
452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 goto exit;
455 }
456
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100457 /* Use constant-time buffer comparison */
458 diff = 0;
459 for( i = 0; i < 32; i++ )
460 diff |= digest[i] ^ buffer[i];
461
462 if( diff != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker5121ce52009-01-03 21:22:43 +0000465 "or file corrupted.\n" );
466 goto exit;
467 }
468 }
469
Andres Amaya Garcia15cbf612018-04-29 19:01:34 +0100470 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
472exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000473 if( fin )
474 fclose( fin );
475 if( fout )
476 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
Hanno Beckerce37e622017-06-27 08:24:34 +0100478 /* Zeroize all command line arguments to also cover
479 the case when the user has missed or reordered some,
480 in which case the key might not be in argv[4]. */
481 for( i = 0; i < (unsigned int) argc; i++ )
Hanno Becker9a1a1512018-10-12 16:46:37 +0100482 mbedtls_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerce37e622017-06-27 08:24:34 +0100483
Hanno Becker9a1a1512018-10-12 16:46:37 +0100484 mbedtls_zeroize( IV, sizeof( IV ) );
485 mbedtls_zeroize( key, sizeof( key ) );
486 mbedtls_zeroize( tmp, sizeof( tmp ) );
487 mbedtls_zeroize( buffer, sizeof( buffer ) );
488 mbedtls_zeroize( digest, sizeof( digest ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_aes_free( &aes_ctx );
491 mbedtls_md_free( &sha_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200493 mbedtls_exit( exit_code );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495#endif /* MBEDTLS_AES_C && MBEDTLS_SHA256_C && MBEDTLS_FS_IO */