blob: 5885bdb5a8d2ae7e165947df53474a1a4ccaad80 [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 Bakkera9f02c82016-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>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_fprintf fprintf
34#define mbedtls_printf printf
Rich Evans18b78c72015-02-11 14:06:19 +000035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
38 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/cipher.h"
40#include "mbedtls/md.h"
Rich Evans18b78c72015-02-11 14:06:19 +000041
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000045#endif
46
Paul Bakker494c0b82011-04-24 15:30:07 +000047#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000048#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000049#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000050#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000051#endif
Paul Bakker20a78082011-01-21 09:32:12 +000052#else
53#include <sys/types.h>
54#include <unistd.h>
55#endif
56
Paul Bakker20a78082011-01-21 09:32:12 +000057#define MODE_ENCRYPT 0
58#define MODE_DECRYPT 1
59
60#define USAGE \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
Paul Bakker20a78082011-01-21 09:32:12 +000062 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
63 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
64 "\n"
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
67 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000068int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000069{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 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 +000071 return( 0 );
72}
73#else
Paul Bakker20a78082011-01-21 09:32:12 +000074int main( int argc, char *argv[] )
75{
Paul Bakker25b5fe52011-05-26 14:02:58 +000076 int ret = 1, i, n;
Paul Bakkera9f02c82016-09-02 22:44:09 +020077 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +000078 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000079 FILE *fkey, *fin = NULL, *fout = NULL;
80
81 char *p;
82 unsigned char IV[16];
83 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +000085 unsigned char buffer[1024];
86 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +010087 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +000088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 const mbedtls_cipher_info_t *cipher_info;
90 const mbedtls_md_info_t *md_info;
91 mbedtls_cipher_context_t cipher_ctx;
92 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +000093#if defined(_WIN32_WCE)
94 long filesize, offset;
95#elif defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000096 LARGE_INTEGER li_size;
97 __int64 filesize, offset;
98#else
99 off_t filesize, offset;
100#endif
101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_cipher_init( &cipher_ctx );
103 mbedtls_md_init( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000104
105 /*
106 * Parse the command-line arguments.
107 */
108 if( argc != 7 )
109 {
110 const int *list;
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_printf( USAGE );
Paul Bakker20a78082011-01-21 09:32:12 +0000113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_printf( "Available ciphers:\n" );
115 list = mbedtls_cipher_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000116 while( *list )
117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 cipher_info = mbedtls_cipher_info_from_type( *list );
119 mbedtls_printf( " %s\n", cipher_info->name );
Paul Bakker20a78082011-01-21 09:32:12 +0000120 list++;
121 }
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_printf( "\nAvailable message digests:\n" );
124 list = mbedtls_md_list();
Paul Bakker20a78082011-01-21 09:32:12 +0000125 while( *list )
126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 md_info = mbedtls_md_info_from_type( *list );
128 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000129 list++;
130 }
131
Paul Bakkercce9d772011-11-18 14:26:47 +0000132#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000134 fflush( stdout ); getchar();
135#endif
136
137 goto exit;
138 }
139
140 mode = atoi( argv[1] );
141
142 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_fprintf( stderr, "invalid operation mode\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000145 goto exit;
146 }
147
148 if( strcmp( argv[2], argv[3] ) == 0 )
149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000151 goto exit;
152 }
153
154 if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
Paul Bakker20a78082011-01-21 09:32:12 +0000157 goto exit;
158 }
159
160 if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
Paul Bakker20a78082011-01-21 09:32:12 +0000163 goto exit;
164 }
165
166 /*
167 * Read the Cipher and MD from the command line
168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 cipher_info = mbedtls_cipher_info_from_string( argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000170 if( cipher_info == NULL )
171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
Paul Bakker20a78082011-01-21 09:32:12 +0000173 goto exit;
174 }
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200175 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200176 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200177 mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200178 goto exit;
179 }
Paul Bakker20a78082011-01-21 09:32:12 +0000180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 md_info = mbedtls_md_info_from_string( argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000182 if( md_info == NULL )
183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
Paul Bakker20a78082011-01-21 09:32:12 +0000185 goto exit;
186 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_md_setup( &md_ctx, md_info, 1 );
Paul Bakker20a78082011-01-21 09:32:12 +0000188
189 /*
Hanno Becker09362012017-06-27 11:40:30 +0100190 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000191 */
192 if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
193 {
194 keylen = fread( key, 1, sizeof( key ), fkey );
195 fclose( fkey );
196 }
197 else
198 {
199 if( memcmp( argv[6], "hex:", 4 ) == 0 )
200 {
201 p = &argv[6][4];
202 keylen = 0;
203
204 while( sscanf( p, "%02X", &n ) > 0 &&
205 keylen < (int) sizeof( key ) )
206 {
207 key[keylen++] = (unsigned char) n;
208 p += 2;
209 }
210 }
211 else
212 {
213 keylen = strlen( argv[6] );
214
215 if( keylen > (int) sizeof( key ) )
216 keylen = (int) sizeof( key );
217
218 memcpy( key, argv[6], keylen );
219 }
220 }
221
Paul Bakkercce9d772011-11-18 14:26:47 +0000222#if defined(_WIN32_WCE)
223 filesize = fseek( fin, 0L, SEEK_END );
224#else
225#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000226 /*
227 * Support large files (> 2Gb) on Win32
228 */
229 li_size.QuadPart = 0;
230 li_size.LowPart =
231 SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
232 li_size.LowPart, &li_size.HighPart, FILE_END );
233
234 if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000237 goto exit;
238 }
239
240 filesize = li_size.QuadPart;
241#else
242 if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
243 {
244 perror( "lseek" );
245 goto exit;
246 }
247#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000248#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000249
250 if( fseek( fin, 0, SEEK_SET ) < 0 )
251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000253 goto exit;
254 }
255
256 if( mode == MODE_ENCRYPT )
257 {
258 /*
259 * Generate the initialization vector as:
Paul Bakkera9f02c82016-09-02 22:44:09 +0200260 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000261 */
262 for( i = 0; i < 8; i++ )
263 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
264
265 p = argv[2];
266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_md_starts( &md_ctx );
268 mbedtls_md_update( &md_ctx, buffer, 8 );
269 mbedtls_md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
270 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000271
272 memcpy( IV, digest, 16 );
273
274 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000275 * Append the IV at the beginning of the output.
276 */
277 if( fwrite( IV, 1, 16, fout ) != 16 )
278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000280 goto exit;
281 }
282
283 /*
284 * Hash the IV and the secret key together 8192 times
285 * using the result to setup the AES context and HMAC.
286 */
287 memset( digest, 0, 32 );
288 memcpy( digest, IV, 16 );
289
290 for( i = 0; i < 8192; i++ )
291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_md_starts( &md_ctx );
293 mbedtls_md_update( &md_ctx, digest, 32 );
294 mbedtls_md_update( &md_ctx, key, keylen );
295 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000296
297 }
298
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200299 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 MBEDTLS_ENCRYPT ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000303 goto exit;
304 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200308 goto exit;
309 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000313 goto exit;
314 }
Paul Bakker20a78082011-01-21 09:32:12 +0000315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000317
318 /*
319 * Encrypt and write the ciphertext.
320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
324 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
Paul Bakker20a78082011-01-21 09:32:12 +0000325
Paul Bakker25b5fe52011-05-26 14:02:58 +0000326 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
Paul Bakker20a78082011-01-21 09:32:12 +0000329 goto exit;
330 }
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200335 goto exit;
336 }
337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_md_hmac_update( &md_ctx, output, olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000339
Paul Bakker2c0994e2011-05-25 13:51:57 +0000340 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000343 goto exit;
344 }
345 }
346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000350 goto exit;
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 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000359
Paul Bakker20a78082011-01-21 09:32:12 +0000360 /*
361 * Finally write the HMAC.
362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000368 goto exit;
369 }
370 }
371
372 if( mode == MODE_DECRYPT )
373 {
374 /*
375 * The encrypted file must be structured as follows:
376 *
377 * 00 .. 15 Initialization Vector
Paul Bakkera9f02c82016-09-02 22:44:09 +0200378 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000379 * ..
Paul Bakkera9f02c82016-09-02 22:44:09 +0200380 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
381 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 if( filesize < 16 + mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000384 {
Simon Butcherd7fb3fd2016-09-03 12:43:44 +0100385 mbedtls_fprintf( stderr, "File too short to be decrypted.\n" );
Paul Bakker20a78082011-01-21 09:32:12 +0000386 goto exit;
387 }
388
Simon Butcherd7fb3fd2016-09-03 12:43:44 +0100389 if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
Paul Bakkera9f02c82016-09-02 22:44:09 +0200390 {
391 mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
392 goto exit;
393 }
394
395 /*
396 * Check the file size.
397 */
398 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
399 ( ( filesize - mbedtls_md_get_size( md_info ) ) %
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
403 mbedtls_cipher_get_block_size( &cipher_ctx ));
Paul Bakker20a78082011-01-21 09:32:12 +0000404 goto exit;
405 }
406
407 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100408 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000411
412 /*
413 * Read the IV and original filesize modulo 16.
414 */
415 if( fread( buffer, 1, 16, fin ) != 16 )
416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000418 goto exit;
419 }
420
421 memcpy( IV, buffer, 16 );
Paul Bakker20a78082011-01-21 09:32:12 +0000422
423 /*
424 * Hash the IV and the secret key together 8192 times
425 * using the result to setup the AES context and HMAC.
426 */
427 memset( digest, 0, 32 );
428 memcpy( digest, IV, 16 );
429
430 for( i = 0; i < 8192; i++ )
431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_md_starts( &md_ctx );
433 mbedtls_md_update( &md_ctx, digest, 32 );
434 mbedtls_md_update( &md_ctx, key, keylen );
435 mbedtls_md_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000436 }
437
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200438 if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 MBEDTLS_DECRYPT ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200442 goto exit;
443 }
444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200448 goto exit;
449 }
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200454 goto exit;
455 }
Paul Bakker20a78082011-01-21 09:32:12 +0000456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
Paul Bakker20a78082011-01-21 09:32:12 +0000458
459 /*
460 * Decrypt and write the plaintext.
461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000463 {
Paul Bakkera9f02c82016-09-02 22:44:09 +0200464 ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
465 mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
466
467 if( fread( buffer, 1, ilen, fin ) != ilen )
Paul Bakker20a78082011-01-21 09:32:12 +0000468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n",
470 mbedtls_cipher_get_block_size( &cipher_ctx ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000471 goto exit;
472 }
473
Paul Bakkera9f02c82016-09-02 22:44:09 +0200474 mbedtls_md_hmac_update( &md_ctx, buffer, ilen );
475 if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
476 &olen ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200479 goto exit;
480 }
Paul Bakker20a78082011-01-21 09:32:12 +0000481
Paul Bakker2c0994e2011-05-25 13:51:57 +0000482 if( fwrite( output, 1, olen, fout ) != olen )
Paul Bakker20a78082011-01-21 09:32:12 +0000483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Paul Bakker20a78082011-01-21 09:32:12 +0000485 goto exit;
486 }
487 }
488
489 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000490 * Verify the message authentication code.
491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_md_hmac_finish( &md_ctx, digest );
Paul Bakker20a78082011-01-21 09:32:12 +0000493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
Paul Bakker20a78082011-01-21 09:32:12 +0000495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000497 goto exit;
498 }
499
Paul Bakker424cd692013-10-31 14:22:08 +0100500 /* Use constant-time buffer comparison */
501 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakker424cd692013-10-31 14:22:08 +0100503 diff |= digest[i] ^ buffer[i];
504
505 if( diff != 0 )
Paul Bakker20a78082011-01-21 09:32:12 +0000506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
Paul Bakker20a78082011-01-21 09:32:12 +0000508 "or file corrupted.\n" );
509 goto exit;
510 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100511
512 /*
513 * Write the final block of data
514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_cipher_finish( &cipher_ctx, output, &olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100516
517 if( fwrite( output, 1, olen, fout ) != olen )
518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100520 goto exit;
521 }
Paul Bakker20a78082011-01-21 09:32:12 +0000522 }
523
524 ret = 0;
525
526exit:
Paul Bakker6d440322011-02-06 12:49:19 +0000527 if( fin )
528 fclose( fin );
529 if( fout )
530 fclose( fout );
Paul Bakker20a78082011-01-21 09:32:12 +0000531
Hanno Beckere0c35a62017-06-27 08:22:17 +0100532 /* Zeroize all command line arguments to also cover
533 the case when the user has missed or reordered some,
534 in which case the key might not be in argv[6]. */
535 for( i = 0; i < argc; i++ )
536 memset( argv[i], 0, strlen( argv[i] ) );
537
538 memset( IV, 0, sizeof( IV ) );
539 memset( key, 0, sizeof( key ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000540 memset( buffer, 0, sizeof( buffer ) );
Hanno Beckere0c35a62017-06-27 08:22:17 +0100541 memset( output, 0, sizeof( output ) );
Paul Bakker20a78082011-01-21 09:32:12 +0000542 memset( digest, 0, sizeof( digest ) );
543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_cipher_free( &cipher_ctx );
545 mbedtls_md_free( &md_ctx );
Paul Bakker20a78082011-01-21 09:32:12 +0000546
547 return( ret );
548}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */