blob: 476c20e2262f0b85d08d9ca5aaf1941e47675850 [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é-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
31 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/cipher.h"
33#include "mbedtls/md.h"
Hanno Becker0b44d5c2018-10-12 16:46:37 +010034#include "mbedtls/platform_util.h"
Rich Evans18b78c72015-02-11 14:06:19 +000035
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000039#endif
40
Paul Bakker494c0b82011-04-24 15:30:07 +000041#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000042#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000043#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000044#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000045#endif
Paul Bakker20a78082011-01-21 09:32:12 +000046#else
47#include <sys/types.h>
48#include <unistd.h>
49#endif
50
Paul Bakker20a78082011-01-21 09:32:12 +000051#define MODE_ENCRYPT 0
52#define MODE_DECRYPT 1
53
54#define USAGE \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
Paul Bakker20a78082011-01-21 09:32:12 +000056 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
57 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
58 "\n"
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
61 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000062int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 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 +020065 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000066}
67#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000068
Simon Butcher63cb97e2018-12-06 17:43:31 +000069
Paul Bakker20a78082011-01-21 09:32:12 +000070int main( int argc, char *argv[] )
71{
Gilles Peskinea5fc9392020-04-14 19:34:19 +020072 int ret = 1, i;
73 unsigned n;
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010074 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +020075 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +000076 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000077 FILE *fkey, *fin = NULL, *fout = NULL;
78
79 char *p;
80 unsigned char IV[16];
81 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +000083 unsigned char buffer[1024];
84 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +010085 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 const mbedtls_cipher_info_t *cipher_info;
88 const mbedtls_md_info_t *md_info;
89 mbedtls_cipher_context_t cipher_ctx;
90 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +000091#if defined(_WIN32_WCE)
92 long filesize, offset;
93#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000094 LARGE_INTEGER li_size;
95 __int64 filesize, offset;
96#else
97 off_t filesize, offset;
98#endif
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_cipher_init( &cipher_ctx );
101 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000102
103 /*
104 * Parse the command-line arguments.
105 */
106 if( argc != 7 )
107 {
108 const int *list;
109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_printf( "Available ciphers:\n" );
113 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000114 while( *list )
115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 cipher_info = mbedtls_cipher_info_from_type( *list );
Gilles Peskine80932fa2021-07-19 17:34:02 +0200117 mbedtls_printf( " %s\n", mbedtls_cipher_info_get_name( cipher_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000118 list++;
119 }
120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_printf( "\nAvailable message digests:\n" );
122 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000123 while( *list )
124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 md_info = mbedtls_md_info_from_type( *list );
126 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000127 list++;
128 }
129
Paul Bakker20a78082011-01-21 09:32:12 +0000130 goto exit;
131 }
132
133 mode = atoi( argv[1] );
134
135 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000138 goto exit;
139 }
140
141 if( strcmp( argv[2], argv[3] ) == 0 )
142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000144 goto exit;
145 }
146
147 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000150 goto exit;
151 }
152
153 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000156 goto exit;
157 }
158
Gilles Peskine6d576c92022-06-30 17:06:11 +0200159 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
160 mbedtls_setbuf( fin, NULL );
161 mbedtls_setbuf( fout, NULL );
162
Paul Bakker20a78082011-01-21 09:32:12 +0000163 /*
164 * Read the Cipher and MD from the command line
165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000167 if( cipher_info == NULL )
168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000170 goto exit;
171 }
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200172 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200173 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200174 mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200175 goto exit;
176 }
Paul Bakker20a78082011-01-21 09:32:12 +0000177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 md_info = mbedtls_md_info_from_string( argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000179 if( md_info == NULL )
180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000182 goto exit;
183 }
Janos Follath8eb64132016-06-03 15:40:57 +0100184
185 if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
186 {
Janos Follath352dbe22016-06-07 10:29:05 +0100187 mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100188 goto exit;
189 }
Paul Bakker20a78082011-01-21 09:32:12 +0000190
191 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100192 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000193 */
194 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
195 {
196 keylen = fread( key, 1, sizeof( key ), fkey );
197 fclose( fkey );
198 }
199 else
200 {
201 if( memcmp( argv[6], "hex:", 4 ) == 0 )
202 {
203 p = &argv[6][4];
204 keylen = 0;
205
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200206 while( sscanf( p, "%02X", (unsigned int*) &n ) > 0 &&
Paul Bakker20a78082011-01-21 09:32:12 +0000207 keylen < (int) sizeof( key ) )
208 {
209 key[keylen++] = (unsigned char) n;
210 p += 2;
211 }
212 }
213 else
214 {
215 keylen = strlen( argv[6] );
216
217 if( keylen > (int) sizeof( key ) )
218 keylen = (int) sizeof( key );
219
220 memcpy( key, argv[6], keylen );
221 }
222 }
223
Paul Bakkercce9d772011-11-18 14:26:47 +0000224#if defined(_WIN32_WCE)
225 filesize = fseek( fin, 0L, SEEK_END );
226#else
227#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000228 /*
229 * Support large files (> 2Gb) on Win32
230 */
231 li_size.QuadPart = 0;
232 li_size.LowPart =
233 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
234 li_size.LowPart, &li_size.HighPart, FILE_END );
235
236 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000239 goto exit;
240 }
241
242 filesize = li_size.QuadPart;
243#else
244 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
245 {
246 perror( "lseek" );
247 goto exit;
248 }
249#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000250#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000251
252 if( fseek( fin, 0, SEEK_SET ) < 0 )
253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000255 goto exit;
256 }
257
258 if( mode == MODE_ENCRYPT )
259 {
260 /*
261 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200262 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000263 */
264 for( i = 0; i < 8; i++ )
265 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
266
267 p = argv[2];
268
Paul Elliottd79d3eb2021-12-09 17:18:10 +0000269 if( mbedtls_md_starts( &md_ctx ) != 0 )
270 {
271 mbedtls_fprintf( stderr, "mbedtls_md_starts() returned error\n" );
272 goto exit;
273 }
274 if( mbedtls_md_update( &md_ctx, buffer, 8 ) != 0 )
275 {
276 mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
277 goto exit;
278 }
279 if( mbedtls_md_update( &md_ctx, ( unsigned char * ) p, strlen( p ) )
280 != 0 )
281 {
282 mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
283 goto exit;
284 }
285 if( mbedtls_md_finish( &md_ctx, digest ) != 0 )
286 {
287 mbedtls_fprintf( stderr, "mbedtls_md_finish() returned error\n" );
288 goto exit;
289 }
Paul Bakker20a78082011-01-21 09:32:12 +0000290
291 memcpy( IV, digest, 16 );
292
293 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000294 * Append the IV at the beginning of the output.
295 */
296 if( fwrite( IV, 1, 16, fout ) != 16 )
297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000299 goto exit;
300 }
301
302 /*
303 * Hash the IV and the secret key together 8192 times
304 * using the result to setup the AES context and HMAC.
305 */
306 memset( digest, 0, 32 );
307 memcpy( digest, IV, 16 );
308
309 for( i = 0; i < 8192; i++ )
310 {
Paul Elliottd79d3eb2021-12-09 17:18:10 +0000311 if( mbedtls_md_starts( &md_ctx ) != 0 )
312 {
313 mbedtls_fprintf( stderr,
314 "mbedtls_md_starts() returned error\n" );
315 goto exit;
316 }
317 if( mbedtls_md_update( &md_ctx, digest, 32 ) != 0 )
318 {
319 mbedtls_fprintf( stderr,
320 "mbedtls_md_update() returned error\n" );
321 goto exit;
322 }
323 if( mbedtls_md_update( &md_ctx, key, keylen ) != 0 )
324 {
325 mbedtls_fprintf( stderr,
326 "mbedtls_md_update() returned error\n" );
327 goto exit;
328 }
329 if( mbedtls_md_finish( &md_ctx, digest ) != 0 )
330 {
331 mbedtls_fprintf( stderr,
332 "mbedtls_md_finish() returned error\n" );
333 goto exit;
334 }
Paul Bakker20a78082011-01-21 09:32:12 +0000335
336 }
337
Gilles Peskine80932fa2021-07-19 17:34:02 +0200338 if( mbedtls_cipher_setkey( &cipher_ctx,
339 digest,
Gilles Peskine88d681c2021-09-01 11:19:33 +0200340 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000344 goto exit;
345 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200349 goto exit;
350 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000354 goto exit;
355 }
Paul Bakker20a78082011-01-21 09:32:12 +0000356
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100357 if( mbedtls_md_hmac_starts( &md_ctx, digest, 32 ) != 0 )
358 {
359 mbedtls_fprintf( stderr, "mbedtls_md_hmac_starts() returned error\n" );
360 goto exit;
361 }
Paul Bakker20a78082011-01-21 09:32:12 +0000362
363 /*
364 * Encrypt and write the ciphertext.
365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
369 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000370
Paul Bakker25b5fe52011-05-26 14:02:58 +0000371 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000374 goto exit;
375 }
376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200380 goto exit;
381 }
382
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100383 if( mbedtls_md_hmac_update( &md_ctx, output, olen ) != 0 )
384 {
385 mbedtls_fprintf( stderr, "mbedtls_md_hmac_update() returned error\n" );
386 goto exit;
387 }
Paul Bakker20a78082011-01-21 09:32:12 +0000388
Paul Bakker2c0994e2011-05-25 13:51:57 +0000389 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000392 goto exit;
393 }
394 }
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000399 goto exit;
400 }
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100401 if( mbedtls_md_hmac_update( &md_ctx, output, olen ) != 0 )
402 {
403 mbedtls_fprintf( stderr, "mbedtls_md_hmac_update() returned error\n" );
404 goto exit;
405 }
Paul Bakker20a78082011-01-21 09:32:12 +0000406
Paul Bakker2c0994e2011-05-25 13:51:57 +0000407 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000410 goto exit;
411 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000412
Paul Bakker20a78082011-01-21 09:32:12 +0000413 /*
414 * Finally write the HMAC.
415 */
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100416 if( mbedtls_md_hmac_finish( &md_ctx, digest ) != 0 )
417 {
418 mbedtls_fprintf( stderr, "mbedtls_md_hmac_finish() returned error\n" );
419 goto exit;
420 }
Paul Bakker20a78082011-01-21 09:32:12 +0000421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000425 goto exit;
426 }
427 }
428
429 if( mode == MODE_DECRYPT )
430 {
431 /*
432 * The encrypted file must be structured as follows:
433 *
434 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200435 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000436 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200437 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
438 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000443 goto exit;
444 }
445
Janos Follath8eb64132016-06-03 15:40:57 +0100446 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
447 {
Janos Follath352dbe22016-06-07 10:29:05 +0100448 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100449 goto exit;
450 }
451
452 /*
453 * Check the file size.
454 */
Gilles Peskine80932fa2021-07-19 17:34:02 +0200455 if( mbedtls_cipher_info_get_mode( cipher_info ) != MBEDTLS_MODE_GCM &&
Paul Bakker243f48e2016-09-02 22:44:09 +0200456 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000458 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200459 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%u).\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000461 goto exit;
462 }
463
464 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100465 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000468
469 /*
470 * Read the IV and original filesize modulo 16.
471 */
472 if( fread( buffer, 1, 16, fin ) != 16 )
473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000475 goto exit;
476 }
477
478 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000479
480 /*
481 * Hash the IV and the secret key together 8192 times
482 * using the result to setup the AES context and HMAC.
483 */
484 memset( digest, 0, 32 );
485 memcpy( digest, IV, 16 );
486
487 for( i = 0; i < 8192; i++ )
488 {
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100489 if( mbedtls_md_starts( &md_ctx ) != 0 )
490 {
491 mbedtls_fprintf( stderr, "mbedtls_md_starts() returned error\n" );
492 goto exit;
493 }
494 if( mbedtls_md_update( &md_ctx, digest, 32 ) != 0 )
495 {
496 mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
497 goto exit;
498 }
499 if( mbedtls_md_update( &md_ctx, key, keylen ) != 0 )
500 {
501 mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
502 goto exit;
503 }
504 if( mbedtls_md_finish( &md_ctx, digest ) != 0 )
505 {
506 mbedtls_fprintf( stderr, "mbedtls_md_finish() returned error\n" );
507 goto exit;
508 }
Paul Bakker20a78082011-01-21 09:32:12 +0000509 }
510
Gilles Peskine80932fa2021-07-19 17:34:02 +0200511 if( mbedtls_cipher_setkey( &cipher_ctx,
512 digest,
Gilles Peskine88d681c2021-09-01 11:19:33 +0200513 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200517 goto exit;
518 }
519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200523 goto exit;
524 }
525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200529 goto exit;
530 }
Paul Bakker20a78082011-01-21 09:32:12 +0000531
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100532 if( mbedtls_md_hmac_starts( &md_ctx, digest, 32 ) != 0 )
533 {
534 mbedtls_fprintf( stderr, "mbedtls_md_hmac_starts() returned error\n" );
535 goto exit;
536 }
Paul Bakker20a78082011-01-21 09:32:12 +0000537
538 /*
539 * Decrypt and write the plaintext.
540 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000542 {
Paul Bakker243f48e2016-09-02 22:44:09 +0200543 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
544 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
545
546 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000547 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200548 mbedtls_fprintf( stderr, "fread(%u bytes) failed\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000550 goto exit;
551 }
552
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100553 if( mbedtls_md_hmac_update( &md_ctx, buffer, ilen ) != 0 )
554 {
555 mbedtls_fprintf( stderr, "mbedtls_md_hmac_update() returned error\n" );
556 goto exit;
557 }
Paul Bakker243f48e2016-09-02 22:44:09 +0200558 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
559 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200562 goto exit;
563 }
Paul Bakker20a78082011-01-21 09:32:12 +0000564
Paul Bakker2c0994e2011-05-25 13:51:57 +0000565 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000568 goto exit;
569 }
570 }
571
572 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000573 * Verify the message authentication code.
574 */
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100575 if( mbedtls_md_hmac_finish( &md_ctx, digest ) != 0 )
576 {
577 mbedtls_fprintf( stderr, "mbedtls_md_hmac_finish() returned error\n" );
578 goto exit;
579 }
Paul Bakker20a78082011-01-21 09:32:12 +0000580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000584 goto exit;
585 }
586
Paul Bakker424cd692013-10-31 14:22:08 +0100587 /* Use constant-time buffer comparison */
588 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100590 diff |= digest[i] ^ buffer[i];
591
592 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000595 "or file corrupted.\n" );
596 goto exit;
597 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100598
599 /*
600 * Write the final block of data
601 */
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100602 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
603 {
604 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
605 goto exit;
606 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100607
608 if( fwrite( output, 1, olen, fout ) != olen )
609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100611 goto exit;
612 }
Paul Bakker20a78082011-01-21 09:32:12 +0000613 }
614
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100615 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000616
617exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000618 if( fin )
619 fclose( fin );
620 if( fout )
621 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000622
Hanno Beckerf601ec52017-06-27 08:22:17 +0100623 /* Zeroize all command line arguments to also cover
624 the case when the user has missed or reordered some,
625 in which case the key might not be in argv[6]. */
626 for( i = 0; i < argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100627 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerf601ec52017-06-27 08:22:17 +0100628
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100629 mbedtls_platform_zeroize( IV, sizeof( IV ) );
630 mbedtls_platform_zeroize( key, sizeof( key ) );
631 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
632 mbedtls_platform_zeroize( output, sizeof( output ) );
633 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_cipher_free( &cipher_ctx );
636 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000637
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200638 mbedtls_exit( exit_code );
Paul Bakker20a78082011-01-21 09:32:12 +0000639}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */