blob: 9c566e77e64e44b57c7192fc1d12c77f64e227f4 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_md_starts( &md_ctx );
312 mbedtls_md_update( &md_ctx, buffer, 8 );
313 mbedtls_md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
314 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000315
316 memcpy( IV, digest, 16 );
317
318 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000319 * Append the IV at the beginning of the output.
320 */
321 if( fwrite( IV, 1, 16, fout ) != 16 )
322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000324 goto exit;
325 }
326
327 /*
328 * Hash the IV and the secret key together 8192 times
329 * using the result to setup the AES context and HMAC.
330 */
331 memset( digest, 0, 32 );
332 memcpy( digest, IV, 16 );
333
334 for( i = 0; i < 8192; i++ )
335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_md_starts( &md_ctx );
337 mbedtls_md_update( &md_ctx, digest, 32 );
338 mbedtls_md_update( &md_ctx, key, keylen );
339 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000340
341 }
342
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200343 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000347 goto exit;
348 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200352 goto exit;
353 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000357 goto exit;
358 }
Paul Bakker20a78082011-01-21 09:32:12 +0000359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000361
362 /*
363 * Encrypt and write the ciphertext.
364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
368 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000369
Paul Bakker25b5fe52011-05-26 14:02:58 +0000370 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000373 goto exit;
374 }
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200379 goto exit;
380 }
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000383
Paul Bakker2c0994e2011-05-25 13:51:57 +0000384 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000387 goto exit;
388 }
389 }
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000394 goto exit;
395 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000397
Paul Bakker2c0994e2011-05-25 13:51:57 +0000398 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000401 goto exit;
402 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000403
Paul Bakker20a78082011-01-21 09:32:12 +0000404 /*
405 * Finally write the HMAC.
406 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000412 goto exit;
413 }
414 }
415
416 if( mode == MODE_DECRYPT )
417 {
418 /*
419 * The encrypted file must be structured as follows:
420 *
421 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200422 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000423 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200424 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
425 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000430 goto exit;
431 }
432
Janos Follath8eb64132016-06-03 15:40:57 +0100433 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
434 {
Janos Follath352dbe22016-06-07 10:29:05 +0100435 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100436 goto exit;
437 }
438
439 /*
440 * Check the file size.
441 */
Paul Bakker243f48e2016-09-02 22:44:09 +0200442 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
443 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
447 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000448 goto exit;
449 }
450
451 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100452 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000455
456 /*
457 * Read the IV and original filesize modulo 16.
458 */
459 if( fread( buffer, 1, 16, fin ) != 16 )
460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000462 goto exit;
463 }
464
465 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000466
467 /*
468 * Hash the IV and the secret key together 8192 times
469 * using the result to setup the AES context and HMAC.
470 */
471 memset( digest, 0, 32 );
472 memcpy( digest, IV, 16 );
473
474 for( i = 0; i < 8192; i++ )
475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_md_starts( &md_ctx );
477 mbedtls_md_update( &md_ctx, digest, 32 );
478 mbedtls_md_update( &md_ctx, key, keylen );
479 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000480 }
481
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200482 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200486 goto exit;
487 }
488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200492 goto exit;
493 }
494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200498 goto exit;
499 }
Paul Bakker20a78082011-01-21 09:32:12 +0000500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000502
503 /*
504 * Decrypt and write the plaintext.
505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000507 {
Paul Bakker243f48e2016-09-02 22:44:09 +0200508 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
509 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
510
511 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n",
514 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000515 goto exit;
516 }
517
Paul Bakker243f48e2016-09-02 22:44:09 +0200518 mbedtls_md_hmac_update( &md_ctx, buffer, ilen );
519 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
520 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200523 goto exit;
524 }
Paul Bakker20a78082011-01-21 09:32:12 +0000525
Paul Bakker2c0994e2011-05-25 13:51:57 +0000526 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000529 goto exit;
530 }
531 }
532
533 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000534 * Verify the message authentication code.
535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000541 goto exit;
542 }
543
Paul Bakker424cd692013-10-31 14:22:08 +0100544 /* Use constant-time buffer comparison */
545 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100547 diff |= digest[i] ^ buffer[i];
548
549 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000552 "or file corrupted.\n" );
553 goto exit;
554 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100555
556 /*
557 * Write the final block of data
558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_cipher_finish( &cipher_ctx, output, &olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100560
561 if( fwrite( output, 1, olen, fout ) != olen )
562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100564 goto exit;
565 }
Paul Bakker20a78082011-01-21 09:32:12 +0000566 }
567
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100568 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000569
570exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000571 if( fin )
572 fclose( fin );
573 if( fout )
574 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000575
Hanno Beckerf601ec52017-06-27 08:22:17 +0100576 /* Zeroize all command line arguments to also cover
577 the case when the user has missed or reordered some,
578 in which case the key might not be in argv[6]. */
579 for( i = 0; i < argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100580 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerf601ec52017-06-27 08:22:17 +0100581
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100582 mbedtls_platform_zeroize( IV, sizeof( IV ) );
583 mbedtls_platform_zeroize( key, sizeof( key ) );
584 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
585 mbedtls_platform_zeroize( output, sizeof( output ) );
586 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_cipher_free( &cipher_ctx );
589 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000590
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200591 mbedtls_exit( exit_code );
Paul Bakker20a78082011-01-21 09:32:12 +0000592}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */