blob: 57b128bf3d63470693e43f40a483344b9b169302 [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
Bence Szépkútif744bd72020-06-05 13:02:18 +02006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7 *
8 * This file is provided under the Apache License 2.0, or the
9 * GNU General Public License v2.0 or later.
10 *
11 * **********
12 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020013 *
14 * Licensed under the Apache License, Version 2.0 (the "License"); you may
15 * not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
Paul Bakker20a78082011-01-21 09:32:12 +000025 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020026 * **********
27 *
28 * **********
29 * GNU General Public License v2.0 or later:
30 *
31 * This program is free software; you can redistribute it and/or modify
32 * it under the terms of the GNU General Public License as published by
33 * the Free Software Foundation; either version 2 of the License, or
34 * (at your option) any later version.
35 *
36 * This program is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
40 *
41 * You should have received a copy of the GNU General Public License along
42 * with this program; if not, write to the Free Software Foundation, Inc.,
43 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
44 *
45 * **********
46 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000047 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker20a78082011-01-21 09:32:12 +000048 */
49
Nicholas Wilson2682edf2017-12-05 12:08:15 +000050/* Enable definition of fileno() even when compiling with -std=c99. Must be
51 * set before config.h, which pulls in glibc's features.h indirectly.
52 * Harmless on other platforms. */
53#define _POSIX_C_SOURCE 1
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020057#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020059#endif
Paul Bakker20a78082011-01-21 09:32:12 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000063#else
Rich Evans18b78c72015-02-11 14:06:19 +000064#include <stdio.h>
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010065#include <stdlib.h>
66#define mbedtls_fprintf fprintf
67#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010068#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010069#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010070#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
71#endif /* MBEDTLS_PLATFORM_C */
Rich Evans18b78c72015-02-11 14:06:19 +000072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
74 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/cipher.h"
76#include "mbedtls/md.h"
Hanno Becker0b44d5c2018-10-12 16:46:37 +010077#include "mbedtls/platform_util.h"
Rich Evans18b78c72015-02-11 14:06:19 +000078
79#include <stdio.h>
80#include <stdlib.h>
81#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000082#endif
83
Paul Bakker494c0b82011-04-24 15:30:07 +000084#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000085#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000086#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000087#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000088#endif
Paul Bakker20a78082011-01-21 09:32:12 +000089#else
90#include <sys/types.h>
91#include <unistd.h>
92#endif
93
Paul Bakker20a78082011-01-21 09:32:12 +000094#define MODE_ENCRYPT 0
95#define MODE_DECRYPT 1
96
97#define USAGE \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
Paul Bakker20a78082011-01-21 09:32:12 +000099 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
100 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
101 "\n"
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
104 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +0000105int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +0000106{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_printf("MBEDTLS_CIPHER_C and/or MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200108 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000109}
110#else
Simon Butcher63cb97e2018-12-06 17:43:31 +0000111
Simon Butcher63cb97e2018-12-06 17:43:31 +0000112
Paul Bakker20a78082011-01-21 09:32:12 +0000113int main( int argc, char *argv[] )
114{
Gilles Peskine971e5e92020-04-14 19:34:19 +0200115 int ret = 1, i;
116 unsigned n;
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100117 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +0200118 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +0000119 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +0000120 FILE *fkey, *fin = NULL, *fout = NULL;
121
122 char *p;
123 unsigned char IV[16];
124 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +0000126 unsigned char buffer[1024];
127 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +0100128 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +0000129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 const mbedtls_cipher_info_t *cipher_info;
131 const mbedtls_md_info_t *md_info;
132 mbedtls_cipher_context_t cipher_ctx;
133 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +0000134#if defined(_WIN32_WCE)
135 long filesize, offset;
136#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000137 LARGE_INTEGER li_size;
138 __int64 filesize, offset;
139#else
140 off_t filesize, offset;
141#endif
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_cipher_init( &cipher_ctx );
144 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000145
146 /*
147 * Parse the command-line arguments.
148 */
149 if( argc != 7 )
150 {
151 const int *list;
152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_printf( "Available ciphers:\n" );
156 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000157 while( *list )
158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 cipher_info = mbedtls_cipher_info_from_type( *list );
160 mbedtls_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000161 list++;
162 }
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_printf( "\nAvailable message digests:\n" );
165 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000166 while( *list )
167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 md_info = mbedtls_md_info_from_type( *list );
169 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000170 list++;
171 }
172
Paul Bakkercce9d772011-11-18 14:26:47 +0000173#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000175 fflush( stdout ); getchar();
176#endif
177
178 goto exit;
179 }
180
181 mode = atoi( argv[1] );
182
183 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000186 goto exit;
187 }
188
189 if( strcmp( argv[2], argv[3] ) == 0 )
190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000192 goto exit;
193 }
194
195 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000198 goto exit;
199 }
200
201 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000204 goto exit;
205 }
206
207 /*
208 * Read the Cipher and MD from the command line
209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000211 if( cipher_info == NULL )
212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000214 goto exit;
215 }
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200216 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200217 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200218 mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200219 goto exit;
220 }
Paul Bakker20a78082011-01-21 09:32:12 +0000221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 md_info = mbedtls_md_info_from_string( argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000223 if( md_info == NULL )
224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000226 goto exit;
227 }
Janos Follath8eb64132016-06-03 15:40:57 +0100228
229 if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
230 {
Janos Follath352dbe22016-06-07 10:29:05 +0100231 mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100232 goto exit;
233 }
Paul Bakker20a78082011-01-21 09:32:12 +0000234
235 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100236 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000237 */
238 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
239 {
240 keylen = fread( key, 1, sizeof( key ), fkey );
241 fclose( fkey );
242 }
243 else
244 {
245 if( memcmp( argv[6], "hex:", 4 ) == 0 )
246 {
247 p = &argv[6][4];
248 keylen = 0;
249
250 while( sscanf( p, "%02X", &n ) > 0 &&
251 keylen < (int) sizeof( key ) )
252 {
253 key[keylen++] = (unsigned char) n;
254 p += 2;
255 }
256 }
257 else
258 {
259 keylen = strlen( argv[6] );
260
261 if( keylen > (int) sizeof( key ) )
262 keylen = (int) sizeof( key );
263
264 memcpy( key, argv[6], keylen );
265 }
266 }
267
Paul Bakkercce9d772011-11-18 14:26:47 +0000268#if defined(_WIN32_WCE)
269 filesize = fseek( fin, 0L, SEEK_END );
270#else
271#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000272 /*
273 * Support large files (> 2Gb) on Win32
274 */
275 li_size.QuadPart = 0;
276 li_size.LowPart =
277 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
278 li_size.LowPart, &li_size.HighPart, FILE_END );
279
280 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000283 goto exit;
284 }
285
286 filesize = li_size.QuadPart;
287#else
288 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
289 {
290 perror( "lseek" );
291 goto exit;
292 }
293#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000294#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000295
296 if( fseek( fin, 0, SEEK_SET ) < 0 )
297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000299 goto exit;
300 }
301
302 if( mode == MODE_ENCRYPT )
303 {
304 /*
305 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200306 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000307 */
308 for( i = 0; i < 8; i++ )
309 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
310
311 p = argv[2];
312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_md_starts( &md_ctx );
314 mbedtls_md_update( &md_ctx, buffer, 8 );
315 mbedtls_md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
316 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000317
318 memcpy( IV, digest, 16 );
319
320 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000321 * Append the IV at the beginning of the output.
322 */
323 if( fwrite( IV, 1, 16, fout ) != 16 )
324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000326 goto exit;
327 }
328
329 /*
330 * Hash the IV and the secret key together 8192 times
331 * using the result to setup the AES context and HMAC.
332 */
333 memset( digest, 0, 32 );
334 memcpy( digest, IV, 16 );
335
336 for( i = 0; i < 8192; i++ )
337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_md_starts( &md_ctx );
339 mbedtls_md_update( &md_ctx, digest, 32 );
340 mbedtls_md_update( &md_ctx, key, keylen );
341 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000342
343 }
344
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200345 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000349 goto exit;
350 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200354 goto exit;
355 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000359 goto exit;
360 }
Paul Bakker20a78082011-01-21 09:32:12 +0000361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000363
364 /*
365 * Encrypt and write the ciphertext.
366 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
370 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000371
Paul Bakker25b5fe52011-05-26 14:02:58 +0000372 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000375 goto exit;
376 }
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200381 goto exit;
382 }
383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000385
Paul Bakker2c0994e2011-05-25 13:51:57 +0000386 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000389 goto exit;
390 }
391 }
392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000396 goto exit;
397 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000399
Paul Bakker2c0994e2011-05-25 13:51:57 +0000400 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000403 goto exit;
404 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000405
Paul Bakker20a78082011-01-21 09:32:12 +0000406 /*
407 * Finally write the HMAC.
408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000414 goto exit;
415 }
416 }
417
418 if( mode == MODE_DECRYPT )
419 {
420 /*
421 * The encrypted file must be structured as follows:
422 *
423 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200424 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000425 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200426 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
427 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000432 goto exit;
433 }
434
Janos Follath8eb64132016-06-03 15:40:57 +0100435 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
436 {
Janos Follath352dbe22016-06-07 10:29:05 +0100437 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100438 goto exit;
439 }
440
441 /*
442 * Check the file size.
443 */
Paul Bakker243f48e2016-09-02 22:44:09 +0200444 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
445 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
449 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000450 goto exit;
451 }
452
453 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100454 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000455 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000457
458 /*
459 * Read the IV and original filesize modulo 16.
460 */
461 if( fread( buffer, 1, 16, fin ) != 16 )
462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000464 goto exit;
465 }
466
467 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000468
469 /*
470 * Hash the IV and the secret key together 8192 times
471 * using the result to setup the AES context and HMAC.
472 */
473 memset( digest, 0, 32 );
474 memcpy( digest, IV, 16 );
475
476 for( i = 0; i < 8192; i++ )
477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_md_starts( &md_ctx );
479 mbedtls_md_update( &md_ctx, digest, 32 );
480 mbedtls_md_update( &md_ctx, key, keylen );
481 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000482 }
483
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200484 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200488 goto exit;
489 }
490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200494 goto exit;
495 }
496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200500 goto exit;
501 }
Paul Bakker20a78082011-01-21 09:32:12 +0000502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000504
505 /*
506 * Decrypt and write the plaintext.
507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000509 {
Paul Bakker243f48e2016-09-02 22:44:09 +0200510 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
511 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
512
513 if( fread( buffer, 1, ilen, fin ) != ilen )
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",
516 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000517 goto exit;
518 }
519
Paul Bakker243f48e2016-09-02 22:44:09 +0200520 mbedtls_md_hmac_update( &md_ctx, buffer, ilen );
521 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
522 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200525 goto exit;
526 }
Paul Bakker20a78082011-01-21 09:32:12 +0000527
Paul Bakker2c0994e2011-05-25 13:51:57 +0000528 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000531 goto exit;
532 }
533 }
534
535 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000536 * Verify the message authentication code.
537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000543 goto exit;
544 }
545
Paul Bakker424cd692013-10-31 14:22:08 +0100546 /* Use constant-time buffer comparison */
547 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100549 diff |= digest[i] ^ buffer[i];
550
551 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000554 "or file corrupted.\n" );
555 goto exit;
556 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100557
558 /*
559 * Write the final block of data
560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_cipher_finish( &cipher_ctx, output, &olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100562
563 if( fwrite( output, 1, olen, fout ) != olen )
564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100566 goto exit;
567 }
Paul Bakker20a78082011-01-21 09:32:12 +0000568 }
569
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100570 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000571
572exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000573 if( fin )
574 fclose( fin );
575 if( fout )
576 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000577
Hanno Beckerf601ec52017-06-27 08:22:17 +0100578 /* Zeroize all command line arguments to also cover
579 the case when the user has missed or reordered some,
580 in which case the key might not be in argv[6]. */
581 for( i = 0; i < argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100582 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerf601ec52017-06-27 08:22:17 +0100583
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100584 mbedtls_platform_zeroize( IV, sizeof( IV ) );
585 mbedtls_platform_zeroize( key, sizeof( key ) );
586 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
587 mbedtls_platform_zeroize( output, sizeof( output ) );
588 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 mbedtls_cipher_free( &cipher_ctx );
591 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000592
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200593 mbedtls_exit( exit_code );
Paul Bakker20a78082011-01-21 09:32:12 +0000594}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */