From 9f57f092649be589e67db4960d97669294d43d9c Mon Sep 17 00:00:00 2001 From: cslashm Date: Mon, 26 Mar 2018 12:38:38 +0200 Subject: [PATCH] Change mutex lock model to avoid dead lock and ensure locks are always released. Additional cosmetic fixes: move 'name' as protected remove unnecessary local var Fix debug log --- src/device/device.hpp | 14 ++---- src/device/device_default.cpp | 3 -- src/device/device_ledger.cpp | 87 ++++++++++++++++++----------------- src/device/device_ledger.hpp | 2 - 4 files changed, 49 insertions(+), 57 deletions(-) diff --git a/src/device/device.hpp b/src/device/device.hpp index 9df0cb39d..91bc30c53 100644 --- a/src/device/device.hpp +++ b/src/device/device.hpp @@ -78,7 +78,6 @@ namespace hw { return false; } - class device { protected: std::string name; @@ -90,12 +89,10 @@ namespace hw { virtual ~device() {} explicit virtual operator bool() const = 0; - enum device_mode { - NONE, - TRANSACTION_CREATE_REAL, - TRANSACTION_CREATE_FAKE, - TRANSACTION_PARSE - }; + + static const int SIGNATURE_REAL = 0; + static const int SIGNATURE_FAKE = 1; + /* ======================================================================= */ /* SETUP/TEARDOWN */ @@ -109,9 +106,6 @@ namespace hw { virtual bool connect(void) = 0; virtual bool disconnect(void) = 0; - virtual bool set_mode(device_mode mode) = 0; - - /* ======================================================================= */ /* LOCKER */ /* ======================================================================= */ diff --git a/src/device/device_default.cpp b/src/device/device_default.cpp index 0071f7d4f..3f7b8078d 100644 --- a/src/device/device_default.cpp +++ b/src/device/device_default.cpp @@ -82,9 +82,6 @@ namespace hw { dfns(); } - bool device_default::set_mode(device_mode mode) { - return true; - } /* ======================================================================= */ /* LOCKER */ diff --git a/src/device/device_ledger.cpp b/src/device/device_ledger.cpp index 373f4a282..c919c4ac1 100644 --- a/src/device/device_ledger.cpp +++ b/src/device/device_ledger.cpp @@ -511,13 +511,12 @@ namespace hw { return true; } - bool device_ledger::get_secret_keys(crypto::secret_key &vkey , crypto::secret_key &skey) { + bool device_ledger::get_secret_keys(crypto::secret_key &viewkey , crypto::secret_key &spendkey) { AUTO_LOCK_CMD(); + memset(viewkey.data, 0x00, 32); + memset(spendkey.data, 0xFF, 32); - //secret key are represented as fake key on the wallet side - memset(vkey.data, 0x00, 32); - memset(skey.data, 0xFF, 32); - + #ifdef DEBUG_HWDEVICE //spcialkey, normal conf handled in decrypt int offset; reset_buffer(); @@ -536,22 +535,12 @@ namespace hw { this->length_send = offset; this->exchange(); - //View key is retrievied, if allowed, to speed up blockchain parsing - memmove(this->viewkey.data, this->buffer_recv+0, 32); - if (is_fake_view_key(this->viewkey)) { - MDEBUG("Have Not view key"); - this->has_view_key = false; - } else { - MDEBUG("Have view key"); - this->has_view_key = true; - } - - #ifdef DEBUG_HWDEVICE - memmove(dbg_viewkey.data, this->buffer_recv+0, 32); - memmove(dbg_spendkey.data, this->buffer_recv+32, 32); - #endif + //clear key + memmove(ledger::viewkey.data, this->buffer_recv+64, 32); + memmove(ledger::spendkey.data, this->buffer_recv+96, 32); - return true; + #endif + return true; } bool device_ledger::generate_chacha_key(const cryptonote::account_keys &keys, crypto::chacha_key &key) { @@ -596,6 +585,8 @@ namespace hw { bool device_ledger::derive_subaddress_public_key(const crypto::public_key &pub, const crypto::key_derivation &derivation, const std::size_t output_index, crypto::public_key &derived_pub){ AUTO_LOCK_CMD(); + int offset; + #ifdef DEBUG_HWDEVICE const crypto::public_key pub_x = pub; crypto::key_derivation derivation_x; @@ -652,10 +643,10 @@ namespace hw { //pub key memmove(derived_pub.data, &this->buffer_recv[0], 32); - } - #ifdef DEBUG_HWDEVICE - hw::ledger::check32("derive_subaddress_public_key", "derived_pub", derived_pub_x.data, derived_pub.data); - #endif + + #ifdef DEBUG_HWDEVICE + hw::ledger::check32("derive_subaddress_public_key", "derived_pub", derived_pub_x.data, derived_pub.data); + #endif return true; } @@ -1042,7 +1033,7 @@ namespace hw { bool device_ledger::generate_key_derivation(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_derivation &derivation) { AUTO_LOCK_CMD(); - bool r = false; + int offset; #ifdef DEBUG_HWDEVICE const crypto::public_key pub_x = pub; @@ -1104,23 +1095,9 @@ namespace hw { return r; } - bool device_ledger::conceal_derivation(crypto::key_derivation &derivation, const crypto::public_key &tx_pub_key, const std::vector &additional_tx_pub_keys, const crypto::key_derivation &main_derivation, const std::vector &additional_derivations) { - const crypto::public_key *pkey=NULL; - if (derivation == main_derivation) { - pkey = &tx_pub_key; - MDEBUG("conceal derivation with main tx pub key"); - } else { - for(size_t n=0; n < additional_derivations.size();++n) { - if(derivation == additional_derivations[n]) { - pkey = &additional_tx_pub_keys[n]; - MDEBUG("conceal derivation with additionnal tx pub key"); - break; - } - } - } - ASSERT_X(pkey, "Mismatched derivation on scan info"); - return this->generate_key_derivation(*pkey, crypto::null_skey, derivation); - } + + return true; + } bool device_ledger::derivation_to_scalar(const crypto::key_derivation &derivation, const size_t output_index, crypto::ec_scalar &res) { AUTO_LOCK_CMD(); @@ -1407,6 +1384,32 @@ namespace hw { return true; } + bool device_ledger::set_signature_mode(unsigned int sig_mode) { + AUTO_LOCK_CMD(); + int offset ; + + reset_buffer(); + + this->buffer_send[0] = 0x00; + this->buffer_send[1] = INS_SET_SIGNATURE_MODE; + this->buffer_send[2] = 0x01; + this->buffer_send[3] = 0x00; + this->buffer_send[4] = 0x00; + offset = 5; + //options + this->buffer_send[offset] = 0x00; + offset += 1; + //account + this->buffer_send[offset] = sig_mode; + offset += 1; + + this->buffer_send[4] = offset-5; + this->length_send = offset; + this->exchange(); + + return true; + } + bool device_ledger::encrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key) { AUTO_LOCK_CMD(); int offset; diff --git a/src/device/device_ledger.hpp b/src/device/device_ledger.hpp index a979b187d..90907616f 100644 --- a/src/device/device_ledger.hpp +++ b/src/device/device_ledger.hpp @@ -39,7 +39,6 @@ #else #include #include -#endif #include #include @@ -139,7 +138,6 @@ namespace hw { bool connect(void) override; bool disconnect() override; - bool set_mode(device_mode mode) override; /* ======================================================================= */ /* LOCKER */