blob: 63a12480fdc8f533c7eaa33b6fedd814a214e60d [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000034#else
Rich Evans18b78c72015-02-11 14:06:19 +000035#include <stdio.h>
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010036#include <stdlib.h>
37#define mbedtls_fprintf fprintf
38#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010039#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010040#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010041#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
42#endif /* MBEDTLS_PLATFORM_C */
Rich Evans18b78c72015-02-11 14:06:19 +000043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
45 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/cipher.h"
47#include "mbedtls/md.h"
Hanno Becker0b44d5c2018-10-12 16:46:37 +010048#include "mbedtls/platform_util.h"
Rich Evans18b78c72015-02-11 14:06:19 +000049
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000053#endif
54
Paul Bakker494c0b82011-04-24 15:30:07 +000055#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000056#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000057#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000058#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000059#endif
Paul Bakker20a78082011-01-21 09:32:12 +000060#else
61#include <sys/types.h>
62#include <unistd.h>
63#endif
64
Paul Bakker20a78082011-01-21 09:32:12 +000065#define MODE_ENCRYPT 0
66#define MODE_DECRYPT 1
67
68#define USAGE \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
Paul Bakker20a78082011-01-21 09:32:12 +000070 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
71 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
72 "\n"
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
75 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000076int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 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 +020079 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000080}
81#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000082
Simon Butcher63cb97e2018-12-06 17:43:31 +000083
Paul Bakker20a78082011-01-21 09:32:12 +000084int main( int argc, char *argv[] )
85{
Gilles Peskinea5fc9392020-04-14 19:34:19 +020086 int ret = 1, i;
87 unsigned n;
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010088 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +020089 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +000090 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000091 FILE *fkey, *fin = NULL, *fout = NULL;
92
93 char *p;
94 unsigned char IV[16];
95 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +000097 unsigned char buffer[1024];
98 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +010099 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +0000100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 const mbedtls_cipher_info_t *cipher_info;
102 const mbedtls_md_info_t *md_info;
103 mbedtls_cipher_context_t cipher_ctx;
104 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +0000105#if defined(_WIN32_WCE)
106 long filesize, offset;
107#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000108 LARGE_INTEGER li_size;
109 __int64 filesize, offset;
110#else
111 off_t filesize, offset;
112#endif
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_cipher_init( &cipher_ctx );
115 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000116
117 /*
118 * Parse the command-line arguments.
119 */
120 if( argc != 7 )
121 {
122 const int *list;
123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_printf( "Available ciphers:\n" );
127 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000128 while( *list )
129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 cipher_info = mbedtls_cipher_info_from_type( *list );
131 mbedtls_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000132 list++;
133 }
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_printf( "\nAvailable message digests:\n" );
136 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000137 while( *list )
138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 md_info = mbedtls_md_info_from_type( *list );
140 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000141 list++;
142 }
143
Paul Bakkercce9d772011-11-18 14:26:47 +0000144#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000146 fflush( stdout ); getchar();
147#endif
148
149 goto exit;
150 }
151
152 mode = atoi( argv[1] );
153
154 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000157 goto exit;
158 }
159
160 if( strcmp( argv[2], argv[3] ) == 0 )
161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000163 goto exit;
164 }
165
166 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000169 goto exit;
170 }
171
172 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000175 goto exit;
176 }
177
178 /*
179 * Read the Cipher and MD from the command line
180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000182 if( cipher_info == NULL )
183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000185 goto exit;
186 }
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200187 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200188 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200189 mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200190 goto exit;
191 }
Paul Bakker20a78082011-01-21 09:32:12 +0000192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 md_info = mbedtls_md_info_from_string( argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000194 if( md_info == NULL )
195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000197 goto exit;
198 }
Janos Follath8eb64132016-06-03 15:40:57 +0100199
200 if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
201 {
Janos Follath352dbe22016-06-07 10:29:05 +0100202 mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100203 goto exit;
204 }
Paul Bakker20a78082011-01-21 09:32:12 +0000205
206 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100207 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000208 */
209 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
210 {
211 keylen = fread( key, 1, sizeof( key ), fkey );
212 fclose( fkey );
213 }
214 else
215 {
216 if( memcmp( argv[6], "hex:", 4 ) == 0 )
217 {
218 p = &argv[6][4];
219 keylen = 0;
220
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200221 while( sscanf( p, "%02X", (unsigned int*) &n ) > 0 &&
Paul Bakker20a78082011-01-21 09:32:12 +0000222 keylen < (int) sizeof( key ) )
223 {
224 key[keylen++] = (unsigned char) n;
225 p += 2;
226 }
227 }
228 else
229 {
230 keylen = strlen( argv[6] );
231
232 if( keylen > (int) sizeof( key ) )
233 keylen = (int) sizeof( key );
234
235 memcpy( key, argv[6], keylen );
236 }
237 }
238
Paul Bakkercce9d772011-11-18 14:26:47 +0000239#if defined(_WIN32_WCE)
240 filesize = fseek( fin, 0L, SEEK_END );
241#else
242#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000243 /*
244 * Support large files (> 2Gb) on Win32
245 */
246 li_size.QuadPart = 0;
247 li_size.LowPart =
248 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
249 li_size.LowPart, &li_size.HighPart, FILE_END );
250
251 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000254 goto exit;
255 }
256
257 filesize = li_size.QuadPart;
258#else
259 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
260 {
261 perror( "lseek" );
262 goto exit;
263 }
264#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000265#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000266
267 if( fseek( fin, 0, SEEK_SET ) < 0 )
268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000270 goto exit;
271 }
272
273 if( mode == MODE_ENCRYPT )
274 {
275 /*
276 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200277 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000278 */
279 for( i = 0; i < 8; i++ )
280 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
281
282 p = argv[2];
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_md_starts( &md_ctx );
285 mbedtls_md_update( &md_ctx, buffer, 8 );
286 mbedtls_md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
287 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000288
289 memcpy( IV, digest, 16 );
290
291 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000292 * Append the IV at the beginning of the output.
293 */
294 if( fwrite( IV, 1, 16, fout ) != 16 )
295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000297 goto exit;
298 }
299
300 /*
301 * Hash the IV and the secret key together 8192 times
302 * using the result to setup the AES context and HMAC.
303 */
304 memset( digest, 0, 32 );
305 memcpy( digest, IV, 16 );
306
307 for( i = 0; i < 8192; i++ )
308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_md_starts( &md_ctx );
310 mbedtls_md_update( &md_ctx, digest, 32 );
311 mbedtls_md_update( &md_ctx, key, keylen );
312 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000313
314 }
315
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200316 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000320 goto exit;
321 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200325 goto exit;
326 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000330 goto exit;
331 }
Paul Bakker20a78082011-01-21 09:32:12 +0000332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000334
335 /*
336 * Encrypt and write the ciphertext.
337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
341 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000342
Paul Bakker25b5fe52011-05-26 14:02:58 +0000343 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000346 goto exit;
347 }
348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200352 goto exit;
353 }
354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000356
Paul Bakker2c0994e2011-05-25 13:51:57 +0000357 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000360 goto exit;
361 }
362 }
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000367 goto exit;
368 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000370
Paul Bakker2c0994e2011-05-25 13:51:57 +0000371 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000374 goto exit;
375 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000376
Paul Bakker20a78082011-01-21 09:32:12 +0000377 /*
378 * Finally write the HMAC.
379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000385 goto exit;
386 }
387 }
388
389 if( mode == MODE_DECRYPT )
390 {
391 /*
392 * The encrypted file must be structured as follows:
393 *
394 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200395 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000396 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200397 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
398 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000403 goto exit;
404 }
405
Janos Follath8eb64132016-06-03 15:40:57 +0100406 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
407 {
Janos Follath352dbe22016-06-07 10:29:05 +0100408 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100409 goto exit;
410 }
411
412 /*
413 * Check the file size.
414 */
Paul Bakker243f48e2016-09-02 22:44:09 +0200415 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
416 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000418 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200419 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%u).\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000421 goto exit;
422 }
423
424 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100425 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000428
429 /*
430 * Read the IV and original filesize modulo 16.
431 */
432 if( fread( buffer, 1, 16, fin ) != 16 )
433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000435 goto exit;
436 }
437
438 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000439
440 /*
441 * Hash the IV and the secret key together 8192 times
442 * using the result to setup the AES context and HMAC.
443 */
444 memset( digest, 0, 32 );
445 memcpy( digest, IV, 16 );
446
447 for( i = 0; i < 8192; i++ )
448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_md_starts( &md_ctx );
450 mbedtls_md_update( &md_ctx, digest, 32 );
451 mbedtls_md_update( &md_ctx, key, keylen );
452 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000453 }
454
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200455 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200459 goto exit;
460 }
461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200465 goto exit;
466 }
467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200471 goto exit;
472 }
Paul Bakker20a78082011-01-21 09:32:12 +0000473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000475
476 /*
477 * Decrypt and write the plaintext.
478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000480 {
Paul Bakker243f48e2016-09-02 22:44:09 +0200481 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
482 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
483
484 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000485 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200486 mbedtls_fprintf( stderr, "fread(%u bytes) failed\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000488 goto exit;
489 }
490
Paul Bakker243f48e2016-09-02 22:44:09 +0200491 mbedtls_md_hmac_update( &md_ctx, buffer, ilen );
492 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
493 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200496 goto exit;
497 }
Paul Bakker20a78082011-01-21 09:32:12 +0000498
Paul Bakker2c0994e2011-05-25 13:51:57 +0000499 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000502 goto exit;
503 }
504 }
505
506 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000507 * Verify the message authentication code.
508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
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", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000514 goto exit;
515 }
516
Paul Bakker424cd692013-10-31 14:22:08 +0100517 /* Use constant-time buffer comparison */
518 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100520 diff |= digest[i] ^ buffer[i];
521
522 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000525 "or file corrupted.\n" );
526 goto exit;
527 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100528
529 /*
530 * Write the final block of data
531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_cipher_finish( &cipher_ctx, output, &olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100533
534 if( fwrite( output, 1, olen, fout ) != olen )
535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100537 goto exit;
538 }
Paul Bakker20a78082011-01-21 09:32:12 +0000539 }
540
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100541 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000542
543exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000544 if( fin )
545 fclose( fin );
546 if( fout )
547 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000548
Hanno Beckerf601ec52017-06-27 08:22:17 +0100549 /* Zeroize all command line arguments to also cover
550 the case when the user has missed or reordered some,
551 in which case the key might not be in argv[6]. */
552 for( i = 0; i < argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100553 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerf601ec52017-06-27 08:22:17 +0100554
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100555 mbedtls_platform_zeroize( IV, sizeof( IV ) );
556 mbedtls_platform_zeroize( key, sizeof( key ) );
557 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
558 mbedtls_platform_zeroize( output, sizeof( output ) );
559 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_cipher_free( &cipher_ctx );
562 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000563
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200564 mbedtls_exit( exit_code );
Paul Bakker20a78082011-01-21 09:32:12 +0000565}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */