Skip to content

Commit

Permalink
device: made function prototypes consistent with pre-monero-project#3303
Browse files Browse the repository at this point in the history
 codebase
  • Loading branch information
stoffu committed Mar 14, 2018
1 parent a03d34f commit c9b38b4
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 65 deletions.
8 changes: 2 additions & 6 deletions src/crypto/crypto_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,11 @@
namespace crypto {

secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key& recovery_key, bool recover, hw::device &hwdev) {
secret_key rng;
hwdev.generate_keys(pub, sec, recovery_key, recover, rng);
return rng;
return hwdev.generate_keys(pub, sec, recovery_key, recover);
}

secret_key generate_keys(public_key &pub, secret_key &sec, hw::device &hwdev) {
secret_key rng;
hwdev.generate_keys(pub, sec, secret_key(), false, rng);
return rng;
return hwdev.generate_keys(pub, sec, secret_key(), false);
}


Expand Down
10 changes: 3 additions & 7 deletions src/cryptonote_basic/cryptonote_format_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,7 @@ namespace cryptonote
}
crypto::secret_key get_subaddress_secret_key(const crypto::secret_key& a, const subaddress_index& index, hw::device &hwdev)
{
crypto::secret_key m;
hwdev.get_subaddress_secret_key(a, index, m);
return m;
return hwdev.get_subaddress_secret_key(a, index);
}

//---------------------------------------------------------------
Expand Down Expand Up @@ -232,9 +230,7 @@ namespace cryptonote

std::vector<crypto::public_key> get_subaddress_spend_public_keys(const cryptonote::account_keys &keys, uint32_t account, uint32_t begin, uint32_t end, hw::device &hwdev)
{
std::vector<crypto::public_key> pkeys;
hwdev.get_subaddress_spend_public_keys(keys, account, begin, end, pkeys);
return pkeys;
return hwdev.get_subaddress_spend_public_keys(keys, account, begin, end);
}

//---------------------------------------------------------------
Expand Down Expand Up @@ -282,7 +278,7 @@ namespace cryptonote
}
else
{
hwdev.get_subaddress_secret_key(ack.m_view_secret_key, received_index, subaddr_sk);
subaddr_sk = hwdev.get_subaddress_secret_key(ack.m_view_secret_key, received_index);
hwdev.sc_secret_add(scalar_step2, scalar_step1,subaddr_sk);
}

Expand Down
32 changes: 26 additions & 6 deletions src/device/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ namespace hw {
/* SUB ADDRESS */
/* ======================================================================= */
virtual bool 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) = 0;
virtual bool get_subaddress_spend_public_key(const cryptonote::account_keys& keys, const cryptonote::subaddress_index& index, crypto::public_key &D) = 0;
virtual bool get_subaddress_spend_public_keys(const cryptonote::account_keys &keys, uint32_t account, uint32_t begin, uint32_t end, std::vector<crypto::public_key> &pkeys) = 0;
virtual bool get_subaddress(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index, cryptonote::account_public_address &address) = 0;
virtual bool get_subaddress_secret_key(const crypto::secret_key &sec, const cryptonote::subaddress_index &index, crypto::secret_key &sub_sec) = 0;
virtual crypto::public_key get_subaddress_spend_public_key(const cryptonote::account_keys& keys, const cryptonote::subaddress_index& index) = 0;
virtual std::vector<crypto::public_key> get_subaddress_spend_public_keys(const cryptonote::account_keys &keys, uint32_t account, uint32_t begin, uint32_t end) = 0;
virtual cryptonote::account_public_address get_subaddress(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) = 0;
virtual crypto::secret_key get_subaddress_secret_key(const crypto::secret_key &sec, const cryptonote::subaddress_index &index) = 0;

/* ======================================================================= */
/* DERIVATION & KEY */
Expand All @@ -121,14 +121,29 @@ namespace hw {
virtual bool scalarmultKey(rct::key & aP, const rct::key &P, const rct::key &a) = 0;
virtual bool scalarmultBase(rct::key &aG, const rct::key &a) = 0;
virtual bool sc_secret_add( crypto::secret_key &r, const crypto::secret_key &a, const crypto::secret_key &b) = 0;
virtual bool generate_keys(crypto::public_key &pub, crypto::secret_key &sec, const crypto::secret_key& recovery_key, bool recover, crypto::secret_key &rng) = 0;
virtual crypto::secret_key generate_keys(crypto::public_key &pub, crypto::secret_key &sec, const crypto::secret_key& recovery_key = crypto::secret_key(), bool recover = false) = 0;
virtual bool generate_key_derivation(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_derivation &derivation) = 0;
virtual bool derivation_to_scalar(const crypto::key_derivation &derivation, const size_t output_index, crypto::ec_scalar &res) = 0;
virtual bool derive_secret_key(const crypto::key_derivation &derivation, const std::size_t output_index, const crypto::secret_key &sec, crypto::secret_key &derived_sec) = 0;
virtual bool derive_public_key(const crypto::key_derivation &derivation, const std::size_t output_index, const crypto::public_key &pub, crypto::public_key &derived_pub) = 0;
virtual bool secret_key_to_public_key(const crypto::secret_key &sec, crypto::public_key &pub) = 0;
virtual bool generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image) = 0;

// alternative prototypes available in libringct
rct::key scalarmultKey(const rct::key &P, const rct::key &a)
{
rct::key aP;
scalarmultKey(aP, P, a);
return aP;
}

rct::key scalarmultBase(const rct::key &a)
{
rct::key aG;
scalarmultBase(aG, a);
return aG;
}

/* ======================================================================= */
/* TRANSACTION */
/* ======================================================================= */
Expand All @@ -137,7 +152,12 @@ namespace hw {

virtual bool set_signature_mode(unsigned int sig_mode) = 0;

virtual bool encrypt_payment_id(const crypto::public_key &public_key, const crypto::secret_key &secret_key, crypto::hash8 &payment_id ) = 0;
virtual bool encrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key) = 0;
bool decrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key)
{
// Encryption and decryption are the same operation (xor with a key)
return encrypt_payment_id(payment_id, public_key, secret_key);
}

virtual bool ecdhEncode(rct::ecdhTuple & unmasked, const rct::key & sharedSec) = 0;
virtual bool ecdhDecode(rct::ecdhTuple & masked, const rct::key & sharedSec) = 0;
Expand Down
27 changes: 11 additions & 16 deletions src/device/device_default.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,20 @@ namespace hw {
return crypto::derive_subaddress_public_key(out_key, derivation, output_index,derived_key);
}

bool device_default::get_subaddress_spend_public_key(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index, crypto::public_key &D) {
D = cryptonote::get_subaddress_spend_public_key(keys,index);
return true;
crypto::public_key device_default::get_subaddress_spend_public_key(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) {
return cryptonote::get_subaddress_spend_public_key(keys,index);
}

bool device_default::get_subaddress_spend_public_keys(const cryptonote::account_keys &keys, uint32_t account, uint32_t begin, uint32_t end, std::vector<crypto::public_key> &pkeys) {
pkeys = cryptonote::get_subaddress_spend_public_keys(keys, account, begin, end);
return true;
std::vector<crypto::public_key> device_default::get_subaddress_spend_public_keys(const cryptonote::account_keys &keys, uint32_t account, uint32_t begin, uint32_t end) {
return cryptonote::get_subaddress_spend_public_keys(keys, account, begin, end);
}

bool device_default::get_subaddress(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index, cryptonote::account_public_address &address) {
address = cryptonote::get_subaddress(keys,index);
return true;
cryptonote::account_public_address device_default::get_subaddress(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) {
return cryptonote::get_subaddress(keys,index);
}

bool device_default::get_subaddress_secret_key(const crypto::secret_key &a, const cryptonote::subaddress_index &index, crypto::secret_key &m) {
m = cryptonote::get_subaddress_secret_key(a,index);
return true;
crypto::secret_key device_default::get_subaddress_secret_key(const crypto::secret_key &a, const cryptonote::subaddress_index &index) {
return cryptonote::get_subaddress_secret_key(a,index);
}

/* ======================================================================= */
Expand All @@ -142,9 +138,8 @@ namespace hw {
return true;
}

bool device_default::generate_keys(crypto::public_key &pub, crypto::secret_key &sec, const crypto::secret_key& recovery_key, bool recover, crypto::secret_key &rng) {
rng = crypto::generate_keys(pub, sec, recovery_key, recover);
return true;
crypto::secret_key device_default::generate_keys(crypto::public_key &pub, crypto::secret_key &sec, const crypto::secret_key& recovery_key, bool recover) {
return crypto::generate_keys(pub, sec, recovery_key, recover);
}

bool device_default::generate_key_derivation(const crypto::public_key &key1, const crypto::secret_key &key2, crypto::key_derivation &derivation) {
Expand Down Expand Up @@ -194,7 +189,7 @@ namespace hw {
return true;
}

bool device_default::encrypt_payment_id(const crypto::public_key &public_key, const crypto::secret_key &secret_key, crypto::hash8 &payment_id ) {
bool device_default::encrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key) {
return cryptonote::encrypt_payment_id(payment_id, public_key, secret_key);
}

Expand Down
12 changes: 6 additions & 6 deletions src/device/device_default.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ namespace hw {
/* SUB ADDRESS */
/* ======================================================================= */
bool 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) override;
bool get_subaddress_spend_public_key(const cryptonote::account_keys& keys, const cryptonote::subaddress_index& index, crypto::public_key &D) override;
bool get_subaddress_spend_public_keys(const cryptonote::account_keys &keys, uint32_t account, uint32_t begin, uint32_t end, std::vector<crypto::public_key> &pkeys) override;
bool get_subaddress(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index, cryptonote::account_public_address &address) override;
bool get_subaddress_secret_key(const crypto::secret_key &sec, const cryptonote::subaddress_index &index, crypto::secret_key &sub_sec) override;
crypto::public_key get_subaddress_spend_public_key(const cryptonote::account_keys& keys, const cryptonote::subaddress_index& index) override;
std::vector<crypto::public_key> get_subaddress_spend_public_keys(const cryptonote::account_keys &keys, uint32_t account, uint32_t begin, uint32_t end) override;
cryptonote::account_public_address get_subaddress(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) override;
crypto::secret_key get_subaddress_secret_key(const crypto::secret_key &sec, const cryptonote::subaddress_index &index) override;

/* ======================================================================= */
/* DERIVATION & KEY */
Expand All @@ -82,7 +82,7 @@ namespace hw {
bool scalarmultKey(rct::key & aP, const rct::key &P, const rct::key &a) override;
bool scalarmultBase(rct::key &aG, const rct::key &a) override;
bool sc_secret_add(crypto::secret_key &r, const crypto::secret_key &a, const crypto::secret_key &b) override;
bool generate_keys(crypto::public_key &pub, crypto::secret_key &sec, const crypto::secret_key& recovery_key, bool recover, crypto::secret_key &rng) override;
crypto::secret_key generate_keys(crypto::public_key &pub, crypto::secret_key &sec, const crypto::secret_key& recovery_key = crypto::secret_key(), bool recover = false) override;
bool generate_key_derivation(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_derivation &derivation) override;
bool derivation_to_scalar(const crypto::key_derivation &derivation, const size_t output_index, crypto::ec_scalar &res) override;
bool derive_secret_key(const crypto::key_derivation &derivation, const std::size_t output_index, const crypto::secret_key &sec, crypto::secret_key &derived_sec) override;
Expand All @@ -100,7 +100,7 @@ namespace hw {
//bool get_additional_key(const bool subaddr, cryptonote::keypair &additional_txkey) override;
bool set_signature_mode(unsigned int sig_mode) override;

bool encrypt_payment_id(const crypto::public_key &public_key, const crypto::secret_key &secret_key, crypto::hash8 &payment_id ) override;
bool encrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key) override;

bool ecdhEncode(rct::ecdhTuple & unmasked, const rct::key & sharedSec) override;
bool ecdhDecode(rct::ecdhTuple & masked, const rct::key & sharedSec) override;
Expand Down
28 changes: 16 additions & 12 deletions src/device/device_ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,8 @@ namespace hw {
return true;
}

bool device_ledger::get_subaddress_spend_public_key(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index, crypto::public_key &D) {
crypto::public_key device_ledger::get_subaddress_spend_public_key(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) {
crypto::public_key D;
lock_device();
try {
int offset =0;
Expand Down Expand Up @@ -646,21 +647,23 @@ namespace hw {
unlock_device();
throw;
}
return true;
return D;
}

bool device_ledger::get_subaddress_spend_public_keys(const cryptonote::account_keys &keys, uint32_t account, uint32_t begin, uint32_t end, std::vector<crypto::public_key> &pkeys) {
std::vector<crypto::public_key> device_ledger::get_subaddress_spend_public_keys(const cryptonote::account_keys &keys, uint32_t account, uint32_t begin, uint32_t end) {
std::vector<crypto::public_key> pkeys;
cryptonote::subaddress_index index = {account, begin};
crypto::public_key D;
for (uint32_t idx = begin; idx < end; ++idx) {
index.minor = idx;
this->get_subaddress_spend_public_key(keys, index, D);
D = this->get_subaddress_spend_public_key(keys, index);
pkeys.push_back(D);
}
return true;
return pkeys;
}

bool device_ledger::get_subaddress(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index, cryptonote::account_public_address &address) {
cryptonote::account_public_address device_ledger::get_subaddress(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) {
cryptonote::account_public_address address;
lock_device();
try {
int offset =0;
Expand Down Expand Up @@ -717,10 +720,11 @@ namespace hw {
unlock_device();
throw;
}
return true;
return address;
}

bool device_ledger::get_subaddress_secret_key(const crypto::secret_key &sec, const cryptonote::subaddress_index &index, crypto::secret_key &sub_sec) {
crypto::secret_key device_ledger::get_subaddress_secret_key(const crypto::secret_key &sec, const cryptonote::subaddress_index &index) {
crypto::secret_key sub_sec;
lock_device();
try {
int offset =0;
Expand Down Expand Up @@ -771,7 +775,7 @@ namespace hw {
unlock_device();
throw;
}
return true;
return sub_sec;
}

/* ======================================================================= */
Expand Down Expand Up @@ -979,7 +983,7 @@ namespace hw {
return true;
}

bool device_ledger::generate_keys(crypto::public_key &pub, crypto::secret_key &sec, const crypto::secret_key& recovery_key, bool recover, crypto::secret_key &rng) {
crypto::secret_key device_ledger::generate_keys(crypto::public_key &pub, crypto::secret_key &sec, const crypto::secret_key& recovery_key, bool recover) {
if (recover) {
throw std::runtime_error("device generate key does not support recover");
}
Expand Down Expand Up @@ -1030,7 +1034,7 @@ namespace hw {
unlock_device();
throw;
}
return true;
return sec;

}

Expand Down Expand Up @@ -1457,7 +1461,7 @@ namespace hw {
return true;
}

bool device_ledger::encrypt_payment_id(const crypto::public_key &public_key, const crypto::secret_key &secret_key, crypto::hash8 &payment_id) {
bool device_ledger::encrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key) {
lock_device();
try {
int offset =0;
Expand Down
Loading

0 comments on commit c9b38b4

Please sign in to comment.