blob: 91c4f50198c341fab51008b630ca6623040f1460 [file] [log] [blame]
Paul Bakker20a78082011-01-21 09:32:12 +00001/*
2 * \brief Generic file encryption program using generic wrappers for configured
3 * security.
4 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02005 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7 *
8 * This file is provided under the Apache License 2.0, or the
9 * GNU General Public License v2.0 or later.
10 *
11 * **********
12 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020013 *
14 * Licensed under the Apache License, Version 2.0 (the "License"); you may
15 * not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
Paul Bakker20a78082011-01-21 09:32:12 +000025 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020026 * **********
27 *
28 * **********
29 * GNU General Public License v2.0 or later:
30 *
31 * This program is free software; you can redistribute it and/or modify
32 * it under the terms of the GNU General Public License as published by
33 * the Free Software Foundation; either version 2 of the License, or
34 * (at your option) any later version.
35 *
36 * This program is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
40 *
41 * You should have received a copy of the GNU General Public License along
42 * with this program; if not, write to the Free Software Foundation, Inc.,
43 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
44 *
45 * **********
Paul Bakker20a78082011-01-21 09:32:12 +000046 */
47
Nicholas Wilson2682edf2017-12-05 12:08:15 +000048/* Enable definition of fileno() even when compiling with -std=c99. Must be
49 * set before config.h, which pulls in glibc's features.h indirectly.
50 * Harmless on other platforms. */
51#define _POSIX_C_SOURCE 1
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020055#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020057#endif
Paul Bakker20a78082011-01-21 09:32:12 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000061#else
Rich Evans18b78c72015-02-11 14:06:19 +000062#include <stdio.h>
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010063#include <stdlib.h>
64#define mbedtls_fprintf fprintf
65#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010066#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010067#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010068#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
69#endif /* MBEDTLS_PLATFORM_C */
Rich Evans18b78c72015-02-11 14:06:19 +000070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
72 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000073#include "mbedtls/cipher.h"
74#include "mbedtls/md.h"
Hanno Becker0b44d5c2018-10-12 16:46:37 +010075#include "mbedtls/platform_util.h"
Rich Evans18b78c72015-02-11 14:06:19 +000076
77#include <stdio.h>
78#include <stdlib.h>
79#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000080#endif
81
Paul Bakker494c0b82011-04-24 15:30:07 +000082#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000083#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000084#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000085#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000086#endif
Paul Bakker20a78082011-01-21 09:32:12 +000087#else
88#include <sys/types.h>
89#include <unistd.h>
90#endif
91
Paul Bakker20a78082011-01-21 09:32:12 +000092#define MODE_ENCRYPT 0
93#define MODE_DECRYPT 1
94
95#define USAGE \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
Paul Bakker20a78082011-01-21 09:32:12 +000097 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
98 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
99 "\n"
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
102 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +0000103int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +0000104{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_printf("MBEDTLS_CIPHER_C and/or MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200106 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000107}
108#else
Simon Butcher63cb97e2018-12-06 17:43:31 +0000109
Simon Butcher63cb97e2018-12-06 17:43:31 +0000110
Paul Bakker20a78082011-01-21 09:32:12 +0000111int main( int argc, char *argv[] )
112{
Gilles Peskine971e5e92020-04-14 19:34:19 +0200113 int ret = 1, i;
114 unsigned n;
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100115 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +0200116 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +0000117 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +0000118 FILE *fkey, *fin = NULL, *fout = NULL;
119
120 char *p;
121 unsigned char IV[16];
122 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +0000124 unsigned char buffer[1024];
125 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +0100126 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +0000127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 const mbedtls_cipher_info_t *cipher_info;
129 const mbedtls_md_info_t *md_info;
130 mbedtls_cipher_context_t cipher_ctx;
131 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +0000132#if defined(_WIN32_WCE)
133 long filesize, offset;
134#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000135 LARGE_INTEGER li_size;
136 __int64 filesize, offset;
137#else
138 off_t filesize, offset;
139#endif
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_cipher_init( &cipher_ctx );
142 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000143
144 /*
145 * Parse the command-line arguments.
146 */
147 if( argc != 7 )
148 {
149 const int *list;
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_printf( "Available ciphers:\n" );
154 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000155 while( *list )
156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 cipher_info = mbedtls_cipher_info_from_type( *list );
158 mbedtls_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000159 list++;
160 }
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_printf( "\nAvailable message digests:\n" );
163 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000164 while( *list )
165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 md_info = mbedtls_md_info_from_type( *list );
167 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000168 list++;
169 }
170
Paul Bakkercce9d772011-11-18 14:26:47 +0000171#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000173 fflush( stdout ); getchar();
174#endif
175
176 goto exit;
177 }
178
179 mode = atoi( argv[1] );
180
181 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000184 goto exit;
185 }
186
187 if( strcmp( argv[2], argv[3] ) == 0 )
188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000190 goto exit;
191 }
192
193 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000196 goto exit;
197 }
198
199 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000202 goto exit;
203 }
204
205 /*
206 * Read the Cipher and MD from the command line
207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000209 if( cipher_info == NULL )
210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000212 goto exit;
213 }
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200214 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200215 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200216 mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200217 goto exit;
218 }
Paul Bakker20a78082011-01-21 09:32:12 +0000219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 md_info = mbedtls_md_info_from_string( argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000221 if( md_info == NULL )
222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000224 goto exit;
225 }
Janos Follath8eb64132016-06-03 15:40:57 +0100226
227 if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
228 {
Janos Follath352dbe22016-06-07 10:29:05 +0100229 mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100230 goto exit;
231 }
Paul Bakker20a78082011-01-21 09:32:12 +0000232
233 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100234 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000235 */
236 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
237 {
238 keylen = fread( key, 1, sizeof( key ), fkey );
239 fclose( fkey );
240 }
241 else
242 {
243 if( memcmp( argv[6], "hex:", 4 ) == 0 )
244 {
245 p = &argv[6][4];
246 keylen = 0;
247
248 while( sscanf( p, "%02X", &n ) > 0 &&
249 keylen < (int) sizeof( key ) )
250 {
251 key[keylen++] = (unsigned char) n;
252 p += 2;
253 }
254 }
255 else
256 {
257 keylen = strlen( argv[6] );
258
259 if( keylen > (int) sizeof( key ) )
260 keylen = (int) sizeof( key );
261
262 memcpy( key, argv[6], keylen );
263 }
264 }
265
Paul Bakkercce9d772011-11-18 14:26:47 +0000266#if defined(_WIN32_WCE)
267 filesize = fseek( fin, 0L, SEEK_END );
268#else
269#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000270 /*
271 * Support large files (> 2Gb) on Win32
272 */
273 li_size.QuadPart = 0;
274 li_size.LowPart =
275 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
276 li_size.LowPart, &li_size.HighPart, FILE_END );
277
278 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000281 goto exit;
282 }
283
284 filesize = li_size.QuadPart;
285#else
286 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
287 {
288 perror( "lseek" );
289 goto exit;
290 }
291#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000292#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000293
294 if( fseek( fin, 0, SEEK_SET ) < 0 )
295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000297 goto exit;
298 }
299
300 if( mode == MODE_ENCRYPT )
301 {
302 /*
303 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200304 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000305 */
306 for( i = 0; i < 8; i++ )
307 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
308
309 p = argv[2];
310
Paul Elliott32c42242021-12-09 17:18:10 +0000311 if( mbedtls_md_starts( &md_ctx ) != 0 )
312 {
313 mbedtls_fprintf( stderr, "mbedtls_md_starts() returned error\n" );
314 goto exit;
315 }
316 if( mbedtls_md_update( &md_ctx, buffer, 8 ) != 0 )
317 {
318 mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
319 goto exit;
320 }
321 if( mbedtls_md_update( &md_ctx, ( unsigned char * ) p, strlen( p ) )
322 != 0 )
323 {
324 mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
325 goto exit;
326 }
327 if( mbedtls_md_finish( &md_ctx, digest ) != 0 )
328 {
329 mbedtls_fprintf( stderr, "mbedtls_md_finish() returned error\n" );
330 goto exit;
331 }
Paul Bakker20a78082011-01-21 09:32:12 +0000332
333 memcpy( IV, digest, 16 );
334
335 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000336 * Append the IV at the beginning of the output.
337 */
338 if( fwrite( IV, 1, 16, fout ) != 16 )
339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000341 goto exit;
342 }
343
344 /*
345 * Hash the IV and the secret key together 8192 times
346 * using the result to setup the AES context and HMAC.
347 */
348 memset( digest, 0, 32 );
349 memcpy( digest, IV, 16 );
350
351 for( i = 0; i < 8192; i++ )
352 {
Paul Elliott32c42242021-12-09 17:18:10 +0000353 if( mbedtls_md_starts( &md_ctx ) != 0 )
354 {
355 mbedtls_fprintf( stderr,
356 "mbedtls_md_starts() returned error\n" );
357 goto exit;
358 }
359 if( mbedtls_md_update( &md_ctx, digest, 32 ) != 0 )
360 {
361 mbedtls_fprintf( stderr,
362 "mbedtls_md_update() returned error\n" );
363 goto exit;
364 }
365 if( mbedtls_md_update( &md_ctx, key, keylen ) != 0 )
366 {
367 mbedtls_fprintf( stderr,
368 "mbedtls_md_update() returned error\n" );
369 goto exit;
370 }
371 if( mbedtls_md_finish( &md_ctx, digest ) != 0 )
372 {
373 mbedtls_fprintf( stderr,
374 "mbedtls_md_finish() returned error\n" );
375 goto exit;
376 }
Paul Bakker20a78082011-01-21 09:32:12 +0000377
378 }
379
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200380 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000384 goto exit;
385 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200389 goto exit;
390 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000394 goto exit;
395 }
Paul Bakker20a78082011-01-21 09:32:12 +0000396
Gilles Peskine4423cd32021-12-10 14:25:45 +0100397 if( mbedtls_md_hmac_starts( &md_ctx, digest, 32 ) != 0 )
398 {
399 mbedtls_fprintf( stderr, "mbedtls_md_hmac_starts() returned error\n" );
400 goto exit;
401 }
Paul Bakker20a78082011-01-21 09:32:12 +0000402
403 /*
404 * Encrypt and write the ciphertext.
405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
409 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000410
Paul Bakker25b5fe52011-05-26 14:02:58 +0000411 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000414 goto exit;
415 }
416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200420 goto exit;
421 }
422
Gilles Peskine4423cd32021-12-10 14:25:45 +0100423 if( mbedtls_md_hmac_update( &md_ctx, output, olen ) != 0 )
424 {
425 mbedtls_fprintf( stderr, "mbedtls_md_hmac_update() returned error\n" );
426 goto exit;
427 }
Paul Bakker20a78082011-01-21 09:32:12 +0000428
Paul Bakker2c0994e2011-05-25 13:51:57 +0000429 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000432 goto exit;
433 }
434 }
435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000439 goto exit;
440 }
Gilles Peskine4423cd32021-12-10 14:25:45 +0100441 if( mbedtls_md_hmac_update( &md_ctx, output, olen ) != 0 )
442 {
443 mbedtls_fprintf( stderr, "mbedtls_md_hmac_update() returned error\n" );
444 goto exit;
445 }
Paul Bakker20a78082011-01-21 09:32:12 +0000446
Paul Bakker2c0994e2011-05-25 13:51:57 +0000447 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000450 goto exit;
451 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000452
Paul Bakker20a78082011-01-21 09:32:12 +0000453 /*
454 * Finally write the HMAC.
455 */
Gilles Peskine4423cd32021-12-10 14:25:45 +0100456 if( mbedtls_md_hmac_finish( &md_ctx, digest ) != 0 )
457 {
458 mbedtls_fprintf( stderr, "mbedtls_md_hmac_finish() returned error\n" );
459 goto exit;
460 }
Paul Bakker20a78082011-01-21 09:32:12 +0000461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000465 goto exit;
466 }
467 }
468
469 if( mode == MODE_DECRYPT )
470 {
471 /*
472 * The encrypted file must be structured as follows:
473 *
474 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200475 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000476 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200477 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
478 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000483 goto exit;
484 }
485
Janos Follath8eb64132016-06-03 15:40:57 +0100486 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
487 {
Janos Follath352dbe22016-06-07 10:29:05 +0100488 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100489 goto exit;
490 }
491
492 /*
493 * Check the file size.
494 */
Paul Bakker243f48e2016-09-02 22:44:09 +0200495 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
496 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
500 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000501 goto exit;
502 }
503
504 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100505 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000508
509 /*
510 * Read the IV and original filesize modulo 16.
511 */
512 if( fread( buffer, 1, 16, fin ) != 16 )
513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000515 goto exit;
516 }
517
518 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000519
520 /*
521 * Hash the IV and the secret key together 8192 times
522 * using the result to setup the AES context and HMAC.
523 */
524 memset( digest, 0, 32 );
525 memcpy( digest, IV, 16 );
526
527 for( i = 0; i < 8192; i++ )
528 {
Gilles Peskine4423cd32021-12-10 14:25:45 +0100529 if( mbedtls_md_starts( &md_ctx ) != 0 )
530 {
531 mbedtls_fprintf( stderr, "mbedtls_md_starts() returned error\n" );
532 goto exit;
533 }
534 if( mbedtls_md_update( &md_ctx, digest, 32 ) != 0 )
535 {
536 mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
537 goto exit;
538 }
539 if( mbedtls_md_update( &md_ctx, key, keylen ) != 0 )
540 {
541 mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
542 goto exit;
543 }
544 if( mbedtls_md_finish( &md_ctx, digest ) != 0 )
545 {
546 mbedtls_fprintf( stderr, "mbedtls_md_finish() returned error\n" );
547 goto exit;
548 }
Paul Bakker20a78082011-01-21 09:32:12 +0000549 }
550
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200551 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200555 goto exit;
556 }
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200561 goto exit;
562 }
563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200567 goto exit;
568 }
Paul Bakker20a78082011-01-21 09:32:12 +0000569
Gilles Peskine4423cd32021-12-10 14:25:45 +0100570 if( mbedtls_md_hmac_starts( &md_ctx, digest, 32 ) != 0 )
571 {
572 mbedtls_fprintf( stderr, "mbedtls_md_hmac_starts() returned error\n" );
573 goto exit;
574 }
Paul Bakker20a78082011-01-21 09:32:12 +0000575
576 /*
577 * Decrypt and write the plaintext.
578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000580 {
Paul Bakker243f48e2016-09-02 22:44:09 +0200581 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
582 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
583
584 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n",
587 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000588 goto exit;
589 }
590
Gilles Peskine4423cd32021-12-10 14:25:45 +0100591 if( mbedtls_md_hmac_update( &md_ctx, buffer, ilen ) != 0 )
592 {
593 mbedtls_fprintf( stderr, "mbedtls_md_hmac_update() returned error\n" );
594 goto exit;
595 }
Paul Bakker243f48e2016-09-02 22:44:09 +0200596 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
597 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200600 goto exit;
601 }
Paul Bakker20a78082011-01-21 09:32:12 +0000602
Paul Bakker2c0994e2011-05-25 13:51:57 +0000603 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000606 goto exit;
607 }
608 }
609
610 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000611 * Verify the message authentication code.
612 */
Gilles Peskine4423cd32021-12-10 14:25:45 +0100613 if( mbedtls_md_hmac_finish( &md_ctx, digest ) != 0 )
614 {
615 mbedtls_fprintf( stderr, "mbedtls_md_hmac_finish() returned error\n" );
616 goto exit;
617 }
Paul Bakker20a78082011-01-21 09:32:12 +0000618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000622 goto exit;
623 }
624
Paul Bakker424cd692013-10-31 14:22:08 +0100625 /* Use constant-time buffer comparison */
626 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100628 diff |= digest[i] ^ buffer[i];
629
630 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000633 "or file corrupted.\n" );
634 goto exit;
635 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100636
637 /*
638 * Write the final block of data
639 */
Gilles Peskine4423cd32021-12-10 14:25:45 +0100640 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
641 {
642 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
643 goto exit;
644 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100645
646 if( fwrite( output, 1, olen, fout ) != olen )
647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100649 goto exit;
650 }
Paul Bakker20a78082011-01-21 09:32:12 +0000651 }
652
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100653 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000654
655exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000656 if( fin )
657 fclose( fin );
658 if( fout )
659 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000660
Hanno Beckerf601ec52017-06-27 08:22:17 +0100661 /* Zeroize all command line arguments to also cover
662 the case when the user has missed or reordered some,
663 in which case the key might not be in argv[6]. */
664 for( i = 0; i < argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100665 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerf601ec52017-06-27 08:22:17 +0100666
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100667 mbedtls_platform_zeroize( IV, sizeof( IV ) );
668 mbedtls_platform_zeroize( key, sizeof( key ) );
669 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
670 mbedtls_platform_zeroize( output, sizeof( output ) );
671 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_cipher_free( &cipher_ctx );
674 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000675
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200676 mbedtls_exit( exit_code );
Paul Bakker20a78082011-01-21 09:32:12 +0000677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */