blob: 5c7048045fd972dcbdbc8636d3acf9a9c86ed39d [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 *
Paul Bakker243f48e2016-09-02 22:44:09 +02005 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000020 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker20a78082011-01-21 09:32:12 +000021 */
22
Nicholas Wilson2682edf2017-12-05 12:08:15 +000023/* Enable definition of fileno() even when compiling with -std=c99. Must be
24 * set before config.h, which pulls in glibc's features.h indirectly.
25 * Harmless on other platforms. */
nia1c0c8372020-06-11 12:03:45 +010026#define _POSIX_C_SOURCE 200112L
Nicholas Wilson2682edf2017-12-05 12:08:15 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#endif
Paul Bakker20a78082011-01-21 09:32:12 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000036#else
Rich Evans18b78c72015-02-11 14:06:19 +000037#include <stdio.h>
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010038#include <stdlib.h>
39#define mbedtls_fprintf fprintf
40#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010041#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010042#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010043#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
44#endif /* MBEDTLS_PLATFORM_C */
Rich Evans18b78c72015-02-11 14:06:19 +000045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
47 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/cipher.h"
49#include "mbedtls/md.h"
Hanno Becker0b44d5c2018-10-12 16:46:37 +010050#include "mbedtls/platform_util.h"
Rich Evans18b78c72015-02-11 14:06:19 +000051
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000055#endif
56
Paul Bakker494c0b82011-04-24 15:30:07 +000057#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000058#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000059#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000060#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000061#endif
Paul Bakker20a78082011-01-21 09:32:12 +000062#else
63#include <sys/types.h>
64#include <unistd.h>
65#endif
66
Paul Bakker20a78082011-01-21 09:32:12 +000067#define MODE_ENCRYPT 0
68#define MODE_DECRYPT 1
69
70#define USAGE \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
Paul Bakker20a78082011-01-21 09:32:12 +000072 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
73 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
74 "\n"
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
77 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000078int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000079{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 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 +020081 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000082}
83#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000084
Simon Butcher63cb97e2018-12-06 17:43:31 +000085
Paul Bakker20a78082011-01-21 09:32:12 +000086int main( int argc, char *argv[] )
87{
Gilles Peskinea5fc9392020-04-14 19:34:19 +020088 int ret = 1, i;
89 unsigned n;
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010090 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +020091 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +000092 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000093 FILE *fkey, *fin = NULL, *fout = NULL;
94
95 char *p;
96 unsigned char IV[16];
97 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +000099 unsigned char buffer[1024];
100 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +0100101 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +0000102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 const mbedtls_cipher_info_t *cipher_info;
104 const mbedtls_md_info_t *md_info;
105 mbedtls_cipher_context_t cipher_ctx;
106 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +0000107#if defined(_WIN32_WCE)
108 long filesize, offset;
109#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000110 LARGE_INTEGER li_size;
111 __int64 filesize, offset;
112#else
113 off_t filesize, offset;
114#endif
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_cipher_init( &cipher_ctx );
117 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000118
119 /*
120 * Parse the command-line arguments.
121 */
122 if( argc != 7 )
123 {
124 const int *list;
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_printf( "Available ciphers:\n" );
129 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000130 while( *list )
131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 cipher_info = mbedtls_cipher_info_from_type( *list );
133 mbedtls_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000134 list++;
135 }
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_printf( "\nAvailable message digests:\n" );
138 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000139 while( *list )
140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 md_info = mbedtls_md_info_from_type( *list );
142 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000143 list++;
144 }
145
Paul Bakkercce9d772011-11-18 14:26:47 +0000146#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000148 fflush( stdout ); getchar();
149#endif
150
151 goto exit;
152 }
153
154 mode = atoi( argv[1] );
155
156 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000159 goto exit;
160 }
161
162 if( strcmp( argv[2], argv[3] ) == 0 )
163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000165 goto exit;
166 }
167
168 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000171 goto exit;
172 }
173
174 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000177 goto exit;
178 }
179
180 /*
181 * Read the Cipher and MD from the command line
182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000184 if( cipher_info == NULL )
185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000187 goto exit;
188 }
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200189 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200190 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200191 mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200192 goto exit;
193 }
Paul Bakker20a78082011-01-21 09:32:12 +0000194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 md_info = mbedtls_md_info_from_string( argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000196 if( md_info == NULL )
197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000199 goto exit;
200 }
Janos Follath8eb64132016-06-03 15:40:57 +0100201
202 if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
203 {
Janos Follath352dbe22016-06-07 10:29:05 +0100204 mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100205 goto exit;
206 }
Paul Bakker20a78082011-01-21 09:32:12 +0000207
208 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100209 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000210 */
211 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
212 {
213 keylen = fread( key, 1, sizeof( key ), fkey );
214 fclose( fkey );
215 }
216 else
217 {
218 if( memcmp( argv[6], "hex:", 4 ) == 0 )
219 {
220 p = &argv[6][4];
221 keylen = 0;
222
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200223 while( sscanf( p, "%02X", (unsigned int*) &n ) > 0 &&
Paul Bakker20a78082011-01-21 09:32:12 +0000224 keylen < (int) sizeof( key ) )
225 {
226 key[keylen++] = (unsigned char) n;
227 p += 2;
228 }
229 }
230 else
231 {
232 keylen = strlen( argv[6] );
233
234 if( keylen > (int) sizeof( key ) )
235 keylen = (int) sizeof( key );
236
237 memcpy( key, argv[6], keylen );
238 }
239 }
240
Paul Bakkercce9d772011-11-18 14:26:47 +0000241#if defined(_WIN32_WCE)
242 filesize = fseek( fin, 0L, SEEK_END );
243#else
244#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000245 /*
246 * Support large files (> 2Gb) on Win32
247 */
248 li_size.QuadPart = 0;
249 li_size.LowPart =
250 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
251 li_size.LowPart, &li_size.HighPart, FILE_END );
252
253 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000256 goto exit;
257 }
258
259 filesize = li_size.QuadPart;
260#else
261 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
262 {
263 perror( "lseek" );
264 goto exit;
265 }
266#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000267#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000268
269 if( fseek( fin, 0, SEEK_SET ) < 0 )
270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000272 goto exit;
273 }
274
275 if( mode == MODE_ENCRYPT )
276 {
277 /*
278 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200279 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000280 */
281 for( i = 0; i < 8; i++ )
282 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
283
284 p = argv[2];
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_md_starts( &md_ctx );
287 mbedtls_md_update( &md_ctx, buffer, 8 );
288 mbedtls_md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
289 mbedtls_md_finish( &md_ctx, digest );
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 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_md_starts( &md_ctx );
312 mbedtls_md_update( &md_ctx, digest, 32 );
313 mbedtls_md_update( &md_ctx, key, keylen );
314 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000315
316 }
317
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200318 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000322 goto exit;
323 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200327 goto exit;
328 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000332 goto exit;
333 }
Paul Bakker20a78082011-01-21 09:32:12 +0000334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000336
337 /*
338 * Encrypt and write the ciphertext.
339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
343 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000344
Paul Bakker25b5fe52011-05-26 14:02:58 +0000345 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000348 goto exit;
349 }
350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200354 goto exit;
355 }
356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000358
Paul Bakker2c0994e2011-05-25 13:51:57 +0000359 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000362 goto exit;
363 }
364 }
365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000369 goto exit;
370 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000372
Paul Bakker2c0994e2011-05-25 13:51:57 +0000373 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000376 goto exit;
377 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000378
Paul Bakker20a78082011-01-21 09:32:12 +0000379 /*
380 * Finally write the HMAC.
381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000387 goto exit;
388 }
389 }
390
391 if( mode == MODE_DECRYPT )
392 {
393 /*
394 * The encrypted file must be structured as follows:
395 *
396 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200397 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000398 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200399 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
400 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000405 goto exit;
406 }
407
Janos Follath8eb64132016-06-03 15:40:57 +0100408 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
409 {
Janos Follath352dbe22016-06-07 10:29:05 +0100410 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100411 goto exit;
412 }
413
414 /*
415 * Check the file size.
416 */
Paul Bakker243f48e2016-09-02 22:44:09 +0200417 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
418 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000420 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200421 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%u).\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000423 goto exit;
424 }
425
426 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100427 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000430
431 /*
432 * Read the IV and original filesize modulo 16.
433 */
434 if( fread( buffer, 1, 16, fin ) != 16 )
435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000437 goto exit;
438 }
439
440 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000441
442 /*
443 * Hash the IV and the secret key together 8192 times
444 * using the result to setup the AES context and HMAC.
445 */
446 memset( digest, 0, 32 );
447 memcpy( digest, IV, 16 );
448
449 for( i = 0; i < 8192; i++ )
450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_md_starts( &md_ctx );
452 mbedtls_md_update( &md_ctx, digest, 32 );
453 mbedtls_md_update( &md_ctx, key, keylen );
454 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000455 }
456
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200457 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200461 goto exit;
462 }
463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200467 goto exit;
468 }
469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200473 goto exit;
474 }
Paul Bakker20a78082011-01-21 09:32:12 +0000475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000477
478 /*
479 * Decrypt and write the plaintext.
480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000482 {
Paul Bakker243f48e2016-09-02 22:44:09 +0200483 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
484 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
485
486 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000487 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200488 mbedtls_fprintf( stderr, "fread(%u bytes) failed\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000490 goto exit;
491 }
492
Paul Bakker243f48e2016-09-02 22:44:09 +0200493 mbedtls_md_hmac_update( &md_ctx, buffer, ilen );
494 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
495 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200498 goto exit;
499 }
Paul Bakker20a78082011-01-21 09:32:12 +0000500
Paul Bakker2c0994e2011-05-25 13:51:57 +0000501 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000504 goto exit;
505 }
506 }
507
508 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000509 * Verify the message authentication code.
510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000516 goto exit;
517 }
518
Paul Bakker424cd692013-10-31 14:22:08 +0100519 /* Use constant-time buffer comparison */
520 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100522 diff |= digest[i] ^ buffer[i];
523
524 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000527 "or file corrupted.\n" );
528 goto exit;
529 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100530
531 /*
532 * Write the final block of data
533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_cipher_finish( &cipher_ctx, output, &olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100535
536 if( fwrite( output, 1, olen, fout ) != olen )
537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100539 goto exit;
540 }
Paul Bakker20a78082011-01-21 09:32:12 +0000541 }
542
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100543 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000544
545exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000546 if( fin )
547 fclose( fin );
548 if( fout )
549 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000550
Hanno Beckerf601ec52017-06-27 08:22:17 +0100551 /* Zeroize all command line arguments to also cover
552 the case when the user has missed or reordered some,
553 in which case the key might not be in argv[6]. */
554 for( i = 0; i < argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100555 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerf601ec52017-06-27 08:22:17 +0100556
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100557 mbedtls_platform_zeroize( IV, sizeof( IV ) );
558 mbedtls_platform_zeroize( key, sizeof( key ) );
559 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
560 mbedtls_platform_zeroize( output, sizeof( output ) );
561 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_cipher_free( &cipher_ctx );
564 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000565
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200566 mbedtls_exit( exit_code );
Paul Bakker20a78082011-01-21 09:32:12 +0000567}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */