blob: c92ca133f04a34ca80ef51c68af44c3c3fc0ecca [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
22 * set before config.h, which pulls in glibc's features.h indirectly.
23 * Harmless on other platforms. */
nia1c0c8372020-06-11 12:03:45 +010024#define _POSIX_C_SOURCE 200112L
Nicholas Wilson2682edf2017-12-05 12:08:15 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker20a78082011-01-21 09:32:12 +000031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
35 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/cipher.h"
37#include "mbedtls/md.h"
Hanno Becker0b44d5c2018-10-12 16:46:37 +010038#include "mbedtls/platform_util.h"
Rich Evans18b78c72015-02-11 14:06:19 +000039
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000043#endif
44
Paul Bakker494c0b82011-04-24 15:30:07 +000045#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000046#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000047#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000048#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000049#endif
Paul Bakker20a78082011-01-21 09:32:12 +000050#else
51#include <sys/types.h>
52#include <unistd.h>
53#endif
54
Paul Bakker20a78082011-01-21 09:32:12 +000055#define MODE_ENCRYPT 0
56#define MODE_DECRYPT 1
57
58#define USAGE \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
Paul Bakker20a78082011-01-21 09:32:12 +000060 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
61 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
62 "\n"
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
65 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000066int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 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 +020069 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000070}
71#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000072
Simon Butcher63cb97e2018-12-06 17:43:31 +000073
Paul Bakker20a78082011-01-21 09:32:12 +000074int main( int argc, char *argv[] )
75{
Gilles Peskinea5fc9392020-04-14 19:34:19 +020076 int ret = 1, i;
77 unsigned n;
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010078 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +020079 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +000080 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000081 FILE *fkey, *fin = NULL, *fout = NULL;
82
83 char *p;
84 unsigned char IV[16];
85 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +000087 unsigned char buffer[1024];
88 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +010089 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +000090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 const mbedtls_cipher_info_t *cipher_info;
92 const mbedtls_md_info_t *md_info;
93 mbedtls_cipher_context_t cipher_ctx;
94 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +000095#if defined(_WIN32_WCE)
96 long filesize, offset;
97#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000098 LARGE_INTEGER li_size;
99 __int64 filesize, offset;
100#else
101 off_t filesize, offset;
102#endif
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_cipher_init( &cipher_ctx );
105 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000106
107 /*
108 * Parse the command-line arguments.
109 */
110 if( argc != 7 )
111 {
112 const int *list;
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_printf( "Available ciphers:\n" );
117 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000118 while( *list )
119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 cipher_info = mbedtls_cipher_info_from_type( *list );
121 mbedtls_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000122 list++;
123 }
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_printf( "\nAvailable message digests:\n" );
126 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000127 while( *list )
128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 md_info = mbedtls_md_info_from_type( *list );
130 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000131 list++;
132 }
133
Paul Bakkercce9d772011-11-18 14:26:47 +0000134#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000136 fflush( stdout ); getchar();
137#endif
138
139 goto exit;
140 }
141
142 mode = atoi( argv[1] );
143
144 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000147 goto exit;
148 }
149
150 if( strcmp( argv[2], argv[3] ) == 0 )
151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000153 goto exit;
154 }
155
156 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000159 goto exit;
160 }
161
162 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000165 goto exit;
166 }
167
168 /*
169 * Read the Cipher and MD from the command line
170 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000172 if( cipher_info == NULL )
173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000175 goto exit;
176 }
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200177 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200178 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200179 mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200180 goto exit;
181 }
Paul Bakker20a78082011-01-21 09:32:12 +0000182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 md_info = mbedtls_md_info_from_string( argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000184 if( md_info == NULL )
185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000187 goto exit;
188 }
Janos Follath8eb64132016-06-03 15:40:57 +0100189
190 if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
191 {
Janos Follath352dbe22016-06-07 10:29:05 +0100192 mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100193 goto exit;
194 }
Paul Bakker20a78082011-01-21 09:32:12 +0000195
196 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100197 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000198 */
199 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
200 {
201 keylen = fread( key, 1, sizeof( key ), fkey );
202 fclose( fkey );
203 }
204 else
205 {
206 if( memcmp( argv[6], "hex:", 4 ) == 0 )
207 {
208 p = &argv[6][4];
209 keylen = 0;
210
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200211 while( sscanf( p, "%02X", (unsigned int*) &n ) > 0 &&
Paul Bakker20a78082011-01-21 09:32:12 +0000212 keylen < (int) sizeof( key ) )
213 {
214 key[keylen++] = (unsigned char) n;
215 p += 2;
216 }
217 }
218 else
219 {
220 keylen = strlen( argv[6] );
221
222 if( keylen > (int) sizeof( key ) )
223 keylen = (int) sizeof( key );
224
225 memcpy( key, argv[6], keylen );
226 }
227 }
228
Paul Bakkercce9d772011-11-18 14:26:47 +0000229#if defined(_WIN32_WCE)
230 filesize = fseek( fin, 0L, SEEK_END );
231#else
232#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000233 /*
234 * Support large files (> 2Gb) on Win32
235 */
236 li_size.QuadPart = 0;
237 li_size.LowPart =
238 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
239 li_size.LowPart, &li_size.HighPart, FILE_END );
240
241 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000244 goto exit;
245 }
246
247 filesize = li_size.QuadPart;
248#else
249 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
250 {
251 perror( "lseek" );
252 goto exit;
253 }
254#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000255#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000256
257 if( fseek( fin, 0, SEEK_SET ) < 0 )
258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000260 goto exit;
261 }
262
263 if( mode == MODE_ENCRYPT )
264 {
265 /*
266 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200267 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000268 */
269 for( i = 0; i < 8; i++ )
270 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
271
272 p = argv[2];
273
Paul Elliottd0688762021-12-09 17:18:10 +0000274 if( mbedtls_md_starts( &md_ctx ) != 0 )
275 {
276 mbedtls_fprintf( stderr, "mbedtls_md_starts() returned error\n" );
277 goto exit;
278 }
279 if( mbedtls_md_update( &md_ctx, buffer, 8 ) != 0 )
280 {
281 mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
282 goto exit;
283 }
284 if( mbedtls_md_update( &md_ctx, ( unsigned char * ) p, strlen( p ) )
285 != 0 )
286 {
287 mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
288 goto exit;
289 }
290 if( mbedtls_md_finish( &md_ctx, digest ) != 0 )
291 {
292 mbedtls_fprintf( stderr, "mbedtls_md_finish() returned error\n" );
293 goto exit;
294 }
Paul Bakker20a78082011-01-21 09:32:12 +0000295
296 memcpy( IV, digest, 16 );
297
298 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000299 * Append the IV at the beginning of the output.
300 */
301 if( fwrite( IV, 1, 16, fout ) != 16 )
302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000304 goto exit;
305 }
306
307 /*
308 * Hash the IV and the secret key together 8192 times
309 * using the result to setup the AES context and HMAC.
310 */
311 memset( digest, 0, 32 );
312 memcpy( digest, IV, 16 );
313
314 for( i = 0; i < 8192; i++ )
315 {
Paul Elliottd0688762021-12-09 17:18:10 +0000316 if( mbedtls_md_starts( &md_ctx ) != 0 )
317 {
318 mbedtls_fprintf( stderr,
319 "mbedtls_md_starts() returned error\n" );
320 goto exit;
321 }
322 if( mbedtls_md_update( &md_ctx, digest, 32 ) != 0 )
323 {
324 mbedtls_fprintf( stderr,
325 "mbedtls_md_update() returned error\n" );
326 goto exit;
327 }
328 if( mbedtls_md_update( &md_ctx, key, keylen ) != 0 )
329 {
330 mbedtls_fprintf( stderr,
331 "mbedtls_md_update() returned error\n" );
332 goto exit;
333 }
334 if( mbedtls_md_finish( &md_ctx, digest ) != 0 )
335 {
336 mbedtls_fprintf( stderr,
337 "mbedtls_md_finish() returned error\n" );
338 goto exit;
339 }
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
Gilles Peskine3d283782021-12-10 14:25:45 +0100360 if( mbedtls_md_hmac_starts( &md_ctx, digest, 32 ) != 0 )
361 {
362 mbedtls_fprintf( stderr, "mbedtls_md_hmac_starts() returned error\n" );
363 goto exit;
364 }
Paul Bakker20a78082011-01-21 09:32:12 +0000365
366 /*
367 * Encrypt and write the ciphertext.
368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
372 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000373
Paul Bakker25b5fe52011-05-26 14:02:58 +0000374 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000377 goto exit;
378 }
379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200383 goto exit;
384 }
385
Gilles Peskine3d283782021-12-10 14:25:45 +0100386 if( mbedtls_md_hmac_update( &md_ctx, output, olen ) != 0 )
387 {
388 mbedtls_fprintf( stderr, "mbedtls_md_hmac_update() returned error\n" );
389 goto exit;
390 }
Paul Bakker20a78082011-01-21 09:32:12 +0000391
Paul Bakker2c0994e2011-05-25 13:51:57 +0000392 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000395 goto exit;
396 }
397 }
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000402 goto exit;
403 }
Gilles Peskine3d283782021-12-10 14:25:45 +0100404 if( mbedtls_md_hmac_update( &md_ctx, output, olen ) != 0 )
405 {
406 mbedtls_fprintf( stderr, "mbedtls_md_hmac_update() returned error\n" );
407 goto exit;
408 }
Paul Bakker20a78082011-01-21 09:32:12 +0000409
Paul Bakker2c0994e2011-05-25 13:51:57 +0000410 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000413 goto exit;
414 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000415
Paul Bakker20a78082011-01-21 09:32:12 +0000416 /*
417 * Finally write the HMAC.
418 */
Gilles Peskine3d283782021-12-10 14:25:45 +0100419 if( mbedtls_md_hmac_finish( &md_ctx, digest ) != 0 )
420 {
421 mbedtls_fprintf( stderr, "mbedtls_md_hmac_finish() returned error\n" );
422 goto exit;
423 }
Paul Bakker20a78082011-01-21 09:32:12 +0000424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000428 goto exit;
429 }
430 }
431
432 if( mode == MODE_DECRYPT )
433 {
434 /*
435 * The encrypted file must be structured as follows:
436 *
437 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200438 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000439 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200440 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
441 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000446 goto exit;
447 }
448
Janos Follath8eb64132016-06-03 15:40:57 +0100449 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
450 {
Janos Follath352dbe22016-06-07 10:29:05 +0100451 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100452 goto exit;
453 }
454
455 /*
456 * Check the file size.
457 */
Paul Bakker243f48e2016-09-02 22:44:09 +0200458 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
459 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000461 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200462 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%u).\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000464 goto exit;
465 }
466
467 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100468 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000471
472 /*
473 * Read the IV and original filesize modulo 16.
474 */
475 if( fread( buffer, 1, 16, fin ) != 16 )
476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000478 goto exit;
479 }
480
481 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000482
483 /*
484 * Hash the IV and the secret key together 8192 times
485 * using the result to setup the AES context and HMAC.
486 */
487 memset( digest, 0, 32 );
488 memcpy( digest, IV, 16 );
489
490 for( i = 0; i < 8192; i++ )
491 {
Gilles Peskine3d283782021-12-10 14:25:45 +0100492 if( mbedtls_md_starts( &md_ctx ) != 0 )
493 {
494 mbedtls_fprintf( stderr, "mbedtls_md_starts() returned error\n" );
495 goto exit;
496 }
497 if( mbedtls_md_update( &md_ctx, digest, 32 ) != 0 )
498 {
499 mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
500 goto exit;
501 }
502 if( mbedtls_md_update( &md_ctx, key, keylen ) != 0 )
503 {
504 mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
505 goto exit;
506 }
507 if( mbedtls_md_finish( &md_ctx, digest ) != 0 )
508 {
509 mbedtls_fprintf( stderr, "mbedtls_md_finish() returned error\n" );
510 goto exit;
511 }
Paul Bakker20a78082011-01-21 09:32:12 +0000512 }
513
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200514 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200518 goto exit;
519 }
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200524 goto exit;
525 }
526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200530 goto exit;
531 }
Paul Bakker20a78082011-01-21 09:32:12 +0000532
Gilles Peskine3d283782021-12-10 14:25:45 +0100533 if( mbedtls_md_hmac_starts( &md_ctx, digest, 32 ) != 0 )
534 {
535 mbedtls_fprintf( stderr, "mbedtls_md_hmac_starts() returned error\n" );
536 goto exit;
537 }
Paul Bakker20a78082011-01-21 09:32:12 +0000538
539 /*
540 * Decrypt and write the plaintext.
541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000543 {
Paul Bakker243f48e2016-09-02 22:44:09 +0200544 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
545 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
546
547 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000548 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200549 mbedtls_fprintf( stderr, "fread(%u bytes) failed\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000551 goto exit;
552 }
553
Gilles Peskine3d283782021-12-10 14:25:45 +0100554 if( mbedtls_md_hmac_update( &md_ctx, buffer, ilen ) != 0 )
555 {
556 mbedtls_fprintf( stderr, "mbedtls_md_hmac_update() returned error\n" );
557 goto exit;
558 }
Paul Bakker243f48e2016-09-02 22:44:09 +0200559 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
560 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200563 goto exit;
564 }
Paul Bakker20a78082011-01-21 09:32:12 +0000565
Paul Bakker2c0994e2011-05-25 13:51:57 +0000566 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000569 goto exit;
570 }
571 }
572
573 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000574 * Verify the message authentication code.
575 */
Gilles Peskine3d283782021-12-10 14:25:45 +0100576 if( mbedtls_md_hmac_finish( &md_ctx, digest ) != 0 )
577 {
578 mbedtls_fprintf( stderr, "mbedtls_md_hmac_finish() returned error\n" );
579 goto exit;
580 }
Paul Bakker20a78082011-01-21 09:32:12 +0000581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000585 goto exit;
586 }
587
Paul Bakker424cd692013-10-31 14:22:08 +0100588 /* Use constant-time buffer comparison */
589 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100591 diff |= digest[i] ^ buffer[i];
592
593 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000596 "or file corrupted.\n" );
597 goto exit;
598 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100599
600 /*
601 * Write the final block of data
602 */
Gilles Peskine3d283782021-12-10 14:25:45 +0100603 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
604 {
605 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
606 goto exit;
607 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100608
609 if( fwrite( output, 1, olen, fout ) != olen )
610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100612 goto exit;
613 }
Paul Bakker20a78082011-01-21 09:32:12 +0000614 }
615
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100616 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000617
618exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000619 if( fin )
620 fclose( fin );
621 if( fout )
622 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000623
Hanno Beckerf601ec52017-06-27 08:22:17 +0100624 /* Zeroize all command line arguments to also cover
625 the case when the user has missed or reordered some,
626 in which case the key might not be in argv[6]. */
627 for( i = 0; i < argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100628 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerf601ec52017-06-27 08:22:17 +0100629
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100630 mbedtls_platform_zeroize( IV, sizeof( IV ) );
631 mbedtls_platform_zeroize( key, sizeof( key ) );
632 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
633 mbedtls_platform_zeroize( output, sizeof( output ) );
634 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_cipher_free( &cipher_ctx );
637 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000638
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200639 mbedtls_exit( exit_code );
Paul Bakker20a78082011-01-21 09:32:12 +0000640}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */