summaryrefslogtreecommitdiff
path: root/keyDerivation.cpp
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anytun.org>2008-02-24 19:10:34 +0000
committerChristian Pointner <equinox@anytun.org>2008-02-24 19:10:34 +0000
commitc597afc9f59af4d4596068d042f467127311bfed (patch)
treec08334c099c0608c2368c1978f1c9150abfc094d /keyDerivation.cpp
parentchanged cipher option default value to aes-ctr (diff)
added NullKeyDerivation
added KeyDerivationFactory
Diffstat (limited to 'keyDerivation.cpp')
-rw-r--r--keyDerivation.cpp64
1 files changed, 41 insertions, 23 deletions
diff --git a/keyDerivation.cpp b/keyDerivation.cpp
index cfd70d4..79086b2 100644
--- a/keyDerivation.cpp
+++ b/keyDerivation.cpp
@@ -41,49 +41,66 @@
#include <gcrypt.h>
-void KeyDerivation::init(Buffer key, Buffer salt)
+void KeyDerivation::setLogKDRate(const uint8_t log_rate)
{
Lock lock(mutex_);
- gcry_error_t err;
+ if( log_rate < 49 )
+ ld_kdr_ = log_rate;
+}
- // TODO: hardcoded cipher-type and keysize??
- err = gcry_cipher_open( &cipher_, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CTR, 0 );
- if( err ) {
- cLog.msg(Log::PRIO_ERR) << "KeyDerivation::init: Failed to open cipher: " << gpg_strerror( err );
- return;
- }
+//****** NullKeyDerivation ******
- master_salt_ = SyncBuffer(salt);
- master_key_ = SyncBuffer(key);
+void NullKeyDerivation::generate(satp_prf_label label, seq_nr_t seq_nr, Buffer& key)
+{
+ for(u_int32_t i=0; i < key.getLength(); ++i) key[i] = 0;
+}
- updateMasterKey();
+//****** AesIcmKeyDerivation ******
+
+AesIcmKeyDerivation::~AesIcmKeyDerivation()
+{
+ Lock lock(mutex_);
+ if(cipher_)
+ gcry_cipher_close( cipher_ );
}
-void KeyDerivation::updateMasterKey()
+void AesIcmKeyDerivation::updateMasterKey()
{
- gcry_error_t err;
+ if(!cipher_)
+ return;
- err = gcry_cipher_setkey( cipher_, master_key_.getBuf(), master_key_.getLength() );
+ gcry_error_t err = gcry_cipher_setkey( cipher_, master_key_.getBuf(), master_key_.getLength() );
if( err )
cLog.msg(Log::PRIO_ERR) << "KeyDerivation::updateMasterKey: Failed to set cipher key: " << gpg_strerror( err );
}
-KeyDerivation::~KeyDerivation()
+void AesIcmKeyDerivation::init(Buffer key, Buffer salt)
{
Lock lock(mutex_);
- gcry_cipher_close( cipher_ );
-}
+ if(cipher_)
+ gcry_cipher_close( cipher_ );
-void KeyDerivation::setLogKDRate(const uint8_t log_rate)
-{
- Lock lock(mutex_);
- if( log_rate < 49 )
- ld_kdr_ = log_rate;
+ // TODO: hardcoded cipher-type and keysize??
+ gcry_error_t err = gcry_cipher_open( &cipher_, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CTR, 0 );
+ if( err ) {
+ cLog.msg(Log::PRIO_ERR) << "KeyDerivation::init: Failed to open cipher: " << gpg_strerror( err );
+ return;
+ }
+
+ master_salt_ = SyncBuffer(salt);
+ master_key_ = SyncBuffer(key);
+
+ updateMasterKey();
}
-void KeyDerivation::generate(satp_prf_label label, seq_nr_t seq_nr, Buffer& key)
+void AesIcmKeyDerivation::generate(satp_prf_label label, seq_nr_t seq_nr, Buffer& key)
{
Lock lock(mutex_);
+ if(!cipher_)
+ {
+ cLog.msg(Log::PRIO_ERR) << "KeyDerivation::generate: cipher not opened";
+ return;
+ }
gcry_error_t err = gcry_cipher_reset( cipher_ );
if( err )
@@ -133,3 +150,4 @@ void KeyDerivation::generate(satp_prf_label label, seq_nr_t seq_nr, Buffer& key)
if( err )
cLog.msg(Log::PRIO_ERR) << "KeyDerivation::generate: Failed to generate cipher bitstream: " << gpg_strerror( err );
}
+