blob: e2949f25be7656e862af79f0832939aaeee802f5 [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
Simon Butcher63cb97e2018-12-06 17:43:31 +000085
Paul Bakker20a78082011-01-21 09:32:12 +000086int main( int argc, char *argv[] )
87{
Paul Bakker25b5fe52011-05-26 14:02:58 +000088 int ret = 1, i, n;
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010089 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +020090 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +000091 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000092 FILE *fkey, *fin = NULL, *fout = NULL;
93
94 char *p;
95 unsigned char IV[16];
96 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +000098 unsigned char buffer[1024];
99 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +0100100 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +0000101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 const mbedtls_cipher_info_t *cipher_info;
103 const mbedtls_md_info_t *md_info;
104 mbedtls_cipher_context_t cipher_ctx;
105 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +0000106#if defined(_WIN32_WCE)
107 long filesize, offset;
108#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000109 LARGE_INTEGER li_size;
110 __int64 filesize, offset;
111#else
112 off_t filesize, offset;
113#endif
114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_cipher_init( &cipher_ctx );
116 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000117
118 /*
119 * Parse the command-line arguments.
120 */
121 if( argc != 7 )
122 {
123 const int *list;
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_printf( "Available ciphers:\n" );
128 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000129 while( *list )
130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 cipher_info = mbedtls_cipher_info_from_type( *list );
132 mbedtls_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000133 list++;
134 }
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_printf( "\nAvailable message digests:\n" );
137 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000138 while( *list )
139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 md_info = mbedtls_md_info_from_type( *list );
141 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000142 list++;
143 }
144
Paul Bakkercce9d772011-11-18 14:26:47 +0000145#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000147 fflush( stdout ); getchar();
148#endif
149
150 goto exit;
151 }
152
153 mode = atoi( argv[1] );
154
155 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000158 goto exit;
159 }
160
161 if( strcmp( argv[2], argv[3] ) == 0 )
162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000164 goto exit;
165 }
166
167 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000170 goto exit;
171 }
172
173 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000176 goto exit;
177 }
178
179 /*
180 * Read the Cipher and MD from the command line
181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000183 if( cipher_info == NULL )
184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000186 goto exit;
187 }
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200188 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200189 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200190 mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200191 goto exit;
192 }
Paul Bakker20a78082011-01-21 09:32:12 +0000193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 md_info = mbedtls_md_info_from_string( argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000195 if( md_info == NULL )
196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000198 goto exit;
199 }
Janos Follath8eb64132016-06-03 15:40:57 +0100200
201 if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
202 {
Janos Follath352dbe22016-06-07 10:29:05 +0100203 mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100204 goto exit;
205 }
Paul Bakker20a78082011-01-21 09:32:12 +0000206
207 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100208 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000209 */
210 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
211 {
212 keylen = fread( key, 1, sizeof( key ), fkey );
213 fclose( fkey );
214 }
215 else
216 {
217 if( memcmp( argv[6], "hex:", 4 ) == 0 )
218 {
219 p = &argv[6][4];
220 keylen = 0;
221
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200222 while( sscanf( p, "%02X", (unsigned int*) &n ) > 0 &&
Paul Bakker20a78082011-01-21 09:32:12 +0000223 keylen < (int) sizeof( key ) )
224 {
225 key[keylen++] = (unsigned char) n;
226 p += 2;
227 }
228 }
229 else
230 {
231 keylen = strlen( argv[6] );
232
233 if( keylen > (int) sizeof( key ) )
234 keylen = (int) sizeof( key );
235
236 memcpy( key, argv[6], keylen );
237 }
238 }
239
Paul Bakkercce9d772011-11-18 14:26:47 +0000240#if defined(_WIN32_WCE)
241 filesize = fseek( fin, 0L, SEEK_END );
242#else
243#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000244 /*
245 * Support large files (> 2Gb) on Win32
246 */
247 li_size.QuadPart = 0;
248 li_size.LowPart =
249 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
250 li_size.LowPart, &li_size.HighPart, FILE_END );
251
252 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000255 goto exit;
256 }
257
258 filesize = li_size.QuadPart;
259#else
260 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
261 {
262 perror( "lseek" );
263 goto exit;
264 }
265#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000266#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000267
268 if( fseek( fin, 0, SEEK_SET ) < 0 )
269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000271 goto exit;
272 }
273
274 if( mode == MODE_ENCRYPT )
275 {
276 /*
277 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200278 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000279 */
280 for( i = 0; i < 8; i++ )
281 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
282
283 p = argv[2];
284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_md_starts( &md_ctx );
286 mbedtls_md_update( &md_ctx, buffer, 8 );
287 mbedtls_md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
288 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000289
290 memcpy( IV, digest, 16 );
291
292 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000293 * Append the IV at the beginning of the output.
294 */
295 if( fwrite( IV, 1, 16, fout ) != 16 )
296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000298 goto exit;
299 }
300
301 /*
302 * Hash the IV and the secret key together 8192 times
303 * using the result to setup the AES context and HMAC.
304 */
305 memset( digest, 0, 32 );
306 memcpy( digest, IV, 16 );
307
308 for( i = 0; i < 8192; i++ )
309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 mbedtls_md_starts( &md_ctx );
311 mbedtls_md_update( &md_ctx, digest, 32 );
312 mbedtls_md_update( &md_ctx, key, keylen );
313 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000314
315 }
316
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200317 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000321 goto exit;
322 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200326 goto exit;
327 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000331 goto exit;
332 }
Paul Bakker20a78082011-01-21 09:32:12 +0000333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000335
336 /*
337 * Encrypt and write the ciphertext.
338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
342 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000343
Paul Bakker25b5fe52011-05-26 14:02:58 +0000344 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000347 goto exit;
348 }
349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200353 goto exit;
354 }
355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000357
Paul Bakker2c0994e2011-05-25 13:51:57 +0000358 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000361 goto exit;
362 }
363 }
364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000368 goto exit;
369 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000371
Paul Bakker2c0994e2011-05-25 13:51:57 +0000372 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000375 goto exit;
376 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000377
Paul Bakker20a78082011-01-21 09:32:12 +0000378 /*
379 * Finally write the HMAC.
380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000386 goto exit;
387 }
388 }
389
390 if( mode == MODE_DECRYPT )
391 {
392 /*
393 * The encrypted file must be structured as follows:
394 *
395 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200396 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000397 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200398 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
399 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000404 goto exit;
405 }
406
Janos Follath8eb64132016-06-03 15:40:57 +0100407 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
408 {
Janos Follath352dbe22016-06-07 10:29:05 +0100409 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100410 goto exit;
411 }
412
413 /*
414 * Check the file size.
415 */
Paul Bakker243f48e2016-09-02 22:44:09 +0200416 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
417 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000419 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200420 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%u).\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000422 goto exit;
423 }
424
425 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100426 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000429
430 /*
431 * Read the IV and original filesize modulo 16.
432 */
433 if( fread( buffer, 1, 16, fin ) != 16 )
434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000436 goto exit;
437 }
438
439 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000440
441 /*
442 * Hash the IV and the secret key together 8192 times
443 * using the result to setup the AES context and HMAC.
444 */
445 memset( digest, 0, 32 );
446 memcpy( digest, IV, 16 );
447
448 for( i = 0; i < 8192; i++ )
449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_md_starts( &md_ctx );
451 mbedtls_md_update( &md_ctx, digest, 32 );
452 mbedtls_md_update( &md_ctx, key, keylen );
453 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000454 }
455
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200456 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200460 goto exit;
461 }
462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200466 goto exit;
467 }
468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200472 goto exit;
473 }
Paul Bakker20a78082011-01-21 09:32:12 +0000474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000476
477 /*
478 * Decrypt and write the plaintext.
479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000481 {
Paul Bakker243f48e2016-09-02 22:44:09 +0200482 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
483 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
484
485 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000486 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200487 mbedtls_fprintf( stderr, "fread(%u bytes) failed\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000489 goto exit;
490 }
491
Paul Bakker243f48e2016-09-02 22:44:09 +0200492 mbedtls_md_hmac_update( &md_ctx, buffer, ilen );
493 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
494 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200497 goto exit;
498 }
Paul Bakker20a78082011-01-21 09:32:12 +0000499
Paul Bakker2c0994e2011-05-25 13:51:57 +0000500 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000503 goto exit;
504 }
505 }
506
507 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000508 * Verify the message authentication code.
509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000515 goto exit;
516 }
517
Paul Bakker424cd692013-10-31 14:22:08 +0100518 /* Use constant-time buffer comparison */
519 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100521 diff |= digest[i] ^ buffer[i];
522
523 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000526 "or file corrupted.\n" );
527 goto exit;
528 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100529
530 /*
531 * Write the final block of data
532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_cipher_finish( &cipher_ctx, output, &olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100534
535 if( fwrite( output, 1, olen, fout ) != olen )
536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100538 goto exit;
539 }
Paul Bakker20a78082011-01-21 09:32:12 +0000540 }
541
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100542 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000543
544exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000545 if( fin )
546 fclose( fin );
547 if( fout )
548 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000549
Hanno Beckerf601ec52017-06-27 08:22:17 +0100550 /* Zeroize all command line arguments to also cover
551 the case when the user has missed or reordered some,
552 in which case the key might not be in argv[6]. */
553 for( i = 0; i < argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100554 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerf601ec52017-06-27 08:22:17 +0100555
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100556 mbedtls_platform_zeroize( IV, sizeof( IV ) );
557 mbedtls_platform_zeroize( key, sizeof( key ) );
558 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
559 mbedtls_platform_zeroize( output, sizeof( output ) );
560 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_cipher_free( &cipher_ctx );
563 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000564
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100565 return( exit_code );
Paul Bakker20a78082011-01-21 09:32:12 +0000566}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */