blob: 9e234e6727c341a511e3ba9e9a27da98503a1d17 [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 Garcia4c47df62018-04-29 19:11:26 +010033#include <stdlib.h>
34#define mbedtls_fprintf fprintf
35#define mbedtls_printf printf
36#define MBEDTLS_EXTI_SUCCESS EXIT_SUCCESS
37#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
38#endif /* MBEDTLS_PLATFORM_C */
Rich Evans18b78c72015-02-11 14:06:19 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
41 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/cipher.h"
43#include "mbedtls/md.h"
Rich Evans18b78c72015-02-11 14:06:19 +000044
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000048#endif
49
Paul Bakker494c0b82011-04-24 15:30:07 +000050#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000051#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000052#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000053#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000054#endif
Paul Bakker20a78082011-01-21 09:32:12 +000055#else
56#include <sys/types.h>
57#include <unistd.h>
58#endif
59
Paul Bakker20a78082011-01-21 09:32:12 +000060#define MODE_ENCRYPT 0
61#define MODE_DECRYPT 1
62
63#define USAGE \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
Paul Bakker20a78082011-01-21 09:32:12 +000065 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
66 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
67 "\n"
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
70 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000071int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000072{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 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 +000074 return( 0 );
75}
76#else
Paul Bakker20a78082011-01-21 09:32:12 +000077int main( int argc, char *argv[] )
78{
Paul Bakker25b5fe52011-05-26 14:02:58 +000079 int ret = 1, i, n;
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010080 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +020081 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +000082 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000083 FILE *fkey, *fin = NULL, *fout = NULL;
84
85 char *p;
86 unsigned char IV[16];
87 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +000089 unsigned char buffer[1024];
90 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +010091 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +000092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 const mbedtls_cipher_info_t *cipher_info;
94 const mbedtls_md_info_t *md_info;
95 mbedtls_cipher_context_t cipher_ctx;
96 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +000097#if defined(_WIN32_WCE)
98 long filesize, offset;
99#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000100 LARGE_INTEGER li_size;
101 __int64 filesize, offset;
102#else
103 off_t filesize, offset;
104#endif
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 mbedtls_cipher_init( &cipher_ctx );
107 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000108
109 /*
110 * Parse the command-line arguments.
111 */
112 if( argc != 7 )
113 {
114 const int *list;
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_printf( "Available ciphers:\n" );
119 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000120 while( *list )
121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 cipher_info = mbedtls_cipher_info_from_type( *list );
123 mbedtls_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000124 list++;
125 }
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_printf( "\nAvailable message digests:\n" );
128 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000129 while( *list )
130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 md_info = mbedtls_md_info_from_type( *list );
132 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000133 list++;
134 }
135
Paul Bakkercce9d772011-11-18 14:26:47 +0000136#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000138 fflush( stdout ); getchar();
139#endif
140
141 goto exit;
142 }
143
144 mode = atoi( argv[1] );
145
146 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000149 goto exit;
150 }
151
152 if( strcmp( argv[2], argv[3] ) == 0 )
153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000155 goto exit;
156 }
157
158 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000161 goto exit;
162 }
163
164 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000167 goto exit;
168 }
169
170 /*
171 * Read the Cipher and MD from the command line
172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000174 if( cipher_info == NULL )
175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000177 goto exit;
178 }
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200179 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200180 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200181 mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200182 goto exit;
183 }
Paul Bakker20a78082011-01-21 09:32:12 +0000184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 md_info = mbedtls_md_info_from_string( argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000186 if( md_info == NULL )
187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000189 goto exit;
190 }
Janos Follath8eb64132016-06-03 15:40:57 +0100191
192 if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
193 {
Janos Follath352dbe22016-06-07 10:29:05 +0100194 mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100195 goto exit;
196 }
Paul Bakker20a78082011-01-21 09:32:12 +0000197
198 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100199 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000200 */
201 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
202 {
203 keylen = fread( key, 1, sizeof( key ), fkey );
204 fclose( fkey );
205 }
206 else
207 {
208 if( memcmp( argv[6], "hex:", 4 ) == 0 )
209 {
210 p = &argv[6][4];
211 keylen = 0;
212
213 while( sscanf( p, "%02X", &n ) > 0 &&
214 keylen < (int) sizeof( key ) )
215 {
216 key[keylen++] = (unsigned char) n;
217 p += 2;
218 }
219 }
220 else
221 {
222 keylen = strlen( argv[6] );
223
224 if( keylen > (int) sizeof( key ) )
225 keylen = (int) sizeof( key );
226
227 memcpy( key, argv[6], keylen );
228 }
229 }
230
Paul Bakkercce9d772011-11-18 14:26:47 +0000231#if defined(_WIN32_WCE)
232 filesize = fseek( fin, 0L, SEEK_END );
233#else
234#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000235 /*
236 * Support large files (> 2Gb) on Win32
237 */
238 li_size.QuadPart = 0;
239 li_size.LowPart =
240 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
241 li_size.LowPart, &li_size.HighPart, FILE_END );
242
243 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000246 goto exit;
247 }
248
249 filesize = li_size.QuadPart;
250#else
251 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
252 {
253 perror( "lseek" );
254 goto exit;
255 }
256#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000257#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000258
259 if( fseek( fin, 0, SEEK_SET ) < 0 )
260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000262 goto exit;
263 }
264
265 if( mode == MODE_ENCRYPT )
266 {
267 /*
268 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200269 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000270 */
271 for( i = 0; i < 8; i++ )
272 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
273
274 p = argv[2];
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_md_starts( &md_ctx );
277 mbedtls_md_update( &md_ctx, buffer, 8 );
278 mbedtls_md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
279 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000280
281 memcpy( IV, digest, 16 );
282
283 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000284 * Append the IV at the beginning of the output.
285 */
286 if( fwrite( IV, 1, 16, fout ) != 16 )
287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000289 goto exit;
290 }
291
292 /*
293 * Hash the IV and the secret key together 8192 times
294 * using the result to setup the AES context and HMAC.
295 */
296 memset( digest, 0, 32 );
297 memcpy( digest, IV, 16 );
298
299 for( i = 0; i < 8192; i++ )
300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 mbedtls_md_starts( &md_ctx );
302 mbedtls_md_update( &md_ctx, digest, 32 );
303 mbedtls_md_update( &md_ctx, key, keylen );
304 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000305
306 }
307
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200308 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000312 goto exit;
313 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200317 goto exit;
318 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000322 goto exit;
323 }
Paul Bakker20a78082011-01-21 09:32:12 +0000324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000326
327 /*
328 * Encrypt and write the ciphertext.
329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
333 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000334
Paul Bakker25b5fe52011-05-26 14:02:58 +0000335 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000338 goto exit;
339 }
340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200344 goto exit;
345 }
346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000348
Paul Bakker2c0994e2011-05-25 13:51:57 +0000349 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000352 goto exit;
353 }
354 }
355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000359 goto exit;
360 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000362
Paul Bakker2c0994e2011-05-25 13:51:57 +0000363 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000366 goto exit;
367 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000368
Paul Bakker20a78082011-01-21 09:32:12 +0000369 /*
370 * Finally write the HMAC.
371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000377 goto exit;
378 }
379 }
380
381 if( mode == MODE_DECRYPT )
382 {
383 /*
384 * The encrypted file must be structured as follows:
385 *
386 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200387 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000388 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200389 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
390 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000395 goto exit;
396 }
397
Janos Follath8eb64132016-06-03 15:40:57 +0100398 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
399 {
Janos Follath352dbe22016-06-07 10:29:05 +0100400 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100401 goto exit;
402 }
403
404 /*
405 * Check the file size.
406 */
Paul Bakker243f48e2016-09-02 22:44:09 +0200407 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
408 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
412 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000413 goto exit;
414 }
415
416 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100417 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000420
421 /*
422 * Read the IV and original filesize modulo 16.
423 */
424 if( fread( buffer, 1, 16, fin ) != 16 )
425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000427 goto exit;
428 }
429
430 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000431
432 /*
433 * Hash the IV and the secret key together 8192 times
434 * using the result to setup the AES context and HMAC.
435 */
436 memset( digest, 0, 32 );
437 memcpy( digest, IV, 16 );
438
439 for( i = 0; i < 8192; i++ )
440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_md_starts( &md_ctx );
442 mbedtls_md_update( &md_ctx, digest, 32 );
443 mbedtls_md_update( &md_ctx, key, keylen );
444 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000445 }
446
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200447 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200451 goto exit;
452 }
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200457 goto exit;
458 }
459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200463 goto exit;
464 }
Paul Bakker20a78082011-01-21 09:32:12 +0000465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000467
468 /*
469 * Decrypt and write the plaintext.
470 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000472 {
Paul Bakker243f48e2016-09-02 22:44:09 +0200473 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
474 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
475
476 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n",
479 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000480 goto exit;
481 }
482
Paul Bakker243f48e2016-09-02 22:44:09 +0200483 mbedtls_md_hmac_update( &md_ctx, buffer, ilen );
484 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
485 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200488 goto exit;
489 }
Paul Bakker20a78082011-01-21 09:32:12 +0000490
Paul Bakker2c0994e2011-05-25 13:51:57 +0000491 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000494 goto exit;
495 }
496 }
497
498 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000499 * Verify the message authentication code.
500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000506 goto exit;
507 }
508
Paul Bakker424cd692013-10-31 14:22:08 +0100509 /* Use constant-time buffer comparison */
510 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100512 diff |= digest[i] ^ buffer[i];
513
514 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000517 "or file corrupted.\n" );
518 goto exit;
519 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100520
521 /*
522 * Write the final block of data
523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_cipher_finish( &cipher_ctx, output, &olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100525
526 if( fwrite( output, 1, olen, fout ) != olen )
527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100529 goto exit;
530 }
Paul Bakker20a78082011-01-21 09:32:12 +0000531 }
532
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100533 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000534
535exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000536 if( fin )
537 fclose( fin );
538 if( fout )
539 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000540
Hanno Beckerf601ec52017-06-27 08:22:17 +0100541 /* Zeroize all command line arguments to also cover
542 the case when the user has missed or reordered some,
543 in which case the key might not be in argv[6]. */
544 for( i = 0; i < argc; i++ )
545 memset( argv[i], 0, strlen( argv[i] ) );
546
547 memset( IV, 0, sizeof( IV ) );
548 memset( key, 0, sizeof( key ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000549 memset( buffer, 0, sizeof( buffer ) );
Hanno Beckerf601ec52017-06-27 08:22:17 +0100550 memset( output, 0, sizeof( output ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000551 memset( digest, 0, sizeof( digest ) );
552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_cipher_free( &cipher_ctx );
554 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000555
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100556 return( exit_code );
Paul Bakker20a78082011-01-21 09:32:12 +0000557}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */