aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormoneromooo-monero <moneromooo-monero@users.noreply.github.com>2018-06-20 10:40:09 +0100
committermoneromooo-monero <moneromooo-monero@users.noreply.github.com>2018-06-25 12:35:10 +0100
commit87e158b5b9282fca932b9eff0488dd933409a3b3 (patch)
tree7ababaf41e853f694acf6fadbf980f93a0c06e28
parentMerge pull request #3878 (diff)
downloadmonero-87e158b5b9282fca932b9eff0488dd933409a3b3.tar.xz
device_ledger: factor the prologue code
-rw-r--r--src/device/device_ledger.cpp470
-rw-r--r--src/device/device_ledger.hpp3
2 files changed, 61 insertions, 412 deletions
diff --git a/src/device/device_ledger.cpp b/src/device/device_ledger.cpp
index aedaf8382..6f2bcf37f 100644
--- a/src/device/device_ledger.cpp
+++ b/src/device/device_ledger.cpp
@@ -272,22 +272,32 @@ namespace hw {
/* ======================================================================= */
/* MISC */
/* ======================================================================= */
- bool device_ledger::reset() {
- int offset;
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_RESET;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0x00;
+ int device_ledger::set_command_header(BYTE ins, BYTE p1, BYTE p2) {
+ reset_buffer();
+ int offset = 0;
+ this->buffer_send[0] = 0x00;
+ this->buffer_send[1] = ins;
+ this->buffer_send[2] = p1;
+ this->buffer_send[3] = p2;
+ this->buffer_send[4] = 0x00;
+ return 5;
+ }
- this->buffer_send[4] = offset-5;
- this->length_send = offset;
- this->exchange();
+ int device_ledger::set_command_header_noopt(BYTE ins, BYTE p1, BYTE p2) {
+ int offset = set_command_header(ins, p1, p2);
+ //options
+ this->buffer_send[offset++] = 0;
+ this->buffer_send[4] = offset - 5;
+ return offset;
+ }
+
+ void device_ledger::send_simple(BYTE ins, BYTE p1) {
+ this->length_send = set_command_header_noopt(ins, p1);
+ this->exchange();
+ }
+
+ bool device_ledger::reset() {
+ send_simple(INS_RESET);
return true;
}
@@ -448,20 +458,10 @@ namespace hw {
int offset;
- reset_buffer();
-
switch(mode) {
case TRANSACTION_CREATE_REAL:
case TRANSACTION_CREATE_FAKE:
- 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;
+ offset = set_command_header_noopt(INS_SET_SIGNATURE_MODE, 1);
//account
this->buffer_send[offset] = mode;
offset += 1;
@@ -492,22 +492,7 @@ namespace hw {
bool device_ledger::get_public_address(cryptonote::account_public_address &pubkey){
AUTO_LOCK_CMD();
- int offset;
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_GET_KEY;
- 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;
-
- this->buffer_send[4] = offset-5;
- this->length_send = offset;
- this->exchange();
+ send_simple(INS_GET_KEY, 1);
memmove(pubkey.m_view_public_key.data, this->buffer_recv, 32);
memmove(pubkey.m_spend_public_key.data, this->buffer_recv+32, 32);
@@ -523,22 +508,7 @@ namespace hw {
memset(skey.data, 0xFF, 32);
//spcialkey, normal conf handled in decrypt
- int offset;
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_GET_KEY;
- this->buffer_send[2] = 0x02;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0x00;
- offset += 1;
-
- this->buffer_send[4] = offset-5;
- this->length_send = offset;
- this->exchange();
+ send_simple(INS_GET_KEY, 0x02);
//View key is retrievied, if allowed, to speed up blockchain parsing
memmove(this->viewkey.data, this->buffer_recv+0, 32);
@@ -560,7 +530,6 @@ namespace hw {
bool device_ledger::generate_chacha_key(const cryptonote::account_keys &keys, crypto::chacha_key &key) {
AUTO_LOCK_CMD();
- int offset;
#ifdef DEBUG_HWDEVICE
crypto::chacha_key key_x;
@@ -568,20 +537,7 @@ namespace hw {
this->controle_device->generate_chacha_key(keys_x, key_x);
#endif
- reset_buffer();
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_GET_CHACHA8_PREKEY;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0x00;
- offset += 1;
-
- this->buffer_send[4] = offset-5;
- this->length_send = offset;
- this->exchange();
+ send_simple(INS_GET_CHACHA8_PREKEY);
char prekey[200];
memmove(prekey, &this->buffer_recv[0], 200);
@@ -624,19 +580,7 @@ namespace hw {
crypto::derive_subaddress_public_key(pub, derivation, output_index,derived_pub);
} else {
- int offset =0;
-
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_DERIVE_SUBADDRESS_PUBLIC_KEY;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0;
- offset += 1;
+ int offset = set_command_header_noopt(INS_DERIVE_SUBADDRESS_PUBLIC_KEY);
//pub
memmove(this->buffer_send+offset, pub.data, 32);
offset += 32;
@@ -667,7 +611,6 @@ namespace hw {
crypto::public_key device_ledger::get_subaddress_spend_public_key(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) {
AUTO_LOCK_CMD();
crypto::public_key D;
- int offset;
#ifdef DEBUG_HWDEVICE
const cryptonote::account_keys keys_x = hw::ledger::decrypt(keys);
@@ -684,17 +627,7 @@ namespace hw {
D = keys.m_account_address.m_spend_public_key;
} else {
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_GET_SUBADDRESS_SPEND_PUBLIC_KEY;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0x00;
- offset += 1;
+ int offset = set_command_header_noopt(INS_GET_SUBADDRESS_SPEND_PUBLIC_KEY);
//index
static_assert(sizeof(cryptonote::subaddress_index) == 8, "cryptonote::subaddress_index shall be 8 bytes length");
memmove(this->buffer_send+offset, &index, sizeof(cryptonote::subaddress_index));
@@ -729,7 +662,6 @@ namespace hw {
cryptonote::account_public_address device_ledger::get_subaddress(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) {
AUTO_LOCK_CMD();
cryptonote::account_public_address address;
- int offset;
#ifdef DEBUG_HWDEVICE
const cryptonote::account_keys keys_x = hw::ledger::decrypt(keys);
@@ -748,17 +680,7 @@ namespace hw {
if (index.is_zero()) {
address = keys.m_account_address;
} else {
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_GET_SUBADDRESS;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0x00;
- offset += 1;
+ int offset = set_command_header_noopt(INS_GET_SUBADDRESS);
//index
static_assert(sizeof(cryptonote::subaddress_index) == 8, "cryptonote::subaddress_index shall be 8 bytes length");
memmove(this->buffer_send+offset, &index, sizeof(cryptonote::subaddress_index));
@@ -783,7 +705,6 @@ namespace hw {
crypto::secret_key device_ledger::get_subaddress_secret_key(const crypto::secret_key &sec, const cryptonote::subaddress_index &index) {
AUTO_LOCK_CMD();
crypto::secret_key sub_sec;
- int offset;
#ifdef DEBUG_HWDEVICE
const crypto::secret_key sec_x = hw::ledger::decrypt(sec);
@@ -795,17 +716,7 @@ namespace hw {
hw::ledger::log_hexbuffer("get_subaddress_secret_key: [[OUT]] sub_sec", sub_sec_x.data, 32);
#endif
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_GET_SUBADDRESS_SECRET_KEY;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0;
- offset += 1;
+ int offset = set_command_header_noopt(INS_GET_SUBADDRESS_SECRET_KEY);
//sec
memmove(this->buffer_send+offset, sec.data, 32);
offset += 32;
@@ -836,16 +747,7 @@ namespace hw {
AUTO_LOCK_CMD();
int offset, sw;
- reset_buffer();
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_VERIFY_KEY;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0x00;
- offset += 1;
+ offset = set_command_header_noopt(INS_VERIFY_KEY);
//sec
memmove(this->buffer_send+offset, secret_key.data, 32);
offset += 32;
@@ -868,7 +770,6 @@ namespace hw {
bool device_ledger::scalarmultKey(rct::key & aP, const rct::key &P, const rct::key &a) {
AUTO_LOCK_CMD();
- int offset;
#ifdef DEBUG_HWDEVICE
const rct::key P_x = P;
@@ -880,17 +781,7 @@ namespace hw {
hw::ledger::log_hexbuffer("scalarmultKey: [[OUT]] aP", (char*)aP_x.bytes, 32);
#endif
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_SECRET_SCAL_MUL_KEY;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0;
- offset += 1;
+ int offset = set_command_header_noopt(INS_SECRET_SCAL_MUL_KEY);
//pub
memmove(this->buffer_send+offset, P.bytes, 32);
offset += 32;
@@ -915,7 +806,6 @@ namespace hw {
bool device_ledger::scalarmultBase(rct::key &aG, const rct::key &a) {
AUTO_LOCK_CMD();
- int offset;
#ifdef DEBUG_HWDEVICE
const rct::key a_x = hw::ledger::decrypt(a);
@@ -925,17 +815,7 @@ namespace hw {
hw::ledger::log_hexbuffer("scalarmultKey: [[OUT]] aG", (char*)aG_x.bytes, 32);
#endif
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_SECRET_SCAL_MUL_BASE;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0;
- offset += 1;
+ int offset = set_command_header_noopt(INS_SECRET_SCAL_MUL_BASE);
//sec
memmove(this->buffer_send+offset, a.bytes, 32);
offset += 32;
@@ -956,7 +836,6 @@ namespace hw {
bool device_ledger::sc_secret_add( crypto::secret_key &r, const crypto::secret_key &a, const crypto::secret_key &b) {
AUTO_LOCK_CMD();
- int offset;
#ifdef DEBUG_HWDEVICE
const crypto::secret_key a_x = hw::ledger::decrypt(a);
@@ -965,17 +844,7 @@ namespace hw {
this->controle_device->sc_secret_add(r_x, a_x, b_x);
#endif
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_SECRET_KEY_ADD;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0;
- offset += 1;
+ int offset = set_command_header_noopt(INS_SECRET_KEY_ADD);
//sec key
memmove(this->buffer_send+offset, a.data, 32);
offset += 32;
@@ -1004,8 +873,6 @@ namespace hw {
throw std::runtime_error("device generate key does not support recover");
}
- int offset;
-
#ifdef DEBUG_HWDEVICE
bool recover_x = recover;
const crypto::secret_key recovery_key_x = recovery_key;
@@ -1013,21 +880,7 @@ namespace hw {
crypto::secret_key sec_x;
#endif
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_GENERATE_KEYPAIR;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0;
- offset += 1;
-
- this->buffer_send[4] = offset-5;
- this->length_send = offset;
- this->exchange();
+ send_simple(INS_GENERATE_KEYPAIR);
//pub key
memmove(pub.data, &this->buffer_recv[0], 32);
@@ -1067,19 +920,7 @@ namespace hw {
r = crypto::generate_key_derivation(pub, this->viewkey, derivation);
} else {
- int offset;
-
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_GEN_KEY_DERIVATION;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0;
- offset += 1;
+ int offset = set_command_header_noopt(INS_GEN_KEY_DERIVATION);
//pub
memmove(this->buffer_send+offset, pub.data, 32);
offset += 32;
@@ -1128,7 +969,6 @@ namespace hw {
bool device_ledger::derivation_to_scalar(const crypto::key_derivation &derivation, const size_t output_index, crypto::ec_scalar &res) {
AUTO_LOCK_CMD();
- int offset;
#ifdef DEBUG_HWDEVICE
const crypto::key_derivation derivation_x = hw::ledger::decrypt(derivation);
@@ -1140,17 +980,7 @@ namespace hw {
hw::ledger::log_hexbuffer("derivation_to_scalar: [[OUT]] res ", res_x.data, 32);
#endif
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_DERIVATION_TO_SCALAR;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0;
- offset += 1;
+ int offset = set_command_header_noopt(INS_DERIVATION_TO_SCALAR);
//derivattion
memmove(this->buffer_send+offset, derivation.data, 32);
offset += 32;
@@ -1178,7 +1008,6 @@ namespace hw {
bool device_ledger::derive_secret_key(const crypto::key_derivation &derivation, const std::size_t output_index, const crypto::secret_key &sec, crypto::secret_key &derived_sec) {
AUTO_LOCK_CMD();
- int offset;
#ifdef DEBUG_HWDEVICE
const crypto::key_derivation derivation_x = hw::ledger::decrypt(derivation);
@@ -1192,17 +1021,7 @@ namespace hw {
hw::ledger::log_hexbuffer("derive_secret_key: [[OUT]] derived_sec", derived_sec_x.data, 32);
#endif
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_DERIVE_SECRET_KEY;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0;
- offset += 1;
+ int offset = set_command_header_noopt(INS_DERIVE_SECRET_KEY);
//derivation
memmove(this->buffer_send+offset, derivation.data, 32);
offset += 32;
@@ -1233,7 +1052,6 @@ namespace hw {
bool device_ledger::derive_public_key(const crypto::key_derivation &derivation, const std::size_t output_index, const crypto::public_key &pub, crypto::public_key &derived_pub){
AUTO_LOCK_CMD();
- int offset;
#ifdef DEBUG_HWDEVICE
const crypto::key_derivation derivation_x = hw::ledger::decrypt(derivation);
@@ -1247,17 +1065,7 @@ namespace hw {
hw::ledger::log_hexbuffer("derive_public_key: [[OUT]] derived_pub ", derived_pub_x.data, 32);
#endif
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_DERIVE_PUBLIC_KEY;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0;
- offset += 1;
+ int offset = set_command_header_noopt(INS_DERIVE_PUBLIC_KEY);
//derivation
memmove(this->buffer_send+offset, derivation.data, 32);
offset += 32;
@@ -1287,7 +1095,6 @@ namespace hw {
bool device_ledger::secret_key_to_public_key(const crypto::secret_key &sec, crypto::public_key &pub) {
AUTO_LOCK_CMD();
- int offset;
#ifdef DEBUG_HWDEVICE
const crypto::secret_key sec_x = hw::ledger::decrypt(sec);
@@ -1300,17 +1107,7 @@ namespace hw {
}
#endif
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_SECRET_KEY_TO_PUBLIC_KEY;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0;
- offset += 1;
+ int offset = set_command_header_noopt(INS_SECRET_KEY_TO_PUBLIC_KEY);
//sec key
memmove(this->buffer_send+offset, sec.data, 32);
offset += 32;
@@ -1331,7 +1128,6 @@ namespace hw {
bool device_ledger::generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image){
AUTO_LOCK_CMD();
- int offset;
#ifdef DEBUG_HWDEVICE
const crypto::public_key pub_x = pub;
@@ -1343,17 +1139,7 @@ namespace hw {
hw::ledger::log_hexbuffer("generate_key_image: [[OUT]] image ", image_x.data, 32);
#endif
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_GEN_KEY_IMAGE;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0;
- offset += 1;
+ int offset = set_command_header_noopt(INS_GEN_KEY_IMAGE);
//pub
memmove(this->buffer_send+offset, pub.data, 32);
offset += 32;
@@ -1381,20 +1167,10 @@ namespace hw {
bool device_ledger::open_tx(crypto::secret_key &tx_key) {
AUTO_LOCK_CMD();
- int offset;
- reset_buffer();
key_map.clear();
+ int offset = set_command_header_noopt(INS_OPEN_TX, 0x01);
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_OPEN_TX;
- 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+0] = 0x00;
this->buffer_send[offset+1] = 0x00;
@@ -1413,7 +1189,6 @@ namespace hw {
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;
#ifdef DEBUG_HWDEVICE
const crypto::public_key public_key_x = public_key;
@@ -1422,17 +1197,7 @@ namespace hw {
this->controle_device->encrypt_payment_id(payment_id_x, public_key_x, secret_key_x);
#endif
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_STEALTH;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0x00;
- offset += 1;
+ int offset = set_command_header_noopt(INS_STEALTH);
//pub
memmove(&this->buffer_send[offset], public_key.data, 32);
offset += 32;
@@ -1464,7 +1229,6 @@ namespace hw {
bool device_ledger::ecdhEncode(rct::ecdhTuple & unmasked, const rct::key & AKout) {
AUTO_LOCK_CMD();
- int offset;
#ifdef DEBUG_HWDEVICE
const rct::key AKout_x = hw::ledger::decrypt(AKout);
@@ -1472,17 +1236,7 @@ namespace hw {
this->controle_device->ecdhEncode(unmasked_x, AKout_x);
#endif
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_BLIND;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0x00;
- offset += 1;
+ int offset = set_command_header_noopt(INS_BLIND);
// AKout
memmove(this->buffer_send+offset, AKout.bytes, 32);
offset += 32;
@@ -1512,7 +1266,6 @@ namespace hw {
bool device_ledger::ecdhDecode(rct::ecdhTuple & masked, const rct::key & AKout) {
AUTO_LOCK_CMD();
- int offset;
#ifdef DEBUG_HWDEVICE
const rct::key AKout_x = hw::ledger::decrypt(AKout);
@@ -1520,17 +1273,8 @@ namespace hw {
this->controle_device->ecdhDecode(masked_x, AKout_x);
#endif
- reset_buffer();
+ int offset = set_command_header_noopt(INS_UNBLIND);
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_UNBLIND;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0x00;
- offset += 1;
// AKout
memmove(this->buffer_send+offset, AKout.bytes, 32);
offset += 32;
@@ -1580,16 +1324,7 @@ namespace hw {
data = blob.data();
// ====== u8 type, varint txnfee ======
- int offset;
-
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_VALIDATE;
- this->buffer_send[2] = 0x01;
- this->buffer_send[3] = 0x01;
- this->buffer_send[4] = 0x00;
- offset = 5;
+ int offset = set_command_header(INS_VALIDATE, 0x01, 0x01);
//options
this->buffer_send[offset] = (inputs_size == 0)?0x00:0x80;
offset += 1;
@@ -1617,13 +1352,7 @@ namespace hw {
//pseudoOuts
if ((type == rct::RCTTypeSimple) || (type == rct::RCTTypeSimpleBulletproof)) {
for ( i = 0; i < inputs_size; i++) {
- reset_buffer();
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_VALIDATE;
- this->buffer_send[2] = 0x01;
- this->buffer_send[3] = i+2;
- this->buffer_send[4] = 0x00;
- offset = 5;
+ offset = set_command_header(INS_VALIDATE, 0x01, i+2);
//options
this->buffer_send[offset] = (i==inputs_size-1)? 0x00:0x80;
offset += 1;
@@ -1650,14 +1379,7 @@ namespace hw {
log_hexbuffer("Pout not found", (char*)outPk[i].dest.bytes, 32);
CHECK_AND_ASSERT_THROW_MES(found, "Pout not found");
}
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_VALIDATE;
- this->buffer_send[2] = 0x02;
- this->buffer_send[3] = i+1;
- this->buffer_send[4] = 0x00;
- offset = 5;
+ offset = set_command_header(INS_VALIDATE, 0x02, i+1);
//options
this->buffer_send[offset] = (i==outputs_size-1)? 0x00:0x80 ;
offset += 1;
@@ -1702,14 +1424,7 @@ namespace hw {
// ====== C[], message, proof======
C_offset = kv_offset;
for (i = 0; i < outputs_size; i++) {
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_VALIDATE;
- this->buffer_send[2] = 0x03;
- this->buffer_send[3] = i+1;
- this->buffer_send[4] = 0x00;
- offset = 5;
+ offset = set_command_header(INS_VALIDATE, 0x03, i+1);
//options
this->buffer_send[offset] = 0x80 ;
offset += 1;
@@ -1724,17 +1439,7 @@ namespace hw {
}
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_VALIDATE;
- this->buffer_send[2] = 0x03;
- this->buffer_send[3] = i+1;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0x00;
- offset += 1;
+ offset = set_command_header_noopt(INS_VALIDATE, 0x03, i+1);
//message
memmove(this->buffer_send+offset, hashes[0].bytes,32);
offset += 32;
@@ -1759,7 +1464,6 @@ namespace hw {
bool device_ledger::mlsag_prepare(const rct::key &H, const rct::key &xx,
rct::key &a, rct::key &aG, rct::key &aHP, rct::key &II) {
AUTO_LOCK_CMD();
- int offset;
unsigned char options;
#ifdef DEBUG_HWDEVICE
@@ -1771,17 +1475,7 @@ namespace hw {
rct::key II_x;
#endif
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_MLSAG;
- 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;
+ int offset = set_command_header_noopt(INS_MLSAG, 0x01);
//value H
memmove(this->buffer_send+offset, H.bytes, 32);
offset += 32;
@@ -1814,7 +1508,6 @@ namespace hw {
bool device_ledger::mlsag_prepare(rct::key &a, rct::key &aG) {
AUTO_LOCK_CMD();
- int offset;
unsigned char options;
#ifdef DEBUG_HWDEVICE
@@ -1822,21 +1515,7 @@ namespace hw {
rct::key aG_x;
#endif
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_MLSAG;
- 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;
-
- this->buffer_send[4] = offset-5;
- this->length_send = offset;
- this->exchange();
+ send_simple(INS_MLSAG, 0x01);
memmove(a.bytes, &this->buffer_recv[32*0], 32);
memmove(aG.bytes, &this->buffer_recv[32*1], 32);
@@ -1852,7 +1531,6 @@ namespace hw {
bool device_ledger::mlsag_hash(const rct::keyV &long_message, rct::key &c) {
AUTO_LOCK_CMD();
- int offset;
unsigned char options;
size_t cnt;
@@ -1864,14 +1542,7 @@ namespace hw {
cnt = long_message.size();
for (size_t i = 0; i<cnt; i++) {
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_MLSAG;
- this->buffer_send[2] = 0x02;
- this->buffer_send[3] = i+1;
- this->buffer_send[4] = 0x00;
- offset = 5;
+ int offset = set_command_header(INS_MLSAG, 0x02, i+1);
//options
this->buffer_send[offset] =
(i==(cnt-1))?0x00:0x80; //last
@@ -1896,7 +1567,6 @@ namespace hw {
bool device_ledger::mlsag_sign(const rct::key &c, const rct::keyV &xx, const rct::keyV &alpha, const size_t rows, const size_t dsRows, rct::keyV &ss) {
AUTO_LOCK_CMD();
- int offset;
CHECK_AND_ASSERT_THROW_MES(dsRows<=rows, "dsRows greater than rows");
CHECK_AND_ASSERT_THROW_MES(xx.size() == rows, "xx size does not match rows");
@@ -1914,14 +1584,7 @@ namespace hw {
#endif
for (size_t j = 0; j < dsRows; j++) {
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_MLSAG;
- this->buffer_send[2] = 0x03;
- this->buffer_send[3] = j+1;
- this->buffer_send[4] = 0x00;
- offset = 5;
+ int offset = set_command_header(INS_MLSAG, 0x03, j+1);
//options
this->buffer_send[offset] = 0x00;
if (j==(dsRows-1)) {
@@ -1958,24 +1621,7 @@ namespace hw {
bool device_ledger::close_tx() {
AUTO_LOCK_CMD();
- int offset;
-
- reset_buffer();
-
- this->buffer_send[0] = 0x00;
- this->buffer_send[1] = INS_CLOSE_TX;
- this->buffer_send[2] = 0x00;
- this->buffer_send[3] = 0x00;
- this->buffer_send[4] = 0x00;
- offset = 5;
- //options
- this->buffer_send[offset] = 0x00;
- offset += 1;
-
- this->buffer_send[4] = offset-5;
- this->length_send = offset;
- this->exchange();
-
+ send_simple(INS_CLOSE_TX);
return true;
}
diff --git a/src/device/device_ledger.hpp b/src/device/device_ledger.hpp
index b62bdf959..c30a38aca 100644
--- a/src/device/device_ledger.hpp
+++ b/src/device/device_ledger.hpp
@@ -102,6 +102,9 @@ namespace hw {
void logRESP(void);
unsigned int exchange(unsigned int ok=0x9000, unsigned int mask=0xFFFF);
void reset_buffer(void);
+ int set_command_header(BYTE ins, BYTE p1 = 0x00, BYTE p2 = 0x00);
+ int set_command_header_noopt(BYTE ins, BYTE p1 = 0x00, BYTE p2 = 0x00);
+ void send_simple(BYTE ins, BYTE p1 = 0x00);
// hw running mode
device_mode mode;