Merge pull request #4032

87e158b device_ledger: factor the prologue code (moneromooo-monero)
This commit is contained in:
luigi1111 2018-07-19 13:54:39 -05:00
commit cff15e4123
No known key found for this signature in database
GPG Key ID: F4ACA0183641E010
2 changed files with 61 additions and 412 deletions

View File

@ -274,22 +274,32 @@ namespace hw {
/* ======================================================================= */ /* ======================================================================= */
/* MISC */ /* MISC */
/* ======================================================================= */ /* ======================================================================= */
bool device_ledger::reset() { int device_ledger::set_command_header(BYTE ins, BYTE p1, BYTE p2) {
int offset;
reset_buffer(); reset_buffer();
int offset = 0;
this->buffer_send[0] = 0x00; this->buffer_send[0] = 0x00;
this->buffer_send[1] = INS_RESET; this->buffer_send[1] = ins;
this->buffer_send[2] = 0x00; this->buffer_send[2] = p1;
this->buffer_send[3] = 0x00; this->buffer_send[3] = p2;
this->buffer_send[4] = 0x00; this->buffer_send[4] = 0x00;
offset = 5; return 5;
//options }
this->buffer_send[offset] = 0x00;
this->buffer_send[4] = offset-5; int device_ledger::set_command_header_noopt(BYTE ins, BYTE p1, BYTE p2) {
this->length_send = offset; 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(); this->exchange();
}
bool device_ledger::reset() {
send_simple(INS_RESET);
return true; return true;
} }
@ -451,20 +461,10 @@ namespace hw {
int offset; int offset;
reset_buffer();
switch(mode) { switch(mode) {
case TRANSACTION_CREATE_REAL: case TRANSACTION_CREATE_REAL:
case TRANSACTION_CREATE_FAKE: case TRANSACTION_CREATE_FAKE:
this->buffer_send[0] = 0x00; offset = set_command_header_noopt(INS_SET_SIGNATURE_MODE, 1);
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 //account
this->buffer_send[offset] = mode; this->buffer_send[offset] = mode;
offset += 1; offset += 1;
@ -495,22 +495,7 @@ namespace hw {
bool device_ledger::get_public_address(cryptonote::account_public_address &pubkey){ bool device_ledger::get_public_address(cryptonote::account_public_address &pubkey){
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
int offset; send_simple(INS_GET_KEY, 1);
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();
memmove(pubkey.m_view_public_key.data, this->buffer_recv, 32); memmove(pubkey.m_view_public_key.data, this->buffer_recv, 32);
memmove(pubkey.m_spend_public_key.data, this->buffer_recv+32, 32); memmove(pubkey.m_spend_public_key.data, this->buffer_recv+32, 32);
@ -526,22 +511,7 @@ namespace hw {
memset(skey.data, 0xFF, 32); memset(skey.data, 0xFF, 32);
//spcialkey, normal conf handled in decrypt //spcialkey, normal conf handled in decrypt
int offset; send_simple(INS_GET_KEY, 0x02);
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();
//View key is retrievied, if allowed, to speed up blockchain parsing //View key is retrievied, if allowed, to speed up blockchain parsing
memmove(this->viewkey.data, this->buffer_recv+0, 32); memmove(this->viewkey.data, this->buffer_recv+0, 32);
@ -563,7 +533,6 @@ namespace hw {
bool device_ledger::generate_chacha_key(const cryptonote::account_keys &keys, crypto::chacha_key &key) { bool device_ledger::generate_chacha_key(const cryptonote::account_keys &keys, crypto::chacha_key &key) {
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
int offset;
#ifdef DEBUG_HWDEVICE #ifdef DEBUG_HWDEVICE
crypto::chacha_key key_x; crypto::chacha_key key_x;
@ -571,20 +540,7 @@ namespace hw {
this->controle_device->generate_chacha_key(keys_x, key_x); this->controle_device->generate_chacha_key(keys_x, key_x);
#endif #endif
reset_buffer(); send_simple(INS_GET_CHACHA8_PREKEY);
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();
char prekey[200]; char prekey[200];
memmove(prekey, &this->buffer_recv[0], 200); memmove(prekey, &this->buffer_recv[0], 200);
@ -627,19 +583,7 @@ namespace hw {
crypto::derive_subaddress_public_key(pub, derivation, output_index,derived_pub); crypto::derive_subaddress_public_key(pub, derivation, output_index,derived_pub);
} else { } else {
int offset =0; int offset = set_command_header_noopt(INS_DERIVE_SUBADDRESS_PUBLIC_KEY);
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;
//pub //pub
memmove(this->buffer_send+offset, pub.data, 32); memmove(this->buffer_send+offset, pub.data, 32);
offset += 32; offset += 32;
@ -670,7 +614,6 @@ namespace hw {
crypto::public_key device_ledger::get_subaddress_spend_public_key(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) { crypto::public_key device_ledger::get_subaddress_spend_public_key(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) {
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
crypto::public_key D; crypto::public_key D;
int offset;
#ifdef DEBUG_HWDEVICE #ifdef DEBUG_HWDEVICE
const cryptonote::account_keys keys_x = hw::ledger::decrypt(keys); const cryptonote::account_keys keys_x = hw::ledger::decrypt(keys);
@ -687,17 +630,7 @@ namespace hw {
D = keys.m_account_address.m_spend_public_key; D = keys.m_account_address.m_spend_public_key;
} else { } else {
reset_buffer(); int offset = set_command_header_noopt(INS_GET_SUBADDRESS_SPEND_PUBLIC_KEY);
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;
//index //index
static_assert(sizeof(cryptonote::subaddress_index) == 8, "cryptonote::subaddress_index shall be 8 bytes length"); 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)); memmove(this->buffer_send+offset, &index, sizeof(cryptonote::subaddress_index));
@ -732,7 +665,6 @@ namespace hw {
cryptonote::account_public_address device_ledger::get_subaddress(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) { cryptonote::account_public_address device_ledger::get_subaddress(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) {
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
cryptonote::account_public_address address; cryptonote::account_public_address address;
int offset;
#ifdef DEBUG_HWDEVICE #ifdef DEBUG_HWDEVICE
const cryptonote::account_keys keys_x = hw::ledger::decrypt(keys); const cryptonote::account_keys keys_x = hw::ledger::decrypt(keys);
@ -751,17 +683,7 @@ namespace hw {
if (index.is_zero()) { if (index.is_zero()) {
address = keys.m_account_address; address = keys.m_account_address;
} else { } else {
reset_buffer(); int offset = set_command_header_noopt(INS_GET_SUBADDRESS);
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;
//index //index
static_assert(sizeof(cryptonote::subaddress_index) == 8, "cryptonote::subaddress_index shall be 8 bytes length"); 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)); memmove(this->buffer_send+offset, &index, sizeof(cryptonote::subaddress_index));
@ -786,7 +708,6 @@ namespace hw {
crypto::secret_key device_ledger::get_subaddress_secret_key(const crypto::secret_key &sec, const cryptonote::subaddress_index &index) { crypto::secret_key device_ledger::get_subaddress_secret_key(const crypto::secret_key &sec, const cryptonote::subaddress_index &index) {
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
crypto::secret_key sub_sec; crypto::secret_key sub_sec;
int offset;
#ifdef DEBUG_HWDEVICE #ifdef DEBUG_HWDEVICE
const crypto::secret_key sec_x = hw::ledger::decrypt(sec); const crypto::secret_key sec_x = hw::ledger::decrypt(sec);
@ -798,17 +719,7 @@ namespace hw {
hw::ledger::log_hexbuffer("get_subaddress_secret_key: [[OUT]] sub_sec", sub_sec_x.data, 32); hw::ledger::log_hexbuffer("get_subaddress_secret_key: [[OUT]] sub_sec", sub_sec_x.data, 32);
#endif #endif
reset_buffer(); int offset = set_command_header_noopt(INS_GET_SUBADDRESS_SECRET_KEY);
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;
//sec //sec
memmove(this->buffer_send+offset, sec.data, 32); memmove(this->buffer_send+offset, sec.data, 32);
offset += 32; offset += 32;
@ -839,16 +750,7 @@ namespace hw {
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
int offset, sw; int offset, sw;
reset_buffer(); offset = set_command_header_noopt(INS_VERIFY_KEY);
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;
//sec //sec
memmove(this->buffer_send+offset, secret_key.data, 32); memmove(this->buffer_send+offset, secret_key.data, 32);
offset += 32; offset += 32;
@ -871,7 +773,6 @@ namespace hw {
bool device_ledger::scalarmultKey(rct::key & aP, const rct::key &P, const rct::key &a) { bool device_ledger::scalarmultKey(rct::key & aP, const rct::key &P, const rct::key &a) {
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
int offset;
#ifdef DEBUG_HWDEVICE #ifdef DEBUG_HWDEVICE
const rct::key P_x = P; const rct::key P_x = P;
@ -883,17 +784,7 @@ namespace hw {
hw::ledger::log_hexbuffer("scalarmultKey: [[OUT]] aP", (char*)aP_x.bytes, 32); hw::ledger::log_hexbuffer("scalarmultKey: [[OUT]] aP", (char*)aP_x.bytes, 32);
#endif #endif
reset_buffer(); int offset = set_command_header_noopt(INS_SECRET_SCAL_MUL_KEY);
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;
//pub //pub
memmove(this->buffer_send+offset, P.bytes, 32); memmove(this->buffer_send+offset, P.bytes, 32);
offset += 32; offset += 32;
@ -918,7 +809,6 @@ namespace hw {
bool device_ledger::scalarmultBase(rct::key &aG, const rct::key &a) { bool device_ledger::scalarmultBase(rct::key &aG, const rct::key &a) {
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
int offset;
#ifdef DEBUG_HWDEVICE #ifdef DEBUG_HWDEVICE
const rct::key a_x = hw::ledger::decrypt(a); const rct::key a_x = hw::ledger::decrypt(a);
@ -928,17 +818,7 @@ namespace hw {
hw::ledger::log_hexbuffer("scalarmultKey: [[OUT]] aG", (char*)aG_x.bytes, 32); hw::ledger::log_hexbuffer("scalarmultKey: [[OUT]] aG", (char*)aG_x.bytes, 32);
#endif #endif
reset_buffer(); int offset = set_command_header_noopt(INS_SECRET_SCAL_MUL_BASE);
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;
//sec //sec
memmove(this->buffer_send+offset, a.bytes, 32); memmove(this->buffer_send+offset, a.bytes, 32);
offset += 32; offset += 32;
@ -959,7 +839,6 @@ namespace hw {
bool device_ledger::sc_secret_add( crypto::secret_key &r, const crypto::secret_key &a, const crypto::secret_key &b) { bool device_ledger::sc_secret_add( crypto::secret_key &r, const crypto::secret_key &a, const crypto::secret_key &b) {
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
int offset;
#ifdef DEBUG_HWDEVICE #ifdef DEBUG_HWDEVICE
const crypto::secret_key a_x = hw::ledger::decrypt(a); const crypto::secret_key a_x = hw::ledger::decrypt(a);
@ -968,17 +847,7 @@ namespace hw {
this->controle_device->sc_secret_add(r_x, a_x, b_x); this->controle_device->sc_secret_add(r_x, a_x, b_x);
#endif #endif
reset_buffer(); int offset = set_command_header_noopt(INS_SECRET_KEY_ADD);
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;
//sec key //sec key
memmove(this->buffer_send+offset, a.data, 32); memmove(this->buffer_send+offset, a.data, 32);
offset += 32; offset += 32;
@ -1007,8 +876,6 @@ namespace hw {
throw std::runtime_error("device generate key does not support recover"); throw std::runtime_error("device generate key does not support recover");
} }
int offset;
#ifdef DEBUG_HWDEVICE #ifdef DEBUG_HWDEVICE
bool recover_x = recover; bool recover_x = recover;
const crypto::secret_key recovery_key_x = recovery_key; const crypto::secret_key recovery_key_x = recovery_key;
@ -1016,21 +883,7 @@ namespace hw {
crypto::secret_key sec_x; crypto::secret_key sec_x;
#endif #endif
reset_buffer(); send_simple(INS_GENERATE_KEYPAIR);
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();
//pub key //pub key
memmove(pub.data, &this->buffer_recv[0], 32); memmove(pub.data, &this->buffer_recv[0], 32);
@ -1070,19 +923,7 @@ namespace hw {
r = crypto::generate_key_derivation(pub, this->viewkey, derivation); r = crypto::generate_key_derivation(pub, this->viewkey, derivation);
} else { } else {
int offset; int offset = set_command_header_noopt(INS_GEN_KEY_DERIVATION);
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;
//pub //pub
memmove(this->buffer_send+offset, pub.data, 32); memmove(this->buffer_send+offset, pub.data, 32);
offset += 32; offset += 32;
@ -1131,7 +972,6 @@ namespace hw {
bool device_ledger::derivation_to_scalar(const crypto::key_derivation &derivation, const size_t output_index, crypto::ec_scalar &res) { bool device_ledger::derivation_to_scalar(const crypto::key_derivation &derivation, const size_t output_index, crypto::ec_scalar &res) {
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
int offset;
#ifdef DEBUG_HWDEVICE #ifdef DEBUG_HWDEVICE
const crypto::key_derivation derivation_x = hw::ledger::decrypt(derivation); const crypto::key_derivation derivation_x = hw::ledger::decrypt(derivation);
@ -1143,17 +983,7 @@ namespace hw {
hw::ledger::log_hexbuffer("derivation_to_scalar: [[OUT]] res ", res_x.data, 32); hw::ledger::log_hexbuffer("derivation_to_scalar: [[OUT]] res ", res_x.data, 32);
#endif #endif
reset_buffer(); int offset = set_command_header_noopt(INS_DERIVATION_TO_SCALAR);
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;
//derivattion //derivattion
memmove(this->buffer_send+offset, derivation.data, 32); memmove(this->buffer_send+offset, derivation.data, 32);
offset += 32; offset += 32;
@ -1181,7 +1011,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) { 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(); AUTO_LOCK_CMD();
int offset;
#ifdef DEBUG_HWDEVICE #ifdef DEBUG_HWDEVICE
const crypto::key_derivation derivation_x = hw::ledger::decrypt(derivation); const crypto::key_derivation derivation_x = hw::ledger::decrypt(derivation);
@ -1195,17 +1024,7 @@ namespace hw {
hw::ledger::log_hexbuffer("derive_secret_key: [[OUT]] derived_sec", derived_sec_x.data, 32); hw::ledger::log_hexbuffer("derive_secret_key: [[OUT]] derived_sec", derived_sec_x.data, 32);
#endif #endif
reset_buffer(); int offset = set_command_header_noopt(INS_DERIVE_SECRET_KEY);
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;
//derivation //derivation
memmove(this->buffer_send+offset, derivation.data, 32); memmove(this->buffer_send+offset, derivation.data, 32);
offset += 32; offset += 32;
@ -1236,7 +1055,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){ 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(); AUTO_LOCK_CMD();
int offset;
#ifdef DEBUG_HWDEVICE #ifdef DEBUG_HWDEVICE
const crypto::key_derivation derivation_x = hw::ledger::decrypt(derivation); const crypto::key_derivation derivation_x = hw::ledger::decrypt(derivation);
@ -1250,17 +1068,7 @@ namespace hw {
hw::ledger::log_hexbuffer("derive_public_key: [[OUT]] derived_pub ", derived_pub_x.data, 32); hw::ledger::log_hexbuffer("derive_public_key: [[OUT]] derived_pub ", derived_pub_x.data, 32);
#endif #endif
reset_buffer(); int offset = set_command_header_noopt(INS_DERIVE_PUBLIC_KEY);
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;
//derivation //derivation
memmove(this->buffer_send+offset, derivation.data, 32); memmove(this->buffer_send+offset, derivation.data, 32);
offset += 32; offset += 32;
@ -1290,7 +1098,6 @@ namespace hw {
bool device_ledger::secret_key_to_public_key(const crypto::secret_key &sec, crypto::public_key &pub) { bool device_ledger::secret_key_to_public_key(const crypto::secret_key &sec, crypto::public_key &pub) {
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
int offset;
#ifdef DEBUG_HWDEVICE #ifdef DEBUG_HWDEVICE
const crypto::secret_key sec_x = hw::ledger::decrypt(sec); const crypto::secret_key sec_x = hw::ledger::decrypt(sec);
@ -1303,17 +1110,7 @@ namespace hw {
} }
#endif #endif
reset_buffer(); int offset = set_command_header_noopt(INS_SECRET_KEY_TO_PUBLIC_KEY);
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;
//sec key //sec key
memmove(this->buffer_send+offset, sec.data, 32); memmove(this->buffer_send+offset, sec.data, 32);
offset += 32; offset += 32;
@ -1334,7 +1131,6 @@ namespace hw {
bool device_ledger::generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image){ bool device_ledger::generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image){
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
int offset;
#ifdef DEBUG_HWDEVICE #ifdef DEBUG_HWDEVICE
const crypto::public_key pub_x = pub; const crypto::public_key pub_x = pub;
@ -1346,17 +1142,7 @@ namespace hw {
hw::ledger::log_hexbuffer("generate_key_image: [[OUT]] image ", image_x.data, 32); hw::ledger::log_hexbuffer("generate_key_image: [[OUT]] image ", image_x.data, 32);
#endif #endif
reset_buffer(); int offset = set_command_header_noopt(INS_GEN_KEY_IMAGE);
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;
//pub //pub
memmove(this->buffer_send+offset, pub.data, 32); memmove(this->buffer_send+offset, pub.data, 32);
offset += 32; offset += 32;
@ -1384,20 +1170,10 @@ namespace hw {
bool device_ledger::open_tx(crypto::secret_key &tx_key) { bool device_ledger::open_tx(crypto::secret_key &tx_key) {
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
int offset;
reset_buffer();
key_map.clear(); 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 //account
this->buffer_send[offset+0] = 0x00; this->buffer_send[offset+0] = 0x00;
this->buffer_send[offset+1] = 0x00; this->buffer_send[offset+1] = 0x00;
@ -1416,7 +1192,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) { 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(); AUTO_LOCK_CMD();
int offset;
#ifdef DEBUG_HWDEVICE #ifdef DEBUG_HWDEVICE
const crypto::public_key public_key_x = public_key; const crypto::public_key public_key_x = public_key;
@ -1425,17 +1200,7 @@ namespace hw {
this->controle_device->encrypt_payment_id(payment_id_x, public_key_x, secret_key_x); this->controle_device->encrypt_payment_id(payment_id_x, public_key_x, secret_key_x);
#endif #endif
reset_buffer(); int offset = set_command_header_noopt(INS_STEALTH);
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;
//pub //pub
memmove(&this->buffer_send[offset], public_key.data, 32); memmove(&this->buffer_send[offset], public_key.data, 32);
offset += 32; offset += 32;
@ -1467,7 +1232,6 @@ namespace hw {
bool device_ledger::ecdhEncode(rct::ecdhTuple & unmasked, const rct::key & AKout) { bool device_ledger::ecdhEncode(rct::ecdhTuple & unmasked, const rct::key & AKout) {
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
int offset;
#ifdef DEBUG_HWDEVICE #ifdef DEBUG_HWDEVICE
const rct::key AKout_x = hw::ledger::decrypt(AKout); const rct::key AKout_x = hw::ledger::decrypt(AKout);
@ -1475,17 +1239,7 @@ namespace hw {
this->controle_device->ecdhEncode(unmasked_x, AKout_x); this->controle_device->ecdhEncode(unmasked_x, AKout_x);
#endif #endif
reset_buffer(); int offset = set_command_header_noopt(INS_BLIND);
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;
// AKout // AKout
memmove(this->buffer_send+offset, AKout.bytes, 32); memmove(this->buffer_send+offset, AKout.bytes, 32);
offset += 32; offset += 32;
@ -1515,7 +1269,6 @@ namespace hw {
bool device_ledger::ecdhDecode(rct::ecdhTuple & masked, const rct::key & AKout) { bool device_ledger::ecdhDecode(rct::ecdhTuple & masked, const rct::key & AKout) {
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
int offset;
#ifdef DEBUG_HWDEVICE #ifdef DEBUG_HWDEVICE
const rct::key AKout_x = hw::ledger::decrypt(AKout); const rct::key AKout_x = hw::ledger::decrypt(AKout);
@ -1523,17 +1276,8 @@ namespace hw {
this->controle_device->ecdhDecode(masked_x, AKout_x); this->controle_device->ecdhDecode(masked_x, AKout_x);
#endif #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 // AKout
memmove(this->buffer_send+offset, AKout.bytes, 32); memmove(this->buffer_send+offset, AKout.bytes, 32);
offset += 32; offset += 32;
@ -1583,16 +1327,7 @@ namespace hw {
data = blob.data(); data = blob.data();
// ====== u8 type, varint txnfee ====== // ====== u8 type, varint txnfee ======
int offset; int offset = set_command_header(INS_VALIDATE, 0x01, 0x01);
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;
//options //options
this->buffer_send[offset] = (inputs_size == 0)?0x00:0x80; this->buffer_send[offset] = (inputs_size == 0)?0x00:0x80;
offset += 1; offset += 1;
@ -1620,13 +1355,7 @@ namespace hw {
//pseudoOuts //pseudoOuts
if ((type == rct::RCTTypeSimple) || (type == rct::RCTTypeSimpleBulletproof)) { if ((type == rct::RCTTypeSimple) || (type == rct::RCTTypeSimpleBulletproof)) {
for ( i = 0; i < inputs_size; i++) { for ( i = 0; i < inputs_size; i++) {
reset_buffer(); offset = set_command_header(INS_VALIDATE, 0x01, i+2);
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;
//options //options
this->buffer_send[offset] = (i==inputs_size-1)? 0x00:0x80; this->buffer_send[offset] = (i==inputs_size-1)? 0x00:0x80;
offset += 1; offset += 1;
@ -1653,14 +1382,7 @@ namespace hw {
log_hexbuffer("Pout not found", (char*)outPk[i].dest.bytes, 32); log_hexbuffer("Pout not found", (char*)outPk[i].dest.bytes, 32);
CHECK_AND_ASSERT_THROW_MES(found, "Pout not found"); CHECK_AND_ASSERT_THROW_MES(found, "Pout not found");
} }
reset_buffer(); offset = set_command_header(INS_VALIDATE, 0x02, i+1);
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;
//options //options
this->buffer_send[offset] = (i==outputs_size-1)? 0x00:0x80 ; this->buffer_send[offset] = (i==outputs_size-1)? 0x00:0x80 ;
offset += 1; offset += 1;
@ -1705,14 +1427,7 @@ namespace hw {
// ====== C[], message, proof====== // ====== C[], message, proof======
C_offset = kv_offset; C_offset = kv_offset;
for (i = 0; i < outputs_size; i++) { for (i = 0; i < outputs_size; i++) {
reset_buffer(); offset = set_command_header(INS_VALIDATE, 0x03, i+1);
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 //options
this->buffer_send[offset] = 0x80 ; this->buffer_send[offset] = 0x80 ;
offset += 1; offset += 1;
@ -1727,17 +1442,7 @@ namespace hw {
} }
reset_buffer(); offset = set_command_header_noopt(INS_VALIDATE, 0x03, i+1);
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;
//message //message
memmove(this->buffer_send+offset, hashes[0].bytes,32); memmove(this->buffer_send+offset, hashes[0].bytes,32);
offset += 32; offset += 32;
@ -1762,7 +1467,6 @@ namespace hw {
bool device_ledger::mlsag_prepare(const rct::key &H, const rct::key &xx, 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) { rct::key &a, rct::key &aG, rct::key &aHP, rct::key &II) {
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
int offset;
unsigned char options; unsigned char options;
#ifdef DEBUG_HWDEVICE #ifdef DEBUG_HWDEVICE
@ -1774,17 +1478,7 @@ namespace hw {
rct::key II_x; rct::key II_x;
#endif #endif
reset_buffer(); int offset = set_command_header_noopt(INS_MLSAG, 0x01);
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;
//value H //value H
memmove(this->buffer_send+offset, H.bytes, 32); memmove(this->buffer_send+offset, H.bytes, 32);
offset += 32; offset += 32;
@ -1817,7 +1511,6 @@ namespace hw {
bool device_ledger::mlsag_prepare(rct::key &a, rct::key &aG) { bool device_ledger::mlsag_prepare(rct::key &a, rct::key &aG) {
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
int offset;
unsigned char options; unsigned char options;
#ifdef DEBUG_HWDEVICE #ifdef DEBUG_HWDEVICE
@ -1825,21 +1518,7 @@ namespace hw {
rct::key aG_x; rct::key aG_x;
#endif #endif
reset_buffer(); send_simple(INS_MLSAG, 0x01);
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();
memmove(a.bytes, &this->buffer_recv[32*0], 32); memmove(a.bytes, &this->buffer_recv[32*0], 32);
memmove(aG.bytes, &this->buffer_recv[32*1], 32); memmove(aG.bytes, &this->buffer_recv[32*1], 32);
@ -1855,7 +1534,6 @@ namespace hw {
bool device_ledger::mlsag_hash(const rct::keyV &long_message, rct::key &c) { bool device_ledger::mlsag_hash(const rct::keyV &long_message, rct::key &c) {
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
int offset;
unsigned char options; unsigned char options;
size_t cnt; size_t cnt;
@ -1867,14 +1545,7 @@ namespace hw {
cnt = long_message.size(); cnt = long_message.size();
for (size_t i = 0; i<cnt; i++) { for (size_t i = 0; i<cnt; i++) {
reset_buffer(); int offset = set_command_header(INS_MLSAG, 0x02, i+1);
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;
//options //options
this->buffer_send[offset] = this->buffer_send[offset] =
(i==(cnt-1))?0x00:0x80; //last (i==(cnt-1))?0x00:0x80; //last
@ -1899,7 +1570,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) { 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(); AUTO_LOCK_CMD();
int offset;
CHECK_AND_ASSERT_THROW_MES(dsRows<=rows, "dsRows greater than rows"); 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"); CHECK_AND_ASSERT_THROW_MES(xx.size() == rows, "xx size does not match rows");
@ -1917,14 +1587,7 @@ namespace hw {
#endif #endif
for (size_t j = 0; j < dsRows; j++) { for (size_t j = 0; j < dsRows; j++) {
reset_buffer(); int offset = set_command_header(INS_MLSAG, 0x03, j+1);
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;
//options //options
this->buffer_send[offset] = 0x00; this->buffer_send[offset] = 0x00;
if (j==(dsRows-1)) { if (j==(dsRows-1)) {
@ -1961,24 +1624,7 @@ namespace hw {
bool device_ledger::close_tx() { bool device_ledger::close_tx() {
AUTO_LOCK_CMD(); AUTO_LOCK_CMD();
int offset; send_simple(INS_CLOSE_TX);
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();
return true; return true;
} }

View File

@ -102,6 +102,9 @@ namespace hw {
void logRESP(void); void logRESP(void);
unsigned int exchange(unsigned int ok=0x9000, unsigned int mask=0xFFFF); unsigned int exchange(unsigned int ok=0x9000, unsigned int mask=0xFFFF);
void reset_buffer(void); 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 // hw running mode
device_mode mode; device_mode mode;