blob: 542b37ef83b0d049867235f712c3c5e38a9a53b8 [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
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010041#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010042#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
43#endif /* MBEDTLS_PLATFORM_C */
Rich Evans18b78c72015-02-11 14:06:19 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
46 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/cipher.h"
48#include "mbedtls/md.h"
Hanno Becker0b44d5c2018-10-12 16:46:37 +010049#include "mbedtls/platform_util.h"
Rich Evans18b78c72015-02-11 14:06:19 +000050
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000054#endif
55
Paul Bakker494c0b82011-04-24 15:30:07 +000056#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000057#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000058#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000059#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000060#endif
Paul Bakker20a78082011-01-21 09:32:12 +000061#else
62#include <sys/types.h>
63#include <unistd.h>
64#endif
65
Paul Bakker20a78082011-01-21 09:32:12 +000066#define MODE_ENCRYPT 0
67#define MODE_DECRYPT 1
68
69#define USAGE \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
Paul Bakker20a78082011-01-21 09:32:12 +000071 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
72 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
73 "\n"
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
76 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000077int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000078{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 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 +000080 return( 0 );
81}
82#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000083
84#if defined( MBEDTLS_CHECK_PARAMS ) && defined(MBEDTLS_PLATFORM_C)
85void mbedtls_param_failed( char* failure_condition, char* file, int line )
86{
87 mbedtls_printf("%s:%i: Input param failed - %s\n", file, line,
88 failure_condition );
89 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
90}
91#endif
92
Paul Bakker20a78082011-01-21 09:32:12 +000093int main( int argc, char *argv[] )
94{
Paul Bakker25b5fe52011-05-26 14:02:58 +000095 int ret = 1, i, n;
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010096 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +020097 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +000098 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000099 FILE *fkey, *fin = NULL, *fout = NULL;
100
101 char *p;
102 unsigned char IV[16];
103 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +0000105 unsigned char buffer[1024];
106 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +0100107 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +0000108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 const mbedtls_cipher_info_t *cipher_info;
110 const mbedtls_md_info_t *md_info;
111 mbedtls_cipher_context_t cipher_ctx;
112 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +0000113#if defined(_WIN32_WCE)
114 long filesize, offset;
115#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000116 LARGE_INTEGER li_size;
117 __int64 filesize, offset;
118#else
119 off_t filesize, offset;
120#endif
121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_cipher_init( &cipher_ctx );
123 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000124
125 /*
126 * Parse the command-line arguments.
127 */
128 if( argc != 7 )
129 {
130 const int *list;
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_printf( "Available ciphers:\n" );
135 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000136 while( *list )
137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 cipher_info = mbedtls_cipher_info_from_type( *list );
139 mbedtls_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000140 list++;
141 }
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_printf( "\nAvailable message digests:\n" );
144 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000145 while( *list )
146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 md_info = mbedtls_md_info_from_type( *list );
148 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000149 list++;
150 }
151
Paul Bakkercce9d772011-11-18 14:26:47 +0000152#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000154 fflush( stdout ); getchar();
155#endif
156
157 goto exit;
158 }
159
160 mode = atoi( argv[1] );
161
162 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000165 goto exit;
166 }
167
168 if( strcmp( argv[2], argv[3] ) == 0 )
169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000171 goto exit;
172 }
173
174 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000177 goto exit;
178 }
179
180 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000183 goto exit;
184 }
185
186 /*
187 * Read the Cipher and MD from the command line
188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000190 if( cipher_info == NULL )
191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000193 goto exit;
194 }
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200195 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200196 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200197 mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200198 goto exit;
199 }
Paul Bakker20a78082011-01-21 09:32:12 +0000200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 md_info = mbedtls_md_info_from_string( argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000202 if( md_info == NULL )
203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000205 goto exit;
206 }
Janos Follath8eb64132016-06-03 15:40:57 +0100207
208 if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
209 {
Janos Follath352dbe22016-06-07 10:29:05 +0100210 mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100211 goto exit;
212 }
Paul Bakker20a78082011-01-21 09:32:12 +0000213
214 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100215 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000216 */
217 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
218 {
219 keylen = fread( key, 1, sizeof( key ), fkey );
220 fclose( fkey );
221 }
222 else
223 {
224 if( memcmp( argv[6], "hex:", 4 ) == 0 )
225 {
226 p = &argv[6][4];
227 keylen = 0;
228
229 while( sscanf( p, "%02X", &n ) > 0 &&
230 keylen < (int) sizeof( key ) )
231 {
232 key[keylen++] = (unsigned char) n;
233 p += 2;
234 }
235 }
236 else
237 {
238 keylen = strlen( argv[6] );
239
240 if( keylen > (int) sizeof( key ) )
241 keylen = (int) sizeof( key );
242
243 memcpy( key, argv[6], keylen );
244 }
245 }
246
Paul Bakkercce9d772011-11-18 14:26:47 +0000247#if defined(_WIN32_WCE)
248 filesize = fseek( fin, 0L, SEEK_END );
249#else
250#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000251 /*
252 * Support large files (> 2Gb) on Win32
253 */
254 li_size.QuadPart = 0;
255 li_size.LowPart =
256 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
257 li_size.LowPart, &li_size.HighPart, FILE_END );
258
259 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000262 goto exit;
263 }
264
265 filesize = li_size.QuadPart;
266#else
267 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
268 {
269 perror( "lseek" );
270 goto exit;
271 }
272#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000273#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000274
275 if( fseek( fin, 0, SEEK_SET ) < 0 )
276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000278 goto exit;
279 }
280
281 if( mode == MODE_ENCRYPT )
282 {
283 /*
284 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200285 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000286 */
287 for( i = 0; i < 8; i++ )
288 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
289
290 p = argv[2];
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_md_starts( &md_ctx );
293 mbedtls_md_update( &md_ctx, buffer, 8 );
294 mbedtls_md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
295 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000296
297 memcpy( IV, digest, 16 );
298
299 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000300 * Append the IV at the beginning of the output.
301 */
302 if( fwrite( IV, 1, 16, fout ) != 16 )
303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000305 goto exit;
306 }
307
308 /*
309 * Hash the IV and the secret key together 8192 times
310 * using the result to setup the AES context and HMAC.
311 */
312 memset( digest, 0, 32 );
313 memcpy( digest, IV, 16 );
314
315 for( i = 0; i < 8192; i++ )
316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_md_starts( &md_ctx );
318 mbedtls_md_update( &md_ctx, digest, 32 );
319 mbedtls_md_update( &md_ctx, key, keylen );
320 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000321
322 }
323
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200324 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000328 goto exit;
329 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200333 goto exit;
334 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000338 goto exit;
339 }
Paul Bakker20a78082011-01-21 09:32:12 +0000340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000342
343 /*
344 * Encrypt and write the ciphertext.
345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
349 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000350
Paul Bakker25b5fe52011-05-26 14:02:58 +0000351 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000354 goto exit;
355 }
356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200360 goto exit;
361 }
362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000364
Paul Bakker2c0994e2011-05-25 13:51:57 +0000365 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000368 goto exit;
369 }
370 }
371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000375 goto exit;
376 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000378
Paul Bakker2c0994e2011-05-25 13:51:57 +0000379 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000382 goto exit;
383 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000384
Paul Bakker20a78082011-01-21 09:32:12 +0000385 /*
386 * Finally write the HMAC.
387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000393 goto exit;
394 }
395 }
396
397 if( mode == MODE_DECRYPT )
398 {
399 /*
400 * The encrypted file must be structured as follows:
401 *
402 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200403 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000404 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200405 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
406 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000411 goto exit;
412 }
413
Janos Follath8eb64132016-06-03 15:40:57 +0100414 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
415 {
Janos Follath352dbe22016-06-07 10:29:05 +0100416 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100417 goto exit;
418 }
419
420 /*
421 * Check the file size.
422 */
Paul Bakker243f48e2016-09-02 22:44:09 +0200423 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
424 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
428 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000429 goto exit;
430 }
431
432 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100433 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000436
437 /*
438 * Read the IV and original filesize modulo 16.
439 */
440 if( fread( buffer, 1, 16, fin ) != 16 )
441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000443 goto exit;
444 }
445
446 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000447
448 /*
449 * Hash the IV and the secret key together 8192 times
450 * using the result to setup the AES context and HMAC.
451 */
452 memset( digest, 0, 32 );
453 memcpy( digest, IV, 16 );
454
455 for( i = 0; i < 8192; i++ )
456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_md_starts( &md_ctx );
458 mbedtls_md_update( &md_ctx, digest, 32 );
459 mbedtls_md_update( &md_ctx, key, keylen );
460 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000461 }
462
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200463 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() 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_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200473 goto exit;
474 }
475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200479 goto exit;
480 }
Paul Bakker20a78082011-01-21 09:32:12 +0000481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000483
484 /*
485 * Decrypt and write the plaintext.
486 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000488 {
Paul Bakker243f48e2016-09-02 22:44:09 +0200489 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
490 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
491
492 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n",
495 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000496 goto exit;
497 }
498
Paul Bakker243f48e2016-09-02 22:44:09 +0200499 mbedtls_md_hmac_update( &md_ctx, buffer, ilen );
500 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
501 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200504 goto exit;
505 }
Paul Bakker20a78082011-01-21 09:32:12 +0000506
Paul Bakker2c0994e2011-05-25 13:51:57 +0000507 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000510 goto exit;
511 }
512 }
513
514 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000515 * Verify the message authentication code.
516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000522 goto exit;
523 }
524
Paul Bakker424cd692013-10-31 14:22:08 +0100525 /* Use constant-time buffer comparison */
526 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100528 diff |= digest[i] ^ buffer[i];
529
530 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000533 "or file corrupted.\n" );
534 goto exit;
535 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100536
537 /*
538 * Write the final block of data
539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_cipher_finish( &cipher_ctx, output, &olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100541
542 if( fwrite( output, 1, olen, fout ) != olen )
543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100545 goto exit;
546 }
Paul Bakker20a78082011-01-21 09:32:12 +0000547 }
548
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100549 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000550
551exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000552 if( fin )
553 fclose( fin );
554 if( fout )
555 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000556
Hanno Beckerf601ec52017-06-27 08:22:17 +0100557 /* Zeroize all command line arguments to also cover
558 the case when the user has missed or reordered some,
559 in which case the key might not be in argv[6]. */
560 for( i = 0; i < argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100561 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerf601ec52017-06-27 08:22:17 +0100562
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100563 mbedtls_platform_zeroize( IV, sizeof( IV ) );
564 mbedtls_platform_zeroize( key, sizeof( key ) );
565 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
566 mbedtls_platform_zeroize( output, sizeof( output ) );
567 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_cipher_free( &cipher_ctx );
570 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000571
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100572 return( exit_code );
Paul Bakker20a78082011-01-21 09:32:12 +0000573}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */