diff options
-rw-r--r-- | keyDerivation.cpp | 5 | ||||
-rw-r--r-- | mpi.cpp | 31 | ||||
-rw-r--r-- | mpi.h | 4 |
3 files changed, 29 insertions, 11 deletions
diff --git a/keyDerivation.cpp b/keyDerivation.cpp index b068c0c..3c25ae3 100644 --- a/keyDerivation.cpp +++ b/keyDerivation.cpp @@ -101,7 +101,7 @@ void KeyDerivation::generate(satp_prf_label label, seq_nr_t seq_nr, Buffer& key, gcry_error_t err; Mpi r; - Mpi key_id; + Mpi key_id(128); Mpi iv(128); // see at: http://tools.ietf.org/html/rfc3711#section-4.3 @@ -118,7 +118,7 @@ void KeyDerivation::generate(satp_prf_label label, seq_nr_t seq_nr, Buffer& key, // FIXXME: kdr can be greater than 2^32 (= 2^48) r = static_cast<long unsigned int>(seq_nr / ( 0x01 << ld_kdr_ )); - r.rShift(8); + r = r.mul2exp(8); key_id = r + Mpi(static_cast<long unsigned int>(label)); Mpi salt = Mpi(salt_.getBuf(), salt_.getLength()); @@ -127,7 +127,6 @@ void KeyDerivation::generate(satp_prf_label label, seq_nr_t seq_nr, Buffer& key, err = gcry_cipher_reset( cipher_ ); if( err ) cLog.msg(Log::PRIO_ERR) << "KeyDerivation::generate: Failed to reset cipher: " << gpg_strerror( err ); - err = gcry_cipher_setiv( cipher_ , iv.getBuf().getBuf(), iv.getBuf().getLength()); if( err ) @@ -55,6 +55,7 @@ Mpi::Mpi(const u_int8_t * src, u_int32_t len) { gcry_mpi_scan( &val_, GCRYMPI_FMT_STD, src, len, NULL ); } + void Mpi::operator=(const Mpi &src) { val_ = gcry_mpi_copy(src.val_); @@ -72,18 +73,27 @@ Mpi Mpi::operator+(const Mpi &b) const return res; } -Mpi Mpi::operator^(const Mpi &b) const +Mpi Mpi::operator*(const unsigned long int n) const { - u_int32_t len = 0; + Mpi res; + gcry_mpi_mul_ui(res.val_, val_, n); + return res; +} - Mpi res(gcry_mpi_get_nbits(val_)); +Mpi Mpi::operator^(const Mpi &b) const +{ + u_int32_t a_len=0, b_len=0; + Mpi res; - if(gcry_mpi_get_nbits(val_) != gcry_mpi_get_nbits(b.val_)) - throw std::length_error("mpi::operator^ const"); + a_len = gcry_mpi_get_nbits(val_); + b_len = gcry_mpi_get_nbits(b.val_); - len = gcry_mpi_get_nbits(val_); + if(a_len>=b_len) + res = Mpi(*this); + else + res = Mpi(b); - for(u_int32_t i=0; i<len; i++) { + for(u_int32_t i=0; i<a_len && i<b_len; i++) { if(gcry_mpi_test_bit(val_, i) ^ gcry_mpi_test_bit(b.val_, i)) gcry_mpi_set_bit(res.val_, i); } @@ -95,6 +105,13 @@ void Mpi::rShift(u_int8_t n) gcry_mpi_rshift(val_, val_, n); } +Mpi Mpi::mul2exp(u_int32_t e) const +{ + Mpi res; + gcry_mpi_mul_2exp( res.val_, val_, e ); + return res; +} + Buffer Mpi::getBuf() const { u_int32_t len = 0, written = 0; @@ -49,8 +49,10 @@ public: void operator=(long unsigned int); Mpi operator+(const Mpi &b) const; Mpi operator^(const Mpi &b) const; + Mpi operator*(const unsigned long int n) const; - void rShift(u_int8_t n); + void rShift(u_int8_t n); // LSB on the right side! + Mpi mul2exp(u_int32_t e) const; // value * 2^e Buffer getBuf() const; u_int32_t getLen() const; |