blob: 67f47c2709d1a60a836b045f33f26ac1ae78e4d2 [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 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00005 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakker20a78082011-01-21 09:32:12 +00006 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00007 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker20a78082011-01-21 09:32:12 +00008 *
Paul Bakker20a78082011-01-21 09:32:12 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000025#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#endif
Paul Bakker20a78082011-01-21 09:32:12 +000029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000032#else
Rich Evans18b78c72015-02-11 14:06:19 +000033#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#define mbedtls_fprintf fprintf
35#define mbedtls_printf printf
Rich Evans18b78c72015-02-11 14:06:19 +000036#endif
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
39 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/cipher.h"
41#include "mbedtls/md.h"
Rich Evans18b78c72015-02-11 14:06:19 +000042
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000046#endif
47
Paul Bakker494c0b82011-04-24 15:30:07 +000048#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000049#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000050#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000051#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000052#endif
Paul Bakker20a78082011-01-21 09:32:12 +000053#else
54#include <sys/types.h>
55#include <unistd.h>
56#endif
57
Paul Bakker20a78082011-01-21 09:32:12 +000058#define MODE_ENCRYPT 0
59#define MODE_DECRYPT 1
60
61#define USAGE \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
Paul Bakker20a78082011-01-21 09:32:12 +000063 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
64 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
65 "\n"
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
68 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000069int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000070{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 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 +000072 return( 0 );
73}
74#else
Paul Bakker20a78082011-01-21 09:32:12 +000075int main( int argc, char *argv[] )
76{
Paul Bakker25b5fe52011-05-26 14:02:58 +000077 int ret = 1, i, n;
Paul Bakker23986e52011-04-24 08:57:21 +000078 int mode, lastn;
Paul Bakker25b5fe52011-05-26 14:02:58 +000079 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000080 FILE *fkey, *fin = NULL, *fout = NULL;
81
82 char *p;
83 unsigned char IV[16];
84 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +000086 unsigned char buffer[1024];
87 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +010088 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +000089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 const mbedtls_cipher_info_t *cipher_info;
91 const mbedtls_md_info_t *md_info;
92 mbedtls_cipher_context_t cipher_ctx;
93 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +000094#if defined(_WIN32_WCE)
95 long filesize, offset;
96#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000097 LARGE_INTEGER li_size;
98 __int64 filesize, offset;
99#else
100 off_t filesize, offset;
101#endif
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_cipher_init( &cipher_ctx );
104 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000105
106 /*
107 * Parse the command-line arguments.
108 */
109 if( argc != 7 )
110 {
111 const int *list;
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_printf( "Available ciphers:\n" );
116 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000117 while( *list )
118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 cipher_info = mbedtls_cipher_info_from_type( *list );
120 mbedtls_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000121 list++;
122 }
123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_printf( "\nAvailable message digests:\n" );
125 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000126 while( *list )
127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 md_info = mbedtls_md_info_from_type( *list );
129 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000130 list++;
131 }
132
Paul Bakkercce9d772011-11-18 14:26:47 +0000133#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000135 fflush( stdout ); getchar();
136#endif
137
138 goto exit;
139 }
140
141 mode = atoi( argv[1] );
142
143 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000146 goto exit;
147 }
148
149 if( strcmp( argv[2], argv[3] ) == 0 )
150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000152 goto exit;
153 }
154
155 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000158 goto exit;
159 }
160
161 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000164 goto exit;
165 }
166
167 /*
168 * Read the Cipher and MD from the command line
169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000171 if( cipher_info == NULL )
172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000174 goto exit;
175 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 if( ( ret = mbedtls_cipher_init_ctx( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_fprintf( stderr, "mbedtls_cipher_init_ctx failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200179 goto exit;
180 }
Paul Bakker20a78082011-01-21 09:32:12 +0000181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 md_info = mbedtls_md_info_from_string( argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000183 if( md_info == NULL )
184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000186 goto exit;
187 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_md_setup( &md_ctx, md_info, 1 );
Paul Bakker20a78082011-01-21 09:32:12 +0000189
190 /*
191 * Read the secret key and clean the command line.
192 */
193 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
194 {
195 keylen = fread( key, 1, sizeof( key ), fkey );
196 fclose( fkey );
197 }
198 else
199 {
200 if( memcmp( argv[6], "hex:", 4 ) == 0 )
201 {
202 p = &argv[6][4];
203 keylen = 0;
204
205 while( sscanf( p, "%02X", &n ) > 0 &&
206 keylen < (int) sizeof( key ) )
207 {
208 key[keylen++] = (unsigned char) n;
209 p += 2;
210 }
211 }
212 else
213 {
214 keylen = strlen( argv[6] );
215
216 if( keylen > (int) sizeof( key ) )
217 keylen = (int) sizeof( key );
218
219 memcpy( key, argv[6], keylen );
220 }
221 }
222
223 memset( argv[6], 0, strlen( argv[6] ) );
224
Paul Bakkercce9d772011-11-18 14:26:47 +0000225#if defined(_WIN32_WCE)
226 filesize = fseek( fin, 0L, SEEK_END );
227#else
228#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000229 /*
230 * Support large files (> 2Gb) on Win32
231 */
232 li_size.QuadPart = 0;
233 li_size.LowPart =
234 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
235 li_size.LowPart, &li_size.HighPart, FILE_END );
236
237 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000240 goto exit;
241 }
242
243 filesize = li_size.QuadPart;
244#else
245 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
246 {
247 perror( "lseek" );
248 goto exit;
249 }
250#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000251#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000252
253 if( fseek( fin, 0, SEEK_SET ) < 0 )
254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000256 goto exit;
257 }
258
259 if( mode == MODE_ENCRYPT )
260 {
261 /*
262 * Generate the initialization vector as:
263 * IV = SHA-256( filesize || filename )[0..15]
264 */
265 for( i = 0; i < 8; i++ )
266 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
267
268 p = argv[2];
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_md_starts( &md_ctx );
271 mbedtls_md_update( &md_ctx, buffer, 8 );
272 mbedtls_md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
273 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000274
275 memcpy( IV, digest, 16 );
276
277 /*
278 * The last four bits in the IV are actually used
279 * to store the file size modulo the AES block size.
280 */
281 lastn = (int)( filesize & 0x0F );
282
283 IV[15] = (unsigned char)
284 ( ( IV[15] & 0xF0 ) | lastn );
285
286 /*
287 * Append the IV at the beginning of the output.
288 */
289 if( fwrite( IV, 1, 16, fout ) != 16 )
290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000292 goto exit;
293 }
294
295 /*
296 * Hash the IV and the secret key together 8192 times
297 * using the result to setup the AES context and HMAC.
298 */
299 memset( digest, 0, 32 );
300 memcpy( digest, IV, 16 );
301
302 for( i = 0; i < 8192; i++ )
303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_md_starts( &md_ctx );
305 mbedtls_md_update( &md_ctx, digest, 32 );
306 mbedtls_md_update( &md_ctx, key, keylen );
307 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000308
309 }
310
311 memset( key, 0, sizeof( key ) );
312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
314 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000317 goto exit;
318 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200322 goto exit;
323 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000327 goto exit;
328 }
Paul Bakker20a78082011-01-21 09:32:12 +0000329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000331
332 /*
333 * Encrypt and write the ciphertext.
334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
338 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000339
Paul Bakker25b5fe52011-05-26 14:02:58 +0000340 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000343 goto exit;
344 }
345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200349 goto exit;
350 }
351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000353
Paul Bakker2c0994e2011-05-25 13:51:57 +0000354 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000357 goto exit;
358 }
359 }
360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000364 goto exit;
365 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000367
Paul Bakker2c0994e2011-05-25 13:51:57 +0000368 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000371 goto exit;
372 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000373
Paul Bakker20a78082011-01-21 09:32:12 +0000374 /*
375 * Finally write the HMAC.
376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000382 goto exit;
383 }
384 }
385
386 if( mode == MODE_DECRYPT )
387 {
388 /*
389 * The encrypted file must be structured as follows:
390 *
391 * 00 .. 15 Initialization Vector
392 * 16 .. 31 AES Encrypted Block #1
393 * ..
394 * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
395 * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000400 goto exit;
401 }
402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 if( ( ( filesize - mbedtls_md_get_size( md_info ) ) %
404 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
407 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000408 goto exit;
409 }
410
411 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100412 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000415
416 /*
417 * Read the IV and original filesize modulo 16.
418 */
419 if( fread( buffer, 1, 16, fin ) != 16 )
420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000422 goto exit;
423 }
424
425 memcpy( IV, buffer, 16 );
426 lastn = IV[15] & 0x0F;
427
428 /*
429 * Hash the IV and the secret key together 8192 times
430 * using the result to setup the AES context and HMAC.
431 */
432 memset( digest, 0, 32 );
433 memcpy( digest, IV, 16 );
434
435 for( i = 0; i < 8192; i++ )
436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_md_starts( &md_ctx );
438 mbedtls_md_update( &md_ctx, digest, 32 );
439 mbedtls_md_update( &md_ctx, key, keylen );
440 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000441 }
442
443 memset( key, 0, sizeof( key ) );
444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_length,
446 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200449 goto exit;
450 }
451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200455 goto exit;
456 }
457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200461 goto exit;
462 }
Paul Bakker20a78082011-01-21 09:32:12 +0000463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000465
466 /*
467 * Decrypt and write the plaintext.
468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 if( fread( buffer, 1, mbedtls_cipher_get_block_size( &cipher_ctx ), fin ) !=
472 (size_t) mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n",
475 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000476 goto exit;
477 }
478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 mbedtls_md_hmac_update( &md_ctx, buffer, mbedtls_cipher_get_block_size( &cipher_ctx ) );
480 if( mbedtls_cipher_update( &cipher_ctx, buffer,
481 mbedtls_cipher_get_block_size( &cipher_ctx ),
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200482 output, &olen ) != 0 )
483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200485 goto exit;
486 }
Paul Bakker20a78082011-01-21 09:32:12 +0000487
Paul Bakker2c0994e2011-05-25 13:51:57 +0000488 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000491 goto exit;
492 }
493 }
494
495 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000496 * Verify the message authentication code.
497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000503 goto exit;
504 }
505
Paul Bakker424cd692013-10-31 14:22:08 +0100506 /* Use constant-time buffer comparison */
507 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100509 diff |= digest[i] ^ buffer[i];
510
511 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000514 "or file corrupted.\n" );
515 goto exit;
516 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100517
518 /*
519 * Write the final block of data
520 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_cipher_finish( &cipher_ctx, output, &olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100522
523 if( fwrite( output, 1, olen, fout ) != olen )
524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100526 goto exit;
527 }
Paul Bakker20a78082011-01-21 09:32:12 +0000528 }
529
530 ret = 0;
531
532exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000533 if( fin )
534 fclose( fin );
535 if( fout )
536 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000537
538 memset( buffer, 0, sizeof( buffer ) );
539 memset( digest, 0, sizeof( digest ) );
540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_cipher_free( &cipher_ctx );
542 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000543
544 return( ret );
545}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */