blob: f58e6166dca845ab12d53148c6e3684fab45a843 [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. */
26#define _POSIX_C_SOURCE 1
27
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");
Paul Bakker5690efc2011-05-26 13:16:06 +000081 return( 0 );
82}
83#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000084
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010085#if defined(MBEDTLS_CHECK_PARAMS)
86#include "mbedtls/platform_util.h"
87void mbedtls_param_failed( const char *failure_condition,
88 const char *file,
89 int line )
Simon Butcher63cb97e2018-12-06 17:43:31 +000090{
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010091 mbedtls_printf( "%s:%i: Input param failed - %s\n",
92 file, line, failure_condition );
Simon Butcher63cb97e2018-12-06 17:43:31 +000093 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
94}
95#endif
96
Paul Bakker20a78082011-01-21 09:32:12 +000097int main( int argc, char *argv[] )
98{
Paul Bakker25b5fe52011-05-26 14:02:58 +000099 int ret = 1, i, n;
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100100 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +0200101 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +0000102 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +0000103 FILE *fkey, *fin = NULL, *fout = NULL;
104
105 char *p;
106 unsigned char IV[16];
107 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +0000109 unsigned char buffer[1024];
110 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +0100111 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +0000112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 const mbedtls_cipher_info_t *cipher_info;
114 const mbedtls_md_info_t *md_info;
115 mbedtls_cipher_context_t cipher_ctx;
116 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +0000117#if defined(_WIN32_WCE)
118 long filesize, offset;
119#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000120 LARGE_INTEGER li_size;
121 __int64 filesize, offset;
122#else
123 off_t filesize, offset;
124#endif
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_cipher_init( &cipher_ctx );
127 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000128
129 /*
130 * Parse the command-line arguments.
131 */
132 if( argc != 7 )
133 {
134 const int *list;
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_printf( "Available ciphers:\n" );
139 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000140 while( *list )
141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 cipher_info = mbedtls_cipher_info_from_type( *list );
143 mbedtls_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000144 list++;
145 }
146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_printf( "\nAvailable message digests:\n" );
148 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000149 while( *list )
150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 md_info = mbedtls_md_info_from_type( *list );
152 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000153 list++;
154 }
155
Paul Bakkercce9d772011-11-18 14:26:47 +0000156#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000158 fflush( stdout ); getchar();
159#endif
160
161 goto exit;
162 }
163
164 mode = atoi( argv[1] );
165
166 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000169 goto exit;
170 }
171
172 if( strcmp( argv[2], argv[3] ) == 0 )
173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000175 goto exit;
176 }
177
178 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000181 goto exit;
182 }
183
184 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000187 goto exit;
188 }
189
190 /*
191 * Read the Cipher and MD from the command line
192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000194 if( cipher_info == NULL )
195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000197 goto exit;
198 }
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200199 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200200 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200201 mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200202 goto exit;
203 }
Paul Bakker20a78082011-01-21 09:32:12 +0000204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 md_info = mbedtls_md_info_from_string( argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000206 if( md_info == NULL )
207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000209 goto exit;
210 }
Janos Follath8eb64132016-06-03 15:40:57 +0100211
212 if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
213 {
Janos Follath352dbe22016-06-07 10:29:05 +0100214 mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100215 goto exit;
216 }
Paul Bakker20a78082011-01-21 09:32:12 +0000217
218 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100219 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000220 */
221 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
222 {
223 keylen = fread( key, 1, sizeof( key ), fkey );
224 fclose( fkey );
225 }
226 else
227 {
228 if( memcmp( argv[6], "hex:", 4 ) == 0 )
229 {
230 p = &argv[6][4];
231 keylen = 0;
232
233 while( sscanf( p, "%02X", &n ) > 0 &&
234 keylen < (int) sizeof( key ) )
235 {
236 key[keylen++] = (unsigned char) n;
237 p += 2;
238 }
239 }
240 else
241 {
242 keylen = strlen( argv[6] );
243
244 if( keylen > (int) sizeof( key ) )
245 keylen = (int) sizeof( key );
246
247 memcpy( key, argv[6], keylen );
248 }
249 }
250
Paul Bakkercce9d772011-11-18 14:26:47 +0000251#if defined(_WIN32_WCE)
252 filesize = fseek( fin, 0L, SEEK_END );
253#else
254#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000255 /*
256 * Support large files (> 2Gb) on Win32
257 */
258 li_size.QuadPart = 0;
259 li_size.LowPart =
260 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
261 li_size.LowPart, &li_size.HighPart, FILE_END );
262
263 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000266 goto exit;
267 }
268
269 filesize = li_size.QuadPart;
270#else
271 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
272 {
273 perror( "lseek" );
274 goto exit;
275 }
276#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000277#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000278
279 if( fseek( fin, 0, SEEK_SET ) < 0 )
280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000282 goto exit;
283 }
284
285 if( mode == MODE_ENCRYPT )
286 {
287 /*
288 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200289 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000290 */
291 for( i = 0; i < 8; i++ )
292 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
293
294 p = argv[2];
295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_md_starts( &md_ctx );
297 mbedtls_md_update( &md_ctx, buffer, 8 );
298 mbedtls_md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
299 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000300
301 memcpy( IV, digest, 16 );
302
303 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000304 * Append the IV at the beginning of the output.
305 */
306 if( fwrite( IV, 1, 16, fout ) != 16 )
307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000309 goto exit;
310 }
311
312 /*
313 * Hash the IV and the secret key together 8192 times
314 * using the result to setup the AES context and HMAC.
315 */
316 memset( digest, 0, 32 );
317 memcpy( digest, IV, 16 );
318
319 for( i = 0; i < 8192; i++ )
320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_md_starts( &md_ctx );
322 mbedtls_md_update( &md_ctx, digest, 32 );
323 mbedtls_md_update( &md_ctx, key, keylen );
324 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000325
326 }
327
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200328 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000332 goto exit;
333 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200337 goto exit;
338 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000342 goto exit;
343 }
Paul Bakker20a78082011-01-21 09:32:12 +0000344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000346
347 /*
348 * Encrypt and write the ciphertext.
349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
353 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000354
Paul Bakker25b5fe52011-05-26 14:02:58 +0000355 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000358 goto exit;
359 }
360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200364 goto exit;
365 }
366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000368
Paul Bakker2c0994e2011-05-25 13:51:57 +0000369 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000372 goto exit;
373 }
374 }
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000379 goto exit;
380 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000382
Paul Bakker2c0994e2011-05-25 13:51:57 +0000383 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000386 goto exit;
387 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000388
Paul Bakker20a78082011-01-21 09:32:12 +0000389 /*
390 * Finally write the HMAC.
391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000397 goto exit;
398 }
399 }
400
401 if( mode == MODE_DECRYPT )
402 {
403 /*
404 * The encrypted file must be structured as follows:
405 *
406 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200407 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000408 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200409 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
410 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000415 goto exit;
416 }
417
Janos Follath8eb64132016-06-03 15:40:57 +0100418 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
419 {
Janos Follath352dbe22016-06-07 10:29:05 +0100420 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100421 goto exit;
422 }
423
424 /*
425 * Check the file size.
426 */
Paul Bakker243f48e2016-09-02 22:44:09 +0200427 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
428 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
432 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000433 goto exit;
434 }
435
436 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100437 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000440
441 /*
442 * Read the IV and original filesize modulo 16.
443 */
444 if( fread( buffer, 1, 16, fin ) != 16 )
445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000447 goto exit;
448 }
449
450 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000451
452 /*
453 * Hash the IV and the secret key together 8192 times
454 * using the result to setup the AES context and HMAC.
455 */
456 memset( digest, 0, 32 );
457 memcpy( digest, IV, 16 );
458
459 for( i = 0; i < 8192; i++ )
460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_md_starts( &md_ctx );
462 mbedtls_md_update( &md_ctx, digest, 32 );
463 mbedtls_md_update( &md_ctx, key, keylen );
464 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000465 }
466
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200467 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200471 goto exit;
472 }
473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200477 goto exit;
478 }
479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200483 goto exit;
484 }
Paul Bakker20a78082011-01-21 09:32:12 +0000485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000487
488 /*
489 * Decrypt and write the plaintext.
490 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000492 {
Paul Bakker243f48e2016-09-02 22:44:09 +0200493 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
494 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
495
496 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n",
499 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000500 goto exit;
501 }
502
Paul Bakker243f48e2016-09-02 22:44:09 +0200503 mbedtls_md_hmac_update( &md_ctx, buffer, ilen );
504 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
505 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200508 goto exit;
509 }
Paul Bakker20a78082011-01-21 09:32:12 +0000510
Paul Bakker2c0994e2011-05-25 13:51:57 +0000511 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000514 goto exit;
515 }
516 }
517
518 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000519 * Verify the message authentication code.
520 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000526 goto exit;
527 }
528
Paul Bakker424cd692013-10-31 14:22:08 +0100529 /* Use constant-time buffer comparison */
530 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100532 diff |= digest[i] ^ buffer[i];
533
534 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000537 "or file corrupted.\n" );
538 goto exit;
539 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100540
541 /*
542 * Write the final block of data
543 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_cipher_finish( &cipher_ctx, output, &olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100545
546 if( fwrite( output, 1, olen, fout ) != olen )
547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100549 goto exit;
550 }
Paul Bakker20a78082011-01-21 09:32:12 +0000551 }
552
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100553 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000554
555exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000556 if( fin )
557 fclose( fin );
558 if( fout )
559 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000560
Hanno Beckerf601ec52017-06-27 08:22:17 +0100561 /* Zeroize all command line arguments to also cover
562 the case when the user has missed or reordered some,
563 in which case the key might not be in argv[6]. */
564 for( i = 0; i < argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100565 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerf601ec52017-06-27 08:22:17 +0100566
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100567 mbedtls_platform_zeroize( IV, sizeof( IV ) );
568 mbedtls_platform_zeroize( key, sizeof( key ) );
569 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
570 mbedtls_platform_zeroize( output, sizeof( output ) );
571 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_cipher_free( &cipher_ctx );
574 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000575
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100576 return( exit_code );
Paul Bakker20a78082011-01-21 09:32:12 +0000577}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */