mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-12-22 14:15:29 +00:00
Merge pull request #4439 from gilles-peskine-arm/aes2crypt-removal-2.16
Backport 2.16: Remove the sample program aescrypt2
This commit is contained in:
commit
5b8f4db757
3
ChangeLog.d/aescrypt2.txt
Normal file
3
ChangeLog.d/aescrypt2.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
Changes
|
||||||
|
* Remove the AES sample application programs/aes/aescrypt2 which shows
|
||||||
|
bad cryptographic practice. Fix #1906.
|
1
programs/.gitignore
vendored
1
programs/.gitignore
vendored
|
@ -2,7 +2,6 @@
|
||||||
*.sln
|
*.sln
|
||||||
*.vcxproj
|
*.vcxproj
|
||||||
|
|
||||||
aes/aescrypt2
|
|
||||||
aes/crypt_and_hash
|
aes/crypt_and_hash
|
||||||
hash/generic_sum
|
hash/generic_sum
|
||||||
hash/hello
|
hash/hello
|
||||||
|
|
|
@ -47,7 +47,7 @@ ifdef ZLIB
|
||||||
LOCAL_LDFLAGS += -lz
|
LOCAL_LDFLAGS += -lz
|
||||||
endif
|
endif
|
||||||
|
|
||||||
APPS = aes/aescrypt2$(EXEXT) aes/crypt_and_hash$(EXEXT) \
|
APPS = aes/crypt_and_hash$(EXEXT) \
|
||||||
hash/hello$(EXEXT) hash/generic_sum$(EXEXT) \
|
hash/hello$(EXEXT) hash/generic_sum$(EXEXT) \
|
||||||
pkey/dh_client$(EXEXT) \
|
pkey/dh_client$(EXEXT) \
|
||||||
pkey/dh_genprime$(EXEXT) pkey/dh_server$(EXEXT) \
|
pkey/dh_genprime$(EXEXT) pkey/dh_server$(EXEXT) \
|
||||||
|
@ -93,10 +93,6 @@ all: $(APPS)
|
||||||
$(DEP):
|
$(DEP):
|
||||||
$(MAKE) -C ../library
|
$(MAKE) -C ../library
|
||||||
|
|
||||||
aes/aescrypt2$(EXEXT): aes/aescrypt2.c $(DEP)
|
|
||||||
echo " CC aes/aescrypt2.c"
|
|
||||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) aes/aescrypt2.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
|
||||||
|
|
||||||
aes/crypt_and_hash$(EXEXT): aes/crypt_and_hash.c $(DEP)
|
aes/crypt_and_hash$(EXEXT): aes/crypt_and_hash.c $(DEP)
|
||||||
echo " CC aes/crypt_and_hash.c"
|
echo " CC aes/crypt_and_hash.c"
|
||||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) aes/crypt_and_hash.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) aes/crypt_and_hash.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||||
|
|
|
@ -5,9 +5,6 @@ This subdirectory mostly contains sample programs that illustrate specific featu
|
||||||
|
|
||||||
## Symmetric cryptography (AES) examples
|
## Symmetric cryptography (AES) examples
|
||||||
|
|
||||||
* [`aes/aescrypt2.c`](aes/aescrypt2.c): file encryption and authentication with a key derived from a low-entropy secret, demonstrating the low-level AES interface, the digest interface and HMAC.
|
|
||||||
Warning: this program illustrates how to use low-level functions in the library. It should not be taken as an example of how to build a secure encryption mechanism. To derive a key from a low-entropy secret such as a password, use a standard key stretching mechanism such as PBKDF2 (provided by the `pkcs5` module). To encrypt and authenticate data, use a standard mode such as GCM or CCM (both available as library module).
|
|
||||||
|
|
||||||
* [`aes/crypt_and_hash.c`](aes/crypt_and_hash.c): file encryption and authentication, demonstrating the generic cipher interface and the generic hash interface.
|
* [`aes/crypt_and_hash.c`](aes/crypt_and_hash.c): file encryption and authentication, demonstrating the generic cipher interface and the generic hash interface.
|
||||||
|
|
||||||
## Hash (digest) examples
|
## Hash (digest) examples
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
add_executable(aescrypt2 aescrypt2.c)
|
|
||||||
target_link_libraries(aescrypt2 mbedtls)
|
|
||||||
|
|
||||||
add_executable(crypt_and_hash crypt_and_hash.c)
|
add_executable(crypt_and_hash crypt_and_hash.c)
|
||||||
target_link_libraries(crypt_and_hash mbedtls)
|
target_link_libraries(crypt_and_hash mbedtls)
|
||||||
|
|
||||||
install(TARGETS aescrypt2 crypt_and_hash
|
install(TARGETS crypt_and_hash
|
||||||
DESTINATION "bin"
|
DESTINATION "bin"
|
||||||
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
|
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
|
||||||
|
|
|
@ -1,495 +0,0 @@
|
||||||
/*
|
|
||||||
* AES-256 file encryption program
|
|
||||||
*
|
|
||||||
* Copyright The Mbed TLS Contributors
|
|
||||||
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
|
||||||
*
|
|
||||||
* This file is provided under the Apache License 2.0, or the
|
|
||||||
* GNU General Public License v2.0 or later.
|
|
||||||
*
|
|
||||||
* **********
|
|
||||||
* Apache License 2.0:
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*
|
|
||||||
* **********
|
|
||||||
*
|
|
||||||
* **********
|
|
||||||
* GNU General Public License v2.0 or later:
|
|
||||||
*
|
|
||||||
* This program is free software; you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation; either version 2 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License along
|
|
||||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
||||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
||||||
*
|
|
||||||
* **********
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* 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
|
|
||||||
|
|
||||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
|
||||||
#include "mbedtls/config.h"
|
|
||||||
#else
|
|
||||||
#include MBEDTLS_CONFIG_FILE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PLATFORM_C)
|
|
||||||
#include "mbedtls/platform.h"
|
|
||||||
#else
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#define mbedtls_fprintf fprintf
|
|
||||||
#define mbedtls_printf printf
|
|
||||||
#define mbedtls_exit exit
|
|
||||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
|
||||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
|
||||||
#endif /* MBEDTLS_PLATFORM_C */
|
|
||||||
|
|
||||||
#include "mbedtls/aes.h"
|
|
||||||
#include "mbedtls/md.h"
|
|
||||||
#include "mbedtls/platform_util.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#if defined(_WIN32)
|
|
||||||
#include <windows.h>
|
|
||||||
#if !defined(_WIN32_WCE)
|
|
||||||
#include <io.h>
|
|
||||||
#endif
|
|
||||||
#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"
|
|
||||||
|
|
||||||
#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_SHA256_C) || \
|
|
||||||
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_MD_C)
|
|
||||||
int main( void )
|
|
||||||
{
|
|
||||||
mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_SHA256_C "
|
|
||||||
"and/or MBEDTLS_FS_IO and/or MBEDTLS_MD_C "
|
|
||||||
"not defined.\n");
|
|
||||||
mbedtls_exit( 0 );
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
|
|
||||||
|
|
||||||
int main( int argc, char *argv[] )
|
|
||||||
{
|
|
||||||
int ret = 0;
|
|
||||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
|
||||||
|
|
||||||
unsigned int i, n;
|
|
||||||
int mode, lastn;
|
|
||||||
size_t keylen;
|
|
||||||
FILE *fkey, *fin = NULL, *fout = NULL;
|
|
||||||
|
|
||||||
char *p;
|
|
||||||
|
|
||||||
unsigned char IV[16];
|
|
||||||
unsigned char tmp[16];
|
|
||||||
unsigned char key[512];
|
|
||||||
unsigned char digest[32];
|
|
||||||
unsigned char buffer[1024];
|
|
||||||
unsigned char diff;
|
|
||||||
|
|
||||||
mbedtls_aes_context aes_ctx;
|
|
||||||
mbedtls_md_context_t sha_ctx;
|
|
||||||
|
|
||||||
#if defined(_WIN32_WCE)
|
|
||||||
long filesize, offset;
|
|
||||||
#elif defined(_WIN32)
|
|
||||||
LARGE_INTEGER li_size;
|
|
||||||
__int64 filesize, offset;
|
|
||||||
#else
|
|
||||||
off_t filesize, offset;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
mbedtls_aes_init( &aes_ctx );
|
|
||||||
mbedtls_md_init( &sha_ctx );
|
|
||||||
|
|
||||||
ret = mbedtls_md_setup( &sha_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 1 );
|
|
||||||
if( ret != 0 )
|
|
||||||
{
|
|
||||||
mbedtls_printf( " ! mbedtls_md_setup() returned -0x%04x\n", -ret );
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Parse the command-line arguments.
|
|
||||||
*/
|
|
||||||
if( argc != 5 )
|
|
||||||
{
|
|
||||||
mbedtls_printf( USAGE );
|
|
||||||
|
|
||||||
#if defined(_WIN32)
|
|
||||||
mbedtls_printf( "\n Press Enter to exit this program.\n" );
|
|
||||||
fflush( stdout ); getchar();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
mode = atoi( argv[1] );
|
|
||||||
memset( IV, 0, sizeof( IV ) );
|
|
||||||
memset( key, 0, sizeof( key ) );
|
|
||||||
memset( digest, 0, sizeof( digest ) );
|
|
||||||
memset( buffer, 0, sizeof( buffer ) );
|
|
||||||
|
|
||||||
if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
|
|
||||||
{
|
|
||||||
mbedtls_fprintf( stderr, "invalide operation mode\n" );
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
if( strcmp( argv[2], argv[3] ) == 0 )
|
|
||||||
{
|
|
||||||
mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
|
|
||||||
{
|
|
||||||
mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
|
|
||||||
{
|
|
||||||
mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Read the secret key from file or command line
|
|
||||||
*/
|
|
||||||
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 );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(_WIN32_WCE)
|
|
||||||
filesize = fseek( fin, 0L, SEEK_END );
|
|
||||||
#else
|
|
||||||
#if defined(_WIN32)
|
|
||||||
/*
|
|
||||||
* 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 )
|
|
||||||
{
|
|
||||||
mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
filesize = li_size.QuadPart;
|
|
||||||
#else
|
|
||||||
if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
|
|
||||||
{
|
|
||||||
perror( "lseek" );
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if( fseek( fin, 0, SEEK_SET ) < 0 )
|
|
||||||
{
|
|
||||||
mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
|
|
||||||
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];
|
|
||||||
|
|
||||||
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 );
|
|
||||||
|
|
||||||
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 )
|
|
||||||
{
|
|
||||||
mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
|
|
||||||
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++ )
|
|
||||||
{
|
|
||||||
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 );
|
|
||||||
}
|
|
||||||
|
|
||||||
mbedtls_aes_setkey_enc( &aes_ctx, digest, 256 );
|
|
||||||
mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
|
|
||||||
|
|
||||||
/*
|
|
||||||
* 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 )
|
|
||||||
{
|
|
||||||
mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", n );
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
for( i = 0; i < 16; i++ )
|
|
||||||
buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
|
|
||||||
|
|
||||||
mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, buffer, buffer );
|
|
||||||
mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
|
|
||||||
|
|
||||||
if( fwrite( buffer, 1, 16, fout ) != 16 )
|
|
||||||
{
|
|
||||||
mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy( IV, buffer, 16 );
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Finally write the HMAC.
|
|
||||||
*/
|
|
||||||
mbedtls_md_hmac_finish( &sha_ctx, digest );
|
|
||||||
|
|
||||||
if( fwrite( digest, 1, 32, fout ) != 32 )
|
|
||||||
{
|
|
||||||
mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
|
|
||||||
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 )
|
|
||||||
{
|
|
||||||
mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
if( ( filesize & 0x0F ) != 0 )
|
|
||||||
{
|
|
||||||
mbedtls_fprintf( stderr, "File size not a multiple of 16.\n" );
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Subtract the IV + HMAC length.
|
|
||||||
*/
|
|
||||||
filesize -= ( 16 + 32 );
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Read the IV and original filesize modulo 16.
|
|
||||||
*/
|
|
||||||
if( fread( buffer, 1, 16, fin ) != 16 )
|
|
||||||
{
|
|
||||||
mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
|
|
||||||
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++ )
|
|
||||||
{
|
|
||||||
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 );
|
|
||||||
}
|
|
||||||
|
|
||||||
mbedtls_aes_setkey_dec( &aes_ctx, digest, 256 );
|
|
||||||
mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Decrypt and write the plaintext.
|
|
||||||
*/
|
|
||||||
for( offset = 0; offset < filesize; offset += 16 )
|
|
||||||
{
|
|
||||||
if( fread( buffer, 1, 16, fin ) != 16 )
|
|
||||||
{
|
|
||||||
mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy( tmp, buffer, 16 );
|
|
||||||
|
|
||||||
mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
|
|
||||||
mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, buffer, buffer );
|
|
||||||
|
|
||||||
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 )
|
|
||||||
{
|
|
||||||
mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Verify the message authentication code.
|
|
||||||
*/
|
|
||||||
mbedtls_md_hmac_finish( &sha_ctx, digest );
|
|
||||||
|
|
||||||
if( fread( buffer, 1, 32, fin ) != 32 )
|
|
||||||
{
|
|
||||||
mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Use constant-time buffer comparison */
|
|
||||||
diff = 0;
|
|
||||||
for( i = 0; i < 32; i++ )
|
|
||||||
diff |= digest[i] ^ buffer[i];
|
|
||||||
|
|
||||||
if( diff != 0 )
|
|
||||||
{
|
|
||||||
mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
|
|
||||||
"or file corrupted.\n" );
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
|
||||||
|
|
||||||
exit:
|
|
||||||
if( fin )
|
|
||||||
fclose( fin );
|
|
||||||
if( fout )
|
|
||||||
fclose( fout );
|
|
||||||
|
|
||||||
/* 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++ )
|
|
||||||
mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
|
|
||||||
|
|
||||||
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 ) );
|
|
||||||
|
|
||||||
mbedtls_aes_free( &aes_ctx );
|
|
||||||
mbedtls_md_free( &sha_ctx );
|
|
||||||
|
|
||||||
mbedtls_exit( exit_code );
|
|
||||||
}
|
|
||||||
#endif /* MBEDTLS_AES_C && MBEDTLS_SHA256_C && MBEDTLS_FS_IO */
|
|
|
@ -1,173 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
|
||||||
<ItemGroup Label="ProjectConfigurations">
|
|
||||||
<ProjectConfiguration Include="Debug|Win32">
|
|
||||||
<Configuration>Debug</Configuration>
|
|
||||||
<Platform>Win32</Platform>
|
|
||||||
</ProjectConfiguration>
|
|
||||||
<ProjectConfiguration Include="Debug|x64">
|
|
||||||
<Configuration>Debug</Configuration>
|
|
||||||
<Platform>x64</Platform>
|
|
||||||
</ProjectConfiguration>
|
|
||||||
<ProjectConfiguration Include="Release|Win32">
|
|
||||||
<Configuration>Release</Configuration>
|
|
||||||
<Platform>Win32</Platform>
|
|
||||||
</ProjectConfiguration>
|
|
||||||
<ProjectConfiguration Include="Release|x64">
|
|
||||||
<Configuration>Release</Configuration>
|
|
||||||
<Platform>x64</Platform>
|
|
||||||
</ProjectConfiguration>
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<ClCompile Include="..\..\programs\aes\aescrypt2.c" />
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="mbedTLS.vcxproj">
|
|
||||||
<Project>{46cf2d25-6a36-4189-b59c-e4815388e554}</Project>
|
|
||||||
<LinkLibraryDependencies>true</LinkLibraryDependencies>
|
|
||||||
</ProjectReference>
|
|
||||||
</ItemGroup>
|
|
||||||
<PropertyGroup Label="Globals">
|
|
||||||
<ProjectGuid>{7A851DBD-7D57-E8F4-85E5-CCA72AEA7DF8}</ProjectGuid>
|
|
||||||
<Keyword>Win32Proj</Keyword>
|
|
||||||
<RootNamespace>aescrypt2</RootNamespace>
|
|
||||||
</PropertyGroup>
|
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
|
||||||
<ConfigurationType>Application</ConfigurationType>
|
|
||||||
<UseDebugLibraries>true</UseDebugLibraries>
|
|
||||||
<CharacterSet>Unicode</CharacterSet>
|
|
||||||
</PropertyGroup>
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
|
||||||
<ConfigurationType>Application</ConfigurationType>
|
|
||||||
<UseDebugLibraries>true</UseDebugLibraries>
|
|
||||||
<CharacterSet>Unicode</CharacterSet>
|
|
||||||
</PropertyGroup>
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
|
||||||
<ConfigurationType>Application</ConfigurationType>
|
|
||||||
<UseDebugLibraries>false</UseDebugLibraries>
|
|
||||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
|
||||||
<CharacterSet>Unicode</CharacterSet>
|
|
||||||
</PropertyGroup>
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
|
||||||
<ConfigurationType>Application</ConfigurationType>
|
|
||||||
<UseDebugLibraries>false</UseDebugLibraries>
|
|
||||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
|
||||||
<CharacterSet>Unicode</CharacterSet>
|
|
||||||
</PropertyGroup>
|
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
|
||||||
<ImportGroup Label="ExtensionSettings">
|
|
||||||
</ImportGroup>
|
|
||||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
|
||||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
|
||||||
</ImportGroup>
|
|
||||||
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
|
|
||||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
|
||||||
</ImportGroup>
|
|
||||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
|
||||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
|
||||||
</ImportGroup>
|
|
||||||
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
|
|
||||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
|
||||||
</ImportGroup>
|
|
||||||
<PropertyGroup Label="UserMacros" />
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
|
||||||
<LinkIncremental>true</LinkIncremental>
|
|
||||||
<IntDir>$(Configuration)\$(TargetName)\</IntDir>
|
|
||||||
</PropertyGroup>
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
|
||||||
<LinkIncremental>true</LinkIncremental>
|
|
||||||
<IntDir>$(Configuration)\$(TargetName)\</IntDir>
|
|
||||||
</PropertyGroup>
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
|
||||||
<LinkIncremental>false</LinkIncremental>
|
|
||||||
<IntDir>$(Configuration)\$(TargetName)\</IntDir>
|
|
||||||
</PropertyGroup>
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
|
||||||
<LinkIncremental>false</LinkIncremental>
|
|
||||||
<IntDir>$(Configuration)\$(TargetName)\</IntDir>
|
|
||||||
</PropertyGroup>
|
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
|
||||||
<ClCompile>
|
|
||||||
<PrecompiledHeader>
|
|
||||||
</PrecompiledHeader>
|
|
||||||
<WarningLevel>Level3</WarningLevel>
|
|
||||||
<Optimization>Disabled</Optimization>
|
|
||||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
|
||||||
<AdditionalIncludeDirectories>../../include</AdditionalIncludeDirectories>
|
|
||||||
</ClCompile>
|
|
||||||
<Link>
|
|
||||||
<SubSystem>Console</SubSystem>
|
|
||||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
|
||||||
<ShowProgress>NotSet</ShowProgress>
|
|
||||||
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
|
||||||
<AdditionalLibraryDirectories>Debug</AdditionalLibraryDirectories>
|
|
||||||
</Link>
|
|
||||||
<ProjectReference>
|
|
||||||
<LinkLibraryDependencies>false</LinkLibraryDependencies>
|
|
||||||
</ProjectReference>
|
|
||||||
</ItemDefinitionGroup>
|
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
|
||||||
<ClCompile>
|
|
||||||
<PrecompiledHeader>
|
|
||||||
</PrecompiledHeader>
|
|
||||||
<WarningLevel>Level3</WarningLevel>
|
|
||||||
<Optimization>Disabled</Optimization>
|
|
||||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
|
||||||
<AdditionalIncludeDirectories>../../include</AdditionalIncludeDirectories>
|
|
||||||
</ClCompile>
|
|
||||||
<Link>
|
|
||||||
<SubSystem>Console</SubSystem>
|
|
||||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
|
||||||
<ShowProgress>NotSet</ShowProgress>
|
|
||||||
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
|
||||||
<AdditionalLibraryDirectories>Debug</AdditionalLibraryDirectories>
|
|
||||||
</Link>
|
|
||||||
<ProjectReference>
|
|
||||||
<LinkLibraryDependencies>false</LinkLibraryDependencies>
|
|
||||||
</ProjectReference>
|
|
||||||
</ItemDefinitionGroup>
|
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
|
||||||
<ClCompile>
|
|
||||||
<WarningLevel>Level3</WarningLevel>
|
|
||||||
<PrecompiledHeader>
|
|
||||||
</PrecompiledHeader>
|
|
||||||
<Optimization>MaxSpeed</Optimization>
|
|
||||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
|
||||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
|
||||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
|
||||||
<AdditionalIncludeDirectories>../../include</AdditionalIncludeDirectories>
|
|
||||||
</ClCompile>
|
|
||||||
<Link>
|
|
||||||
<SubSystem>Console</SubSystem>
|
|
||||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
|
||||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
|
||||||
<OptimizeReferences>true</OptimizeReferences>
|
|
||||||
<AdditionalLibraryDirectories>Release</AdditionalLibraryDirectories>
|
|
||||||
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
|
||||||
</Link>
|
|
||||||
</ItemDefinitionGroup>
|
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
|
||||||
<ClCompile>
|
|
||||||
<WarningLevel>Level3</WarningLevel>
|
|
||||||
<PrecompiledHeader>
|
|
||||||
</PrecompiledHeader>
|
|
||||||
<Optimization>MaxSpeed</Optimization>
|
|
||||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
|
||||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
|
||||||
<PreprocessorDefinitions>WIN64;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
|
||||||
<AdditionalIncludeDirectories>../../include</AdditionalIncludeDirectories>
|
|
||||||
</ClCompile>
|
|
||||||
<Link>
|
|
||||||
<SubSystem>Console</SubSystem>
|
|
||||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
|
||||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
|
||||||
<OptimizeReferences>true</OptimizeReferences>
|
|
||||||
<AdditionalLibraryDirectories>Release</AdditionalLibraryDirectories>
|
|
||||||
<AdditionalDependencies>%(AdditionalDependencies);</AdditionalDependencies>
|
|
||||||
</Link>
|
|
||||||
</ItemDefinitionGroup>
|
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
|
||||||
<ImportGroup Label="ExtensionTargets">
|
|
||||||
</ImportGroup>
|
|
||||||
</Project>
|
|
|
@ -3,11 +3,6 @@ Microsoft Visual Studio Solution File, Format Version 11.00
|
||||||
# Visual C++ Express 2010
|
# Visual C++ Express 2010
|
||||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mbedTLS", "mbedTLS.vcxproj", "{46CF2D25-6A36-4189-B59C-E4815388E554}"
|
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mbedTLS", "mbedTLS.vcxproj", "{46CF2D25-6A36-4189-B59C-E4815388E554}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "aescrypt2", "aescrypt2.vcxproj", "{7A851DBD-7D57-E8F4-85E5-CCA72AEA7DF8}"
|
|
||||||
ProjectSection(ProjectDependencies) = postProject
|
|
||||||
{46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}
|
|
||||||
EndProjectSection
|
|
||||||
EndProject
|
|
||||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "crypt_and_hash", "crypt_and_hash.vcxproj", "{5DBB9FC3-6FD6-CA8D-E0FA-35F1E75EFAE7}"
|
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "crypt_and_hash", "crypt_and_hash.vcxproj", "{5DBB9FC3-6FD6-CA8D-E0FA-35F1E75EFAE7}"
|
||||||
ProjectSection(ProjectDependencies) = postProject
|
ProjectSection(ProjectDependencies) = postProject
|
||||||
{46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}
|
{46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}
|
||||||
|
@ -259,14 +254,6 @@ Global
|
||||||
{46CF2D25-6A36-4189-B59C-E4815388E554}.Release|Win32.Build.0 = Release|Win32
|
{46CF2D25-6A36-4189-B59C-E4815388E554}.Release|Win32.Build.0 = Release|Win32
|
||||||
{46CF2D25-6A36-4189-B59C-E4815388E554}.Release|x64.ActiveCfg = Release|x64
|
{46CF2D25-6A36-4189-B59C-E4815388E554}.Release|x64.ActiveCfg = Release|x64
|
||||||
{46CF2D25-6A36-4189-B59C-E4815388E554}.Release|x64.Build.0 = Release|x64
|
{46CF2D25-6A36-4189-B59C-E4815388E554}.Release|x64.Build.0 = Release|x64
|
||||||
{7A851DBD-7D57-E8F4-85E5-CCA72AEA7DF8}.Debug|Win32.ActiveCfg = Debug|Win32
|
|
||||||
{7A851DBD-7D57-E8F4-85E5-CCA72AEA7DF8}.Debug|Win32.Build.0 = Debug|Win32
|
|
||||||
{7A851DBD-7D57-E8F4-85E5-CCA72AEA7DF8}.Debug|x64.ActiveCfg = Debug|x64
|
|
||||||
{7A851DBD-7D57-E8F4-85E5-CCA72AEA7DF8}.Debug|x64.Build.0 = Debug|x64
|
|
||||||
{7A851DBD-7D57-E8F4-85E5-CCA72AEA7DF8}.Release|Win32.ActiveCfg = Release|Win32
|
|
||||||
{7A851DBD-7D57-E8F4-85E5-CCA72AEA7DF8}.Release|Win32.Build.0 = Release|Win32
|
|
||||||
{7A851DBD-7D57-E8F4-85E5-CCA72AEA7DF8}.Release|x64.ActiveCfg = Release|x64
|
|
||||||
{7A851DBD-7D57-E8F4-85E5-CCA72AEA7DF8}.Release|x64.Build.0 = Release|x64
|
|
||||||
{5DBB9FC3-6FD6-CA8D-E0FA-35F1E75EFAE7}.Debug|Win32.ActiveCfg = Debug|Win32
|
{5DBB9FC3-6FD6-CA8D-E0FA-35F1E75EFAE7}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||||
{5DBB9FC3-6FD6-CA8D-E0FA-35F1E75EFAE7}.Debug|Win32.Build.0 = Debug|Win32
|
{5DBB9FC3-6FD6-CA8D-E0FA-35F1E75EFAE7}.Debug|Win32.Build.0 = Debug|Win32
|
||||||
{5DBB9FC3-6FD6-CA8D-E0FA-35F1E75EFAE7}.Debug|x64.ActiveCfg = Debug|x64
|
{5DBB9FC3-6FD6-CA8D-E0FA-35F1E75EFAE7}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
|
|
Loading…
Reference in a new issue