blob: 49c43b32167f5a4951a548b0d8bbdc0d3cb91ce9 [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>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#define mbedtls_fprintf fprintf
39#define mbedtls_printf printf
Rich Evans18b78c72015-02-11 14:06:19 +000040#endif
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
43 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/cipher.h"
45#include "mbedtls/md.h"
Rich Evans18b78c72015-02-11 14:06:19 +000046
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000050#endif
51
Paul Bakker494c0b82011-04-24 15:30:07 +000052#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000053#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000054#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000055#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000056#endif
Paul Bakker20a78082011-01-21 09:32:12 +000057#else
58#include <sys/types.h>
59#include <unistd.h>
60#endif
61
Paul Bakker20a78082011-01-21 09:32:12 +000062#define MODE_ENCRYPT 0
63#define MODE_DECRYPT 1
64
65#define USAGE \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
Paul Bakker20a78082011-01-21 09:32:12 +000067 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
68 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
69 "\n"
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
72 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000073int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000074{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 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 +000076 return( 0 );
77}
78#else
Paul Bakker20a78082011-01-21 09:32:12 +000079int main( int argc, char *argv[] )
80{
Paul Bakker25b5fe52011-05-26 14:02:58 +000081 int ret = 1, i, n;
Paul Bakker243f48e2016-09-02 22:44:09 +020082 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +000083 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000084 FILE *fkey, *fin = NULL, *fout = NULL;
85
86 char *p;
87 unsigned char IV[16];
88 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +000090 unsigned char buffer[1024];
91 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +010092 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +000093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 const mbedtls_cipher_info_t *cipher_info;
95 const mbedtls_md_info_t *md_info;
96 mbedtls_cipher_context_t cipher_ctx;
97 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +000098#if defined(_WIN32_WCE)
99 long filesize, offset;
100#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000101 LARGE_INTEGER li_size;
102 __int64 filesize, offset;
103#else
104 off_t filesize, offset;
105#endif
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_cipher_init( &cipher_ctx );
108 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000109
110 /*
111 * Parse the command-line arguments.
112 */
113 if( argc != 7 )
114 {
115 const int *list;
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_printf( "Available ciphers:\n" );
120 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000121 while( *list )
122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 cipher_info = mbedtls_cipher_info_from_type( *list );
124 mbedtls_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000125 list++;
126 }
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_printf( "\nAvailable message digests:\n" );
129 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000130 while( *list )
131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 md_info = mbedtls_md_info_from_type( *list );
133 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000134 list++;
135 }
136
Paul Bakkercce9d772011-11-18 14:26:47 +0000137#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000139 fflush( stdout ); getchar();
140#endif
141
142 goto exit;
143 }
144
145 mode = atoi( argv[1] );
146
147 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000150 goto exit;
151 }
152
153 if( strcmp( argv[2], argv[3] ) == 0 )
154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000156 goto exit;
157 }
158
159 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000162 goto exit;
163 }
164
165 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000168 goto exit;
169 }
170
171 /*
172 * Read the Cipher and MD from the command line
173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000175 if( cipher_info == NULL )
176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000178 goto exit;
179 }
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200180 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200181 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200182 mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200183 goto exit;
184 }
Paul Bakker20a78082011-01-21 09:32:12 +0000185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 md_info = mbedtls_md_info_from_string( argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000187 if( md_info == NULL )
188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000190 goto exit;
191 }
Janos Follath8eb64132016-06-03 15:40:57 +0100192
193 if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
194 {
Janos Follath352dbe22016-06-07 10:29:05 +0100195 mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100196 goto exit;
197 }
Paul Bakker20a78082011-01-21 09:32:12 +0000198
199 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100200 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000201 */
202 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
203 {
204 keylen = fread( key, 1, sizeof( key ), fkey );
205 fclose( fkey );
206 }
207 else
208 {
209 if( memcmp( argv[6], "hex:", 4 ) == 0 )
210 {
211 p = &argv[6][4];
212 keylen = 0;
213
214 while( sscanf( p, "%02X", &n ) > 0 &&
215 keylen < (int) sizeof( key ) )
216 {
217 key[keylen++] = (unsigned char) n;
218 p += 2;
219 }
220 }
221 else
222 {
223 keylen = strlen( argv[6] );
224
225 if( keylen > (int) sizeof( key ) )
226 keylen = (int) sizeof( key );
227
228 memcpy( key, argv[6], keylen );
229 }
230 }
231
Paul Bakkercce9d772011-11-18 14:26:47 +0000232#if defined(_WIN32_WCE)
233 filesize = fseek( fin, 0L, SEEK_END );
234#else
235#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000236 /*
237 * Support large files (> 2Gb) on Win32
238 */
239 li_size.QuadPart = 0;
240 li_size.LowPart =
241 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
242 li_size.LowPart, &li_size.HighPart, FILE_END );
243
244 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000247 goto exit;
248 }
249
250 filesize = li_size.QuadPart;
251#else
252 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
253 {
254 perror( "lseek" );
255 goto exit;
256 }
257#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000258#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000259
260 if( fseek( fin, 0, SEEK_SET ) < 0 )
261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000263 goto exit;
264 }
265
266 if( mode == MODE_ENCRYPT )
267 {
268 /*
269 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200270 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000271 */
272 for( i = 0; i < 8; i++ )
273 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
274
275 p = argv[2];
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_md_starts( &md_ctx );
278 mbedtls_md_update( &md_ctx, buffer, 8 );
279 mbedtls_md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
280 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000281
282 memcpy( IV, digest, 16 );
283
284 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000285 * Append the IV at the beginning of the output.
286 */
287 if( fwrite( IV, 1, 16, fout ) != 16 )
288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000290 goto exit;
291 }
292
293 /*
294 * Hash the IV and the secret key together 8192 times
295 * using the result to setup the AES context and HMAC.
296 */
297 memset( digest, 0, 32 );
298 memcpy( digest, IV, 16 );
299
300 for( i = 0; i < 8192; i++ )
301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_md_starts( &md_ctx );
303 mbedtls_md_update( &md_ctx, digest, 32 );
304 mbedtls_md_update( &md_ctx, key, keylen );
305 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000306
307 }
308
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200309 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000313 goto exit;
314 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200318 goto exit;
319 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000323 goto exit;
324 }
Paul Bakker20a78082011-01-21 09:32:12 +0000325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000327
328 /*
329 * Encrypt and write the ciphertext.
330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
334 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000335
Paul Bakker25b5fe52011-05-26 14:02:58 +0000336 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000339 goto exit;
340 }
341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200345 goto exit;
346 }
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000349
Paul Bakker2c0994e2011-05-25 13:51:57 +0000350 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000353 goto exit;
354 }
355 }
356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000360 goto exit;
361 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000363
Paul Bakker2c0994e2011-05-25 13:51:57 +0000364 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000367 goto exit;
368 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000369
Paul Bakker20a78082011-01-21 09:32:12 +0000370 /*
371 * Finally write the HMAC.
372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000378 goto exit;
379 }
380 }
381
382 if( mode == MODE_DECRYPT )
383 {
384 /*
385 * The encrypted file must be structured as follows:
386 *
387 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200388 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000389 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200390 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
391 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000396 goto exit;
397 }
398
Janos Follath8eb64132016-06-03 15:40:57 +0100399 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
400 {
Janos Follath352dbe22016-06-07 10:29:05 +0100401 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
Janos Follath8eb64132016-06-03 15:40:57 +0100402 goto exit;
403 }
404
405 /*
406 * Check the file size.
407 */
Paul Bakker243f48e2016-09-02 22:44:09 +0200408 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
409 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
413 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000414 goto exit;
415 }
416
417 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100418 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000421
422 /*
423 * Read the IV and original filesize modulo 16.
424 */
425 if( fread( buffer, 1, 16, fin ) != 16 )
426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000428 goto exit;
429 }
430
431 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000432
433 /*
434 * Hash the IV and the secret key together 8192 times
435 * using the result to setup the AES context and HMAC.
436 */
437 memset( digest, 0, 32 );
438 memcpy( digest, IV, 16 );
439
440 for( i = 0; i < 8192; i++ )
441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_md_starts( &md_ctx );
443 mbedtls_md_update( &md_ctx, digest, 32 );
444 mbedtls_md_update( &md_ctx, key, keylen );
445 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000446 }
447
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200448 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200452 goto exit;
453 }
454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() 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_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200464 goto exit;
465 }
Paul Bakker20a78082011-01-21 09:32:12 +0000466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000468
469 /*
470 * Decrypt and write the plaintext.
471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000473 {
Paul Bakker243f48e2016-09-02 22:44:09 +0200474 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
475 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
476
477 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n",
480 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000481 goto exit;
482 }
483
Paul Bakker243f48e2016-09-02 22:44:09 +0200484 mbedtls_md_hmac_update( &md_ctx, buffer, ilen );
485 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
486 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200489 goto exit;
490 }
Paul Bakker20a78082011-01-21 09:32:12 +0000491
Paul Bakker2c0994e2011-05-25 13:51:57 +0000492 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000495 goto exit;
496 }
497 }
498
499 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000500 * Verify the message authentication code.
501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000507 goto exit;
508 }
509
Paul Bakker424cd692013-10-31 14:22:08 +0100510 /* Use constant-time buffer comparison */
511 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100513 diff |= digest[i] ^ buffer[i];
514
515 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000518 "or file corrupted.\n" );
519 goto exit;
520 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100521
522 /*
523 * Write the final block of data
524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_cipher_finish( &cipher_ctx, output, &olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100526
527 if( fwrite( output, 1, olen, fout ) != olen )
528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100530 goto exit;
531 }
Paul Bakker20a78082011-01-21 09:32:12 +0000532 }
533
534 ret = 0;
535
536exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000537 if( fin )
538 fclose( fin );
539 if( fout )
540 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000541
Hanno Beckerf601ec52017-06-27 08:22:17 +0100542 /* Zeroize all command line arguments to also cover
543 the case when the user has missed or reordered some,
544 in which case the key might not be in argv[6]. */
545 for( i = 0; i < argc; i++ )
546 memset( argv[i], 0, strlen( argv[i] ) );
547
548 memset( IV, 0, sizeof( IV ) );
549 memset( key, 0, sizeof( key ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000550 memset( buffer, 0, sizeof( buffer ) );
Hanno Beckerf601ec52017-06-27 08:22:17 +0100551 memset( output, 0, sizeof( output ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000552 memset( digest, 0, sizeof( digest ) );
553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_cipher_free( &cipher_ctx );
555 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000556
557 return( ret );
558}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */