blob: 88b852b4bdcdacbc484787f6253268f5e1b329d2 [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
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010041#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010042#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
43#endif /* MBEDTLS_PLATFORM_C */
Rich Evans18b78c72015-02-11 14:06:19 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
46 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/cipher.h"
48#include "mbedtls/md.h"
Hanno Becker0b44d5c2018-10-12 16:46:37 +010049#include "mbedtls/platform_util.h"
Rich Evans18b78c72015-02-11 14:06:19 +000050
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000054#endif
55
Paul Bakker494c0b82011-04-24 15:30:07 +000056#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000057#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000058#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000059#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000060#endif
Paul Bakker20a78082011-01-21 09:32:12 +000061#else
62#include <sys/types.h>
63#include <unistd.h>
64#endif
65
Paul Bakker20a78082011-01-21 09:32:12 +000066#define MODE_ENCRYPT 0
67#define MODE_DECRYPT 1
68
69#define USAGE \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
Paul Bakker20a78082011-01-21 09:32:12 +000071 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
72 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
73 "\n"
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
76 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000077int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000078{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 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 +000080 return( 0 );
81}
82#else
Paul Bakker20a78082011-01-21 09:32:12 +000083int main( int argc, char *argv[] )
84{
Paul Bakker25b5fe52011-05-26 14:02:58 +000085 int ret = 1, i, n;
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010086 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +020087 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +000088 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000089 FILE *fkey, *fin = NULL, *fout = NULL;
90
91 char *p;
92 unsigned char IV[16];
93 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +000095 unsigned char buffer[1024];
96 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +010097 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +000098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 const mbedtls_cipher_info_t *cipher_info;
100 const mbedtls_md_info_t *md_info;
101 mbedtls_cipher_context_t cipher_ctx;
102 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +0000103#if defined(_WIN32_WCE)
104 long filesize, offset;
105#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000106 LARGE_INTEGER li_size;
107 __int64 filesize, offset;
108#else
109 off_t filesize, offset;
110#endif
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_cipher_init( &cipher_ctx );
113 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000114
115 /*
116 * Parse the command-line arguments.
117 */
118 if( argc != 7 )
119 {
120 const int *list;
121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_printf( "Available ciphers:\n" );
125 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000126 while( *list )
127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 cipher_info = mbedtls_cipher_info_from_type( *list );
129 mbedtls_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000130 list++;
131 }
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_printf( "\nAvailable message digests:\n" );
134 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000135 while( *list )
136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 md_info = mbedtls_md_info_from_type( *list );
138 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000139 list++;
140 }
141
Paul Bakkercce9d772011-11-18 14:26:47 +0000142#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000144 fflush( stdout ); getchar();
145#endif
146
147 goto exit;
148 }
149
150 mode = atoi( argv[1] );
151
152 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000155 goto exit;
156 }
157
158 if( strcmp( argv[2], argv[3] ) == 0 )
159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000161 goto exit;
162 }
163
164 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000167 goto exit;
168 }
169
170 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000173 goto exit;
174 }
175
176 /*
177 * Read the Cipher and MD from the command line
178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000180 if( cipher_info == NULL )
181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000183 goto exit;
184 }
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200185 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200186 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200187 mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200188 goto exit;
189 }
Paul Bakker20a78082011-01-21 09:32:12 +0000190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 md_info = mbedtls_md_info_from_string( argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000192 if( md_info == NULL )
193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000195 goto exit;
196 }
Janos Follath8eb64132016-06-03 15:40:57 +0100197
198 if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
199 {
Janos Follath352dbe22016-06-07 10:29:05 +0100200 mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100201 goto exit;
202 }
Paul Bakker20a78082011-01-21 09:32:12 +0000203
204 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100205 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000206 */
207 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
208 {
209 keylen = fread( key, 1, sizeof( key ), fkey );
210 fclose( fkey );
211 }
212 else
213 {
214 if( memcmp( argv[6], "hex:", 4 ) == 0 )
215 {
216 p = &argv[6][4];
217 keylen = 0;
218
219 while( sscanf( p, "%02X", &n ) > 0 &&
220 keylen < (int) sizeof( key ) )
221 {
222 key[keylen++] = (unsigned char) n;
223 p += 2;
224 }
225 }
226 else
227 {
228 keylen = strlen( argv[6] );
229
230 if( keylen > (int) sizeof( key ) )
231 keylen = (int) sizeof( key );
232
233 memcpy( key, argv[6], keylen );
234 }
235 }
236
Paul Bakkercce9d772011-11-18 14:26:47 +0000237#if defined(_WIN32_WCE)
238 filesize = fseek( fin, 0L, SEEK_END );
239#else
240#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000241 /*
242 * Support large files (> 2Gb) on Win32
243 */
244 li_size.QuadPart = 0;
245 li_size.LowPart =
246 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
247 li_size.LowPart, &li_size.HighPart, FILE_END );
248
249 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000252 goto exit;
253 }
254
255 filesize = li_size.QuadPart;
256#else
257 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
258 {
259 perror( "lseek" );
260 goto exit;
261 }
262#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000263#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000264
265 if( fseek( fin, 0, SEEK_SET ) < 0 )
266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000268 goto exit;
269 }
270
271 if( mode == MODE_ENCRYPT )
272 {
273 /*
274 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200275 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000276 */
277 for( i = 0; i < 8; i++ )
278 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
279
280 p = argv[2];
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_md_starts( &md_ctx );
283 mbedtls_md_update( &md_ctx, buffer, 8 );
284 mbedtls_md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
285 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000286
287 memcpy( IV, digest, 16 );
288
289 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000290 * Append the IV at the beginning of the output.
291 */
292 if( fwrite( IV, 1, 16, fout ) != 16 )
293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000295 goto exit;
296 }
297
298 /*
299 * Hash the IV and the secret key together 8192 times
300 * using the result to setup the AES context and HMAC.
301 */
302 memset( digest, 0, 32 );
303 memcpy( digest, IV, 16 );
304
305 for( i = 0; i < 8192; i++ )
306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 mbedtls_md_starts( &md_ctx );
308 mbedtls_md_update( &md_ctx, digest, 32 );
309 mbedtls_md_update( &md_ctx, key, keylen );
310 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000311
312 }
313
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200314 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000318 goto exit;
319 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200323 goto exit;
324 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000328 goto exit;
329 }
Paul Bakker20a78082011-01-21 09:32:12 +0000330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000332
333 /*
334 * Encrypt and write the ciphertext.
335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
339 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000340
Paul Bakker25b5fe52011-05-26 14:02:58 +0000341 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000344 goto exit;
345 }
346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200350 goto exit;
351 }
352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000354
Paul Bakker2c0994e2011-05-25 13:51:57 +0000355 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000358 goto exit;
359 }
360 }
361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000365 goto exit;
366 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000368
Paul Bakker2c0994e2011-05-25 13:51:57 +0000369 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000372 goto exit;
373 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000374
Paul Bakker20a78082011-01-21 09:32:12 +0000375 /*
376 * Finally write the HMAC.
377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000383 goto exit;
384 }
385 }
386
387 if( mode == MODE_DECRYPT )
388 {
389 /*
390 * The encrypted file must be structured as follows:
391 *
392 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200393 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000394 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200395 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
396 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000401 goto exit;
402 }
403
Janos Follath8eb64132016-06-03 15:40:57 +0100404 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
405 {
Janos Follath352dbe22016-06-07 10:29:05 +0100406 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100407 goto exit;
408 }
409
410 /*
411 * Check the file size.
412 */
Paul Bakker243f48e2016-09-02 22:44:09 +0200413 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
414 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
418 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000419 goto exit;
420 }
421
422 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100423 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000426
427 /*
428 * Read the IV and original filesize modulo 16.
429 */
430 if( fread( buffer, 1, 16, fin ) != 16 )
431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000433 goto exit;
434 }
435
436 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000437
438 /*
439 * Hash the IV and the secret key together 8192 times
440 * using the result to setup the AES context and HMAC.
441 */
442 memset( digest, 0, 32 );
443 memcpy( digest, IV, 16 );
444
445 for( i = 0; i < 8192; i++ )
446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_md_starts( &md_ctx );
448 mbedtls_md_update( &md_ctx, digest, 32 );
449 mbedtls_md_update( &md_ctx, key, keylen );
450 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000451 }
452
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200453 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() 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_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200463 goto exit;
464 }
465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200469 goto exit;
470 }
Paul Bakker20a78082011-01-21 09:32:12 +0000471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000473
474 /*
475 * Decrypt and write the plaintext.
476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000478 {
Paul Bakker243f48e2016-09-02 22:44:09 +0200479 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
480 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
481
482 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n",
485 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000486 goto exit;
487 }
488
Paul Bakker243f48e2016-09-02 22:44:09 +0200489 mbedtls_md_hmac_update( &md_ctx, buffer, ilen );
490 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
491 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200494 goto exit;
495 }
Paul Bakker20a78082011-01-21 09:32:12 +0000496
Paul Bakker2c0994e2011-05-25 13:51:57 +0000497 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000500 goto exit;
501 }
502 }
503
504 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000505 * Verify the message authentication code.
506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000512 goto exit;
513 }
514
Paul Bakker424cd692013-10-31 14:22:08 +0100515 /* Use constant-time buffer comparison */
516 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100518 diff |= digest[i] ^ buffer[i];
519
520 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000523 "or file corrupted.\n" );
524 goto exit;
525 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100526
527 /*
528 * Write the final block of data
529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_cipher_finish( &cipher_ctx, output, &olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100531
532 if( fwrite( output, 1, olen, fout ) != olen )
533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100535 goto exit;
536 }
Paul Bakker20a78082011-01-21 09:32:12 +0000537 }
538
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100539 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000540
541exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000542 if( fin )
543 fclose( fin );
544 if( fout )
545 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000546
Hanno Beckerf601ec52017-06-27 08:22:17 +0100547 /* Zeroize all command line arguments to also cover
548 the case when the user has missed or reordered some,
549 in which case the key might not be in argv[6]. */
550 for( i = 0; i < argc; i++ )
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100551 mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
Hanno Beckerf601ec52017-06-27 08:22:17 +0100552
Hanno Becker0b44d5c2018-10-12 16:46:37 +0100553 mbedtls_platform_zeroize( IV, sizeof( IV ) );
554 mbedtls_platform_zeroize( key, sizeof( key ) );
555 mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
556 mbedtls_platform_zeroize( output, sizeof( output ) );
557 mbedtls_platform_zeroize( digest, sizeof( digest ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_cipher_free( &cipher_ctx );
560 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000561
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100562 return( exit_code );
Paul Bakker20a78082011-01-21 09:32:12 +0000563}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */