2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* AES-256 file encryption program
|
|
|
|
*
|
2015-07-27 09:11:48 +00:00
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2015-09-04 12:21:07 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
|
|
|
|
2017-12-05 12:08:15 +00:00
|
|
|
/* Enable definition of fileno() even when compiling with -std=c99. Must be
|
|
|
|
* set before config.h, which pulls in glibc's features.h indirectly.
|
|
|
|
* Harmless on other platforms. */
|
|
|
|
#define _POSIX_C_SOURCE 1
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 10:39:06 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/platform.h"
|
2015-01-19 14:26:37 +00:00
|
|
|
#else
|
2015-02-11 14:06:19 +00:00
|
|
|
#include <stdio.h>
|
2018-04-29 18:01:34 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#define mbedtls_fprintf fprintf
|
|
|
|
#define mbedtls_printf printf
|
2018-12-10 13:31:45 +00:00
|
|
|
#define mbedtls_exit exit
|
2018-04-30 21:42:33 +00:00
|
|
|
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
2018-04-29 18:01:34 +00:00
|
|
|
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|
2015-02-11 14:06:19 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/aes.h"
|
2015-03-24 17:22:59 +00:00
|
|
|
#include "mbedtls/md.h"
|
2018-10-12 15:46:37 +00:00
|
|
|
#include "mbedtls/platform_util.h"
|
2015-02-11 14:06:19 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-01-19 14:26:37 +00:00
|
|
|
|
2011-04-24 15:30:07 +00:00
|
|
|
#if defined(_WIN32)
|
2009-01-03 21:22:43 +00:00
|
|
|
#include <windows.h>
|
2011-11-18 14:26:47 +00:00
|
|
|
#if !defined(_WIN32_WCE)
|
2009-01-03 21:22:43 +00:00
|
|
|
#include <io.h>
|
2011-11-18 14:26:47 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MODE_ENCRYPT 0
|
|
|
|
#define MODE_DECRYPT 1
|
|
|
|
|
|
|
|
#define USAGE \
|
|
|
|
"\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
|
|
|
|
"\n <mode>: 0 = encrypt, 1 = decrypt\n" \
|
|
|
|
"\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
|
|
|
|
"\n"
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_SHA256_C) || \
|
|
|
|
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_MD_C)
|
2015-02-12 11:37:29 +00:00
|
|
|
int main( void )
|
2011-05-26 13:16:06 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_SHA256_C "
|
|
|
|
"and/or MBEDTLS_FS_IO and/or MBEDTLS_MD_C "
|
2015-03-24 17:22:59 +00:00
|
|
|
"not defined.\n");
|
2011-05-26 13:16:06 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#else
|
2018-12-06 17:43:31 +00:00
|
|
|
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
int main( int argc, char *argv[] )
|
|
|
|
{
|
2018-04-29 18:01:34 +00:00
|
|
|
int ret = 0;
|
|
|
|
int exit_code = MBEDTLS_EXIT_FAILURE;
|
2011-05-26 13:16:06 +00:00
|
|
|
|
2016-08-30 13:25:24 +00:00
|
|
|
unsigned int i, n;
|
2011-04-24 08:57:21 +00:00
|
|
|
int mode, lastn;
|
|
|
|
size_t keylen;
|
2011-01-21 09:32:12 +00:00
|
|
|
FILE *fkey, *fin = NULL, *fout = NULL;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
char *p;
|
2017-06-27 07:24:34 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
unsigned char IV[16];
|
2017-06-27 07:24:34 +00:00
|
|
|
unsigned char tmp[16];
|
2009-01-03 21:22:43 +00:00
|
|
|
unsigned char key[512];
|
|
|
|
unsigned char digest[32];
|
|
|
|
unsigned char buffer[1024];
|
2013-10-28 11:51:32 +00:00
|
|
|
unsigned char diff;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_aes_context aes_ctx;
|
|
|
|
mbedtls_md_context_t sha_ctx;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2011-11-18 14:26:47 +00:00
|
|
|
#if defined(_WIN32_WCE)
|
|
|
|
long filesize, offset;
|
|
|
|
#elif defined(_WIN32)
|
2009-01-03 21:22:43 +00:00
|
|
|
LARGE_INTEGER li_size;
|
|
|
|
__int64 filesize, offset;
|
|
|
|
#else
|
|
|
|
off_t filesize, offset;
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_aes_init( &aes_ctx );
|
|
|
|
mbedtls_md_init( &sha_ctx );
|
2015-03-24 17:22:59 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_md_setup( &sha_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 1 );
|
2015-03-24 17:22:59 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " ! mbedtls_md_setup() returned -0x%04x\n", -ret );
|
2015-03-24 17:22:59 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
2014-06-18 09:16:11 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Parse the command-line arguments.
|
|
|
|
*/
|
|
|
|
if( argc != 5 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( USAGE );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2011-11-18 14:26:47 +00:00
|
|
|
#if defined(_WIN32)
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "\n Press Enter to exit this program.\n" );
|
2009-01-03 21:22:43 +00:00
|
|
|
fflush( stdout ); getchar();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
mode = atoi( argv[1] );
|
2017-06-27 07:24:34 +00:00
|
|
|
memset( IV, 0, sizeof( IV ) );
|
|
|
|
memset( key, 0, sizeof( key ) );
|
|
|
|
memset( digest, 0, sizeof( digest ) );
|
|
|
|
memset( buffer, 0, sizeof( buffer ) );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "invalide operation mode\n" );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( strcmp( argv[2], argv[3] ) == 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-06-27 10:36:21 +00:00
|
|
|
* Read the secret key from file or command line
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
|
|
|
if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
|
|
|
|
{
|
|
|
|
keylen = fread( key, 1, sizeof( key ), fkey );
|
|
|
|
fclose( fkey );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( memcmp( argv[4], "hex:", 4 ) == 0 )
|
|
|
|
{
|
|
|
|
p = &argv[4][4];
|
|
|
|
keylen = 0;
|
|
|
|
|
|
|
|
while( sscanf( p, "%02X", &n ) > 0 &&
|
|
|
|
keylen < (int) sizeof( key ) )
|
|
|
|
{
|
|
|
|
key[keylen++] = (unsigned char) n;
|
|
|
|
p += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
keylen = strlen( argv[4] );
|
|
|
|
|
|
|
|
if( keylen > (int) sizeof( key ) )
|
|
|
|
keylen = (int) sizeof( key );
|
|
|
|
|
|
|
|
memcpy( key, argv[4], keylen );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-18 14:26:47 +00:00
|
|
|
#if defined(_WIN32_WCE)
|
|
|
|
filesize = fseek( fin, 0L, SEEK_END );
|
|
|
|
#else
|
|
|
|
#if defined(_WIN32)
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Support large files (> 2Gb) on Win32
|
|
|
|
*/
|
|
|
|
li_size.QuadPart = 0;
|
|
|
|
li_size.LowPart =
|
|
|
|
SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
|
|
|
|
li_size.LowPart, &li_size.HighPart, FILE_END );
|
|
|
|
|
|
|
|
if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
filesize = li_size.QuadPart;
|
|
|
|
#else
|
|
|
|
if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
|
|
|
|
{
|
|
|
|
perror( "lseek" );
|
|
|
|
goto exit;
|
|
|
|
}
|
2011-11-18 14:26:47 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if( fseek( fin, 0, SEEK_SET ) < 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( mode == MODE_ENCRYPT )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Generate the initialization vector as:
|
|
|
|
* IV = SHA-256( filesize || filename )[0..15]
|
|
|
|
*/
|
|
|
|
for( i = 0; i < 8; i++ )
|
|
|
|
buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
|
|
|
|
|
|
|
|
p = argv[2];
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_starts( &sha_ctx );
|
|
|
|
mbedtls_md_update( &sha_ctx, buffer, 8 );
|
|
|
|
mbedtls_md_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
|
|
|
|
mbedtls_md_finish( &sha_ctx, digest );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
memcpy( IV, digest, 16 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The last four bits in the IV are actually used
|
|
|
|
* to store the file size modulo the AES block size.
|
|
|
|
*/
|
|
|
|
lastn = (int)( filesize & 0x0F );
|
|
|
|
|
|
|
|
IV[15] = (unsigned char)
|
|
|
|
( ( IV[15] & 0xF0 ) | lastn );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Append the IV at the beginning of the output.
|
|
|
|
*/
|
|
|
|
if( fwrite( IV, 1, 16, fout ) != 16 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hash the IV and the secret key together 8192 times
|
|
|
|
* using the result to setup the AES context and HMAC.
|
|
|
|
*/
|
|
|
|
memset( digest, 0, 32 );
|
|
|
|
memcpy( digest, IV, 16 );
|
|
|
|
|
|
|
|
for( i = 0; i < 8192; i++ )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_starts( &sha_ctx );
|
|
|
|
mbedtls_md_update( &sha_ctx, digest, 32 );
|
|
|
|
mbedtls_md_update( &sha_ctx, key, keylen );
|
|
|
|
mbedtls_md_finish( &sha_ctx, digest );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_aes_setkey_enc( &aes_ctx, digest, 256 );
|
|
|
|
mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Encrypt and write the ciphertext.
|
|
|
|
*/
|
|
|
|
for( offset = 0; offset < filesize; offset += 16 )
|
|
|
|
{
|
|
|
|
n = ( filesize - offset > 16 ) ? 16 : (int)
|
|
|
|
( filesize - offset );
|
|
|
|
|
|
|
|
if( fread( buffer, 1, n, fin ) != (size_t) n )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", n );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < 16; i++ )
|
|
|
|
buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, buffer, buffer );
|
|
|
|
mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( fwrite( buffer, 1, 16, fout ) != 16 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy( IV, buffer, 16 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finally write the HMAC.
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_hmac_finish( &sha_ctx, digest );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( fwrite( digest, 1, 32, fout ) != 32 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( mode == MODE_DECRYPT )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The encrypted file must be structured as follows:
|
|
|
|
*
|
|
|
|
* 00 .. 15 Initialization Vector
|
|
|
|
* 16 .. 31 AES Encrypted Block #1
|
|
|
|
* ..
|
|
|
|
* N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
|
|
|
|
* (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
|
|
|
|
*/
|
|
|
|
if( filesize < 48 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( filesize & 0x0F ) != 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "File size not a multiple of 16.\n" );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-10-29 09:02:51 +00:00
|
|
|
* Subtract the IV + HMAC length.
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
|
|
|
filesize -= ( 16 + 32 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the IV and original filesize modulo 16.
|
|
|
|
*/
|
|
|
|
if( fread( buffer, 1, 16, fin ) != 16 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy( IV, buffer, 16 );
|
|
|
|
lastn = IV[15] & 0x0F;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hash the IV and the secret key together 8192 times
|
|
|
|
* using the result to setup the AES context and HMAC.
|
|
|
|
*/
|
|
|
|
memset( digest, 0, 32 );
|
|
|
|
memcpy( digest, IV, 16 );
|
|
|
|
|
|
|
|
for( i = 0; i < 8192; i++ )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_starts( &sha_ctx );
|
|
|
|
mbedtls_md_update( &sha_ctx, digest, 32 );
|
|
|
|
mbedtls_md_update( &sha_ctx, key, keylen );
|
|
|
|
mbedtls_md_finish( &sha_ctx, digest );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_aes_setkey_dec( &aes_ctx, digest, 256 );
|
|
|
|
mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Decrypt and write the plaintext.
|
|
|
|
*/
|
|
|
|
for( offset = 0; offset < filesize; offset += 16 )
|
|
|
|
{
|
|
|
|
if( fread( buffer, 1, 16, fin ) != 16 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy( tmp, buffer, 16 );
|
2013-06-30 12:34:05 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
|
|
|
|
mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, buffer, buffer );
|
2013-06-30 12:34:05 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
for( i = 0; i < 16; i++ )
|
|
|
|
buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
|
|
|
|
|
|
|
|
memcpy( IV, tmp, 16 );
|
|
|
|
|
|
|
|
n = ( lastn > 0 && offset == filesize - 16 )
|
|
|
|
? lastn : 16;
|
|
|
|
|
|
|
|
if( fwrite( buffer, 1, n, fout ) != (size_t) n )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify the message authentication code.
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_hmac_finish( &sha_ctx, digest );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( fread( buffer, 1, 32, fin ) != 32 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2013-10-28 11:51:32 +00:00
|
|
|
/* Use constant-time buffer comparison */
|
|
|
|
diff = 0;
|
|
|
|
for( i = 0; i < 32; i++ )
|
|
|
|
diff |= digest[i] ^ buffer[i];
|
|
|
|
|
|
|
|
if( diff != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
|
2009-01-03 21:22:43 +00:00
|
|
|
"or file corrupted.\n" );
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-29 18:01:34 +00:00
|
|
|
exit_code = MBEDTLS_EXIT_SUCCESS;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
exit:
|
2011-02-06 12:49:19 +00:00
|
|
|
if( fin )
|
|
|
|
fclose( fin );
|
|
|
|
if( fout )
|
|
|
|
fclose( fout );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2017-06-27 07:24:34 +00:00
|
|
|
/* Zeroize all command line arguments to also cover
|
|
|
|
the case when the user has missed or reordered some,
|
|
|
|
in which case the key might not be in argv[4]. */
|
|
|
|
for( i = 0; i < (unsigned int) argc; i++ )
|
2018-10-12 15:46:37 +00:00
|
|
|
mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
|
2017-06-27 07:24:34 +00:00
|
|
|
|
2018-10-12 15:46:37 +00:00
|
|
|
mbedtls_platform_zeroize( IV, sizeof( IV ) );
|
|
|
|
mbedtls_platform_zeroize( key, sizeof( key ) );
|
|
|
|
mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
|
|
|
|
mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
|
|
|
|
mbedtls_platform_zeroize( digest, sizeof( digest ) );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_aes_free( &aes_ctx );
|
|
|
|
mbedtls_md_free( &sha_ctx );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2018-04-29 18:01:34 +00:00
|
|
|
return( exit_code );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_AES_C && MBEDTLS_SHA256_C && MBEDTLS_FS_IO */
|