From 20a7808d130d986b5fa3c8543df03ffa874c7300 Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Fri, 21 Jan 2011 09:32:12 +0000 Subject: [PATCH] - Addec crypt_and_hash example program of the generic hash and cipher layers --- ChangeLog | 1 + programs/Makefile | 7 +- programs/aes/CMakeLists.txt | 5 +- programs/aes/aescrypt2.c | 4 +- programs/aes/crypt_and_hash.c | 462 ++++++++++++++++++++++++++++++++++ 5 files changed, 476 insertions(+), 3 deletions(-) create mode 100644 programs/aes/crypt_and_hash.c diff --git a/ChangeLog b/ChangeLog index d9ccc6b82..db25d3c6f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -17,6 +17,7 @@ Note: Most of these features have been donated by Fox-IT status, objects and configuration + Added verification callback on certificate chain verification to allow external blacklisting + + Additional example programs to show usage * Added support for PKCS#11 through the use of the libpkcs11-helper library diff --git a/programs/Makefile b/programs/Makefile index 696ba176e..7380ad2ee 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -7,7 +7,8 @@ CFLAGS = -I../include -D_FILE_OFFSET_BITS=64 -Wall -W -Wdeclaration-after-statem OFLAGS = -O LDFLAGS = -L../library -lpolarssl -APPS = aes/aescrypt2 hash/hello \ +APPS = aes/aescrypt2 aes/crypt_and_hash \ + hash/hello \ hash/md5sum hash/sha1sum \ hash/sha2sum pkey/dh_client \ pkey/dh_genprime pkey/dh_server \ @@ -26,6 +27,10 @@ aes/aescrypt2: aes/aescrypt2.c ../library/libpolarssl.a echo " CC aes/aescrypt2.c" $(CC) $(CFLAGS) $(OFLAGS) aes/aescrypt2.c $(LDFLAGS) -o $@ +aes/crypt_and_hash: aes/crypt_and_hash.c ../library/libpolarssl.a + echo " CC aes/crypt_and_hash.c" + $(CC) $(CFLAGS) $(OFLAGS) aes/crypt_and_hash.c $(LDFLAGS) -o $@ + hash/hello: hash/hello.c ../library/libpolarssl.a echo " CC hash/hello.c" $(CC) $(CFLAGS) $(OFLAGS) hash/hello.c $(LDFLAGS) -o $@ diff --git a/programs/aes/CMakeLists.txt b/programs/aes/CMakeLists.txt index 52b5e1223..0695a345f 100644 --- a/programs/aes/CMakeLists.txt +++ b/programs/aes/CMakeLists.txt @@ -1,6 +1,9 @@ add_executable(aescrypt2 aescrypt2.c) target_link_libraries(aescrypt2 polarssl) -INSTALL(TARGETS aescrypt2 +add_executable(crypt_and_hash crypt_and_hash.c) +target_link_libraries(crypt_and_hash polarssl) + +INSTALL(TARGETS aescrypt2 crypt_and_hash DESTINATION "bin" PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) diff --git a/programs/aes/aescrypt2.c b/programs/aes/aescrypt2.c index 1988ec669..c291c1828 100644 --- a/programs/aes/aescrypt2.c +++ b/programs/aes/aescrypt2.c @@ -56,7 +56,7 @@ int main( int argc, char *argv[] ) { int ret = 1, i, n; int keylen, mode, lastn; - FILE *fkey, *fin, *fout; + FILE *fkey, *fin = NULL, *fout = NULL; char *p; unsigned char IV[16]; @@ -390,6 +390,8 @@ int main( int argc, char *argv[] ) ret = 0; exit: + fclose( fin ); + fclose( fout ); memset( buffer, 0, sizeof( buffer ) ); memset( digest, 0, sizeof( digest ) ); diff --git a/programs/aes/crypt_and_hash.c b/programs/aes/crypt_and_hash.c new file mode 100644 index 000000000..7014af0bd --- /dev/null +++ b/programs/aes/crypt_and_hash.c @@ -0,0 +1,462 @@ +/* + * \brief Generic file encryption program using generic wrappers for configured + * security. + * + * Copyright (C) 2006-2010, Brainspark B.V. + * + * This file is part of PolarSSL (http://www.polarssl.org) + * Lead Maintainer: Paul Bakker + * + * All rights reserved. + * + * 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. + */ + +#ifndef _CRT_SECURE_NO_DEPRECATE +#define _CRT_SECURE_NO_DEPRECATE 1 +#endif + +#if defined(WIN32) +#include +#include +#else +#include +#include +#endif + +#include +#include +#include +#include + +#include "polarssl/cipher.h" +#include "polarssl/md.h" + +#define MODE_ENCRYPT 0 +#define MODE_DECRYPT 1 + +#define USAGE \ + "\n crypt_and_hash \n" \ + "\n : 0 = encrypt, 1 = decrypt\n" \ + "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \ + "\n" + +int main( int argc, char *argv[] ) +{ + int ret = 1, i, n; + int keylen, mode, lastn, olen; + FILE *fkey, *fin = NULL, *fout = NULL; + + char *p; + unsigned char IV[16]; + unsigned char key[512]; + unsigned char digest[POLARSSL_MD_MAX_SIZE]; + unsigned char buffer[1024]; + unsigned char output[1024]; + + const cipher_info_t *cipher_info; + const md_info_t *md_info; + cipher_context_t cipher_ctx; + md_context_t md_ctx; +#if defined(WIN32) + LARGE_INTEGER li_size; + __int64 filesize, offset; +#else + off_t filesize, offset; +#endif + + memset( &cipher_ctx, 0, sizeof( cipher_context_t )); + memset( &md_ctx, 0, sizeof( md_context_t )); + + /* + * Parse the command-line arguments. + */ + if( argc != 7 ) + { + const int *list; + + printf( USAGE ); + + printf( "Available ciphers:\n" ); + list = cipher_list(); + while( *list ) + { + cipher_info = cipher_info_from_type( *list ); + printf( " %s\n", cipher_info->name ); + list++; + } + + printf( "\nAvailable message digests:\n" ); + list = md_list(); + while( *list ) + { + md_info = md_info_from_type( *list ); + printf( " %s\n", md_info->name ); + list++; + } + +#if defined(WIN32) + printf( "\n Press Enter to exit this program.\n" ); + fflush( stdout ); getchar(); +#endif + + goto exit; + } + + mode = atoi( argv[1] ); + + if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT ) + { + fprintf( stderr, "invalid operation mode\n" ); + goto exit; + } + + if( strcmp( argv[2], argv[3] ) == 0 ) + { + fprintf( stderr, "input and output filenames must differ\n" ); + goto exit; + } + + if( ( fin = fopen( argv[2], "rb" ) ) == NULL ) + { + fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] ); + goto exit; + } + + if( ( fout = fopen( argv[3], "wb+" ) ) == NULL ) + { + fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] ); + goto exit; + } + + /* + * Read the Cipher and MD from the command line + */ + cipher_info = cipher_info_from_string( argv[4] ); + if( cipher_info == NULL ) + { + fprintf( stderr, "Cipher '%s' not found\n", argv[4] ); + goto exit; + } + cipher_init_ctx( &cipher_ctx, cipher_info); + + md_info = md_info_from_string( argv[5] ); + if( md_info == NULL ) + { + fprintf( stderr, "Message Digest '%s' not found\n", argv[5] ); + goto exit; + } + md_init_ctx( &md_ctx, md_info); + + /* + * Read the secret key and clean the command line. + */ + if( ( fkey = fopen( argv[6], "rb" ) ) != NULL ) + { + keylen = fread( key, 1, sizeof( key ), fkey ); + fclose( fkey ); + } + else + { + if( memcmp( argv[6], "hex:", 4 ) == 0 ) + { + p = &argv[6][4]; + keylen = 0; + + while( sscanf( p, "%02X", &n ) > 0 && + keylen < (int) sizeof( key ) ) + { + key[keylen++] = (unsigned char) n; + p += 2; + } + } + else + { + keylen = strlen( argv[6] ); + + if( keylen > (int) sizeof( key ) ) + keylen = (int) sizeof( key ); + + memcpy( key, argv[6], keylen ); + } + } + + memset( argv[6], 0, strlen( argv[6] ) ); + +#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 ) + { + 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 + + if( fseek( fin, 0, SEEK_SET ) < 0 ) + { + 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]; + + md_starts( &md_ctx ); + md_update( &md_ctx, buffer, 8 ); + md_update( &md_ctx, (unsigned char *) p, strlen( p ) ); + md_finish( &md_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 ) + { + 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++ ) + { + md_starts( &md_ctx ); + md_update( &md_ctx, digest, 32 ); + md_update( &md_ctx, key, keylen ); + md_finish( &md_ctx, digest ); + + } + + memset( key, 0, sizeof( key ) ); + + cipher_setkey( &cipher_ctx, digest, cipher_info->key_length, + POLARSSL_ENCRYPT ); + cipher_reset( &cipher_ctx, IV); + + md_hmac_starts( &md_ctx, digest, 32 ); + + /* + * Encrypt and write the ciphertext. + */ + for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) ) + { + n = ( filesize - offset > cipher_get_block_size( &cipher_ctx ) ) ? + cipher_get_block_size( &cipher_ctx ) : (int) ( filesize - offset ); + + if( fread( buffer, 1, n, fin ) != (size_t) n ) + { + fprintf( stderr, "fread(%d bytes) failed\n", n ); + goto exit; + } + + cipher_update( &cipher_ctx, buffer, n, output, &olen ); + md_hmac_update( &md_ctx, output, olen ); + + if( fwrite( output, 1, olen, fout ) != (size_t) olen ) + { + fprintf( stderr, "fwrite(%d bytes) failed\n", olen ); + goto exit; + } + } + + cipher_finish( &cipher_ctx, output, &olen ); + md_hmac_update( &md_ctx, output, olen ); + + if( fwrite( output, 1, olen, fout ) != (size_t) olen ) + { + fprintf( stderr, "fwrite(%d bytes) failed\n", n ); + goto exit; + } + /* + * Finally write the HMAC. + */ + md_hmac_finish( &md_ctx, digest ); + + if( fwrite( digest, 1, md_get_size( md_info), fout ) != md_get_size( md_info) ) + { + fprintf( stderr, "fwrite(%d bytes) failed\n", md_get_size( md_info) ); + 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 < 16 + md_get_size( md_info) ) + { + fprintf( stderr, "File too short to be encrypted.\n" ); + goto exit; + } + + if( ( filesize & 0x0F ) != 0 ) + { + fprintf( stderr, "File size not a multiple of 16.\n" ); + goto exit; + } + + /* + * Substract the IV + HMAC length. + */ + filesize -= ( 16 + md_get_size( md_info ) ); + + /* + * Read the IV and original filesize modulo 16. + */ + if( fread( buffer, 1, 16, fin ) != 16 ) + { + 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++ ) + { + md_starts( &md_ctx ); + md_update( &md_ctx, digest, 32 ); + md_update( &md_ctx, key, keylen ); + md_finish( &md_ctx, digest ); + } + + memset( key, 0, sizeof( key ) ); + + cipher_setkey( &cipher_ctx, digest, cipher_info->key_length, + POLARSSL_DECRYPT ); + cipher_reset( &cipher_ctx, IV); + + md_hmac_starts( &md_ctx, digest, 32 ); + + /* + * Decrypt and write the plaintext. + */ + for( offset = 0; offset < filesize; offset += cipher_get_block_size( &cipher_ctx ) ) + { + if( fread( buffer, 1, cipher_get_block_size( &cipher_ctx ), fin ) != + (size_t) cipher_get_block_size( &cipher_ctx ) ) + { + fprintf( stderr, "fread(%d bytes) failed\n", + cipher_get_block_size( &cipher_ctx ) ); + goto exit; + } + + md_hmac_update( &md_ctx, buffer, cipher_get_block_size( &cipher_ctx ) ); + cipher_update( &cipher_ctx, buffer, cipher_get_block_size( &cipher_ctx ), + output, &olen ); + + if( fwrite( output, 1, olen, fout ) != (size_t) olen ) + { + fprintf( stderr, "fwrite(%d bytes) failed\n", olen ); + goto exit; + } + } + + /* + * Write the final block of data + */ + cipher_finish( &cipher_ctx, output, &olen ); + + if( fwrite( output, 1, olen, fout ) != (size_t) olen ) + { + fprintf( stderr, "fwrite(%d bytes) failed\n", olen ); + goto exit; + } + + /* + * Verify the message authentication code. + */ + md_hmac_finish( &md_ctx, digest ); + + if( fread( buffer, 1, md_get_size( md_info ), fin ) != md_get_size( md_info ) ) + { + fprintf( stderr, "fread(%d bytes) failed\n", md_get_size( md_info ) ); + goto exit; + } + + if( memcmp( digest, buffer, md_get_size( md_info ) ) != 0 ) + { + fprintf( stderr, "HMAC check failed: wrong key, " + "or file corrupted.\n" ); + goto exit; + } + } + + ret = 0; + +exit: + fclose( fin ); + fclose( fout ); + + memset( buffer, 0, sizeof( buffer ) ); + memset( digest, 0, sizeof( digest ) ); + + cipher_free_ctx( &cipher_ctx ); + md_free_ctx( &md_ctx ); + + return( ret ); +}