blob: 136e25ba4154eb8b23ac65e68ba9027e585df9c5 [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úti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Paul Bakker20a78082011-01-21 09:32:12 +000019 */
20
Nicholas Wilson2682edf2017-12-05 12:08:15 +000021/* Enable definition of fileno() even when compiling with -std=c99. Must be
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020022 * set before mbedtls_config.h, which pulls in glibc's features.h indirectly.
Nicholas Wilson2682edf2017-12-05 12:08:15 +000023 * Harmless on other platforms. */
nia1c0c8372020-06-11 12:03:45 +010024#define _POSIX_C_SOURCE 200112L
Nicholas Wilson2682edf2017-12-05 12:08:15 +000025
Bence Szépkútic662b362021-05-27 11:25:03 +020026#include "mbedtls/build_info.h"
Paul Bakker20a78082011-01-21 09:32:12 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010032#include <stdlib.h>
33#define mbedtls_fprintf fprintf
34#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010035#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010036#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010037#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
38#endif /* MBEDTLS_PLATFORM_C */
Rich Evans18b78c72015-02-11 14:06:19 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
41 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/cipher.h"
43#include "mbedtls/md.h"
Hanno Becker0b44d5c2018-10-12 16:46:37 +010044#include "mbedtls/platform_util.h"
Rich Evans18b78c72015-02-11 14:06:19 +000045
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000049#endif
50
Paul Bakker494c0b82011-04-24 15:30:07 +000051#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000052#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000053#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000054#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000055#endif
Paul Bakker20a78082011-01-21 09:32:12 +000056#else
57#include <sys/types.h>
58#include <unistd.h>
59#endif
60
Paul Bakker20a78082011-01-21 09:32:12 +000061#define MODE_ENCRYPT 0
62#define MODE_DECRYPT 1
63
64#define USAGE \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
Paul Bakker20a78082011-01-21 09:32:12 +000066 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
67 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
68 "\n"
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
71 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000072int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000073{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_printf("MBEDTLS_CIPHER_C and/or MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020075 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000076}
77#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000078
Simon Butcher63cb97e2018-12-06 17:43:31 +000079
Paul Bakker20a78082011-01-21 09:32:12 +000080int main( int argc, char *argv[] )
81{
Gilles Peskinea5fc9392020-04-14 19:34:19 +020082 int ret = 1, i;
83 unsigned n;
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010084 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +020085 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +000086 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000087 FILE *fkey, *fin = NULL, *fout = NULL;
88
89 char *p;
90 unsigned char IV[16];
91 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +000093 unsigned char buffer[1024];
94 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +010095 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +000096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 const mbedtls_cipher_info_t *cipher_info;
98 const mbedtls_md_info_t *md_info;
99 mbedtls_cipher_context_t cipher_ctx;
100 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +0000101#if defined(_WIN32_WCE)
102 long filesize, offset;
103#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000104 LARGE_INTEGER li_size;
105 __int64 filesize, offset;
106#else
107 off_t filesize, offset;
108#endif
109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_cipher_init( &cipher_ctx );
111 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000112
113 /*
114 * Parse the command-line arguments.
115 */
116 if( argc != 7 )
117 {
118 const int *list;
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_printf( "Available ciphers:\n" );
123 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000124 while( *list )
125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 cipher_info = mbedtls_cipher_info_from_type( *list );
Gilles Peskine80932fa2021-07-19 17:34:02 +0200127 mbedtls_printf( " %s\n", mbedtls_cipher_info_get_name( cipher_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000128 list++;
129 }
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_printf( "\nAvailable message digests:\n" );
132 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000133 while( *list )
134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 md_info = mbedtls_md_info_from_type( *list );
136 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000137 list++;
138 }
139
Paul Bakker20a78082011-01-21 09:32:12 +0000140 goto exit;
141 }
142
143 mode = atoi( argv[1] );
144
145 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000148 goto exit;
149 }
150
151 if( strcmp( argv[2], argv[3] ) == 0 )
152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000154 goto exit;
155 }
156
157 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000160 goto exit;
161 }
162
163 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000166 goto exit;
167 }
168
Gilles Peskine6d576c92022-06-30 17:06:11 +0200169 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
170 mbedtls_setbuf( fin, NULL );
171 mbedtls_setbuf( fout, NULL );
172
Paul Bakker20a78082011-01-21 09:32:12 +0000173 /*
174 * Read the Cipher and MD from the command line
175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000177 if( cipher_info == NULL )
178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000180 goto exit;
181 }
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200182 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200183 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200184 mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200185 goto exit;
186 }
Paul Bakker20a78082011-01-21 09:32:12 +0000187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 md_info = mbedtls_md_info_from_string( argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000189 if( md_info == NULL )
190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000192 goto exit;
193 }
Janos Follath8eb64132016-06-03 15:40:57 +0100194
195 if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
196 {
Janos Follath352dbe22016-06-07 10:29:05 +0100197 mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100198 goto exit;
199 }
Paul Bakker20a78082011-01-21 09:32:12 +0000200
201 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100202 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000203 */
204 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
205 {
206 keylen = fread( key, 1, sizeof( key ), fkey );
207 fclose( fkey );
208 }
209 else
210 {
211 if( memcmp( argv[6], "hex:", 4 ) == 0 )
212 {
213 p = &argv[6][4];
214 keylen = 0;
215
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200216 while( sscanf( p, "%02X", (unsigned int*) &n ) > 0 &&
Paul Bakker20a78082011-01-21 09:32:12 +0000217 keylen < (int) sizeof( key ) )
218 {
219 key[keylen++] = (unsigned char) n;
220 p += 2;
221 }
222 }
223 else
224 {
225 keylen = strlen( argv[6] );
226
227 if( keylen > (int) sizeof( key ) )
228 keylen = (int) sizeof( key );
229
230 memcpy( key, argv[6], keylen );
231 }
232 }
233
Paul Bakkercce9d772011-11-18 14:26:47 +0000234#if defined(_WIN32_WCE)
235 filesize = fseek( fin, 0L, SEEK_END );
236#else
237#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000238 /*
239 * Support large files (> 2Gb) on Win32
240 */
241 li_size.QuadPart = 0;
242 li_size.LowPart =
243 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
244 li_size.LowPart, &li_size.HighPart, FILE_END );
245
246 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000249 goto exit;
250 }
251
252 filesize = li_size.QuadPart;
253#else
254 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
255 {
256 perror( "lseek" );
257 goto exit;
258 }
259#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000260#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000261
262 if( fseek( fin, 0, SEEK_SET ) < 0 )
263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000265 goto exit;
266 }
267
268 if( mode == MODE_ENCRYPT )
269 {
270 /*
271 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200272 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000273 */
274 for( i = 0; i < 8; i++ )
275 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
276
277 p = argv[2];
278
Paul Elliottd79d3eb2021-12-09 17:18:10 +0000279 if( mbedtls_md_starts( &md_ctx ) != 0 )
280 {
281 mbedtls_fprintf( stderr, "mbedtls_md_starts() returned error\n" );
282 goto exit;
283 }
284 if( mbedtls_md_update( &md_ctx, buffer, 8 ) != 0 )
285 {
286 mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
287 goto exit;
288 }
289 if( mbedtls_md_update( &md_ctx, ( unsigned char * ) p, strlen( p ) )
290 != 0 )
291 {
292 mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
293 goto exit;
294 }
295 if( mbedtls_md_finish( &md_ctx, digest ) != 0 )
296 {
297 mbedtls_fprintf( stderr, "mbedtls_md_finish() returned error\n" );
298 goto exit;
299 }
Paul Bakker20a78082011-01-21 09:32:12 +0000300
301 memcpy( IV, digest, 16 );
302
303 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000304 * Append the IV at the beginning of the output.
305 */
306 if( fwrite( IV, 1, 16, fout ) != 16 )
307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000309 goto exit;
310 }
311
312 /*
313 * Hash the IV and the secret key together 8192 times
314 * using the result to setup the AES context and HMAC.
315 */
316 memset( digest, 0, 32 );
317 memcpy( digest, IV, 16 );
318
319 for( i = 0; i < 8192; i++ )
320 {
Paul Elliottd79d3eb2021-12-09 17:18:10 +0000321 if( mbedtls_md_starts( &md_ctx ) != 0 )
322 {
323 mbedtls_fprintf( stderr,
324 "mbedtls_md_starts() returned error\n" );
325 goto exit;
326 }
327 if( mbedtls_md_update( &md_ctx, digest, 32 ) != 0 )
328 {
329 mbedtls_fprintf( stderr,
330 "mbedtls_md_update() returned error\n" );
331 goto exit;
332 }
333 if( mbedtls_md_update( &md_ctx, key, keylen ) != 0 )
334 {
335 mbedtls_fprintf( stderr,
336 "mbedtls_md_update() returned error\n" );
337 goto exit;
338 }
339 if( mbedtls_md_finish( &md_ctx, digest ) != 0 )
340 {
341 mbedtls_fprintf( stderr,
342 "mbedtls_md_finish() returned error\n" );
343 goto exit;
344 }
Paul Bakker20a78082011-01-21 09:32:12 +0000345
346 }
347
Gilles Peskine80932fa2021-07-19 17:34:02 +0200348 if( mbedtls_cipher_setkey( &cipher_ctx,
349 digest,
Gilles Peskine88d681c2021-09-01 11:19:33 +0200350 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000354 goto exit;
355 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200359 goto exit;
360 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000364 goto exit;
365 }
Paul Bakker20a78082011-01-21 09:32:12 +0000366
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100367 if( mbedtls_md_hmac_starts( &md_ctx, digest, 32 ) != 0 )
368 {
369 mbedtls_fprintf( stderr, "mbedtls_md_hmac_starts() returned error\n" );
370 goto exit;
371 }
Paul Bakker20a78082011-01-21 09:32:12 +0000372
373 /*
374 * Encrypt and write the ciphertext.
375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
379 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000380
Paul Bakker25b5fe52011-05-26 14:02:58 +0000381 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000384 goto exit;
385 }
386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200390 goto exit;
391 }
392
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100393 if( mbedtls_md_hmac_update( &md_ctx, output, olen ) != 0 )
394 {
395 mbedtls_fprintf( stderr, "mbedtls_md_hmac_update() returned error\n" );
396 goto exit;
397 }
Paul Bakker20a78082011-01-21 09:32:12 +0000398
Paul Bakker2c0994e2011-05-25 13:51:57 +0000399 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000402 goto exit;
403 }
404 }
405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000409 goto exit;
410 }
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100411 if( mbedtls_md_hmac_update( &md_ctx, output, olen ) != 0 )
412 {
413 mbedtls_fprintf( stderr, "mbedtls_md_hmac_update() returned error\n" );
414 goto exit;
415 }
Paul Bakker20a78082011-01-21 09:32:12 +0000416
Paul Bakker2c0994e2011-05-25 13:51:57 +0000417 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000420 goto exit;
421 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000422
Paul Bakker20a78082011-01-21 09:32:12 +0000423 /*
424 * Finally write the HMAC.
425 */
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100426 if( mbedtls_md_hmac_finish( &md_ctx, digest ) != 0 )
427 {
428 mbedtls_fprintf( stderr, "mbedtls_md_hmac_finish() returned error\n" );
429 goto exit;
430 }
Paul Bakker20a78082011-01-21 09:32:12 +0000431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000435 goto exit;
436 }
437 }
438
439 if( mode == MODE_DECRYPT )
440 {
441 /*
442 * The encrypted file must be structured as follows:
443 *
444 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200445 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000446 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200447 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
448 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000453 goto exit;
454 }
455
Janos Follath8eb64132016-06-03 15:40:57 +0100456 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
457 {
Janos Follath352dbe22016-06-07 10:29:05 +0100458 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100459 goto exit;
460 }
461
462 /*
463 * Check the file size.
464 */
Gilles Peskine80932fa2021-07-19 17:34:02 +0200465 if( mbedtls_cipher_info_get_mode( cipher_info ) != MBEDTLS_MODE_GCM &&
Paul Bakker243f48e2016-09-02 22:44:09 +0200466 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000468 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200469 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%u).\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000471 goto exit;
472 }
473
474 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100475 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000478
479 /*
480 * Read the IV and original filesize modulo 16.
481 */
482 if( fread( buffer, 1, 16, fin ) != 16 )
483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000485 goto exit;
486 }
487
488 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000489
490 /*
491 * Hash the IV and the secret key together 8192 times
492 * using the result to setup the AES context and HMAC.
493 */
494 memset( digest, 0, 32 );
495 memcpy( digest, IV, 16 );
496
497 for( i = 0; i < 8192; i++ )
498 {
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100499 if( mbedtls_md_starts( &md_ctx ) != 0 )
500 {
501 mbedtls_fprintf( stderr, "mbedtls_md_starts() returned error\n" );
502 goto exit;
503 }
504 if( mbedtls_md_update( &md_ctx, digest, 32 ) != 0 )
505 {
506 mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
507 goto exit;
508 }
509 if( mbedtls_md_update( &md_ctx, key, keylen ) != 0 )
510 {
511 mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
512 goto exit;
513 }
514 if( mbedtls_md_finish( &md_ctx, digest ) != 0 )
515 {
516 mbedtls_fprintf( stderr, "mbedtls_md_finish() returned error\n" );
517 goto exit;
518 }
Paul Bakker20a78082011-01-21 09:32:12 +0000519 }
520
Gilles Peskine80932fa2021-07-19 17:34:02 +0200521 if( mbedtls_cipher_setkey( &cipher_ctx,
522 digest,
Gilles Peskine88d681c2021-09-01 11:19:33 +0200523 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200527 goto exit;
528 }
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200533 goto exit;
534 }
535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200539 goto exit;
540 }
Paul Bakker20a78082011-01-21 09:32:12 +0000541
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100542 if( mbedtls_md_hmac_starts( &md_ctx, digest, 32 ) != 0 )
543 {
544 mbedtls_fprintf( stderr, "mbedtls_md_hmac_starts() returned error\n" );
545 goto exit;
546 }
Paul Bakker20a78082011-01-21 09:32:12 +0000547
548 /*
549 * Decrypt and write the plaintext.
550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000552 {
Paul Bakker243f48e2016-09-02 22:44:09 +0200553 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
554 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
555
556 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000557 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200558 mbedtls_fprintf( stderr, "fread(%u bytes) failed\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000560 goto exit;
561 }
562
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100563 if( mbedtls_md_hmac_update( &md_ctx, buffer, ilen ) != 0 )
564 {
565 mbedtls_fprintf( stderr, "mbedtls_md_hmac_update() returned error\n" );
566 goto exit;
567 }
Paul Bakker243f48e2016-09-02 22:44:09 +0200568 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
569 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200572 goto exit;
573 }
Paul Bakker20a78082011-01-21 09:32:12 +0000574
Paul Bakker2c0994e2011-05-25 13:51:57 +0000575 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000578 goto exit;
579 }
580 }
581
582 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000583 * Verify the message authentication code.
584 */
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100585 if( mbedtls_md_hmac_finish( &md_ctx, digest ) != 0 )
586 {
587 mbedtls_fprintf( stderr, "mbedtls_md_hmac_finish() returned error\n" );
588 goto exit;
589 }
Paul Bakker20a78082011-01-21 09:32:12 +0000590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000594 goto exit;
595 }
596
Paul Bakker424cd692013-10-31 14:22:08 +0100597 /* Use constant-time buffer comparison */
598 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100600 diff |= digest[i] ^ buffer[i];
601
602 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000605 "or file corrupted.\n" );
606 goto exit;
607 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100608
609 /*
610 * Write the final block of data
611 */
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100612 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
613 {
614 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
615 goto exit;
616 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100617
618 if( fwrite( output, 1, olen, fout ) != olen )
619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100621 goto exit;
622 }
Paul Bakker20a78082011-01-21 09:32:12 +0000623 }
624
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100625 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000626
627exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000628 if( fin )
629 fclose( fin );
630 if( fout )
631 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000632
Hanno Beckerf601ec52017-06-27 08:22:17 +0100633 /* Zeroize all command line arguments to also cover
634 the case when the user has missed or reordered some,
635 in which case the key might not be in argv[6]. */
636 for( i = 0; i < argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100637 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerf601ec52017-06-27 08:22:17 +0100638
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100639 mbedtls_platform_zeroize( IV, sizeof( IV ) );
640 mbedtls_platform_zeroize( key, sizeof( key ) );
641 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
642 mbedtls_platform_zeroize( output, sizeof( output ) );
643 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_cipher_free( &cipher_ctx );
646 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000647
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200648 mbedtls_exit( exit_code );
Paul Bakker20a78082011-01-21 09:32:12 +0000649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */