blob: 2990cdfafa9ad9f98f789ada8beed406d89abce6 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker20a78082011-01-21 09:32:12 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Andres Amaya Garcia990900f2018-04-29 19:11:26 +010033#include <stdlib.h>
34#define mbedtls_fprintf fprintf
35#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020036#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010037#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia990900f2018-04-29 19:11:26 +010038#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
39#endif /* MBEDTLS_PLATFORM_C */
Rich Evans18b78c72015-02-11 14:06:19 +000040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
42 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/cipher.h"
44#include "mbedtls/md.h"
Rich Evans18b78c72015-02-11 14:06:19 +000045
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000049#endif
50
Paul Bakker494c0b82011-04-24 15:30:07 +000051#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000052#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000053#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000054#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000055#endif
Paul Bakker20a78082011-01-21 09:32:12 +000056#else
57#include <sys/types.h>
58#include <unistd.h>
59#endif
60
Paul Bakker20a78082011-01-21 09:32:12 +000061#define MODE_ENCRYPT 0
62#define MODE_DECRYPT 1
63
64#define USAGE \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
Paul Bakker20a78082011-01-21 09:32:12 +000066 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
67 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
68 "\n"
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
71 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000072int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000073{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_printf("MBEDTLS_CIPHER_C and/or MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020075 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000076}
77#else
Hanno Becker9a1a1512018-10-12 16:46:37 +010078
79/* Implementation that should never be optimized out by the compiler */
80static void mbedtls_zeroize( void *v, size_t n ) {
81 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
82}
83
Paul Bakker20a78082011-01-21 09:32:12 +000084int main( int argc, char *argv[] )
85{
Paul Bakker25b5fe52011-05-26 14:02:58 +000086 int ret = 1, i, n;
Andres Amaya Garcia990900f2018-04-29 19:11:26 +010087 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +020088 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +000089 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000090 FILE *fkey, *fin = NULL, *fout = NULL;
91
92 char *p;
93 unsigned char IV[16];
94 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +000096 unsigned char buffer[1024];
97 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +010098 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +000099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 const mbedtls_cipher_info_t *cipher_info;
101 const mbedtls_md_info_t *md_info;
102 mbedtls_cipher_context_t cipher_ctx;
103 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +0000104#if defined(_WIN32_WCE)
105 long filesize, offset;
106#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000107 LARGE_INTEGER li_size;
108 __int64 filesize, offset;
109#else
110 off_t filesize, offset;
111#endif
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_cipher_init( &cipher_ctx );
114 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000115
116 /*
117 * Parse the command-line arguments.
118 */
119 if( argc != 7 )
120 {
121 const int *list;
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_printf( "Available ciphers:\n" );
126 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000127 while( *list )
128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 cipher_info = mbedtls_cipher_info_from_type( *list );
130 mbedtls_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000131 list++;
132 }
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_printf( "\nAvailable message digests:\n" );
135 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000136 while( *list )
137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 md_info = mbedtls_md_info_from_type( *list );
139 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000140 list++;
141 }
142
Paul Bakkercce9d772011-11-18 14:26:47 +0000143#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000145 fflush( stdout ); getchar();
146#endif
147
148 goto exit;
149 }
150
151 mode = atoi( argv[1] );
152
153 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000156 goto exit;
157 }
158
159 if( strcmp( argv[2], argv[3] ) == 0 )
160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000162 goto exit;
163 }
164
165 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000168 goto exit;
169 }
170
171 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000174 goto exit;
175 }
176
177 /*
178 * Read the Cipher and MD from the command line
179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000181 if( cipher_info == NULL )
182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000184 goto exit;
185 }
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200186 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200187 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200188 mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200189 goto exit;
190 }
Paul Bakker20a78082011-01-21 09:32:12 +0000191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 md_info = mbedtls_md_info_from_string( argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000193 if( md_info == NULL )
194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000196 goto exit;
197 }
Janos Follath8eb64132016-06-03 15:40:57 +0100198
199 if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
200 {
Janos Follath352dbe22016-06-07 10:29:05 +0100201 mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100202 goto exit;
203 }
Paul Bakker20a78082011-01-21 09:32:12 +0000204
205 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100206 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000207 */
208 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
209 {
210 keylen = fread( key, 1, sizeof( key ), fkey );
211 fclose( fkey );
212 }
213 else
214 {
215 if( memcmp( argv[6], "hex:", 4 ) == 0 )
216 {
217 p = &argv[6][4];
218 keylen = 0;
219
220 while( sscanf( p, "%02X", &n ) > 0 &&
221 keylen < (int) sizeof( key ) )
222 {
223 key[keylen++] = (unsigned char) n;
224 p += 2;
225 }
226 }
227 else
228 {
229 keylen = strlen( argv[6] );
230
231 if( keylen > (int) sizeof( key ) )
232 keylen = (int) sizeof( key );
233
234 memcpy( key, argv[6], keylen );
235 }
236 }
237
Paul Bakkercce9d772011-11-18 14:26:47 +0000238#if defined(_WIN32_WCE)
239 filesize = fseek( fin, 0L, SEEK_END );
240#else
241#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000242 /*
243 * Support large files (> 2Gb) on Win32
244 */
245 li_size.QuadPart = 0;
246 li_size.LowPart =
247 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
248 li_size.LowPart, &li_size.HighPart, FILE_END );
249
250 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000253 goto exit;
254 }
255
256 filesize = li_size.QuadPart;
257#else
258 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
259 {
260 perror( "lseek" );
261 goto exit;
262 }
263#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000264#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000265
266 if( fseek( fin, 0, SEEK_SET ) < 0 )
267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000269 goto exit;
270 }
271
272 if( mode == MODE_ENCRYPT )
273 {
274 /*
275 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200276 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000277 */
278 for( i = 0; i < 8; i++ )
279 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
280
281 p = argv[2];
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_md_starts( &md_ctx );
284 mbedtls_md_update( &md_ctx, buffer, 8 );
285 mbedtls_md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
286 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000287
288 memcpy( IV, digest, 16 );
289
290 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000291 * Append the IV at the beginning of the output.
292 */
293 if( fwrite( IV, 1, 16, fout ) != 16 )
294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000296 goto exit;
297 }
298
299 /*
300 * Hash the IV and the secret key together 8192 times
301 * using the result to setup the AES context and HMAC.
302 */
303 memset( digest, 0, 32 );
304 memcpy( digest, IV, 16 );
305
306 for( i = 0; i < 8192; i++ )
307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_md_starts( &md_ctx );
309 mbedtls_md_update( &md_ctx, digest, 32 );
310 mbedtls_md_update( &md_ctx, key, keylen );
311 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000312
313 }
314
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200315 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000319 goto exit;
320 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200324 goto exit;
325 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000329 goto exit;
330 }
Paul Bakker20a78082011-01-21 09:32:12 +0000331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000333
334 /*
335 * Encrypt and write the ciphertext.
336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
340 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000341
Paul Bakker25b5fe52011-05-26 14:02:58 +0000342 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000345 goto exit;
346 }
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200351 goto exit;
352 }
353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000355
Paul Bakker2c0994e2011-05-25 13:51:57 +0000356 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000359 goto exit;
360 }
361 }
362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000366 goto exit;
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 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000375
Paul Bakker20a78082011-01-21 09:32:12 +0000376 /*
377 * Finally write the HMAC.
378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000384 goto exit;
385 }
386 }
387
388 if( mode == MODE_DECRYPT )
389 {
390 /*
391 * The encrypted file must be structured as follows:
392 *
393 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200394 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000395 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200396 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
397 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000402 goto exit;
403 }
404
Janos Follath8eb64132016-06-03 15:40:57 +0100405 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
406 {
Janos Follath352dbe22016-06-07 10:29:05 +0100407 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100408 goto exit;
409 }
410
411 /*
412 * Check the file size.
413 */
Paul Bakker243f48e2016-09-02 22:44:09 +0200414 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
415 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
419 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000420 goto exit;
421 }
422
423 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100424 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000427
428 /*
429 * Read the IV and original filesize modulo 16.
430 */
431 if( fread( buffer, 1, 16, fin ) != 16 )
432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000434 goto exit;
435 }
436
437 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000438
439 /*
440 * Hash the IV and the secret key together 8192 times
441 * using the result to setup the AES context and HMAC.
442 */
443 memset( digest, 0, 32 );
444 memcpy( digest, IV, 16 );
445
446 for( i = 0; i < 8192; i++ )
447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_md_starts( &md_ctx );
449 mbedtls_md_update( &md_ctx, digest, 32 );
450 mbedtls_md_update( &md_ctx, key, keylen );
451 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000452 }
453
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200454 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200458 goto exit;
459 }
460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200464 goto exit;
465 }
466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200470 goto exit;
471 }
Paul Bakker20a78082011-01-21 09:32:12 +0000472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000474
475 /*
476 * Decrypt and write the plaintext.
477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000479 {
Paul Bakker243f48e2016-09-02 22:44:09 +0200480 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
481 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
482
483 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n",
486 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000487 goto exit;
488 }
489
Paul Bakker243f48e2016-09-02 22:44:09 +0200490 mbedtls_md_hmac_update( &md_ctx, buffer, ilen );
491 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
492 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200495 goto exit;
496 }
Paul Bakker20a78082011-01-21 09:32:12 +0000497
Paul Bakker2c0994e2011-05-25 13:51:57 +0000498 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000501 goto exit;
502 }
503 }
504
505 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000506 * Verify the message authentication code.
507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000513 goto exit;
514 }
515
Paul Bakker424cd692013-10-31 14:22:08 +0100516 /* Use constant-time buffer comparison */
517 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100519 diff |= digest[i] ^ buffer[i];
520
521 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000524 "or file corrupted.\n" );
525 goto exit;
526 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100527
528 /*
529 * Write the final block of data
530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_cipher_finish( &cipher_ctx, output, &olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100532
533 if( fwrite( output, 1, olen, fout ) != olen )
534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100536 goto exit;
537 }
Paul Bakker20a78082011-01-21 09:32:12 +0000538 }
539
Andres Amaya Garcia990900f2018-04-29 19:11:26 +0100540 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000541
542exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000543 if( fin )
544 fclose( fin );
545 if( fout )
546 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000547
Hanno Beckerf601ec52017-06-27 08:22:17 +0100548 /* Zeroize all command line arguments to also cover
549 the case when the user has missed or reordered some,
550 in which case the key might not be in argv[6]. */
551 for( i = 0; i < argc; i++ )
Hanno Becker9a1a1512018-10-12 16:46:37 +0100552 mbedtls_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerf601ec52017-06-27 08:22:17 +0100553
Hanno Becker9a1a1512018-10-12 16:46:37 +0100554 mbedtls_zeroize( IV, sizeof( IV ) );
555 mbedtls_zeroize( key, sizeof( key ) );
556 mbedtls_zeroize( buffer, sizeof( buffer ) );
557 mbedtls_zeroize( output, sizeof( output ) );
558 mbedtls_zeroize( digest, sizeof( digest ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_cipher_free( &cipher_ctx );
561 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000562
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200563 mbedtls_exit( exit_code );
Paul Bakker20a78082011-01-21 09:32:12 +0000564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */