Merge pull request #8465

04a1e28 ledger support for hf 15 (BP+, view tags) (j-berman)
This commit is contained in:
luigi1111 2022-08-22 22:49:47 -05:00
commit ab63fbc549
No known key found for this signature in database
GPG Key ID: F4ACA0183641E010
8 changed files with 94 additions and 12 deletions

View File

@ -989,7 +989,7 @@ namespace cryptonote
return true; return true;
} }
//--------------------------------------------------------------- //---------------------------------------------------------------
bool out_can_be_to_acc(const boost::optional<crypto::view_tag>& view_tag_opt, const crypto::key_derivation& derivation, const size_t output_index) bool out_can_be_to_acc(const boost::optional<crypto::view_tag>& view_tag_opt, const crypto::key_derivation& derivation, const size_t output_index, hw::device* hwdev)
{ {
// If there is no view tag to check, the output can possibly belong to the account. // If there is no view tag to check, the output can possibly belong to the account.
// Will need to derive the output pub key to be certain whether or not the output belongs to the account. // Will need to derive the output pub key to be certain whether or not the output belongs to the account.
@ -1002,7 +1002,15 @@ namespace cryptonote
// Therefore can fail out early to avoid expensive crypto ops needlessly deriving output public key to // Therefore can fail out early to avoid expensive crypto ops needlessly deriving output public key to
// determine if output belongs to the account. // determine if output belongs to the account.
crypto::view_tag derived_view_tag; crypto::view_tag derived_view_tag;
crypto::derive_view_tag(derivation, output_index, derived_view_tag); if (hwdev != nullptr)
{
bool r = hwdev->derive_view_tag(derivation, output_index, derived_view_tag);
CHECK_AND_ASSERT_MES(r, false, "Failed to derive view tag");
}
else
{
crypto::derive_view_tag(derivation, output_index, derived_view_tag);
}
return view_tag == derived_view_tag; return view_tag == derived_view_tag;
} }
//--------------------------------------------------------------- //---------------------------------------------------------------
@ -1012,7 +1020,7 @@ namespace cryptonote
bool r = acc.get_device().generate_key_derivation(tx_pub_key, acc.m_view_secret_key, derivation); bool r = acc.get_device().generate_key_derivation(tx_pub_key, acc.m_view_secret_key, derivation);
CHECK_AND_ASSERT_MES(r, false, "Failed to generate key derivation"); CHECK_AND_ASSERT_MES(r, false, "Failed to generate key derivation");
crypto::public_key pk; crypto::public_key pk;
if (out_can_be_to_acc(view_tag_opt, derivation, output_index)) if (out_can_be_to_acc(view_tag_opt, derivation, output_index, &acc.get_device()))
{ {
r = acc.get_device().derive_public_key(derivation, output_index, acc.m_account_address.m_spend_public_key, pk); r = acc.get_device().derive_public_key(derivation, output_index, acc.m_account_address.m_spend_public_key, pk);
CHECK_AND_ASSERT_MES(r, false, "Failed to derive public key"); CHECK_AND_ASSERT_MES(r, false, "Failed to derive public key");
@ -1026,7 +1034,7 @@ namespace cryptonote
CHECK_AND_ASSERT_MES(output_index < additional_tx_pub_keys.size(), false, "wrong number of additional tx pubkeys"); CHECK_AND_ASSERT_MES(output_index < additional_tx_pub_keys.size(), false, "wrong number of additional tx pubkeys");
r = acc.get_device().generate_key_derivation(additional_tx_pub_keys[output_index], acc.m_view_secret_key, derivation); r = acc.get_device().generate_key_derivation(additional_tx_pub_keys[output_index], acc.m_view_secret_key, derivation);
CHECK_AND_ASSERT_MES(r, false, "Failed to generate key derivation"); CHECK_AND_ASSERT_MES(r, false, "Failed to generate key derivation");
if (out_can_be_to_acc(view_tag_opt, derivation, output_index)) if (out_can_be_to_acc(view_tag_opt, derivation, output_index, &acc.get_device()))
{ {
r = acc.get_device().derive_public_key(derivation, output_index, acc.m_account_address.m_spend_public_key, pk); r = acc.get_device().derive_public_key(derivation, output_index, acc.m_account_address.m_spend_public_key, pk);
CHECK_AND_ASSERT_MES(r, false, "Failed to derive public key"); CHECK_AND_ASSERT_MES(r, false, "Failed to derive public key");
@ -1040,7 +1048,7 @@ namespace cryptonote
{ {
// try the shared tx pubkey // try the shared tx pubkey
crypto::public_key subaddress_spendkey; crypto::public_key subaddress_spendkey;
if (out_can_be_to_acc(view_tag_opt, derivation, output_index)) if (out_can_be_to_acc(view_tag_opt, derivation, output_index, &hwdev))
{ {
CHECK_AND_ASSERT_MES(hwdev.derive_subaddress_public_key(out_key, derivation, output_index, subaddress_spendkey), boost::none, "Failed to derive subaddress public key"); CHECK_AND_ASSERT_MES(hwdev.derive_subaddress_public_key(out_key, derivation, output_index, subaddress_spendkey), boost::none, "Failed to derive subaddress public key");
auto found = subaddresses.find(subaddress_spendkey); auto found = subaddresses.find(subaddress_spendkey);
@ -1052,7 +1060,7 @@ namespace cryptonote
if (!additional_derivations.empty()) if (!additional_derivations.empty())
{ {
CHECK_AND_ASSERT_MES(output_index < additional_derivations.size(), boost::none, "wrong number of additional derivations"); CHECK_AND_ASSERT_MES(output_index < additional_derivations.size(), boost::none, "wrong number of additional derivations");
if (out_can_be_to_acc(view_tag_opt, additional_derivations[output_index], output_index)) if (out_can_be_to_acc(view_tag_opt, additional_derivations[output_index], output_index, &hwdev))
{ {
CHECK_AND_ASSERT_MES(hwdev.derive_subaddress_public_key(out_key, additional_derivations[output_index], output_index, subaddress_spendkey), boost::none, "Failed to derive subaddress public key"); CHECK_AND_ASSERT_MES(hwdev.derive_subaddress_public_key(out_key, additional_derivations[output_index], output_index, subaddress_spendkey), boost::none, "Failed to derive subaddress public key");
auto found = subaddresses.find(subaddress_spendkey); auto found = subaddresses.find(subaddress_spendkey);

View File

@ -91,7 +91,7 @@ namespace cryptonote
bool get_encrypted_payment_id_from_tx_extra_nonce(const blobdata& extra_nonce, crypto::hash8& payment_id); bool get_encrypted_payment_id_from_tx_extra_nonce(const blobdata& extra_nonce, crypto::hash8& payment_id);
void set_tx_out(const uint64_t amount, const crypto::public_key& output_public_key, const bool use_view_tags, const crypto::view_tag& view_tag, tx_out& out); void set_tx_out(const uint64_t amount, const crypto::public_key& output_public_key, const bool use_view_tags, const crypto::view_tag& view_tag, tx_out& out);
bool check_output_types(const transaction& tx, const uint8_t hf_version); bool check_output_types(const transaction& tx, const uint8_t hf_version);
bool out_can_be_to_acc(const boost::optional<crypto::view_tag>& view_tag_opt, const crypto::key_derivation& derivation, const size_t output_index); bool out_can_be_to_acc(const boost::optional<crypto::view_tag>& view_tag_opt, const crypto::key_derivation& derivation, const size_t output_index, hw::device *hwdev = nullptr);
bool is_out_to_acc(const account_keys& acc, const crypto::public_key& output_public_key, const crypto::public_key& tx_pub_key, const std::vector<crypto::public_key>& additional_tx_public_keys, size_t output_index, const boost::optional<crypto::view_tag>& view_tag_opt = boost::optional<crypto::view_tag>()); bool is_out_to_acc(const account_keys& acc, const crypto::public_key& output_public_key, const crypto::public_key& tx_pub_key, const std::vector<crypto::public_key>& additional_tx_public_keys, size_t output_index, const boost::optional<crypto::view_tag>& view_tag_opt = boost::optional<crypto::view_tag>());
struct subaddress_receive_info struct subaddress_receive_info
{ {

View File

@ -177,6 +177,7 @@ namespace hw {
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 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 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; virtual bool generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image) = 0;
virtual bool derive_view_tag(const crypto::key_derivation &derivation, const std::size_t output_index, crypto::view_tag &view_tag) = 0;
// alternative prototypes available in libringct // alternative prototypes available in libringct
rct::key scalarmultKey(const rct::key &P, const rct::key &a) rct::key scalarmultKey(const rct::key &P, const rct::key &a)

View File

@ -101,7 +101,7 @@ namespace hw {
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) override; 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) override;
bool secret_key_to_public_key(const crypto::secret_key &sec, crypto::public_key &pub) override; bool secret_key_to_public_key(const crypto::secret_key &sec, crypto::public_key &pub) override;
bool generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image) override; bool generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image) override;
bool derive_view_tag(const crypto::key_derivation &derivation, const std::size_t output_index, crypto::view_tag &view_tag); bool derive_view_tag(const crypto::key_derivation &derivation, const std::size_t output_index, crypto::view_tag &view_tag) override;
/* ======================================================================= */ /* ======================================================================= */

View File

@ -270,6 +270,7 @@ namespace hw {
#define INS_DERIVE_PUBLIC_KEY 0x36 #define INS_DERIVE_PUBLIC_KEY 0x36
#define INS_DERIVE_SECRET_KEY 0x38 #define INS_DERIVE_SECRET_KEY 0x38
#define INS_GEN_KEY_IMAGE 0x3A #define INS_GEN_KEY_IMAGE 0x3A
#define INS_DERIVE_VIEW_TAG 0x3B
#define INS_SECRET_KEY_ADD 0x3C #define INS_SECRET_KEY_ADD 0x3C
#define INS_SECRET_KEY_SUB 0x3E #define INS_SECRET_KEY_SUB 0x3E
#define INS_GENERATE_KEYPAIR 0x40 #define INS_GENERATE_KEYPAIR 0x40
@ -1312,6 +1313,54 @@ namespace hw {
return true; return true;
} }
bool device_ledger::derive_view_tag(const crypto::key_derivation &derivation, const std::size_t output_index, crypto::view_tag &view_tag){
#ifdef DEBUG_HWDEVICE
crypto::key_derivation derivation_x;
if ((this->mode == TRANSACTION_PARSE) && has_view_key) {
derivation_x = derivation;
} else {
derivation_x = hw::ledger::decrypt(derivation);
}
const std::size_t output_index_x = output_index;
crypto::view_tag view_tag_x;
log_hexbuffer("derive_view_tag: [[IN]] derivation ", derivation_x.data, 32);
log_message ("derive_view_tag: [[IN]] output_index", std::to_string(output_index_x));
this->controle_device->derive_view_tag(derivation_x, output_index_x, view_tag_x);
log_hexbuffer("derive_view_tag: [[OUT]] view_tag ", &view_tag_x.data, 1);
#endif
if ((this->mode == TRANSACTION_PARSE) && has_view_key) {
//If we are in TRANSACTION_PARSE, the given derivation has been retrieved uncrypted (wihtout the help
//of the device), so continue that way.
MDEBUG( "derive_view_tag : PARSE mode with known viewkey");
crypto::derive_view_tag(derivation, output_index, view_tag);
} else {
AUTO_LOCK_CMD();
int offset = set_command_header_noopt(INS_DERIVE_VIEW_TAG);
//derivation
this->send_secret((unsigned char*)derivation.data, offset);
//index
this->buffer_send[offset+0] = output_index>>24;
this->buffer_send[offset+1] = output_index>>16;
this->buffer_send[offset+2] = output_index>>8;
this->buffer_send[offset+3] = output_index>>0;
offset += 4;
this->buffer_send[4] = offset-5;
this->length_send = offset;
this->exchange();
//view tag
memmove(&view_tag.data, &this->buffer_recv[0], 1);
}
#ifdef DEBUG_HWDEVICE
hw::ledger::check1("derive_view_tag", "view_tag", &view_tag_x.data, &view_tag.data);
#endif
return true;
}
/* ======================================================================= */ /* ======================================================================= */
/* TRANSACTION */ /* TRANSACTION */
/* ======================================================================= */ /* ======================================================================= */
@ -1552,7 +1601,6 @@ namespace hw {
const size_t output_index_x = output_index; const size_t output_index_x = output_index;
const bool need_additional_txkeys_x = need_additional_txkeys; const bool need_additional_txkeys_x = need_additional_txkeys;
const bool use_view_tags_x = use_view_tags; const bool use_view_tags_x = use_view_tags;
const crypto::view_tag view_tag_x = view_tag;
std::vector<crypto::secret_key> additional_tx_keys_x; std::vector<crypto::secret_key> additional_tx_keys_x;
for (const auto &k: additional_tx_keys) { for (const auto &k: additional_tx_keys) {
@ -1562,6 +1610,7 @@ namespace hw {
std::vector<crypto::public_key> additional_tx_public_keys_x; std::vector<crypto::public_key> additional_tx_public_keys_x;
std::vector<rct::key> amount_keys_x; std::vector<rct::key> amount_keys_x;
crypto::public_key out_eph_public_key_x; crypto::public_key out_eph_public_key_x;
crypto::view_tag view_tag_x;
log_message ("generate_output_ephemeral_keys: [[IN]] tx_version", std::to_string(tx_version_x)); log_message ("generate_output_ephemeral_keys: [[IN]] tx_version", std::to_string(tx_version_x));
//log_hexbuffer("generate_output_ephemeral_keys: [[IN]] sender_account_keys.view", sender_account_keys.m_sview_secret_key.data, 32); //log_hexbuffer("generate_output_ephemeral_keys: [[IN]] sender_account_keys.view", sender_account_keys.m_sview_secret_key.data, 32);
@ -1579,11 +1628,15 @@ namespace hw {
if(need_additional_txkeys_x) { if(need_additional_txkeys_x) {
log_hexbuffer("generate_output_ephemeral_keys: [[IN]] additional_tx_keys[oi]", additional_tx_keys_x[output_index].data, 32); log_hexbuffer("generate_output_ephemeral_keys: [[IN]] additional_tx_keys[oi]", additional_tx_keys_x[output_index].data, 32);
} }
log_message ("generate_output_ephemeral_keys: [[IN]] use_view_tags", std::to_string(use_view_tags_x));
this->controle_device->generate_output_ephemeral_keys(tx_version_x, sender_account_keys_x, txkey_pub_x, tx_key_x, dst_entr_x, change_addr_x, output_index_x, need_additional_txkeys_x, additional_tx_keys_x, this->controle_device->generate_output_ephemeral_keys(tx_version_x, sender_account_keys_x, txkey_pub_x, tx_key_x, dst_entr_x, change_addr_x, output_index_x, need_additional_txkeys_x, additional_tx_keys_x,
additional_tx_public_keys_x, amount_keys_x, out_eph_public_key_x, use_view_tags_x, view_tag_x); additional_tx_public_keys_x, amount_keys_x, out_eph_public_key_x, use_view_tags_x, view_tag_x);
if(need_additional_txkeys_x) { if(need_additional_txkeys_x) {
log_hexbuffer("additional_tx_public_keys_x: [[OUT]] additional_tx_public_keys_x", additional_tx_public_keys_x.back().data, 32); log_hexbuffer("additional_tx_public_keys_x: [[OUT]] additional_tx_public_keys_x", additional_tx_public_keys_x.back().data, 32);
} }
if(use_view_tags_x) {
log_hexbuffer("generate_output_ephemeral_keys: [[OUT]] view_tag", &view_tag_x.data, 1);
}
log_hexbuffer("generate_output_ephemeral_keys: [[OUT]] amount_keys ", (char*)amount_keys_x.back().bytes, 32); log_hexbuffer("generate_output_ephemeral_keys: [[OUT]] amount_keys ", (char*)amount_keys_x.back().bytes, 32);
log_hexbuffer("generate_output_ephemeral_keys: [[OUT]] out_eph_public_key ", out_eph_public_key_x.data, 32); log_hexbuffer("generate_output_ephemeral_keys: [[OUT]] out_eph_public_key ", out_eph_public_key_x.data, 32);
#endif #endif
@ -1637,6 +1690,9 @@ namespace hw {
memset(&this->buffer_send[offset], 0, 32); memset(&this->buffer_send[offset], 0, 32);
offset += 32; offset += 32;
} }
//use_view_tags
this->buffer_send[offset] = use_view_tags;
offset++;
this->buffer_send[4] = offset-5; this->buffer_send[4] = offset-5;
this->length_send = offset; this->length_send = offset;
@ -1667,6 +1723,14 @@ namespace hw {
recv_len -= 32; recv_len -= 32;
} }
if (use_view_tags)
{
ASSERT_X(recv_len>=1, "Not enough data from device");
memmove(&view_tag.data, &this->buffer_recv[offset], 1);
offset++;
recv_len -= 1;
}
// add ABPkeys // add ABPkeys
this->add_output_key_mapping(dst_entr.addr.m_view_public_key, dst_entr.addr.m_spend_public_key, dst_entr.is_subaddress, is_change, this->add_output_key_mapping(dst_entr.addr.m_view_public_key, dst_entr.addr.m_spend_public_key, dst_entr.is_subaddress, is_change,
need_additional_txkeys, output_index, need_additional_txkeys, output_index,
@ -1679,6 +1743,9 @@ namespace hw {
hw::ledger::check32("generate_output_ephemeral_keys", "additional_tx_key", additional_tx_public_keys_x.back().data, additional_tx_public_keys.back().data); hw::ledger::check32("generate_output_ephemeral_keys", "additional_tx_key", additional_tx_public_keys_x.back().data, additional_tx_public_keys.back().data);
} }
hw::ledger::check32("generate_output_ephemeral_keys", "out_eph_public_key", out_eph_public_key_x.data, out_eph_public_key.data); hw::ledger::check32("generate_output_ephemeral_keys", "out_eph_public_key", out_eph_public_key_x.data, out_eph_public_key.data);
if (use_view_tags) {
hw::ledger::check1("generate_output_ephemeral_keys", "view_tag", &view_tag_x.data, &view_tag.data);
}
#endif #endif
return true; return true;
@ -1864,7 +1931,7 @@ namespace hw {
// ====== Aout, Bout, AKout, C, v, k ====== // ====== Aout, Bout, AKout, C, v, k ======
kv_offset = data_offset; kv_offset = data_offset;
if (type==rct::RCTTypeBulletproof2 || type==rct::RCTTypeCLSAG) { if (type==rct::RCTTypeBulletproof2 || type==rct::RCTTypeCLSAG || type==rct::RCTTypeBulletproofPlus) {
C_offset = kv_offset+ (8)*outputs_size; C_offset = kv_offset+ (8)*outputs_size;
} else { } else {
C_offset = kv_offset+ (32+32)*outputs_size; C_offset = kv_offset+ (32+32)*outputs_size;
@ -1881,7 +1948,7 @@ namespace hw {
offset = set_command_header(INS_VALIDATE, 0x02, i+1); offset = set_command_header(INS_VALIDATE, 0x02, i+1);
//options //options
this->buffer_send[offset] = (i==outputs_size-1)? 0x00:0x80 ; this->buffer_send[offset] = (i==outputs_size-1)? 0x00:0x80 ;
this->buffer_send[offset] |= (type==rct::RCTTypeBulletproof2 || type==rct::RCTTypeCLSAG)?0x02:0x00; this->buffer_send[offset] |= (type==rct::RCTTypeBulletproof2 || type==rct::RCTTypeCLSAG || type==rct::RCTTypeBulletproofPlus)?0x02:0x00;
offset += 1; offset += 1;
//is_subaddress //is_subaddress
this->buffer_send[offset] = outKeys.is_subaddress; this->buffer_send[offset] = outKeys.is_subaddress;
@ -1902,7 +1969,7 @@ namespace hw {
memmove(this->buffer_send+offset, data+C_offset,32); memmove(this->buffer_send+offset, data+C_offset,32);
offset += 32; offset += 32;
C_offset += 32; C_offset += 32;
if (type==rct::RCTTypeBulletproof2 || type==rct::RCTTypeCLSAG) { if (type==rct::RCTTypeBulletproof2 || type==rct::RCTTypeCLSAG || type==rct::RCTTypeBulletproofPlus) {
//k //k
memset(this->buffer_send+offset, 0, 32); memset(this->buffer_send+offset, 0, 32);
offset += 32; offset += 32;

View File

@ -245,6 +245,7 @@ namespace hw {
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) override; 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) override;
bool secret_key_to_public_key(const crypto::secret_key &sec, crypto::public_key &pub) override; bool secret_key_to_public_key(const crypto::secret_key &sec, crypto::public_key &pub) override;
bool generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image) override; bool generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image) override;
bool derive_view_tag(const crypto::key_derivation &derivation, const size_t output_index, crypto::view_tag &view_tag) override;
/* ======================================================================= */ /* ======================================================================= */
/* TRANSACTION */ /* TRANSACTION */

View File

@ -165,6 +165,10 @@ namespace hw {
void check8(const std::string &msg, const std::string &info, const char *h, const char *d, bool crypted) { void check8(const std::string &msg, const std::string &info, const char *h, const char *d, bool crypted) {
check(msg, info, h, d, 8, crypted); check(msg, info, h, d, 8, crypted);
} }
void check1(const std::string &msg, const std::string &info, const char *h, const char *d, bool crypted) {
check(msg, info, h, d, 1, crypted);
}
#endif #endif
} }

View File

@ -75,6 +75,7 @@ namespace hw {
void check32(const std::string &msg, const std::string &info, const char *h, const char *d, bool crypted=false); void check32(const std::string &msg, const std::string &info, const char *h, const char *d, bool crypted=false);
void check8(const std::string &msg, const std::string &info, const char *h, const char *d, bool crypted=false); void check8(const std::string &msg, const std::string &info, const char *h, const char *d, bool crypted=false);
void check1(const std::string &msg, const std::string &info, const char *h, const char *d, bool crypted=false);
void set_check_verbose(bool verbose); void set_check_verbose(bool verbose);
#endif #endif