blob: 47497f6f115470df3e366b7ffd4af8c911a6456f [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");
Andrzej Kurek825ebd42020-05-18 11:47:25 -040081 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000082}
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{
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400100 int ret = 1, i;
101 unsigned n;
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100102 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +0200103 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +0000104 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +0000105 FILE *fkey, *fin = NULL, *fout = NULL;
106
107 char *p;
108 unsigned char IV[16];
109 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +0000111 unsigned char buffer[1024];
112 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +0100113 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +0000114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 const mbedtls_cipher_info_t *cipher_info;
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100116 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_cipher_context_t cipher_ctx;
118 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +0000119#if defined(_WIN32_WCE)
120 long filesize, offset;
121#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000122 LARGE_INTEGER li_size;
123 __int64 filesize, offset;
124#else
125 off_t filesize, offset;
126#endif
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_cipher_init( &cipher_ctx );
129 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000130
131 /*
132 * Parse the command-line arguments.
133 */
134 if( argc != 7 )
135 {
136 const int *list;
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_printf( "Available ciphers:\n" );
141 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000142 while( *list )
143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 cipher_info = mbedtls_cipher_info_from_type( *list );
145 mbedtls_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000146 list++;
147 }
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_printf( "\nAvailable message digests:\n" );
150 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000151 while( *list )
152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 md_info = mbedtls_md_info_from_type( *list );
154 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000155 list++;
156 }
157
Paul Bakkercce9d772011-11-18 14:26:47 +0000158#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000160 fflush( stdout ); getchar();
161#endif
162
163 goto exit;
164 }
165
166 mode = atoi( argv[1] );
167
168 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000171 goto exit;
172 }
173
174 if( strcmp( argv[2], argv[3] ) == 0 )
175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000177 goto exit;
178 }
179
180 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000183 goto exit;
184 }
185
186 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000189 goto exit;
190 }
191
192 /*
193 * Read the Cipher and MD from the command line
194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000196 if( cipher_info == NULL )
197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000199 goto exit;
200 }
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200201 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200202 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200203 mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200204 goto exit;
205 }
Paul Bakker20a78082011-01-21 09:32:12 +0000206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 md_info = mbedtls_md_info_from_string( argv[5] );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100208 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Paul Bakker20a78082011-01-21 09:32:12 +0000209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000211 goto exit;
212 }
Janos Follath8eb64132016-06-03 15:40:57 +0100213
214 if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
215 {
Janos Follath352dbe22016-06-07 10:29:05 +0100216 mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100217 goto exit;
218 }
Paul Bakker20a78082011-01-21 09:32:12 +0000219
220 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100221 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000222 */
223 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
224 {
225 keylen = fread( key, 1, sizeof( key ), fkey );
226 fclose( fkey );
227 }
228 else
229 {
230 if( memcmp( argv[6], "hex:", 4 ) == 0 )
231 {
232 p = &argv[6][4];
233 keylen = 0;
234
235 while( sscanf( p, "%02X", &n ) > 0 &&
236 keylen < (int) sizeof( key ) )
237 {
238 key[keylen++] = (unsigned char) n;
239 p += 2;
240 }
241 }
242 else
243 {
244 keylen = strlen( argv[6] );
245
246 if( keylen > (int) sizeof( key ) )
247 keylen = (int) sizeof( key );
248
249 memcpy( key, argv[6], keylen );
250 }
251 }
252
Paul Bakkercce9d772011-11-18 14:26:47 +0000253#if defined(_WIN32_WCE)
254 filesize = fseek( fin, 0L, SEEK_END );
255#else
256#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000257 /*
258 * Support large files (> 2Gb) on Win32
259 */
260 li_size.QuadPart = 0;
261 li_size.LowPart =
262 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
263 li_size.LowPart, &li_size.HighPart, FILE_END );
264
265 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000268 goto exit;
269 }
270
271 filesize = li_size.QuadPart;
272#else
273 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
274 {
275 perror( "lseek" );
276 goto exit;
277 }
278#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000279#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000280
281 if( fseek( fin, 0, SEEK_SET ) < 0 )
282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000284 goto exit;
285 }
286
287 if( mode == MODE_ENCRYPT )
288 {
289 /*
290 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200291 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000292 */
293 for( i = 0; i < 8; i++ )
294 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
295
296 p = argv[2];
297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_md_starts( &md_ctx );
299 mbedtls_md_update( &md_ctx, buffer, 8 );
300 mbedtls_md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
301 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000302
303 memcpy( IV, digest, 16 );
304
305 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000306 * Append the IV at the beginning of the output.
307 */
308 if( fwrite( IV, 1, 16, fout ) != 16 )
309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000311 goto exit;
312 }
313
314 /*
315 * Hash the IV and the secret key together 8192 times
316 * using the result to setup the AES context and HMAC.
317 */
318 memset( digest, 0, 32 );
319 memcpy( digest, IV, 16 );
320
321 for( i = 0; i < 8192; i++ )
322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_md_starts( &md_ctx );
324 mbedtls_md_update( &md_ctx, digest, 32 );
325 mbedtls_md_update( &md_ctx, key, keylen );
326 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000327
328 }
329
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200330 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000334 goto exit;
335 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200339 goto exit;
340 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000344 goto exit;
345 }
Paul Bakker20a78082011-01-21 09:32:12 +0000346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000348
349 /*
350 * Encrypt and write the ciphertext.
351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
355 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000356
Paul Bakker25b5fe52011-05-26 14:02:58 +0000357 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000360 goto exit;
361 }
362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200366 goto exit;
367 }
368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000370
Paul Bakker2c0994e2011-05-25 13:51:57 +0000371 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000374 goto exit;
375 }
376 }
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000381 goto exit;
382 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000384
Paul Bakker2c0994e2011-05-25 13:51:57 +0000385 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000388 goto exit;
389 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000390
Paul Bakker20a78082011-01-21 09:32:12 +0000391 /*
392 * Finally write the HMAC.
393 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000399 goto exit;
400 }
401 }
402
403 if( mode == MODE_DECRYPT )
404 {
405 /*
406 * The encrypted file must be structured as follows:
407 *
408 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200409 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000410 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200411 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
412 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000417 goto exit;
418 }
419
Janos Follath8eb64132016-06-03 15:40:57 +0100420 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
421 {
Janos Follath352dbe22016-06-07 10:29:05 +0100422 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100423 goto exit;
424 }
425
426 /*
427 * Check the file size.
428 */
Paul Bakker243f48e2016-09-02 22:44:09 +0200429 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
430 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
434 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000435 goto exit;
436 }
437
438 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100439 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000442
443 /*
444 * Read the IV and original filesize modulo 16.
445 */
446 if( fread( buffer, 1, 16, fin ) != 16 )
447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000449 goto exit;
450 }
451
452 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000453
454 /*
455 * Hash the IV and the secret key together 8192 times
456 * using the result to setup the AES context and HMAC.
457 */
458 memset( digest, 0, 32 );
459 memcpy( digest, IV, 16 );
460
461 for( i = 0; i < 8192; i++ )
462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_md_starts( &md_ctx );
464 mbedtls_md_update( &md_ctx, digest, 32 );
465 mbedtls_md_update( &md_ctx, key, keylen );
466 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000467 }
468
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200469 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() 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_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200479 goto exit;
480 }
481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200485 goto exit;
486 }
Paul Bakker20a78082011-01-21 09:32:12 +0000487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000489
490 /*
491 * Decrypt and write the plaintext.
492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000494 {
Paul Bakker243f48e2016-09-02 22:44:09 +0200495 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
496 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
497
498 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n",
501 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000502 goto exit;
503 }
504
Paul Bakker243f48e2016-09-02 22:44:09 +0200505 mbedtls_md_hmac_update( &md_ctx, buffer, ilen );
506 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
507 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200510 goto exit;
511 }
Paul Bakker20a78082011-01-21 09:32:12 +0000512
Paul Bakker2c0994e2011-05-25 13:51:57 +0000513 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000516 goto exit;
517 }
518 }
519
520 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000521 * Verify the message authentication code.
522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000528 goto exit;
529 }
530
Paul Bakker424cd692013-10-31 14:22:08 +0100531 /* Use constant-time buffer comparison */
532 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100534 diff |= digest[i] ^ buffer[i];
535
536 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000539 "or file corrupted.\n" );
540 goto exit;
541 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100542
543 /*
544 * Write the final block of data
545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_cipher_finish( &cipher_ctx, output, &olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100547
548 if( fwrite( output, 1, olen, fout ) != olen )
549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100551 goto exit;
552 }
Paul Bakker20a78082011-01-21 09:32:12 +0000553 }
554
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100555 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000556
557exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000558 if( fin )
559 fclose( fin );
560 if( fout )
561 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000562
Hanno Beckerf601ec52017-06-27 08:22:17 +0100563 /* Zeroize all command line arguments to also cover
564 the case when the user has missed or reordered some,
565 in which case the key might not be in argv[6]. */
566 for( i = 0; i < argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100567 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerf601ec52017-06-27 08:22:17 +0100568
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100569 mbedtls_platform_zeroize( IV, sizeof( IV ) );
570 mbedtls_platform_zeroize( key, sizeof( key ) );
571 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
572 mbedtls_platform_zeroize( output, sizeof( output ) );
573 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_cipher_free( &cipher_ctx );
576 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000577
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400578 mbedtls_exit( exit_code );
Paul Bakker20a78082011-01-21 09:32:12 +0000579}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */