blob: e13f6a3311bdde7b215fc34b9e5efd1357bec7d1 [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úti44bfbe32020-08-19 16:54:51 +02005 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-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úti4e9f7122020-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 * **********
Paul Bakker20a78082011-01-21 09:32:12 +000046 */
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020050#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020052#endif
Paul Bakker20a78082011-01-21 09:32:12 +000053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000056#else
Rich Evans18b78c72015-02-11 14:06:19 +000057#include <stdio.h>
Andres Amaya Garcia990900f2018-04-29 19:11:26 +010058#include <stdlib.h>
59#define mbedtls_fprintf fprintf
60#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020061#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010062#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia990900f2018-04-29 19:11:26 +010063#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
64#endif /* MBEDTLS_PLATFORM_C */
Rich Evans18b78c72015-02-11 14:06:19 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
67 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/cipher.h"
69#include "mbedtls/md.h"
Rich Evans18b78c72015-02-11 14:06:19 +000070
71#include <stdio.h>
72#include <stdlib.h>
73#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000074#endif
75
Paul Bakker494c0b82011-04-24 15:30:07 +000076#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000077#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000078#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000079#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000080#endif
Paul Bakker20a78082011-01-21 09:32:12 +000081#else
82#include <sys/types.h>
83#include <unistd.h>
84#endif
85
Paul Bakker20a78082011-01-21 09:32:12 +000086#define MODE_ENCRYPT 0
87#define MODE_DECRYPT 1
88
89#define USAGE \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
Paul Bakker20a78082011-01-21 09:32:12 +000091 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
92 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
93 "\n"
94
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
96 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000097int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_printf("MBEDTLS_CIPHER_C and/or MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200100 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000101}
102#else
Hanno Becker9a1a1512018-10-12 16:46:37 +0100103
104/* Implementation that should never be optimized out by the compiler */
105static void mbedtls_zeroize( void *v, size_t n ) {
106 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
107}
108
Paul Bakker20a78082011-01-21 09:32:12 +0000109int main( int argc, char *argv[] )
110{
Gilles Peskine3aba3f42020-04-14 19:34:19 +0200111 int ret = 1, i;
112 unsigned n;
Andres Amaya Garcia990900f2018-04-29 19:11:26 +0100113 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +0200114 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +0000115 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +0000116 FILE *fkey, *fin = NULL, *fout = NULL;
117
118 char *p;
119 unsigned char IV[16];
120 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +0000122 unsigned char buffer[1024];
123 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +0100124 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +0000125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 const mbedtls_cipher_info_t *cipher_info;
127 const mbedtls_md_info_t *md_info;
128 mbedtls_cipher_context_t cipher_ctx;
129 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +0000130#if defined(_WIN32_WCE)
131 long filesize, offset;
132#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000133 LARGE_INTEGER li_size;
134 __int64 filesize, offset;
135#else
136 off_t filesize, offset;
137#endif
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_cipher_init( &cipher_ctx );
140 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000141
142 /*
143 * Parse the command-line arguments.
144 */
145 if( argc != 7 )
146 {
147 const int *list;
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( "Available ciphers:\n" );
152 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000153 while( *list )
154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 cipher_info = mbedtls_cipher_info_from_type( *list );
156 mbedtls_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000157 list++;
158 }
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_printf( "\nAvailable message digests:\n" );
161 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000162 while( *list )
163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 md_info = mbedtls_md_info_from_type( *list );
165 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000166 list++;
167 }
168
Paul Bakkercce9d772011-11-18 14:26:47 +0000169#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000171 fflush( stdout ); getchar();
172#endif
173
174 goto exit;
175 }
176
177 mode = atoi( argv[1] );
178
179 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000182 goto exit;
183 }
184
185 if( strcmp( argv[2], argv[3] ) == 0 )
186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000188 goto exit;
189 }
190
191 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000194 goto exit;
195 }
196
197 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000200 goto exit;
201 }
202
203 /*
204 * Read the Cipher and MD from the command line
205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000207 if( cipher_info == NULL )
208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000210 goto exit;
211 }
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200212 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200213 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200214 mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200215 goto exit;
216 }
Paul Bakker20a78082011-01-21 09:32:12 +0000217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 md_info = mbedtls_md_info_from_string( argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000219 if( md_info == NULL )
220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000222 goto exit;
223 }
Janos Follath8eb64132016-06-03 15:40:57 +0100224
225 if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
226 {
Janos Follath352dbe22016-06-07 10:29:05 +0100227 mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100228 goto exit;
229 }
Paul Bakker20a78082011-01-21 09:32:12 +0000230
231 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100232 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000233 */
234 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
235 {
236 keylen = fread( key, 1, sizeof( key ), fkey );
237 fclose( fkey );
238 }
239 else
240 {
241 if( memcmp( argv[6], "hex:", 4 ) == 0 )
242 {
243 p = &argv[6][4];
244 keylen = 0;
245
246 while( sscanf( p, "%02X", &n ) > 0 &&
247 keylen < (int) sizeof( key ) )
248 {
249 key[keylen++] = (unsigned char) n;
250 p += 2;
251 }
252 }
253 else
254 {
255 keylen = strlen( argv[6] );
256
257 if( keylen > (int) sizeof( key ) )
258 keylen = (int) sizeof( key );
259
260 memcpy( key, argv[6], keylen );
261 }
262 }
263
Paul Bakkercce9d772011-11-18 14:26:47 +0000264#if defined(_WIN32_WCE)
265 filesize = fseek( fin, 0L, SEEK_END );
266#else
267#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000268 /*
269 * Support large files (> 2Gb) on Win32
270 */
271 li_size.QuadPart = 0;
272 li_size.LowPart =
273 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
274 li_size.LowPart, &li_size.HighPart, FILE_END );
275
276 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000279 goto exit;
280 }
281
282 filesize = li_size.QuadPart;
283#else
284 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
285 {
286 perror( "lseek" );
287 goto exit;
288 }
289#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000290#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000291
292 if( fseek( fin, 0, SEEK_SET ) < 0 )
293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000295 goto exit;
296 }
297
298 if( mode == MODE_ENCRYPT )
299 {
300 /*
301 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200302 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000303 */
304 for( i = 0; i < 8; i++ )
305 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
306
307 p = argv[2];
308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_md_starts( &md_ctx );
310 mbedtls_md_update( &md_ctx, buffer, 8 );
311 mbedtls_md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
312 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000313
314 memcpy( IV, digest, 16 );
315
316 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000317 * Append the IV at the beginning of the output.
318 */
319 if( fwrite( IV, 1, 16, fout ) != 16 )
320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000322 goto exit;
323 }
324
325 /*
326 * Hash the IV and the secret key together 8192 times
327 * using the result to setup the AES context and HMAC.
328 */
329 memset( digest, 0, 32 );
330 memcpy( digest, IV, 16 );
331
332 for( i = 0; i < 8192; i++ )
333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_md_starts( &md_ctx );
335 mbedtls_md_update( &md_ctx, digest, 32 );
336 mbedtls_md_update( &md_ctx, key, keylen );
337 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000338
339 }
340
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200341 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000345 goto exit;
346 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200350 goto exit;
351 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000355 goto exit;
356 }
Paul Bakker20a78082011-01-21 09:32:12 +0000357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000359
360 /*
361 * Encrypt and write the ciphertext.
362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
366 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000367
Paul Bakker25b5fe52011-05-26 14:02:58 +0000368 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000371 goto exit;
372 }
373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200377 goto exit;
378 }
379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000381
Paul Bakker2c0994e2011-05-25 13:51:57 +0000382 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000385 goto exit;
386 }
387 }
388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000392 goto exit;
393 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000395
Paul Bakker2c0994e2011-05-25 13:51:57 +0000396 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000399 goto exit;
400 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000401
Paul Bakker20a78082011-01-21 09:32:12 +0000402 /*
403 * Finally write the HMAC.
404 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000410 goto exit;
411 }
412 }
413
414 if( mode == MODE_DECRYPT )
415 {
416 /*
417 * The encrypted file must be structured as follows:
418 *
419 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200420 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000421 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200422 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
423 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000428 goto exit;
429 }
430
Janos Follath8eb64132016-06-03 15:40:57 +0100431 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
432 {
Janos Follath352dbe22016-06-07 10:29:05 +0100433 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100434 goto exit;
435 }
436
437 /*
438 * Check the file size.
439 */
Paul Bakker243f48e2016-09-02 22:44:09 +0200440 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
441 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
445 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000446 goto exit;
447 }
448
449 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100450 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000453
454 /*
455 * Read the IV and original filesize modulo 16.
456 */
457 if( fread( buffer, 1, 16, fin ) != 16 )
458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000460 goto exit;
461 }
462
463 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000464
465 /*
466 * Hash the IV and the secret key together 8192 times
467 * using the result to setup the AES context and HMAC.
468 */
469 memset( digest, 0, 32 );
470 memcpy( digest, IV, 16 );
471
472 for( i = 0; i < 8192; i++ )
473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_md_starts( &md_ctx );
475 mbedtls_md_update( &md_ctx, digest, 32 );
476 mbedtls_md_update( &md_ctx, key, keylen );
477 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000478 }
479
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200480 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200484 goto exit;
485 }
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200490 goto exit;
491 }
492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200496 goto exit;
497 }
Paul Bakker20a78082011-01-21 09:32:12 +0000498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000500
501 /*
502 * Decrypt and write the plaintext.
503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000505 {
Paul Bakker243f48e2016-09-02 22:44:09 +0200506 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
507 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
508
509 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n",
512 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000513 goto exit;
514 }
515
Paul Bakker243f48e2016-09-02 22:44:09 +0200516 mbedtls_md_hmac_update( &md_ctx, buffer, ilen );
517 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
518 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200521 goto exit;
522 }
Paul Bakker20a78082011-01-21 09:32:12 +0000523
Paul Bakker2c0994e2011-05-25 13:51:57 +0000524 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000527 goto exit;
528 }
529 }
530
531 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000532 * Verify the message authentication code.
533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000539 goto exit;
540 }
541
Paul Bakker424cd692013-10-31 14:22:08 +0100542 /* Use constant-time buffer comparison */
543 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100545 diff |= digest[i] ^ buffer[i];
546
547 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000550 "or file corrupted.\n" );
551 goto exit;
552 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100553
554 /*
555 * Write the final block of data
556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_cipher_finish( &cipher_ctx, output, &olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100558
559 if( fwrite( output, 1, olen, fout ) != olen )
560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100562 goto exit;
563 }
Paul Bakker20a78082011-01-21 09:32:12 +0000564 }
565
Andres Amaya Garcia990900f2018-04-29 19:11:26 +0100566 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000567
568exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000569 if( fin )
570 fclose( fin );
571 if( fout )
572 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000573
Hanno Beckerf601ec52017-06-27 08:22:17 +0100574 /* Zeroize all command line arguments to also cover
575 the case when the user has missed or reordered some,
576 in which case the key might not be in argv[6]. */
577 for( i = 0; i < argc; i++ )
Hanno Becker9a1a1512018-10-12 16:46:37 +0100578 mbedtls_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerf601ec52017-06-27 08:22:17 +0100579
Hanno Becker9a1a1512018-10-12 16:46:37 +0100580 mbedtls_zeroize( IV, sizeof( IV ) );
581 mbedtls_zeroize( key, sizeof( key ) );
582 mbedtls_zeroize( buffer, sizeof( buffer ) );
583 mbedtls_zeroize( output, sizeof( output ) );
584 mbedtls_zeroize( digest, sizeof( digest ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_cipher_free( &cipher_ctx );
587 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000588
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200589 mbedtls_exit( exit_code );
Paul Bakker20a78082011-01-21 09:32:12 +0000590}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */