blob: f9cf6b2bb060306c8d79403c3bb2e169f9a501a8 [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
Jarno Lamsa642596e2019-10-04 12:52:42 +030085#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
86int mbedtls_hardware_poll( void *data, unsigned char *output,
87 size_t len, size_t *olen )
88{
89 size_t i;
90 (void) data;
91 for( i = 0; i < len; ++i )
92 output[i] = rand();
93 *olen = len;
94 return( 0 );
95}
96#endif
Simon Butcher63cb97e2018-12-06 17:43:31 +000097
Paul Bakker20a78082011-01-21 09:32:12 +000098int main( int argc, char *argv[] )
99{
Paul Bakker25b5fe52011-05-26 14:02:58 +0000100 int ret = 1, i, n;
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100101 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +0200102 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +0000103 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +0000104 FILE *fkey, *fin = NULL, *fout = NULL;
105
106 char *p;
107 unsigned char IV[16];
108 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +0000110 unsigned char buffer[1024];
111 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +0100112 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +0000113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 const mbedtls_cipher_info_t *cipher_info;
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100115 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_cipher_context_t cipher_ctx;
117 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +0000118#if defined(_WIN32_WCE)
119 long filesize, offset;
120#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000121 LARGE_INTEGER li_size;
122 __int64 filesize, offset;
123#else
124 off_t filesize, offset;
125#endif
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_cipher_init( &cipher_ctx );
128 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000129
130 /*
131 * Parse the command-line arguments.
132 */
133 if( argc != 7 )
134 {
135 const int *list;
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_printf( "Available ciphers:\n" );
140 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000141 while( *list )
142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 cipher_info = mbedtls_cipher_info_from_type( *list );
144 mbedtls_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000145 list++;
146 }
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_printf( "\nAvailable message digests:\n" );
149 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000150 while( *list )
151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 md_info = mbedtls_md_info_from_type( *list );
153 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000154 list++;
155 }
156
Paul Bakkercce9d772011-11-18 14:26:47 +0000157#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000159 fflush( stdout ); getchar();
160#endif
161
162 goto exit;
163 }
164
165 mode = atoi( argv[1] );
166
167 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000170 goto exit;
171 }
172
173 if( strcmp( argv[2], argv[3] ) == 0 )
174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000176 goto exit;
177 }
178
179 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000182 goto exit;
183 }
184
185 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000188 goto exit;
189 }
190
191 /*
192 * Read the Cipher and MD from the command line
193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000195 if( cipher_info == NULL )
196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000198 goto exit;
199 }
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200200 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200201 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200202 mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200203 goto exit;
204 }
Paul Bakker20a78082011-01-21 09:32:12 +0000205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 md_info = mbedtls_md_info_from_string( argv[5] );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100207 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Paul Bakker20a78082011-01-21 09:32:12 +0000208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000210 goto exit;
211 }
Janos Follath8eb64132016-06-03 15:40:57 +0100212
213 if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
214 {
Janos Follath352dbe22016-06-07 10:29:05 +0100215 mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100216 goto exit;
217 }
Paul Bakker20a78082011-01-21 09:32:12 +0000218
219 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100220 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000221 */
222 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
223 {
224 keylen = fread( key, 1, sizeof( key ), fkey );
225 fclose( fkey );
226 }
227 else
228 {
229 if( memcmp( argv[6], "hex:", 4 ) == 0 )
230 {
231 p = &argv[6][4];
232 keylen = 0;
233
234 while( sscanf( p, "%02X", &n ) > 0 &&
235 keylen < (int) sizeof( key ) )
236 {
237 key[keylen++] = (unsigned char) n;
238 p += 2;
239 }
240 }
241 else
242 {
243 keylen = strlen( argv[6] );
244
245 if( keylen > (int) sizeof( key ) )
246 keylen = (int) sizeof( key );
247
248 memcpy( key, argv[6], keylen );
249 }
250 }
251
Paul Bakkercce9d772011-11-18 14:26:47 +0000252#if defined(_WIN32_WCE)
253 filesize = fseek( fin, 0L, SEEK_END );
254#else
255#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000256 /*
257 * Support large files (> 2Gb) on Win32
258 */
259 li_size.QuadPart = 0;
260 li_size.LowPart =
261 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
262 li_size.LowPart, &li_size.HighPart, FILE_END );
263
264 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000267 goto exit;
268 }
269
270 filesize = li_size.QuadPart;
271#else
272 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
273 {
274 perror( "lseek" );
275 goto exit;
276 }
277#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000278#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000279
280 if( fseek( fin, 0, SEEK_SET ) < 0 )
281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000283 goto exit;
284 }
285
286 if( mode == MODE_ENCRYPT )
287 {
288 /*
289 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200290 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000291 */
292 for( i = 0; i < 8; i++ )
293 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
294
295 p = argv[2];
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_md_starts( &md_ctx );
298 mbedtls_md_update( &md_ctx, buffer, 8 );
299 mbedtls_md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
300 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000301
302 memcpy( IV, digest, 16 );
303
304 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000305 * Append the IV at the beginning of the output.
306 */
307 if( fwrite( IV, 1, 16, fout ) != 16 )
308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000310 goto exit;
311 }
312
313 /*
314 * Hash the IV and the secret key together 8192 times
315 * using the result to setup the AES context and HMAC.
316 */
317 memset( digest, 0, 32 );
318 memcpy( digest, IV, 16 );
319
320 for( i = 0; i < 8192; i++ )
321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 mbedtls_md_starts( &md_ctx );
323 mbedtls_md_update( &md_ctx, digest, 32 );
324 mbedtls_md_update( &md_ctx, key, keylen );
325 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000326
327 }
328
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200329 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000333 goto exit;
334 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200338 goto exit;
339 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000343 goto exit;
344 }
Paul Bakker20a78082011-01-21 09:32:12 +0000345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000347
348 /*
349 * Encrypt and write the ciphertext.
350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
354 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000355
Paul Bakker25b5fe52011-05-26 14:02:58 +0000356 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000359 goto exit;
360 }
361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200365 goto exit;
366 }
367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000369
Paul Bakker2c0994e2011-05-25 13:51:57 +0000370 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000373 goto exit;
374 }
375 }
376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000380 goto exit;
381 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000383
Paul Bakker2c0994e2011-05-25 13:51:57 +0000384 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000387 goto exit;
388 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000389
Paul Bakker20a78082011-01-21 09:32:12 +0000390 /*
391 * Finally write the HMAC.
392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000398 goto exit;
399 }
400 }
401
402 if( mode == MODE_DECRYPT )
403 {
404 /*
405 * The encrypted file must be structured as follows:
406 *
407 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200408 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000409 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200410 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
411 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000416 goto exit;
417 }
418
Janos Follath8eb64132016-06-03 15:40:57 +0100419 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
420 {
Janos Follath352dbe22016-06-07 10:29:05 +0100421 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100422 goto exit;
423 }
424
425 /*
426 * Check the file size.
427 */
Paul Bakker243f48e2016-09-02 22:44:09 +0200428 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
429 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
433 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000434 goto exit;
435 }
436
437 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100438 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000441
442 /*
443 * Read the IV and original filesize modulo 16.
444 */
445 if( fread( buffer, 1, 16, fin ) != 16 )
446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000448 goto exit;
449 }
450
451 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000452
453 /*
454 * Hash the IV and the secret key together 8192 times
455 * using the result to setup the AES context and HMAC.
456 */
457 memset( digest, 0, 32 );
458 memcpy( digest, IV, 16 );
459
460 for( i = 0; i < 8192; i++ )
461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_md_starts( &md_ctx );
463 mbedtls_md_update( &md_ctx, digest, 32 );
464 mbedtls_md_update( &md_ctx, key, keylen );
465 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000466 }
467
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200468 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200472 goto exit;
473 }
474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200478 goto exit;
479 }
480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200484 goto exit;
485 }
Paul Bakker20a78082011-01-21 09:32:12 +0000486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000488
489 /*
490 * Decrypt and write the plaintext.
491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000493 {
Paul Bakker243f48e2016-09-02 22:44:09 +0200494 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
495 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
496
497 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n",
500 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000501 goto exit;
502 }
503
Paul Bakker243f48e2016-09-02 22:44:09 +0200504 mbedtls_md_hmac_update( &md_ctx, buffer, ilen );
505 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
506 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200509 goto exit;
510 }
Paul Bakker20a78082011-01-21 09:32:12 +0000511
Paul Bakker2c0994e2011-05-25 13:51:57 +0000512 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000515 goto exit;
516 }
517 }
518
519 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000520 * Verify the message authentication code.
521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000527 goto exit;
528 }
529
Paul Bakker424cd692013-10-31 14:22:08 +0100530 /* Use constant-time buffer comparison */
531 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100533 diff |= digest[i] ^ buffer[i];
534
535 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000538 "or file corrupted.\n" );
539 goto exit;
540 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100541
542 /*
543 * Write the final block of data
544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_cipher_finish( &cipher_ctx, output, &olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100546
547 if( fwrite( output, 1, olen, fout ) != olen )
548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100550 goto exit;
551 }
Paul Bakker20a78082011-01-21 09:32:12 +0000552 }
553
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100554 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000555
556exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000557 if( fin )
558 fclose( fin );
559 if( fout )
560 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000561
Hanno Beckerf601ec52017-06-27 08:22:17 +0100562 /* Zeroize all command line arguments to also cover
563 the case when the user has missed or reordered some,
564 in which case the key might not be in argv[6]. */
565 for( i = 0; i < argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100566 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerf601ec52017-06-27 08:22:17 +0100567
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100568 mbedtls_platform_zeroize( IV, sizeof( IV ) );
569 mbedtls_platform_zeroize( key, sizeof( key ) );
570 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
571 mbedtls_platform_zeroize( output, sizeof( output ) );
572 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_cipher_free( &cipher_ctx );
575 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000576
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100577 return( exit_code );
Paul Bakker20a78082011-01-21 09:32:12 +0000578}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */