From dd056636dd2cd8c83822ebecfc2448cf8b4b713c Mon Sep 17 00:00:00 2001 From: Erwin Nindl Date: Wed, 7 Nov 2007 19:08:25 +0000 Subject: * TunDevice: changed 'char* getTypeString()' to 'const char* getTypeString()' to fix warning: deprecated conversion from string constant to ‘char*’ * removed libsrtp, use libgcrypt instead now * added buffer funcitons MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TODO: * fix IV issues * add authentification --- keyDerivation.cpp | 113 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 70 insertions(+), 43 deletions(-) (limited to 'keyDerivation.cpp') diff --git a/keyDerivation.cpp b/keyDerivation.cpp index 95b94d2..f8e3c55 100644 --- a/keyDerivation.cpp +++ b/keyDerivation.cpp @@ -31,82 +31,109 @@ #include "keyDerivation.h" +#include +#include +#include + extern "C" { -#include +#include } -err_status_t KeyDerivation::init(Buffer key, Buffer salt) +const char* KeyDerivation::MIN_GCRYPT_VERSION = "1.2.3"; + +void KeyDerivation::init(Buffer key, Buffer salt) { - extern cipher_type_t aes_icm; - err_status_t status = err_status_ok; + gcry_error_t err; + if( !gcry_check_version( MIN_GCRYPT_VERSION ) ) + { + std::cerr << "Invalid Version of libgcrypt, should be >= " << MIN_GCRYPT_VERSION << std::endl; + return; + } - salt_ = salt; + /* Allocate a pool of 16k secure memory. This also drops priviliges + * on some systems. */ + err = gcry_control(GCRYCTL_INIT_SECMEM, 16384, 0); + if( err ) + { + std::cerr << "Failed to allocate 16k secure memory: " << gpg_strerror( err ) << std::endl; + return; + } - // allocate cipher - // FIXXME: why we do not can do this?? -// status = cipher_type_alloc(&aes_icm, &cipher_, key.getLength()); - status = cipher_type_alloc(&aes_icm, &cipher_, 30); - if( status ) - return status; + /* Tell Libgcrypt that initialization has completed. */ + err = gcry_control(GCRYCTL_INITIALIZATION_FINISHED); + if( err ) + { + std::cerr << "Failed to finish the initialization of libgcrypt" << gpg_strerror( err ) << std::endl; + return; + } - // init cipher - status = cipher_init(cipher_, key.getBuf(), direction_any); - if( status ) - cipher_dealloc(cipher_); + err = gcry_cipher_open( &cipher_, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CTR, 0 ); + if( err ) + { + std::cerr << "Failed to open cipher: " << gpg_strerror( err ) << std::endl; + return; + } - return status; } -err_status_t KeyDerivation::setLogKDRate(const uint8_t log_rate) +void KeyDerivation::setLogKDRate(const uint8_t log_rate) { if( log_rate < 49 ) - { ld_kdr_ = log_rate; - return err_status_ok; - } - return err_status_bad_param; } -err_status_t KeyDerivation::generate(satp_prf_label label, seq_nr_t seq_nr, Buffer& key, uint32_t length) +void KeyDerivation::generate(satp_prf_label label, seq_nr_t seq_nr, Buffer& key, u_int32_t length) { - err_status_t status = err_status_ok; - v128_t iv, salt, key_id; - uint8_t r = 0; + gcry_error_t err; + u_int8_t r = 0; + Buffer iv(16); + + u_int8_t tmp_key_id[16]; - v128_set_to_zero(&iv); - v128_set_to_zero(&salt); - v128_set_to_zero(&key_id); + // see at: http://tools.ietf.org/html/rfc3711#section-4.3 + // * Let r = index DIV key_derivation_rate (with DIV as defined above). + // * Let key_id =