| 1 | // Copyright (c) 2017-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | |
| 30 | #include "version.h" |
| 31 | #include "device_ledger.hpp" |
| 32 | #include "device_errors.hpp" |
| 33 | #include "int-util.h" |
| 34 | #include "ringct/rctOps.h" |
| 35 | #include "cryptonote_basic/account.h" |
| 36 | #include "cryptonote_basic/subaddress_index.h" |
| 37 | #include "cryptonote_core/cryptonote_tx_utils.h" |
| 38 | |
| 39 | #include "string_tools.h" |
| 40 | |
| 41 | namespace hw { |
| 42 | |
| 43 | namespace ledger { |
| 44 | |
| 45 | #ifdef WITH_DEVICE_LEDGER |
| 46 | |
| 47 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 48 | #define MONERO_DEFAULT_LOG_CATEGORY "device.ledger" |
| 49 | |
| 50 | /* ===================================================================== */ |
| 51 | /* === Debug ==== */ |
| 52 | /* ===================================================================== */ |
| 53 | |
| 54 | void set_apdu_verbose(bool verbose) { |
| 55 | apdu_verbose = verbose; |
| 56 | } |
| 57 | |
| 58 | #define TRACKD MTRACE("hw") |
| 59 | #define ASSERT_SW(sw,ok,msk) CHECK_AND_ASSERT_THROW_MES(((sw)&(msk))==(ok), \ |
| 60 | "Wrong Device Status: " << "0x" << std::hex << (sw) << " (" << Status::to_string(sw) << "), " << \ |
| 61 | "EXPECTED 0x" << std::hex << (ok) << " (" << Status::to_string(ok) << "), " << \ |
| 62 | "MASK 0x" << std::hex << (msk)); |
| 63 | #define ASSERT_T0(exp) CHECK_AND_ASSERT_THROW_MES(exp, "Protocol assert failure: "#exp ) ; |
| 64 | #define ASSERT_X(exp,msg) CHECK_AND_ASSERT_THROW_MES(exp, msg); |
| 65 | |
| 66 | #ifdef DEBUG_HWDEVICE |
| 67 | crypto::secret_key dbg_viewkey; |
| 68 | crypto::secret_key dbg_spendkey; |
| 69 | #endif |
| 70 | |
| 71 | struct Status |
| 72 | { |
| 73 | unsigned int code; |
| 74 | const char *string; |
| 75 | |
| 76 | constexpr operator unsigned int() const |
| 77 | { |
| 78 | return this->code; |
| 79 | } |
| 80 | |
| 81 | static const char *to_string(unsigned int code); |
| 82 | }; |
| 83 | |
| 84 | // Must be sorted in ascending order by the code |
| 85 | #define LEDGER_STATUS(status) {status, #status} |
| 86 | constexpr Status status_codes[] = { |
| 87 | LEDGER_STATUS(SW_OK), |
| 88 | LEDGER_STATUS(SW_WRONG_LENGTH), |
| 89 | LEDGER_STATUS(SW_SECURITY_PIN_LOCKED), |
| 90 | LEDGER_STATUS(SW_SECURITY_LOAD_KEY), |
| 91 | LEDGER_STATUS(SW_SECURITY_COMMITMENT_CONTROL), |
| 92 | LEDGER_STATUS(SW_SECURITY_AMOUNT_CHAIN_CONTROL), |
| 93 | LEDGER_STATUS(SW_SECURITY_COMMITMENT_CHAIN_CONTROL), |
| 94 | LEDGER_STATUS(SW_SECURITY_OUTKEYS_CHAIN_CONTROL), |
| 95 | LEDGER_STATUS(SW_SECURITY_MAXOUTPUT_REACHED), |
| 96 | LEDGER_STATUS(SW_SECURITY_HMAC), |
| 97 | LEDGER_STATUS(SW_SECURITY_RANGE_VALUE), |
| 98 | LEDGER_STATUS(SW_SECURITY_INTERNAL), |
| 99 | LEDGER_STATUS(SW_SECURITY_MAX_SIGNATURE_REACHED), |
| 100 | LEDGER_STATUS(SW_SECURITY_PREFIX_HASH), |
| 101 | LEDGER_STATUS(SW_SECURITY_LOCKED), |
| 102 | LEDGER_STATUS(SW_COMMAND_NOT_ALLOWED), |
| 103 | LEDGER_STATUS(SW_SUBCOMMAND_NOT_ALLOWED), |
| 104 | LEDGER_STATUS(SW_DENY), |
| 105 | LEDGER_STATUS(SW_KEY_NOT_SET), |
| 106 | LEDGER_STATUS(SW_WRONG_DATA), |
| 107 | LEDGER_STATUS(SW_WRONG_DATA_RANGE), |
| 108 | LEDGER_STATUS(SW_IO_FULL), |
| 109 | LEDGER_STATUS(SW_CLIENT_NOT_SUPPORTED), |
| 110 | LEDGER_STATUS(SW_WRONG_P1P2), |
| 111 | LEDGER_STATUS(SW_INS_NOT_SUPPORTED), |
| 112 | LEDGER_STATUS(SW_PROTOCOL_NOT_SUPPORTED), |
| 113 | LEDGER_STATUS(SW_UNKNOWN) |
| 114 | }; |
| 115 | |
| 116 | const char *Status::to_string(unsigned int code) |
| 117 | { |
| 118 | constexpr size_t status_codes_size = sizeof(status_codes) / sizeof(status_codes[0]); |
| 119 | constexpr const Status *status_codes_end = &status_codes[status_codes_size]; |
| 120 | |
| 121 | const Status *item = std::lower_bound(&status_codes[0], status_codes_end, code); |
| 122 | return (item == status_codes_end || code < *item) ? "UNKNOWN" : item->string; |
| 123 | } |
| 124 | |
| 125 | /* ===================================================================== */ |
| 126 | /* === hmacmap ==== */ |
| 127 | /* ===================================================================== */ |
| 128 | |
| 129 | |
| 130 | SecHMAC::SecHMAC(const uint8_t s[32], const uint8_t h[32]) { |
| 131 | memcpy(this->sec, s, 32); |
| 132 | memcpy(this->hmac, h, 32); |
| 133 | } |
| 134 | |
| 135 | void HMACmap::find_mac(const uint8_t sec[32], uint8_t hmac[32]) { |
| 136 | size_t sz = hmacs.size(); |
| 137 | log_hexbuffer("find_mac: lookup for ", (char*)sec,32); |
| 138 | for (size_t i=0; i<sz; i++) { |
| 139 | log_hexbuffer("find_mac: - try ",(char*)hmacs[i].sec,32); |
| 140 | if (memcmp(sec, hmacs[i].sec, 32) == 0) { |
| 141 | memcpy(hmac, hmacs[i].hmac, 32); |
| 142 | log_hexbuffer("find_mac: - found ",(char*)hmacs[i].hmac,32); |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | } |
| 147 | throw std::runtime_error("Protocol error: try to send untrusted secret"); |
| 148 | } |
| 149 | |
| 150 | void HMACmap::add_mac(const uint8_t sec[32], const uint8_t hmac[32]) { |
| 151 | log_hexbuffer("add_mac: sec ", (char*)sec,32); |
| 152 | log_hexbuffer("add_mac: hmac ", (char*)hmac,32); |
| 153 | hmacs.push_back(SecHMAC(sec,hmac)); |
| 154 | } |
| 155 | |
| 156 | void HMACmap::clear() { |
| 157 | hmacs.clear(); |
| 158 | } |
| 159 | |
| 160 | /* ===================================================================== */ |
| 161 | /* === Keymap ==== */ |
| 162 | /* ===================================================================== */ |
| 163 | |
| 164 | ABPkeys::ABPkeys(const rct::key& A, const rct::key& B, const bool is_subaddr, const bool is_change, const bool need_additional_txkeys, const size_t real_output_index, const rct::key& P, const rct::key& AK) { |
| 165 | Aout = A; |
| 166 | Bout = B; |
| 167 | is_subaddress = is_subaddr; |
| 168 | is_change_address = is_change; |
| 169 | additional_key = need_additional_txkeys; |
| 170 | index = real_output_index; |
| 171 | Pout = P; |
| 172 | AKout = AK; |
| 173 | } |
| 174 | |
| 175 | ABPkeys::ABPkeys(const ABPkeys& keys) { |
| 176 | Aout = keys.Aout; |
| 177 | Bout = keys.Bout; |
| 178 | is_subaddress = keys.is_subaddress; |
| 179 | is_change_address = keys.is_change_address; |
| 180 | additional_key = keys.additional_key; |
| 181 | index = keys.index; |
| 182 | Pout = keys.Pout; |
| 183 | AKout = keys.AKout; |
| 184 | } |
| 185 | |
| 186 | ABPkeys &ABPkeys::operator=(const ABPkeys& keys) { |
| 187 | if (&keys == this) |
| 188 | return *this; |
| 189 | Aout = keys.Aout; |
| 190 | Bout = keys.Bout; |
| 191 | is_subaddress = keys.is_subaddress; |
| 192 | is_change_address = keys.is_change_address; |
| 193 | additional_key = keys.additional_key; |
| 194 | index = keys.index; |
| 195 | Pout = keys.Pout; |
| 196 | AKout = keys.AKout; |
| 197 | return *this; |
| 198 | } |
| 199 | |
| 200 | bool Keymap::find(const rct::key& P, ABPkeys& keys) const { |
| 201 | size_t sz = ABP.size(); |
| 202 | for (size_t i=0; i<sz; i++) { |
| 203 | if (ABP[i].Pout == P) { |
| 204 | keys = ABP[i]; |
| 205 | return true; |
| 206 | } |
| 207 | } |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | void Keymap::add(const ABPkeys& keys) { |
| 212 | ABP.push_back(keys); |
| 213 | } |
| 214 | |
| 215 | void Keymap::clear() { |
| 216 | ABP.clear(); |
| 217 | } |
| 218 | |
| 219 | #ifdef DEBUG_HWDEVICE |
| 220 | void Keymap::log() { |
| 221 | log_message("keymap", "content"); |
| 222 | size_t sz = ABP.size(); |
| 223 | for (size_t i=0; i<sz; i++) { |
| 224 | log_message(" keymap", std::to_string(i)); |
| 225 | log_hexbuffer(" Aout", (char*)ABP[i].Aout.bytes, 32); |
| 226 | log_hexbuffer(" Bout", (char*)ABP[i].Bout.bytes, 32); |
| 227 | log_message (" is_sub", std::to_string(ABP[i].is_subaddress)); |
| 228 | log_message (" index", std::to_string(ABP[i].index)); |
| 229 | log_hexbuffer(" Pout", (char*)ABP[i].Pout.bytes, 32); |
| 230 | } |
| 231 | } |
| 232 | #endif |
| 233 | |
| 234 | /* ===================================================================== */ |
| 235 | /* === Internal Helpers ==== */ |
| 236 | /* ===================================================================== */ |
| 237 | static bool is_fake_view_key(const crypto::secret_key &sec) { |
| 238 | return sec == crypto::null_skey; |
| 239 | } |
| 240 | |
| 241 | bool operator==(const crypto::key_derivation &d0, const crypto::key_derivation &d1) { |
| 242 | static_assert(sizeof(crypto::key_derivation) == 32, "key_derivation must be 32 bytes"); |
| 243 | return !crypto_verify_32((const unsigned char*)&d0, (const unsigned char*)&d1); |
| 244 | } |
| 245 | |
| 246 | /* ===================================================================== */ |
| 247 | /* === Device ==== */ |
| 248 | /* ===================================================================== */ |
| 249 | |
| 250 | static int device_id = 0; |
| 251 | |
| 252 | #define PROTOCOL_VERSION 4 |
| 253 | |
| 254 | #define INS_NONE 0x00 |
| 255 | #define INS_RESET 0x02 |
| 256 | |
| 257 | #define INS_GET_KEY 0x20 |
| 258 | #define INS_DISPLAY_ADDRESS 0x21 |
| 259 | #define INS_PUT_KEY 0x22 |
| 260 | #define INS_GET_CHACHA8_PREKEY 0x24 |
| 261 | #define INS_VERIFY_KEY 0x26 |
| 262 | #define INS_MANAGE_SEEDWORDS 0x28 |
| 263 | |
| 264 | #define INS_SECRET_KEY_TO_PUBLIC_KEY 0x30 |
| 265 | #define INS_GEN_KEY_DERIVATION 0x32 |
| 266 | #define INS_DERIVATION_TO_SCALAR 0x34 |
| 267 | #define INS_DERIVE_PUBLIC_KEY 0x36 |
| 268 | #define INS_DERIVE_SECRET_KEY 0x38 |
| 269 | #define INS_GEN_KEY_IMAGE 0x3A |
| 270 | #define INS_DERIVE_VIEW_TAG 0x3B |
| 271 | #define INS_SECRET_KEY_ADD 0x3C |
| 272 | #define INS_SECRET_KEY_SUB 0x3E |
| 273 | #define INS_GENERATE_KEYPAIR 0x40 |
| 274 | #define INS_SECRET_SCAL_MUL_KEY 0x42 |
| 275 | #define INS_SECRET_SCAL_MUL_BASE 0x44 |
| 276 | |
| 277 | #define INS_DERIVE_SUBADDRESS_PUBLIC_KEY 0x46 |
| 278 | #define INS_GET_SUBADDRESS 0x48 |
| 279 | #define INS_GET_SUBADDRESS_SPEND_PUBLIC_KEY 0x4A |
| 280 | #define INS_GET_SUBADDRESS_SECRET_KEY 0x4C |
| 281 | |
| 282 | #define INS_OPEN_TX 0x70 |
| 283 | #define INS_SET_SIGNATURE_MODE 0x72 |
| 284 | #define INS_GET_ADDITIONAL_KEY 0x74 |
| 285 | #define INS_STEALTH 0x76 |
| 286 | #define INS_GEN_COMMITMENT_MASK 0x77 |
| 287 | #define INS_BLIND 0x78 |
| 288 | #define INS_UNBLIND 0x7A |
| 289 | #define INS_GEN_TXOUT_KEYS 0x7B |
| 290 | #define INS_PREFIX_HASH 0x7D |
| 291 | #define INS_VALIDATE 0x7C |
| 292 | #define INS_MLSAG 0x7E |
| 293 | #define INS_CLSAG 0x7F |
| 294 | #define INS_CLOSE_TX 0x80 |
| 295 | |
| 296 | #define INS_GET_TX_PROOF 0xA0 |
| 297 | |
| 298 | #define INS_GET_RESPONSE 0xc0 |
| 299 | |
| 300 | |
| 301 | device_ledger::device_ledger(): hw_device(0x0101, 0x05, 64, 2000) { |
| 302 | this->id = device_id++; |
| 303 | this->reset_buffer(); |
| 304 | this->mode = NONE; |
| 305 | this->has_view_key = false; |
| 306 | this->tx_in_progress = false; |
| 307 | m_callback = nullptr; |
| 308 | MDEBUG( "Device "<<this->id <<" Created"); |
| 309 | } |
| 310 | |
| 311 | device_ledger::~device_ledger() { |
| 312 | this->release(); |
| 313 | MDEBUG( "Device "<<this->id <<" Destroyed"); |
| 314 | } |
| 315 | |
| 316 | /* ======================================================================= */ |
| 317 | /* LOCKER */ |
| 318 | /* ======================================================================= */ |
| 319 | |
| 320 | //automatic lock one more level on device ensuring the current thread is allowed to use it |
| 321 | #define AUTO_LOCK_CMD() \ |
| 322 | /* lock both mutexes without deadlock*/ \ |
| 323 | std::lock(device_locker, command_locker); \ |
| 324 | /* make sure both already-locked mutexes are unlocked at the end of scope */ \ |
| 325 | std::lock_guard<std::recursive_mutex> lock1(device_locker, std::adopt_lock); \ |
| 326 | std::lock_guard<std::mutex> lock2(command_locker, std::adopt_lock) |
| 327 | |
| 328 | //lock the device for a long sequence |
| 329 | void device_ledger::lock(void) { |
| 330 | MDEBUG( "Ask for LOCKING for device "<<this->name << " in thread "); |
| 331 | device_locker.lock(); |
| 332 | MDEBUG( "Device "<<this->name << " LOCKed"); |
| 333 | } |
| 334 | |
| 335 | //lock the device for a long sequence |
| 336 | bool device_ledger::try_lock(void) { |
| 337 | MDEBUG( "Ask for LOCKING(try) for device "<<this->name << " in thread "); |
| 338 | bool r = device_locker.try_lock(); |
| 339 | if (r) { |
| 340 | MDEBUG( "Device "<<this->name << " LOCKed(try)"); |
| 341 | } else { |
| 342 | MDEBUG( "Device "<<this->name << " not LOCKed(try)"); |
| 343 | } |
| 344 | return r; |
| 345 | } |
| 346 | |
| 347 | //lock the device for a long sequence |
| 348 | void device_ledger::unlock(void) { |
| 349 | try { |
| 350 | MDEBUG( "Ask for UNLOCKING for device "<<this->name << " in thread "); |
| 351 | } catch (...) { |
| 352 | } |
| 353 | device_locker.unlock(); |
| 354 | MDEBUG( "Device "<<this->name << " UNLOCKed"); |
| 355 | } |
| 356 | |
| 357 | |
| 358 | /* ======================================================================= */ |
| 359 | /* IO */ |
| 360 | /* ======================================================================= */ |
| 361 | |
| 362 | #define IO_SW_DENY 0x6982 |
| 363 | #define IO_SECRET_KEY 0x02 |
| 364 | |
| 365 | void device_ledger::logCMD() { |
| 366 | if (apdu_verbose) { |
| 367 | char strbuffer[1024]; |
| 368 | snprintf(strbuffer, sizeof(strbuffer), "%.02x %.02x %.02x %.02x %.02x ", |
| 369 | this->buffer_send[0], |
| 370 | this->buffer_send[1], |
| 371 | this->buffer_send[2], |
| 372 | this->buffer_send[3], |
| 373 | this->buffer_send[4] |
| 374 | ); |
| 375 | const size_t len = strlen(strbuffer); |
| 376 | buffer_to_str(strbuffer+len, sizeof(strbuffer)-len, (char*)(this->buffer_send+5), this->length_send-5); |
| 377 | MDEBUG( "CMD : " << strbuffer); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | void device_ledger::logRESP() { |
| 382 | if (apdu_verbose) { |
| 383 | char strbuffer[1024]; |
| 384 | snprintf(strbuffer, sizeof(strbuffer), "%.04x ", this->sw); |
| 385 | const size_t len = strlen(strbuffer); |
| 386 | buffer_to_str(strbuffer+len, sizeof(strbuffer)-len, (char*)(this->buffer_recv), this->length_recv); |
| 387 | MDEBUG( "RESP : " << strbuffer); |
| 388 | |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | int device_ledger::set_command_header(unsigned char ins, unsigned char p1, unsigned char p2) { |
| 393 | reset_buffer(); |
| 394 | this->buffer_send[0] = PROTOCOL_VERSION; |
| 395 | this->buffer_send[1] = ins; |
| 396 | this->buffer_send[2] = p1; |
| 397 | this->buffer_send[3] = p2; |
| 398 | this->buffer_send[4] = 0x00; |
| 399 | return 5; |
| 400 | } |
| 401 | |
| 402 | int device_ledger::set_command_header_noopt(unsigned char ins, unsigned char p1, unsigned char p2) { |
| 403 | int offset = set_command_header(ins, p1, p2); |
| 404 | //options |
| 405 | this->buffer_send[offset++] = 0; |
| 406 | this->buffer_send[4] = offset - 5; |
| 407 | return offset; |
| 408 | } |
| 409 | |
| 410 | void device_ledger::send_simple(unsigned char ins, unsigned char p1) { |
| 411 | this->length_send = set_command_header_noopt(ins, p1); |
| 412 | if (ins == INS_GET_KEY && p1 == IO_SECRET_KEY) { |
| 413 | // export view key user input |
| 414 | this->exchange_wait_on_input(); |
| 415 | } else { |
| 416 | this->exchange(); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | void device_ledger::send_secret(const unsigned char sec[32], int &offset) { |
| 421 | MDEBUG("send_secret: " << this->tx_in_progress); |
| 422 | ASSERT_X(offset + 32 <= BUFFER_SEND_SIZE, "send_secret: out of bounds write (secret)"); |
| 423 | memmove(this->buffer_send+offset, sec, 32); |
| 424 | offset +=32; |
| 425 | if (this->tx_in_progress) { |
| 426 | ASSERT_X(offset + 32 <= BUFFER_SEND_SIZE, "send_secret: out of bounds write (mac)"); |
| 427 | this->hmac_map.find_mac((uint8_t*)sec, this->buffer_send+offset); |
| 428 | offset += 32; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | void device_ledger::receive_secret(unsigned char sec[32], int &offset) { |
| 433 | MDEBUG("receive_secret: " << this->tx_in_progress); |
| 434 | ASSERT_X(offset + 32 <= BUFFER_RECV_SIZE, "receive_secret: out of bounds read (secret)"); |
| 435 | memmove(sec, this->buffer_recv+offset, 32); |
| 436 | offset += 32; |
| 437 | if (this->tx_in_progress) { |
| 438 | ASSERT_X(offset + 32 <= BUFFER_RECV_SIZE, "receive_secret: out of bounds read (mac)"); |
| 439 | this->hmac_map.add_mac((uint8_t*)sec, this->buffer_recv+offset); |
| 440 | offset += 32; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | bool device_ledger::reset() { |
| 445 | reset_buffer(); |
| 446 | int offset = set_command_header_noopt(INS_RESET); |
| 447 | const size_t verlen = strlen(MONERO_VERSION); |
| 448 | ASSERT_X(offset + verlen <= BUFFER_SEND_SIZE, "MONERO_VERSION is too long") |
| 449 | memmove(this->buffer_send+offset, MONERO_VERSION, verlen); |
| 450 | offset += strlen(MONERO_VERSION); |
| 451 | this->buffer_send[4] = offset-5; |
| 452 | this->length_send = offset; |
| 453 | this->exchange(); |
| 454 | |
| 455 | ASSERT_X(this->length_recv>=3, "Communication error, less than three bytes received. Check your application version."); |
| 456 | |
| 457 | return true; |
| 458 | } |
| 459 | |
| 460 | unsigned int device_ledger::exchange(unsigned int ok, unsigned int mask) { |
| 461 | logCMD(); |
| 462 | |
| 463 | try { |
| 464 | this->length_recv = hw_device.exchange(this->buffer_send, this->length_send, this->buffer_recv, BUFFER_SEND_SIZE, false); |
| 465 | } |
| 466 | catch (const hw::error::device_disconnected &e) { |
| 467 | if (m_callback) { |
| 468 | m_callback->on_error(e.what()); |
| 469 | } |
| 470 | throw; |
| 471 | } |
| 472 | ASSERT_X(this->length_recv>=2, "Communication error, less than tow bytes received"); |
| 473 | |
| 474 | this->length_recv -= 2; |
| 475 | this->sw = (this->buffer_recv[length_recv]<<8) | this->buffer_recv[length_recv+1]; |
| 476 | logRESP(); |
| 477 | MDEBUG("Device "<< this->id << " exchange: sw: " << this->sw << " expected: " << ok); |
| 478 | ASSERT_X(sw != SW_CLIENT_NOT_SUPPORTED, "Monero Ledger App doesn't support current monero version. Try to update the Monero Ledger App, at least " << MINIMAL_APP_VERSION_MAJOR<< "." << MINIMAL_APP_VERSION_MINOR << "." << MINIMAL_APP_VERSION_MICRO << " is required."); |
| 479 | ASSERT_X(sw != SW_PROTOCOL_NOT_SUPPORTED, "Make sure no other program is communicating with the Ledger."); |
| 480 | if (sw == SW_UNKNOWN_PROBABLY_LOCKED) { |
| 481 | hw_device.disconnected = true; |
| 482 | if (m_callback) { |
| 483 | m_callback->on_error("Make sure the device is unlocked", sw); |
| 484 | } |
| 485 | throw hw::error::device_disconnected("Device is locked"); |
| 486 | } |
| 487 | |
| 488 | ASSERT_SW(this->sw,ok,mask); |
| 489 | |
| 490 | return this->sw; |
| 491 | } |
| 492 | |
| 493 | unsigned int device_ledger::exchange_wait_on_input(unsigned int ok, unsigned int mask) { |
| 494 | logCMD(); |
| 495 | unsigned int deny = 0; |
| 496 | this->length_recv = hw_device.exchange(this->buffer_send, this->length_send, this->buffer_recv, BUFFER_SEND_SIZE, true); |
| 497 | ASSERT_X(this->length_recv>=2, "Communication error, less than two bytes received"); |
| 498 | |
| 499 | this->length_recv -= 2; |
| 500 | this->sw = (this->buffer_recv[length_recv]<<8) | this->buffer_recv[length_recv+1]; |
| 501 | if (this->sw == IO_SW_DENY) { |
| 502 | // cancel on device |
| 503 | deny = 1; |
| 504 | } else { |
| 505 | ASSERT_SW(this->sw,ok,mask); |
| 506 | } |
| 507 | |
| 508 | logRESP(); |
| 509 | return deny; |
| 510 | } |
| 511 | |
| 512 | void device_ledger::reset_buffer() { |
| 513 | this->length_send = 0; |
| 514 | memset(this->buffer_send, 0, BUFFER_SEND_SIZE); |
| 515 | this->length_recv = 0; |
| 516 | memset(this->buffer_recv, 0, BUFFER_RECV_SIZE); |
| 517 | } |
| 518 | |
| 519 | /* ======================================================================= */ |
| 520 | /* SETUP/TEARDOWN */ |
| 521 | /* ======================================================================= */ |
| 522 | |
| 523 | bool device_ledger::set_name(const std::string & name) { |
| 524 | this->name = name; |
| 525 | return true; |
| 526 | } |
| 527 | |
| 528 | const std::string device_ledger::get_name() const { |
| 529 | if (!this->connected()) { |
| 530 | return std::string("<disconnected:").append(this->name).append(">"); |
| 531 | } |
| 532 | return this->name; |
| 533 | } |
| 534 | |
| 535 | bool device_ledger::init(void) { |
| 536 | this->controle_device = &hw::get_device("default"); |
| 537 | this->release(); |
| 538 | hw_device.init(); |
| 539 | MDEBUG( "Device "<<this->id <<" HIDUSB inited"); |
| 540 | return true; |
| 541 | } |
| 542 | |
| 543 | static const std::vector<hw::io::hid_conn_params> known_devices { |
| 544 | {0x2c97, 0x0001, 0, 0xffa0}, |
| 545 | {0x2c97, 0x0004, 0, 0xffa0}, |
| 546 | {0x2c97, 0x0005, 0, 0xffa0}, |
| 547 | {0x2c97, 0x0006, 0, 0xffa0}, |
| 548 | {0x2c97, 0x0007, 0, 0xffa0}, |
| 549 | }; |
| 550 | |
| 551 | bool device_ledger::connect(void) { |
| 552 | this->disconnect(); |
| 553 | hw_device.connect(known_devices); |
| 554 | this->reset(); |
| 555 | #ifdef DEBUG_HWDEVICE |
| 556 | cryptonote::account_public_address pubkey; |
| 557 | this->get_public_address(pubkey); |
| 558 | #endif |
| 559 | return true; |
| 560 | } |
| 561 | |
| 562 | bool device_ledger::connected(void) const { |
| 563 | return hw_device.connected(); |
| 564 | } |
| 565 | |
| 566 | bool device_ledger::disconnected() { |
| 567 | return hw_device.disconnected; |
| 568 | } |
| 569 | |
| 570 | bool device_ledger::disconnect() { |
| 571 | hw_device.disconnect(); |
| 572 | return true; |
| 573 | } |
| 574 | |
| 575 | bool device_ledger::release() { |
| 576 | this->disconnect(); |
| 577 | hw_device.release(); |
| 578 | return true; |
| 579 | } |
| 580 | |
| 581 | bool device_ledger::set_mode(device_mode mode) { |
| 582 | AUTO_LOCK_CMD(); |
| 583 | |
| 584 | int offset; |
| 585 | |
| 586 | switch(mode) { |
| 587 | case TRANSACTION_CREATE_REAL: |
| 588 | case TRANSACTION_CREATE_FAKE: |
| 589 | offset = set_command_header_noopt(INS_SET_SIGNATURE_MODE, 1); |
| 590 | //account |
| 591 | this->buffer_send[offset] = mode; |
| 592 | offset += 1; |
| 593 | |
| 594 | this->buffer_send[4] = offset-5; |
| 595 | this->length_send = offset; |
| 596 | this->exchange(); |
| 597 | |
| 598 | this->mode = mode; |
| 599 | break; |
| 600 | |
| 601 | case TRANSACTION_PARSE: |
| 602 | case NONE: |
| 603 | this->mode = mode; |
| 604 | break; |
| 605 | default: |
| 606 | CHECK_AND_ASSERT_THROW_MES(false, " device_ledger::set_mode(unsigned int mode): invalid mode: "<<mode); |
| 607 | } |
| 608 | MDEBUG("Switch to mode: " <<mode); |
| 609 | return device::set_mode(mode); |
| 610 | } |
| 611 | |
| 612 | |
| 613 | /* ======================================================================= */ |
| 614 | /* WALLET & ADDRESS */ |
| 615 | /* ======================================================================= */ |
| 616 | |
| 617 | bool device_ledger::get_public_address(cryptonote::account_public_address &pubkey){ |
| 618 | AUTO_LOCK_CMD(); |
| 619 | |
| 620 | send_simple(INS_GET_KEY, 1); |
| 621 | |
| 622 | memmove(pubkey.m_view_public_key.data, this->buffer_recv, 32); |
| 623 | memmove(pubkey.m_spend_public_key.data, this->buffer_recv+32, 32); |
| 624 | |
| 625 | return true; |
| 626 | } |
| 627 | |
| 628 | bool device_ledger::get_secret_keys(crypto::secret_key &vkey , crypto::secret_key &skey) { |
| 629 | AUTO_LOCK_CMD(); |
| 630 | |
| 631 | //secret spend key is represented as fake key on the wallet side |
| 632 | memset(vkey.data, 0x00, 32); |
| 633 | memset(skey.data, 0xFF, 32); |
| 634 | |
| 635 | //spcialkey, normal conf handled in decrypt |
| 636 | if (m_callback) { |
| 637 | m_callback->on_button_request(0); // Notify GUI that user needs to press button on device |
| 638 | } |
| 639 | send_simple(INS_GET_KEY, 0x02); |
| 640 | |
| 641 | memmove(this->viewkey.data, this->buffer_recv+0, 32); |
| 642 | if (is_fake_view_key(this->viewkey)) { |
| 643 | MDEBUG("Have Not view key"); |
| 644 | this->has_view_key = false; |
| 645 | return false; |
| 646 | } else { |
| 647 | MDEBUG("Have view key"); |
| 648 | memmove(vkey.data, this->viewkey.data, 32); |
| 649 | this->has_view_key = true; |
| 650 | } |
| 651 | |
| 652 | #ifdef DEBUG_HWDEVICE |
| 653 | send_simple(INS_GET_KEY, 0x04); |
| 654 | memmove(dbg_viewkey.data, this->buffer_recv+0, 32); |
| 655 | memmove(dbg_spendkey.data, this->buffer_recv+32, 32); |
| 656 | #endif |
| 657 | |
| 658 | return true; |
| 659 | } |
| 660 | |
| 661 | bool device_ledger::set_secret_view_key(const crypto::secret_key &vkey) { |
| 662 | this->viewkey = vkey; |
| 663 | this->has_view_key = true; |
| 664 | return true; |
| 665 | } |
| 666 | |
| 667 | bool device_ledger::generate_chacha_key(const cryptonote::account_keys &keys, crypto::chacha_key &key, uint64_t kdf_rounds) { |
| 668 | AUTO_LOCK_CMD(); |
| 669 | |
| 670 | #ifdef DEBUG_HWDEVICE |
| 671 | crypto::chacha_key key_x; |
| 672 | cryptonote::account_keys keys_x = hw::ledger::decrypt(keys); |
| 673 | this->controle_device->generate_chacha_key(keys_x, key_x, kdf_rounds); |
| 674 | #endif |
| 675 | |
| 676 | send_simple(INS_GET_CHACHA8_PREKEY); |
| 677 | |
| 678 | char prekey[200]; |
| 679 | memmove(prekey, &this->buffer_recv[0], 200); |
| 680 | crypto::generate_chacha_key_prehashed(&prekey[0], sizeof(prekey), key, kdf_rounds); |
| 681 | |
| 682 | #ifdef DEBUG_HWDEVICE |
| 683 | hw::ledger::check32("generate_chacha_key_prehashed", "key", (char*)key_x.data(), (char*)key.data()); |
| 684 | #endif |
| 685 | |
| 686 | return true; |
| 687 | } |
| 688 | |
| 689 | void device_ledger::display_address(const cryptonote::subaddress_index& index, const boost::optional<crypto::hash8> &payment_id) { |
| 690 | AUTO_LOCK_CMD(); |
| 691 | |
| 692 | int offset = set_command_header_noopt(INS_DISPLAY_ADDRESS, payment_id?1:0); |
| 693 | //index |
| 694 | memmove(this->buffer_send+offset, &index, sizeof(cryptonote::subaddress_index)); |
| 695 | offset +=8 ; |
| 696 | |
| 697 | //payment ID |
| 698 | if (payment_id) { |
| 699 | memmove(this->buffer_send+offset, (*payment_id).data, 8); |
| 700 | } else { |
| 701 | memset(this->buffer_send+offset, 0, 8); |
| 702 | } |
| 703 | offset +=8; |
| 704 | |
| 705 | this->buffer_send[4] = offset-5; |
| 706 | this->length_send = offset; |
| 707 | CHECK_AND_ASSERT_THROW_MES(this->exchange_wait_on_input() == 0, "Timeout/Error on display address."); |
| 708 | } |
| 709 | |
| 710 | /* ======================================================================= */ |
| 711 | /* SUB ADDRESS */ |
| 712 | /* ======================================================================= */ |
| 713 | |
| 714 | bool device_ledger::derive_subaddress_public_key(const crypto::public_key &pub, const crypto::key_derivation &derivation, const std::size_t output_index, crypto::public_key &derived_pub){ |
| 715 | #ifdef DEBUG_HWDEVICE |
| 716 | const crypto::public_key pub_x = pub; |
| 717 | crypto::key_derivation derivation_x; |
| 718 | if ((this->mode == TRANSACTION_PARSE) && has_view_key) { |
| 719 | derivation_x = derivation; |
| 720 | } else { |
| 721 | derivation_x = hw::ledger::decrypt(derivation); |
| 722 | } |
| 723 | const std::size_t output_index_x = output_index; |
| 724 | crypto::public_key derived_pub_x; |
| 725 | log_hexbuffer("derive_subaddress_public_key: [[IN]] pub ", pub_x.data, 32); |
| 726 | log_hexbuffer("derive_subaddress_public_key: [[IN]] derivation", derivation_x.data, 32); |
| 727 | log_message ("derive_subaddress_public_key: [[IN]] index ", std::to_string((int)output_index_x)); |
| 728 | if (!this->controle_device->derive_subaddress_public_key(pub_x, derivation_x,output_index_x,derived_pub_x)) |
| 729 | return false; |
| 730 | log_hexbuffer("derive_subaddress_public_key: [[OUT]] derived_pub", derived_pub_x.data, 32); |
| 731 | #endif |
| 732 | |
| 733 | if ((this->mode == TRANSACTION_PARSE) && has_view_key) { |
| 734 | //If we are in TRANSACTION_PARSE, the given derivation has been retrieved uncrypted (wihtout the help |
| 735 | //of the device), so continue that way. |
| 736 | MDEBUG( "derive_subaddress_public_key : PARSE mode with known viewkey"); |
| 737 | if (!crypto::derive_subaddress_public_key(pub, derivation, output_index,derived_pub)) |
| 738 | return false; |
| 739 | } else { |
| 740 | AUTO_LOCK_CMD(); |
| 741 | int offset = set_command_header_noopt(INS_DERIVE_SUBADDRESS_PUBLIC_KEY); |
| 742 | //pub |
| 743 | memmove(this->buffer_send+offset, pub.data, 32); |
| 744 | offset += 32; |
| 745 | //derivation |
| 746 | this->send_secret((unsigned char*)derivation.data, offset); |
| 747 | //index |
| 748 | this->buffer_send[offset+0] = output_index>>24; |
| 749 | this->buffer_send[offset+1] = output_index>>16; |
| 750 | this->buffer_send[offset+2] = output_index>>8; |
| 751 | this->buffer_send[offset+3] = output_index>>0; |
| 752 | offset += 4; |
| 753 | |
| 754 | this->buffer_send[4] = offset-5; |
| 755 | this->length_send = offset; |
| 756 | this->exchange(); |
| 757 | |
| 758 | //pub key |
| 759 | memmove(derived_pub.data, &this->buffer_recv[0], 32); |
| 760 | } |
| 761 | #ifdef DEBUG_HWDEVICE |
| 762 | hw::ledger::check32("derive_subaddress_public_key", "derived_pub", derived_pub_x.data, derived_pub.data); |
| 763 | #endif |
| 764 | |
| 765 | return true; |
| 766 | } |
| 767 | |
| 768 | crypto::public_key device_ledger::get_subaddress_spend_public_key(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) { |
| 769 | if (has_view_key) { |
| 770 | cryptonote::account_keys keys_{keys}; |
| 771 | keys_.m_view_secret_key = this->viewkey; |
| 772 | return this->controle_device->get_subaddress_spend_public_key(keys_, index); |
| 773 | } |
| 774 | |
| 775 | AUTO_LOCK_CMD(); |
| 776 | crypto::public_key D; |
| 777 | |
| 778 | #ifdef DEBUG_HWDEVICE |
| 779 | const cryptonote::account_keys keys_x = hw::ledger::decrypt(keys); |
| 780 | const cryptonote::subaddress_index index_x = index; |
| 781 | crypto::public_key D_x; |
| 782 | log_hexbuffer("get_subaddress_spend_public_key: [[IN]] keys.m_view_secret_key ", keys_x.m_view_secret_key.data,32); |
| 783 | log_hexbuffer("get_subaddress_spend_public_key: [[IN]] keys.m_spend_secret_key", keys_x.m_spend_secret_key.data,32); |
| 784 | log_message ("get_subaddress_spend_public_key: [[IN]] index ", std::to_string(index_x.major)+"."+std::to_string(index_x.minor)); |
| 785 | D_x = this->controle_device->get_subaddress_spend_public_key(keys_x, index_x); |
| 786 | log_hexbuffer("get_subaddress_spend_public_key: [[OUT]] derivation ", D_x.data, 32); |
| 787 | #endif |
| 788 | |
| 789 | if (index.is_zero()) { |
| 790 | D = keys.m_account_address.m_spend_public_key; |
| 791 | } else { |
| 792 | |
| 793 | int offset = set_command_header_noopt(INS_GET_SUBADDRESS_SPEND_PUBLIC_KEY); |
| 794 | //index |
| 795 | static_assert(sizeof(cryptonote::subaddress_index) == 8, "cryptonote::subaddress_index shall be 8 bytes length"); |
| 796 | memmove(this->buffer_send+offset, &index, sizeof(cryptonote::subaddress_index)); |
| 797 | offset +=8 ; |
| 798 | |
| 799 | this->buffer_send[4] = offset-5; |
| 800 | this->length_send = offset; |
| 801 | this->exchange(); |
| 802 | |
| 803 | memmove(D.data, &this->buffer_recv[0], 32); |
| 804 | } |
| 805 | |
| 806 | #ifdef DEBUG_HWDEVICE |
| 807 | hw::ledger::check32("get_subaddress_spend_public_key", "D", D_x.data, D.data); |
| 808 | #endif |
| 809 | |
| 810 | return D; |
| 811 | } |
| 812 | |
| 813 | 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) { |
| 814 | std::vector<crypto::public_key> pkeys; |
| 815 | cryptonote::subaddress_index index = {account, begin}; |
| 816 | crypto::public_key D; |
| 817 | for (uint32_t idx = begin; idx < end; ++idx) { |
| 818 | index.minor = idx; |
| 819 | D = this->get_subaddress_spend_public_key(keys, index); |
| 820 | pkeys.push_back(D); |
| 821 | } |
| 822 | return pkeys; |
| 823 | } |
| 824 | |
| 825 | cryptonote::account_public_address device_ledger::get_subaddress(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) { |
| 826 | if (has_view_key) { |
| 827 | cryptonote::account_keys keys_{keys}; |
| 828 | keys_.m_view_secret_key = this->viewkey; |
| 829 | return this->controle_device->get_subaddress(keys_, index); |
| 830 | } |
| 831 | |
| 832 | AUTO_LOCK_CMD(); |
| 833 | cryptonote::account_public_address address; |
| 834 | |
| 835 | #ifdef DEBUG_HWDEVICE |
| 836 | const cryptonote::account_keys keys_x = hw::ledger::decrypt(keys); |
| 837 | const cryptonote::subaddress_index index_x = index; |
| 838 | cryptonote::account_public_address address_x; |
| 839 | log_hexbuffer("get_subaddress: [[IN]] keys.m_view_secret_key ", keys_x.m_view_secret_key.data, 32); |
| 840 | log_hexbuffer("get_subaddress: [[IN]] keys.m_view_public_key", keys_x.m_account_address.m_view_public_key.data, 32); |
| 841 | log_hexbuffer("get_subaddress: [[IN]] keys.m_view_secret_key ", keys_x.m_view_secret_key.data, 32); |
| 842 | log_hexbuffer("get_subaddress: [[IN]] keys.m_spend_public_key", keys_x.m_account_address.m_spend_public_key.data, 32); |
| 843 | log_message ("get_subaddress: [[IN]] index ", std::to_string(index_x.major)+"."+std::to_string(index_x.minor)); |
| 844 | address_x = this->controle_device->get_subaddress(keys_x, index_x); |
| 845 | log_hexbuffer("get_subaddress: [[OUT]] keys.m_view_public_key ", address_x.m_view_public_key.data, 32); |
| 846 | log_hexbuffer("get_subaddress: [[OUT]] keys.m_spend_public_key", address_x.m_spend_public_key.data, 32); |
| 847 | #endif |
| 848 | |
| 849 | if (index.is_zero()) { |
| 850 | address = keys.m_account_address; |
| 851 | } else { |
| 852 | int offset = set_command_header_noopt(INS_GET_SUBADDRESS); |
| 853 | //index |
| 854 | static_assert(sizeof(cryptonote::subaddress_index) == 8, "cryptonote::subaddress_index shall be 8 bytes length"); |
| 855 | memmove(this->buffer_send+offset, &index, sizeof(cryptonote::subaddress_index)); |
| 856 | offset +=8 ; |
| 857 | |
| 858 | this->buffer_send[4] = offset-5; |
| 859 | this->length_send = offset; |
| 860 | this->exchange(); |
| 861 | |
| 862 | memmove(address.m_view_public_key.data, &this->buffer_recv[0], 32); |
| 863 | memmove(address.m_spend_public_key.data, &this->buffer_recv[32], 32); |
| 864 | } |
| 865 | |
| 866 | #ifdef DEBUG_HWDEVICE |
| 867 | hw::ledger::check32("get_subaddress", "address.m_view_public_key.data", address_x.m_view_public_key.data, address.m_view_public_key.data); |
| 868 | hw::ledger::check32("get_subaddress", "address.m_spend_public_key.data", address_x.m_spend_public_key.data, address.m_spend_public_key.data); |
| 869 | #endif |
| 870 | |
| 871 | return address; |
| 872 | } |
| 873 | |
| 874 | crypto::secret_key device_ledger::get_subaddress_secret_key(const crypto::secret_key &sec, const cryptonote::subaddress_index &index) { |
| 875 | AUTO_LOCK_CMD(); |
| 876 | crypto::secret_key sub_sec; |
| 877 | |
| 878 | #ifdef DEBUG_HWDEVICE |
| 879 | const crypto::secret_key sec_x = hw::ledger::decrypt(sec); |
| 880 | const cryptonote::subaddress_index index_x = index; |
| 881 | crypto::secret_key sub_sec_x; |
| 882 | log_message ("get_subaddress_secret_key: [[IN]] index ", std::to_string(index.major)+"."+std::to_string(index.minor)); |
| 883 | log_hexbuffer("get_subaddress_secret_key: [[IN]] sec ", sec_x.data, 32); |
| 884 | sub_sec_x = this->controle_device->get_subaddress_secret_key(sec_x, index_x); |
| 885 | log_hexbuffer("get_subaddress_secret_key: [[OUT]] sub_sec", sub_sec_x.data, 32); |
| 886 | #endif |
| 887 | |
| 888 | int offset = set_command_header_noopt(INS_GET_SUBADDRESS_SECRET_KEY); |
| 889 | //sec |
| 890 | this->send_secret((unsigned char*)sec.data, offset); |
| 891 | //index |
| 892 | static_assert(sizeof(cryptonote::subaddress_index) == 8, "cryptonote::subaddress_index shall be 8 bytes length"); |
| 893 | memmove(this->buffer_send+offset, &index, sizeof(cryptonote::subaddress_index)); |
| 894 | offset +=8 ; |
| 895 | |
| 896 | this->buffer_send[4] = offset-5; |
| 897 | this->length_send = offset; |
| 898 | this->exchange(); |
| 899 | |
| 900 | offset = 0; |
| 901 | this->receive_secret((unsigned char*)sub_sec.data, offset); |
| 902 | |
| 903 | #ifdef DEBUG_HWDEVICE |
| 904 | crypto::secret_key sub_sec_clear = hw::ledger::decrypt(sub_sec); |
| 905 | hw::ledger::check32("get_subaddress_secret_key", "sub_sec", sub_sec_x.data, sub_sec_clear.data); |
| 906 | #endif |
| 907 | |
| 908 | return sub_sec; |
| 909 | } |
| 910 | |
| 911 | /* ======================================================================= */ |
| 912 | /* DERIVATION & KEY */ |
| 913 | /* ======================================================================= */ |
| 914 | |
| 915 | bool device_ledger::verify_keys(const crypto::secret_key &secret_key, const crypto::public_key &public_key) { |
| 916 | AUTO_LOCK_CMD(); |
| 917 | int offset; |
| 918 | |
| 919 | offset = set_command_header_noopt(INS_VERIFY_KEY); |
| 920 | //sec |
| 921 | this->send_secret((unsigned char*)secret_key.data, offset); |
| 922 | //pub |
| 923 | memmove(this->buffer_send+offset, public_key.data, 32); |
| 924 | offset += 32; |
| 925 | |
| 926 | this->buffer_send[4] = offset-5; |
| 927 | this->length_send = offset; |
| 928 | this->exchange(); |
| 929 | |
| 930 | uint32_t verified = |
| 931 | this->buffer_recv[0] << 24 | |
| 932 | this->buffer_recv[1] << 16 | |
| 933 | this->buffer_recv[2] << 8 | |
| 934 | this->buffer_recv[3] << 0 ; |
| 935 | |
| 936 | return verified == 1; |
| 937 | } |
| 938 | |
| 939 | bool device_ledger::scalarmultKey(rct::key & aP, const rct::key &P, const rct::key &a) { |
| 940 | AUTO_LOCK_CMD(); |
| 941 | |
| 942 | #ifdef DEBUG_HWDEVICE |
| 943 | const rct::key P_x = P; |
| 944 | const rct::key a_x = hw::ledger::decrypt(a); |
| 945 | rct::key aP_x; |
| 946 | log_hexbuffer("scalarmultKey: [[IN]] P ", (char*)P_x.bytes, 32); |
| 947 | log_hexbuffer("scalarmultKey: [[IN]] a ", (char*)a_x.bytes, 32); |
| 948 | this->controle_device->scalarmultKey(aP_x, P_x, a_x); |
| 949 | log_hexbuffer("scalarmultKey: [[OUT]] aP", (char*)aP_x.bytes, 32); |
| 950 | #endif |
| 951 | |
| 952 | int offset = set_command_header_noopt(INS_SECRET_SCAL_MUL_KEY); |
| 953 | //pub |
| 954 | memmove(this->buffer_send+offset, P.bytes, 32); |
| 955 | offset += 32; |
| 956 | //sec |
| 957 | this->send_secret(a.bytes, offset); |
| 958 | |
| 959 | this->buffer_send[4] = offset-5; |
| 960 | this->length_send = offset; |
| 961 | this->exchange(); |
| 962 | |
| 963 | //pub key |
| 964 | memmove(aP.bytes, &this->buffer_recv[0], 32); |
| 965 | |
| 966 | #ifdef DEBUG_HWDEVICE |
| 967 | hw::ledger::check32("scalarmultKey", "mulkey", (char*)aP_x.bytes, (char*)aP.bytes); |
| 968 | #endif |
| 969 | |
| 970 | return true; |
| 971 | } |
| 972 | |
| 973 | bool device_ledger::scalarmultBase(rct::key &aG, const rct::key &a) { |
| 974 | AUTO_LOCK_CMD(); |
| 975 | |
| 976 | #ifdef DEBUG_HWDEVICE |
| 977 | const rct::key a_x = hw::ledger::decrypt(a); |
| 978 | rct::key aG_x; |
| 979 | log_hexbuffer("scalarmultKey: [[IN]] a ", (char*)a_x.bytes, 32); |
| 980 | this->controle_device->scalarmultBase(aG_x, a_x); |
| 981 | log_hexbuffer("scalarmultKey: [[OUT]] aG", (char*)aG_x.bytes, 32); |
| 982 | #endif |
| 983 | |
| 984 | int offset = set_command_header_noopt(INS_SECRET_SCAL_MUL_BASE); |
| 985 | //sec |
| 986 | this->send_secret(a.bytes, offset); |
| 987 | |
| 988 | this->buffer_send[4] = offset-5; |
| 989 | this->length_send = offset; |
| 990 | this->exchange(); |
| 991 | |
| 992 | //pub key |
| 993 | memmove(aG.bytes, &this->buffer_recv[0], 32); |
| 994 | |
| 995 | #ifdef DEBUG_HWDEVICE |
| 996 | hw::ledger::check32("scalarmultBase", "mulkey", (char*)aG_x.bytes, (char*)aG.bytes); |
| 997 | #endif |
| 998 | |
| 999 | return true; |
| 1000 | } |
| 1001 | |
| 1002 | bool device_ledger::sc_secret_add( crypto::secret_key &r, const crypto::secret_key &a, const crypto::secret_key &b) { |
| 1003 | AUTO_LOCK_CMD(); |
| 1004 | int offset; |
| 1005 | |
| 1006 | #ifdef DEBUG_HWDEVICE |
| 1007 | const crypto::secret_key a_x = hw::ledger::decrypt(a); |
| 1008 | const crypto::secret_key b_x = hw::ledger::decrypt(b); |
| 1009 | crypto::secret_key r_x; |
| 1010 | rct::key aG_x; |
| 1011 | log_hexbuffer("sc_secret_add: [[IN]] a ", (char*)a_x.data, 32); |
| 1012 | log_hexbuffer("sc_secret_add: [[IN]] b ", (char*)b_x.data, 32); |
| 1013 | this->controle_device->sc_secret_add(r_x, a_x, b_x); |
| 1014 | log_hexbuffer("sc_secret_add: [[OUT]] aG", (char*)r_x.data, 32); |
| 1015 | #endif |
| 1016 | |
| 1017 | offset = set_command_header_noopt(INS_SECRET_KEY_ADD); |
| 1018 | //sec key |
| 1019 | this->send_secret((unsigned char*)a.data, offset); |
| 1020 | //sec key |
| 1021 | this->send_secret((unsigned char*)b.data, offset); |
| 1022 | |
| 1023 | this->buffer_send[4] = offset-5; |
| 1024 | this->length_send = offset; |
| 1025 | this->exchange(); |
| 1026 | |
| 1027 | //sec key |
| 1028 | offset = 0; |
| 1029 | this->receive_secret((unsigned char*)r.data, offset); |
| 1030 | |
| 1031 | #ifdef DEBUG_HWDEVICE |
| 1032 | crypto::secret_key r_clear = hw::ledger::decrypt(r); |
| 1033 | hw::ledger::check32("sc_secret_add", "r", r_x.data, r_clear.data); |
| 1034 | #endif |
| 1035 | |
| 1036 | return true; |
| 1037 | } |
| 1038 | |
| 1039 | crypto::secret_key device_ledger::generate_keys(crypto::public_key &pub, crypto::secret_key &sec, const crypto::secret_key& recovery_key, bool recover) { |
| 1040 | AUTO_LOCK_CMD(); |
| 1041 | int offset; |
| 1042 | |
| 1043 | if (recover) { |
| 1044 | throw std::runtime_error("device generate key does not support recover"); |
| 1045 | } |
| 1046 | |
| 1047 | #ifdef DEBUG_HWDEVICE |
| 1048 | crypto::public_key pub_x; |
| 1049 | crypto::secret_key sec_x; |
| 1050 | crypto::secret_key recovery_key_x; |
| 1051 | if (recover) { |
| 1052 | recovery_key_x = hw::ledger::decrypt(recovery_key); |
| 1053 | log_hexbuffer("generate_keys: [[IN]] pub", (char*)recovery_key_x.data, 32); |
| 1054 | } |
| 1055 | #endif |
| 1056 | |
| 1057 | send_simple(INS_GENERATE_KEYPAIR); |
| 1058 | |
| 1059 | offset = 0; |
| 1060 | //pub key |
| 1061 | memmove(pub.data, &this->buffer_recv[0], 32); |
| 1062 | offset += 32; |
| 1063 | this->receive_secret((unsigned char*)sec.data, offset); |
| 1064 | |
| 1065 | #ifdef DEBUG_HWDEVICE |
| 1066 | crypto::secret_key sec_clear = hw::ledger::decrypt(sec); |
| 1067 | sec_x = sec_clear; |
| 1068 | log_hexbuffer("generate_keys: [[OUT]] pub", (char*)pub.data, 32); |
| 1069 | log_hexbuffer("generate_keys: [[OUT]] sec", (char*)sec_clear.data, 32); |
| 1070 | |
| 1071 | crypto::secret_key_to_public_key(sec_x,pub_x); |
| 1072 | hw::ledger::check32("generate_keys", "pub", pub_x.data, pub.data); |
| 1073 | #endif |
| 1074 | |
| 1075 | return sec; |
| 1076 | |
| 1077 | } |
| 1078 | |
| 1079 | bool device_ledger::generate_key_derivation(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_derivation &derivation) { |
| 1080 | bool r = false; |
| 1081 | |
| 1082 | #ifdef DEBUG_HWDEVICE |
| 1083 | const crypto::public_key pub_x = pub; |
| 1084 | const crypto::secret_key sec_x = (sec == rct::rct2sk(rct::I)) ? sec: hw::ledger::decrypt(sec); |
| 1085 | crypto::key_derivation derivation_x; |
| 1086 | log_hexbuffer("generate_key_derivation: [[IN]] pub ", pub_x.data, 32); |
| 1087 | log_hexbuffer("generate_key_derivation: [[IN]] sec ", sec_x.data, 32); |
| 1088 | if (!this->controle_device->generate_key_derivation(pub_x, sec_x, derivation_x)) |
| 1089 | return false; |
| 1090 | log_hexbuffer("generate_key_derivation: [[OUT]] derivation", derivation_x.data, 32); |
| 1091 | #endif |
| 1092 | |
| 1093 | if ((this->mode == TRANSACTION_PARSE) && has_view_key) { |
| 1094 | //A derivation is resquested in PASRE mode and we have the view key, |
| 1095 | //so do that wihtout the device and return the derivation unencrypted. |
| 1096 | r = crypto::generate_key_derivation(pub, this->viewkey, derivation); |
| 1097 | } else { |
| 1098 | AUTO_LOCK_CMD(); |
| 1099 | int offset = set_command_header_noopt(INS_GEN_KEY_DERIVATION); |
| 1100 | //pub |
| 1101 | memmove(this->buffer_send+offset, pub.data, 32); |
| 1102 | offset += 32; |
| 1103 | //sec |
| 1104 | this->send_secret((unsigned char*)sec.data, offset); |
| 1105 | |
| 1106 | this->buffer_send[4] = offset-5; |
| 1107 | this->length_send = offset; |
| 1108 | this->exchange(); |
| 1109 | |
| 1110 | offset = 0; |
| 1111 | //derivattion data |
| 1112 | this->receive_secret((unsigned char*)derivation.data, offset); |
| 1113 | |
| 1114 | r = true; |
| 1115 | } |
| 1116 | #ifdef DEBUG_HWDEVICE |
| 1117 | crypto::key_derivation derivation_clear ; |
| 1118 | if ((this->mode == TRANSACTION_PARSE) && has_view_key) { |
| 1119 | derivation_clear = derivation; |
| 1120 | } else { |
| 1121 | derivation_clear = hw::ledger::decrypt(derivation); |
| 1122 | } |
| 1123 | hw::ledger::check32("generate_key_derivation", "derivation", derivation_x.data, derivation_clear.data); |
| 1124 | #endif |
| 1125 | |
| 1126 | return r; |
| 1127 | } |
| 1128 | |
| 1129 | bool device_ledger::conceal_derivation(crypto::key_derivation &derivation, const crypto::public_key &tx_pub_key, const std::vector<crypto::public_key> &additional_tx_pub_keys, const crypto::key_derivation &main_derivation, const std::vector<crypto::key_derivation> &additional_derivations) { |
| 1130 | const crypto::public_key *pkey=NULL; |
| 1131 | if (derivation == main_derivation) { |
| 1132 | pkey = &tx_pub_key; |
| 1133 | MDEBUG("conceal derivation with main tx pub key"); |
| 1134 | } else { |
| 1135 | for(size_t n=0; n < additional_derivations.size();++n) { |
| 1136 | if(derivation == additional_derivations[n]) { |
| 1137 | pkey = &additional_tx_pub_keys[n]; |
| 1138 | MDEBUG("conceal derivation with additional tx pub key"); |
| 1139 | break; |
| 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | ASSERT_X(pkey, "Mismatched derivation on scan info"); |
| 1144 | return this->generate_key_derivation(*pkey, crypto::null_skey, derivation); |
| 1145 | } |
| 1146 | |
| 1147 | bool device_ledger::derivation_to_scalar(const crypto::key_derivation &derivation, const size_t output_index, crypto::ec_scalar &res) { |
| 1148 | AUTO_LOCK_CMD(); |
| 1149 | |
| 1150 | #ifdef DEBUG_HWDEVICE |
| 1151 | const crypto::key_derivation derivation_x = hw::ledger::decrypt(derivation); |
| 1152 | const size_t output_index_x = output_index; |
| 1153 | crypto::ec_scalar res_x; |
| 1154 | log_hexbuffer("derivation_to_scalar: [[IN]] derivation ", derivation_x.data, 32); |
| 1155 | log_message ("derivation_to_scalar: [[IN]] output_index ", std::to_string(output_index_x)); |
| 1156 | this->controle_device->derivation_to_scalar(derivation_x, output_index_x, res_x); |
| 1157 | log_hexbuffer("derivation_to_scalar: [[OUT]] res ", res_x.data, 32); |
| 1158 | #endif |
| 1159 | |
| 1160 | int offset = set_command_header_noopt(INS_DERIVATION_TO_SCALAR); |
| 1161 | //derivation |
| 1162 | this->send_secret((unsigned char*)derivation.data, offset); |
| 1163 | |
| 1164 | //index |
| 1165 | this->buffer_send[offset+0] = output_index>>24; |
| 1166 | this->buffer_send[offset+1] = output_index>>16; |
| 1167 | this->buffer_send[offset+2] = output_index>>8; |
| 1168 | this->buffer_send[offset+3] = output_index>>0; |
| 1169 | offset += 4; |
| 1170 | |
| 1171 | this->buffer_send[4] = offset-5; |
| 1172 | this->length_send = offset; |
| 1173 | this->exchange(); |
| 1174 | |
| 1175 | //derivation data |
| 1176 | offset = 0; |
| 1177 | this->receive_secret((unsigned char*)res.data, offset); |
| 1178 | |
| 1179 | #ifdef DEBUG_HWDEVICE |
| 1180 | crypto::ec_scalar res_clear = hw::ledger::decrypt(res); |
| 1181 | hw::ledger::check32("derivation_to_scalar", "res", res_x.data, res_clear.data); |
| 1182 | #endif |
| 1183 | |
| 1184 | return true; |
| 1185 | } |
| 1186 | |
| 1187 | 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) { |
| 1188 | AUTO_LOCK_CMD(); |
| 1189 | |
| 1190 | #ifdef DEBUG_HWDEVICE |
| 1191 | const crypto::key_derivation derivation_x = hw::ledger::decrypt(derivation); |
| 1192 | const std::size_t output_index_x = output_index; |
| 1193 | const crypto::secret_key sec_x = hw::ledger::decrypt(sec); |
| 1194 | crypto::secret_key derived_sec_x; |
| 1195 | log_hexbuffer("derive_secret_key: [[IN]] derivation ", derivation_x.data, 32); |
| 1196 | log_message ("derive_secret_key: [[IN]] index ", std::to_string(output_index_x)); |
| 1197 | log_hexbuffer("derive_secret_key: [[IN]] sec ", sec_x.data, 32); |
| 1198 | this->controle_device->derive_secret_key(derivation_x, output_index_x, sec_x, derived_sec_x); |
| 1199 | log_hexbuffer("derive_secret_key: [[OUT]] derived_sec", derived_sec_x.data, 32); |
| 1200 | #endif |
| 1201 | |
| 1202 | int offset = set_command_header_noopt(INS_DERIVE_SECRET_KEY); |
| 1203 | //derivation |
| 1204 | this->send_secret((unsigned char*)derivation.data, offset); |
| 1205 | //index |
| 1206 | this->buffer_send[offset+0] = output_index>>24; |
| 1207 | this->buffer_send[offset+1] = output_index>>16; |
| 1208 | this->buffer_send[offset+2] = output_index>>8; |
| 1209 | this->buffer_send[offset+3] = output_index>>0; |
| 1210 | offset += 4; |
| 1211 | //sec |
| 1212 | this->send_secret((unsigned char*)sec.data, offset); |
| 1213 | |
| 1214 | this->buffer_send[4] = offset-5; |
| 1215 | this->length_send = offset; |
| 1216 | this->exchange(); |
| 1217 | |
| 1218 | offset = 0; |
| 1219 | //sec key |
| 1220 | this->receive_secret((unsigned char*)derived_sec.data, offset); |
| 1221 | |
| 1222 | #ifdef DEBUG_HWDEVICE |
| 1223 | crypto::secret_key derived_sec_clear = hw::ledger::decrypt(derived_sec); |
| 1224 | hw::ledger::check32("derive_secret_key", "derived_sec", derived_sec_x.data, derived_sec_clear.data); |
| 1225 | #endif |
| 1226 | |
| 1227 | return true; |
| 1228 | } |
| 1229 | |
| 1230 | 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){ |
| 1231 | AUTO_LOCK_CMD(); |
| 1232 | |
| 1233 | #ifdef DEBUG_HWDEVICE |
| 1234 | const crypto::key_derivation derivation_x = hw::ledger::decrypt(derivation); |
| 1235 | const std::size_t output_index_x = output_index; |
| 1236 | const crypto::public_key pub_x = pub; |
| 1237 | crypto::public_key derived_pub_x; |
| 1238 | log_hexbuffer("derive_public_key: [[IN]] derivation ", derivation_x.data, 32); |
| 1239 | log_message ("derive_public_key: [[IN]] output_index", std::to_string(output_index_x)); |
| 1240 | log_hexbuffer("derive_public_key: [[IN]] pub ", pub_x.data, 32); |
| 1241 | if (!this->controle_device->derive_public_key(derivation_x, output_index_x, pub_x, derived_pub_x)) |
| 1242 | return false; |
| 1243 | log_hexbuffer("derive_public_key: [[OUT]] derived_pub ", derived_pub_x.data, 32); |
| 1244 | #endif |
| 1245 | |
| 1246 | int offset = set_command_header_noopt(INS_DERIVE_PUBLIC_KEY); |
| 1247 | //derivation |
| 1248 | this->send_secret((unsigned char*)derivation.data, offset); |
| 1249 | //index |
| 1250 | this->buffer_send[offset+0] = output_index>>24; |
| 1251 | this->buffer_send[offset+1] = output_index>>16; |
| 1252 | this->buffer_send[offset+2] = output_index>>8; |
| 1253 | this->buffer_send[offset+3] = output_index>>0; |
| 1254 | offset += 4; |
| 1255 | //pub |
| 1256 | memmove(this->buffer_send+offset, pub.data, 32); |
| 1257 | offset += 32; |
| 1258 | |
| 1259 | this->buffer_send[4] = offset-5; |
| 1260 | this->length_send = offset; |
| 1261 | this->exchange(); |
| 1262 | |
| 1263 | //pub key |
| 1264 | memmove(derived_pub.data, &this->buffer_recv[0], 32); |
| 1265 | |
| 1266 | #ifdef DEBUG_HWDEVICE |
| 1267 | hw::ledger::check32("derive_public_key", "derived_pub", derived_pub_x.data, derived_pub.data); |
| 1268 | #endif |
| 1269 | |
| 1270 | return true; |
| 1271 | } |
| 1272 | |
| 1273 | bool device_ledger::secret_key_to_public_key(const crypto::secret_key &sec, crypto::public_key &pub) { |
| 1274 | AUTO_LOCK_CMD(); |
| 1275 | |
| 1276 | #ifdef DEBUG_HWDEVICE |
| 1277 | const crypto::secret_key sec_x = hw::ledger::decrypt(sec); |
| 1278 | crypto::public_key pub_x; |
| 1279 | log_hexbuffer("secret_key_to_public_key: [[IN]] sec ", sec_x.data, 32); |
| 1280 | bool rc = this->controle_device->secret_key_to_public_key(sec_x, pub_x); |
| 1281 | log_hexbuffer("secret_key_to_public_key: [[OUT]] pub", pub_x.data, 32); |
| 1282 | if (!rc){ |
| 1283 | log_message("FAIL secret_key_to_public_key", "secret_key rejected"); |
| 1284 | } |
| 1285 | #endif |
| 1286 | |
| 1287 | int offset = set_command_header_noopt(INS_SECRET_KEY_TO_PUBLIC_KEY); |
| 1288 | //sec key |
| 1289 | this->send_secret((unsigned char*)sec.data, offset); |
| 1290 | |
| 1291 | this->buffer_send[4] = offset-5; |
| 1292 | this->length_send = offset; |
| 1293 | this->exchange(); |
| 1294 | |
| 1295 | //pub key |
| 1296 | memmove(pub.data, &this->buffer_recv[0], 32); |
| 1297 | |
| 1298 | #ifdef DEBUG_HWDEVICE |
| 1299 | hw::ledger::check32("secret_key_to_public_key", "pub", pub_x.data, pub.data); |
| 1300 | #endif |
| 1301 | |
| 1302 | return true; |
| 1303 | } |
| 1304 | |
| 1305 | bool device_ledger::generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image){ |
| 1306 | AUTO_LOCK_CMD(); |
| 1307 | |
| 1308 | #ifdef DEBUG_HWDEVICE |
| 1309 | const crypto::public_key pub_x = pub; |
| 1310 | const crypto::secret_key sec_x = hw::ledger::decrypt(sec); |
| 1311 | crypto::key_image image_x; |
| 1312 | log_hexbuffer("generate_key_image: [[IN]] pub ", pub_x.data, 32); |
| 1313 | log_hexbuffer("generate_key_image: [[IN]] sec ", sec_x.data, 32); |
| 1314 | this->controle_device->generate_key_image(pub_x, sec_x, image_x); |
| 1315 | log_hexbuffer("generate_key_image: [[OUT]] image ", image_x.data, 32); |
| 1316 | #endif |
| 1317 | |
| 1318 | int offset = set_command_header_noopt(INS_GEN_KEY_IMAGE); |
| 1319 | //pub |
| 1320 | memmove(this->buffer_send+offset, pub.data, 32); |
| 1321 | offset += 32; |
| 1322 | //sec |
| 1323 | this->send_secret((unsigned char*)sec.data, offset); |
| 1324 | |
| 1325 | this->buffer_send[4] = offset-5; |
| 1326 | this->length_send = offset; |
| 1327 | this->exchange(); |
| 1328 | |
| 1329 | //pub key |
| 1330 | memmove(image.data, &this->buffer_recv[0], 32); |
| 1331 | |
| 1332 | #ifdef DEBUG_HWDEVICE |
| 1333 | hw::ledger::check32("generate_key_image", "image", image_x.data, image.data); |
| 1334 | #endif |
| 1335 | |
| 1336 | return true; |
| 1337 | } |
| 1338 | |
| 1339 | bool device_ledger::derive_view_tag(const crypto::key_derivation &derivation, const std::size_t output_index, crypto::view_tag &view_tag){ |
| 1340 | #ifdef DEBUG_HWDEVICE |
| 1341 | crypto::key_derivation derivation_x; |
| 1342 | if ((this->mode == TRANSACTION_PARSE) && has_view_key) { |
| 1343 | derivation_x = derivation; |
| 1344 | } else { |
| 1345 | derivation_x = hw::ledger::decrypt(derivation); |
| 1346 | } |
| 1347 | const std::size_t output_index_x = output_index; |
| 1348 | crypto::view_tag view_tag_x; |
| 1349 | log_hexbuffer("derive_view_tag: [[IN]] derivation ", derivation_x.data, 32); |
| 1350 | log_message ("derive_view_tag: [[IN]] output_index", std::to_string(output_index_x)); |
| 1351 | this->controle_device->derive_view_tag(derivation_x, output_index_x, view_tag_x); |
| 1352 | log_hexbuffer("derive_view_tag: [[OUT]] view_tag ", &view_tag_x.data, 1); |
| 1353 | #endif |
| 1354 | |
| 1355 | if ((this->mode == TRANSACTION_PARSE) && has_view_key) { |
| 1356 | //If we are in TRANSACTION_PARSE, the given derivation has been retrieved uncrypted (wihtout the help |
| 1357 | //of the device), so continue that way. |
| 1358 | MDEBUG( "derive_view_tag : PARSE mode with known viewkey"); |
| 1359 | crypto::derive_view_tag(derivation, output_index, view_tag); |
| 1360 | } else { |
| 1361 | AUTO_LOCK_CMD(); |
| 1362 | int offset = set_command_header_noopt(INS_DERIVE_VIEW_TAG); |
| 1363 | //derivation |
| 1364 | this->send_secret((unsigned char*)derivation.data, offset); |
| 1365 | //index |
| 1366 | this->buffer_send[offset+0] = output_index>>24; |
| 1367 | this->buffer_send[offset+1] = output_index>>16; |
| 1368 | this->buffer_send[offset+2] = output_index>>8; |
| 1369 | this->buffer_send[offset+3] = output_index>>0; |
| 1370 | offset += 4; |
| 1371 | |
| 1372 | this->buffer_send[4] = offset-5; |
| 1373 | this->length_send = offset; |
| 1374 | this->exchange(); |
| 1375 | |
| 1376 | //view tag |
| 1377 | memmove(&view_tag.data, &this->buffer_recv[0], 1); |
| 1378 | } |
| 1379 | |
| 1380 | #ifdef DEBUG_HWDEVICE |
| 1381 | hw::ledger::check1("derive_view_tag", "view_tag", &view_tag_x.data, &view_tag.data); |
| 1382 | #endif |
| 1383 | |
| 1384 | return true; |
| 1385 | } |
| 1386 | |
| 1387 | /* ======================================================================= */ |
| 1388 | /* TRANSACTION */ |
| 1389 | /* ======================================================================= */ |
| 1390 | |
| 1391 | void device_ledger::generate_tx_proof(const crypto::hash &prefix_hash, |
| 1392 | const crypto::public_key &R, const crypto::public_key &A, const boost::optional<crypto::public_key> &B, const crypto::public_key &D, const crypto::secret_key &r, |
| 1393 | crypto::signature &sig) { |
| 1394 | |
| 1395 | AUTO_LOCK_CMD(); |
| 1396 | |
| 1397 | #ifdef DEBUG_HWDEVICE |
| 1398 | const crypto::hash prefix_hash_x = prefix_hash; |
| 1399 | const crypto::public_key R_x = R; |
| 1400 | const crypto::public_key A_x = A; |
| 1401 | const boost::optional<crypto::public_key> B_x = B; |
| 1402 | const crypto::public_key D_x = D; |
| 1403 | const crypto::secret_key r_x = hw::ledger::decrypt(r); |
| 1404 | crypto::signature sig_x; |
| 1405 | log_hexbuffer("generate_tx_proof: [[IN]] prefix_hash ", prefix_hash_x.data, 32); |
| 1406 | log_hexbuffer("generate_tx_proof: [[IN]] R ", R_x.data, 32); |
| 1407 | log_hexbuffer("generate_tx_proof: [[IN]] A ", A_x.data, 32); |
| 1408 | if (B_x) { |
| 1409 | log_hexbuffer("generate_tx_proof: [[IN]] B ", (*B_x).data, 32); |
| 1410 | } |
| 1411 | log_hexbuffer("generate_tx_proof: [[IN]] D ", D_x.data, 32); |
| 1412 | log_hexbuffer("generate_tx_proof: [[IN]] r ", r_x.data, 32); |
| 1413 | #endif |
| 1414 | |
| 1415 | |
| 1416 | int offset = set_command_header(INS_GET_TX_PROOF); |
| 1417 | //options |
| 1418 | this->buffer_send[offset] = B?0x01:0x00; |
| 1419 | offset += 1; |
| 1420 | //prefix_hash |
| 1421 | memmove(&this->buffer_send[offset], prefix_hash.data, 32); |
| 1422 | offset += 32; |
| 1423 | // R |
| 1424 | memmove(&this->buffer_send[offset], R.data, 32); |
| 1425 | offset += 32; |
| 1426 | // A |
| 1427 | memmove(&this->buffer_send[offset], A.data, 32); |
| 1428 | offset += 32; |
| 1429 | // B |
| 1430 | if (B) { |
| 1431 | memmove(&this->buffer_send[offset], (*B).data, 32); |
| 1432 | } else { |
| 1433 | memset(&this->buffer_send[offset], 0, 32); |
| 1434 | } |
| 1435 | offset += 32; |
| 1436 | // D |
| 1437 | memmove(&this->buffer_send[offset], D.data, 32); |
| 1438 | offset += 32; |
| 1439 | // r |
| 1440 | this->send_secret((unsigned char*)r.data, offset); |
| 1441 | |
| 1442 | this->buffer_send[4] = offset-5; |
| 1443 | this->length_send = offset; |
| 1444 | this->exchange(); |
| 1445 | |
| 1446 | memmove(sig.c.data, &this->buffer_recv[0], 32); |
| 1447 | memmove(sig.r.data, &this->buffer_recv[32], 32); |
| 1448 | #ifdef DEBUG_HWDEVICE |
| 1449 | log_hexbuffer("GENERATE_TX_PROOF: **c** ", sig.c.data, sizeof( sig.c.data)); |
| 1450 | log_hexbuffer("GENERATE_TX_PROOF: **r** ", sig.r.data, sizeof( sig.r.data)); |
| 1451 | |
| 1452 | this->controle_device->generate_tx_proof(prefix_hash_x, R_x, A_x, B_x, D_x, r_x, sig_x); |
| 1453 | MDEBUG("FAIL is normal if random is not fixed in proof"); |
| 1454 | hw::ledger::check32("generate_tx_proof", "c", sig_x.c.data, sig.c.data); |
| 1455 | hw::ledger::check32("generate_tx_proof", "r", sig_x.r.data, sig.r.data); |
| 1456 | |
| 1457 | #endif |
| 1458 | } |
| 1459 | |
| 1460 | bool device_ledger::open_tx(crypto::secret_key &tx_key) { |
| 1461 | AUTO_LOCK_CMD(); |
| 1462 | this->lock(); |
| 1463 | key_map.clear(); |
| 1464 | hmac_map.clear(); |
| 1465 | this->tx_in_progress = true; |
| 1466 | int offset = set_command_header_noopt(INS_OPEN_TX, 0x01); |
| 1467 | |
| 1468 | //account |
| 1469 | this->buffer_send[offset+0] = 0x00; |
| 1470 | this->buffer_send[offset+1] = 0x00; |
| 1471 | this->buffer_send[offset+2] = 0x00; |
| 1472 | this->buffer_send[offset+3] = 0x00; |
| 1473 | offset += 4; |
| 1474 | |
| 1475 | this->buffer_send[4] = offset-5; |
| 1476 | this->length_send = offset; |
| 1477 | this->exchange(); |
| 1478 | |
| 1479 | //skip R, receive: r, r_hmac, fake_a, a_hmac, fake_b, hmac_b |
| 1480 | unsigned char tmp[32]; |
| 1481 | offset = 32; |
| 1482 | this->receive_secret((unsigned char*)tx_key.data, offset); |
| 1483 | this->receive_secret(tmp, offset); |
| 1484 | this->receive_secret(tmp, offset); |
| 1485 | |
| 1486 | #ifdef DEBUG_HWDEVICE |
| 1487 | const crypto::secret_key r_x = hw::ledger::decrypt(tx_key); |
| 1488 | log_hexbuffer("open_tx: [[OUT]] R ", (char*)&this->buffer_recv[0], 32); |
| 1489 | log_hexbuffer("open_tx: [[OUT]] r ", r_x.data, 32); |
| 1490 | #endif |
| 1491 | return true; |
| 1492 | } |
| 1493 | |
| 1494 | void device_ledger::get_transaction_prefix_hash(const cryptonote::transaction_prefix& tx, crypto::hash& h) { |
| 1495 | AUTO_LOCK_CMD(); |
| 1496 | |
| 1497 | int pref_length = 0, pref_offset = 0, offset = 0; |
| 1498 | |
| 1499 | #ifdef DEBUG_HWDEVICE |
| 1500 | crypto::hash h_x; |
| 1501 | this->controle_device->get_transaction_prefix_hash(tx,h_x); |
| 1502 | MDEBUG("get_transaction_prefix_hash [[IN]] h_x/1 "<<h_x); |
| 1503 | #endif |
| 1504 | |
| 1505 | std::ostringstream s_x; |
| 1506 | binary_archive<true> a_x(s_x); |
| 1507 | CHECK_AND_ASSERT_THROW_MES(::serialization::serialize(a_x, const_cast<cryptonote::transaction_prefix&>(tx)), |
| 1508 | "unable to serialize transaction prefix"); |
| 1509 | pref_length = s_x.str().size(); |
| 1510 | //auto pref = std::make_unique<unsigned char[]>(pref_length); |
| 1511 | auto uprt_pref = std::unique_ptr<unsigned char[]>{ new unsigned char[pref_length] }; |
| 1512 | unsigned char* pref = uprt_pref.get(); |
| 1513 | memmove(pref, s_x.str().data(), pref_length); |
| 1514 | |
| 1515 | offset = set_command_header_noopt(INS_PREFIX_HASH,1); |
| 1516 | pref_offset = 0; |
| 1517 | unsigned char v; |
| 1518 | |
| 1519 | //version as varint |
| 1520 | do { |
| 1521 | v = pref[pref_offset]; |
| 1522 | this->buffer_send[offset] = v; |
| 1523 | offset += 1; |
| 1524 | pref_offset += 1; |
| 1525 | } while (v&0x80); |
| 1526 | |
| 1527 | //locktime as var int |
| 1528 | do { |
| 1529 | v = pref[pref_offset]; |
| 1530 | this->buffer_send[offset] = v; |
| 1531 | offset += 1; |
| 1532 | pref_offset += 1; |
| 1533 | } while (v&0x80); |
| 1534 | |
| 1535 | this->buffer_send[4] = offset-5; |
| 1536 | this->length_send = offset; |
| 1537 | this->exchange_wait_on_input(); |
| 1538 | |
| 1539 | //hash remains |
| 1540 | int cnt = 0; |
| 1541 | while (pref_offset < pref_length) { |
| 1542 | int len; |
| 1543 | cnt++; |
| 1544 | offset = set_command_header(INS_PREFIX_HASH,2,cnt); |
| 1545 | len = pref_length - pref_offset; |
| 1546 | //options |
| 1547 | if (len > (BUFFER_SEND_SIZE-offset-3)) { |
| 1548 | len = BUFFER_SEND_SIZE-offset-3; |
| 1549 | this->buffer_send[offset] = 0x80; |
| 1550 | } else { |
| 1551 | this->buffer_send[offset] = 0x00; |
| 1552 | } |
| 1553 | offset += 1; |
| 1554 | //send chunk |
| 1555 | memmove(&this->buffer_send[offset], pref+pref_offset, len); |
| 1556 | offset += len; |
| 1557 | pref_offset += len; |
| 1558 | this->buffer_send[4] = offset-5; |
| 1559 | this->length_send = offset; |
| 1560 | this->exchange(); |
| 1561 | } |
| 1562 | memmove(h.data, &this->buffer_recv[0], 32); |
| 1563 | |
| 1564 | #ifdef DEBUG_HWDEVICE |
| 1565 | hw::ledger::check8("prefix_hash", "h", h_x.data, h.data); |
| 1566 | #endif |
| 1567 | } |
| 1568 | |
| 1569 | bool device_ledger::encrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key) { |
| 1570 | AUTO_LOCK_CMD(); |
| 1571 | |
| 1572 | #ifdef DEBUG_HWDEVICE |
| 1573 | const crypto::public_key public_key_x = public_key; |
| 1574 | const crypto::secret_key secret_key_x = hw::ledger::decrypt(secret_key); |
| 1575 | crypto::hash8 payment_id_x = payment_id; |
| 1576 | log_hexbuffer("encrypt_payment_id: [[IN]] payment_id ", payment_id_x.data, 32); |
| 1577 | log_hexbuffer("encrypt_payment_id: [[IN]] public_key ", public_key_x.data, 32); |
| 1578 | log_hexbuffer("encrypt_payment_id: [[IN]] secret_key ", secret_key_x.data, 32); |
| 1579 | this->controle_device->encrypt_payment_id(payment_id_x, public_key_x, secret_key_x); |
| 1580 | log_hexbuffer("encrypt_payment_id: [[OUT]] payment_id ", payment_id_x.data, 32); |
| 1581 | #endif |
| 1582 | |
| 1583 | int offset = set_command_header_noopt(INS_STEALTH); |
| 1584 | //pub |
| 1585 | memmove(&this->buffer_send[offset], public_key.data, 32); |
| 1586 | offset += 32; |
| 1587 | //sec |
| 1588 | this->send_secret((unsigned char*)secret_key.data, offset); |
| 1589 | //id |
| 1590 | memmove(&this->buffer_send[offset], payment_id.data, 8); |
| 1591 | offset += 8; |
| 1592 | |
| 1593 | this->buffer_send[4] = offset-5; |
| 1594 | this->length_send = offset; |
| 1595 | this->exchange(); |
| 1596 | memmove(payment_id.data, &this->buffer_recv[0], 8); |
| 1597 | |
| 1598 | #ifdef DEBUG_HWDEVICE |
| 1599 | hw::ledger::check8("stealth", "payment_id", payment_id_x.data, payment_id.data); |
| 1600 | #endif |
| 1601 | |
| 1602 | return true; |
| 1603 | } |
| 1604 | |
| 1605 | |
| 1606 | bool device_ledger::generate_output_ephemeral_keys(const size_t tx_version, const cryptonote::account_keys &sender_account_keys, const crypto::public_key &txkey_pub, const crypto::secret_key &tx_key, |
| 1607 | const cryptonote::tx_destination_entry &dst_entr, const boost::optional<cryptonote::account_public_address> &change_addr, const size_t output_index, |
| 1608 | const bool &need_additional_txkeys, const std::vector<crypto::secret_key> &additional_tx_keys, |
| 1609 | std::vector<crypto::public_key> &additional_tx_public_keys, |
| 1610 | std::vector<rct::key> &amount_keys, |
| 1611 | crypto::public_key &out_eph_public_key, |
| 1612 | bool use_view_tags, crypto::view_tag &view_tag) { |
| 1613 | AUTO_LOCK_CMD(); |
| 1614 | |
| 1615 | #ifdef DEBUG_HWDEVICE |
| 1616 | const size_t &tx_version_x = tx_version; |
| 1617 | const cryptonote::account_keys sender_account_keys_x = hw::ledger::decrypt(sender_account_keys); |
| 1618 | memmove((void*)sender_account_keys_x.m_view_secret_key.data, dbg_viewkey.data, 32); |
| 1619 | |
| 1620 | const crypto::public_key txkey_pub_x = txkey_pub; |
| 1621 | const crypto::secret_key tx_key_x = hw::ledger::decrypt(tx_key); |
| 1622 | const cryptonote::tx_destination_entry dst_entr_x = dst_entr; |
| 1623 | const boost::optional<cryptonote::account_public_address> change_addr_x = change_addr; |
| 1624 | const size_t output_index_x = output_index; |
| 1625 | const bool need_additional_txkeys_x = need_additional_txkeys; |
| 1626 | const bool use_view_tags_x = use_view_tags; |
| 1627 | |
| 1628 | std::vector<crypto::secret_key> additional_tx_keys_x; |
| 1629 | for (const auto &k: additional_tx_keys) { |
| 1630 | additional_tx_keys_x.push_back(hw::ledger::decrypt(k)); |
| 1631 | } |
| 1632 | |
| 1633 | std::vector<crypto::public_key> additional_tx_public_keys_x; |
| 1634 | std::vector<rct::key> amount_keys_x; |
| 1635 | crypto::public_key out_eph_public_key_x; |
| 1636 | crypto::view_tag view_tag_x; |
| 1637 | |
| 1638 | log_message ("generate_output_ephemeral_keys: [[IN]] tx_version", std::to_string(tx_version_x)); |
| 1639 | //log_hexbuffer("generate_output_ephemeral_keys: [[IN]] sender_account_keys.view", sender_account_keys.m_sview_secret_key.data, 32); |
| 1640 | //log_hexbuffer("generate_output_ephemeral_keys: [[IN]] sender_account_keys.spend", sender_account_keys.m_spend_secret_key.data, 32); |
| 1641 | log_hexbuffer("generate_output_ephemeral_keys: [[IN]] txkey_pub", txkey_pub_x.data, 32); |
| 1642 | log_hexbuffer("generate_output_ephemeral_keys: [[IN]] tx_key", tx_key_x.data, 32); |
| 1643 | log_hexbuffer("generate_output_ephemeral_keys: [[IN]] dst_entr.view", dst_entr_x.addr.m_view_public_key.data, 32); |
| 1644 | log_hexbuffer("generate_output_ephemeral_keys: [[IN]] dst_entr.spend", dst_entr_x.addr.m_spend_public_key.data, 32); |
| 1645 | if (change_addr) { |
| 1646 | log_hexbuffer("generate_output_ephemeral_keys: [[IN]] change_addr.view", (*change_addr_x).m_view_public_key.data, 32); |
| 1647 | log_hexbuffer("generate_output_ephemeral_keys: [[IN]] change_addr.spend", (*change_addr_x).m_spend_public_key.data, 32); |
| 1648 | } |
| 1649 | log_message ("generate_output_ephemeral_keys: [[IN]] output_index", std::to_string(output_index_x)); |
| 1650 | log_message ("generate_output_ephemeral_keys: [[IN]] need_additional_txkeys", std::to_string(need_additional_txkeys_x)); |
| 1651 | if(need_additional_txkeys_x) { |
| 1652 | log_hexbuffer("generate_output_ephemeral_keys: [[IN]] additional_tx_keys[oi]", additional_tx_keys_x[output_index].data, 32); |
| 1653 | } |
| 1654 | log_message ("generate_output_ephemeral_keys: [[IN]] use_view_tags", std::to_string(use_view_tags_x)); |
| 1655 | 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, |
| 1656 | additional_tx_public_keys_x, amount_keys_x, out_eph_public_key_x, use_view_tags_x, view_tag_x); |
| 1657 | if(need_additional_txkeys_x) { |
| 1658 | log_hexbuffer("additional_tx_public_keys_x: [[OUT]] additional_tx_public_keys_x", additional_tx_public_keys_x.back().data, 32); |
| 1659 | } |
| 1660 | if(use_view_tags_x) { |
| 1661 | log_hexbuffer("generate_output_ephemeral_keys: [[OUT]] view_tag", &view_tag_x.data, 1); |
| 1662 | } |
| 1663 | log_hexbuffer("generate_output_ephemeral_keys: [[OUT]] amount_keys ", (char*)amount_keys_x.back().bytes, 32); |
| 1664 | log_hexbuffer("generate_output_ephemeral_keys: [[OUT]] out_eph_public_key ", out_eph_public_key_x.data, 32); |
| 1665 | #endif |
| 1666 | |
| 1667 | ASSERT_X(tx_version > 1, "TX version not supported"<<tx_version); |
| 1668 | |
| 1669 | // make additional tx pubkey if necessary |
| 1670 | cryptonote::keypair additional_txkey; |
| 1671 | if (need_additional_txkeys) { |
| 1672 | additional_txkey.sec = additional_tx_keys[output_index]; |
| 1673 | } |
| 1674 | |
| 1675 | int offset = set_command_header_noopt(INS_GEN_TXOUT_KEYS); |
| 1676 | //tx_version |
| 1677 | this->buffer_send[offset+0] = tx_version>>24; |
| 1678 | this->buffer_send[offset+1] = tx_version>>16; |
| 1679 | this->buffer_send[offset+2] = tx_version>>8; |
| 1680 | this->buffer_send[offset+3] = tx_version>>0; |
| 1681 | offset += 4; |
| 1682 | //tx_key |
| 1683 | this->send_secret((unsigned char*)tx_key.data, offset); |
| 1684 | //txkey_pub |
| 1685 | memmove(&this->buffer_send[offset], txkey_pub.data, 32); |
| 1686 | offset += 32; |
| 1687 | //Aout |
| 1688 | memmove(&this->buffer_send[offset], dst_entr.addr.m_view_public_key.data, 32); |
| 1689 | offset += 32; |
| 1690 | //Bout |
| 1691 | memmove(&this->buffer_send[offset], dst_entr.addr.m_spend_public_key.data, 32); |
| 1692 | offset += 32; |
| 1693 | //output index |
| 1694 | this->buffer_send[offset+0] = output_index>>24; |
| 1695 | this->buffer_send[offset+1] = output_index>>16; |
| 1696 | this->buffer_send[offset+2] = output_index>>8; |
| 1697 | this->buffer_send[offset+3] = output_index>>0; |
| 1698 | offset += 4; |
| 1699 | //is_change, |
| 1700 | bool is_change = (change_addr && dst_entr.addr == *change_addr); |
| 1701 | this->buffer_send[offset] = is_change; |
| 1702 | offset++; |
| 1703 | //is_subaddress |
| 1704 | this->buffer_send[offset] = dst_entr.is_subaddress; |
| 1705 | offset++; |
| 1706 | //need_additional_key |
| 1707 | this->buffer_send[offset] = need_additional_txkeys; |
| 1708 | offset++; |
| 1709 | //additional_tx_key |
| 1710 | if (need_additional_txkeys) { |
| 1711 | this->send_secret((unsigned char*)additional_txkey.sec.data, offset); |
| 1712 | } else { |
| 1713 | memset(&this->buffer_send[offset], 0, 32); |
| 1714 | offset += 32; |
| 1715 | } |
| 1716 | //use_view_tags |
| 1717 | this->buffer_send[offset] = use_view_tags; |
| 1718 | offset++; |
| 1719 | |
| 1720 | this->buffer_send[4] = offset-5; |
| 1721 | this->length_send = offset; |
| 1722 | this->exchange(); |
| 1723 | |
| 1724 | offset = 0; |
| 1725 | unsigned int recv_len = this->length_recv; |
| 1726 | |
| 1727 | //if (tx_version > 1) |
| 1728 | { |
| 1729 | ASSERT_X(recv_len>=32, "Not enough data from device"); |
| 1730 | crypto::secret_key scalar1; |
| 1731 | this->receive_secret((unsigned char*)scalar1.data, offset); |
| 1732 | amount_keys.push_back(rct::sk2rct(scalar1)); |
| 1733 | recv_len -= 32; |
| 1734 | } |
| 1735 | ASSERT_X(recv_len>=32, "Not enough data from device"); |
| 1736 | memmove(out_eph_public_key.data, &this->buffer_recv[offset], 32); |
| 1737 | recv_len -= 32; |
| 1738 | offset += 32; |
| 1739 | |
| 1740 | if (need_additional_txkeys) |
| 1741 | { |
| 1742 | ASSERT_X(recv_len>=32, "Not enough data from device"); |
| 1743 | memmove(additional_txkey.pub.data, &this->buffer_recv[offset], 32); |
| 1744 | additional_tx_public_keys.push_back(additional_txkey.pub); |
| 1745 | offset += 32; |
| 1746 | recv_len -= 32; |
| 1747 | } |
| 1748 | |
| 1749 | if (use_view_tags) |
| 1750 | { |
| 1751 | ASSERT_X(recv_len>=1, "Not enough data from device"); |
| 1752 | memmove(&view_tag.data, &this->buffer_recv[offset], 1); |
| 1753 | offset++; |
| 1754 | recv_len -= 1; |
| 1755 | } |
| 1756 | |
| 1757 | // add ABPkeys |
| 1758 | 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, |
| 1759 | need_additional_txkeys, output_index, |
| 1760 | amount_keys.back(), out_eph_public_key); |
| 1761 | |
| 1762 | #ifdef DEBUG_HWDEVICE |
| 1763 | log_hexbuffer("generate_output_ephemeral_keys: clear amount_key", (const char*)hw::ledger::decrypt(amount_keys.back()).bytes, 32); |
| 1764 | hw::ledger::check32("generate_output_ephemeral_keys", "amount_key", (const char*)amount_keys_x.back().bytes, (const char*)hw::ledger::decrypt(amount_keys.back()).bytes); |
| 1765 | if (need_additional_txkeys) { |
| 1766 | hw::ledger::check32("generate_output_ephemeral_keys", "additional_tx_key", additional_tx_public_keys_x.back().data, additional_tx_public_keys.back().data); |
| 1767 | } |
| 1768 | hw::ledger::check32("generate_output_ephemeral_keys", "out_eph_public_key", out_eph_public_key_x.data, out_eph_public_key.data); |
| 1769 | if (use_view_tags) { |
| 1770 | hw::ledger::check1("generate_output_ephemeral_keys", "view_tag", &view_tag_x.data, &view_tag.data); |
| 1771 | } |
| 1772 | #endif |
| 1773 | |
| 1774 | return true; |
| 1775 | } |
| 1776 | |
| 1777 | bool device_ledger::add_output_key_mapping(const crypto::public_key &Aout, const crypto::public_key &Bout, const bool is_subaddress, const bool is_change, |
| 1778 | const bool need_additional, const size_t real_output_index, |
| 1779 | const rct::key &amount_key, const crypto::public_key &out_eph_public_key) { |
| 1780 | key_map.add(ABPkeys(rct::pk2rct(Aout),rct::pk2rct(Bout), is_subaddress, is_change, need_additional, real_output_index, rct::pk2rct(out_eph_public_key), amount_key)); |
| 1781 | return true; |
| 1782 | } |
| 1783 | |
| 1784 | rct::key device_ledger::genCommitmentMask(const rct::key &AKout) { |
| 1785 | #ifdef DEBUG_HWDEVICE |
| 1786 | const rct::key AKout_x = hw::ledger::decrypt(AKout); |
| 1787 | rct::key mask_x; |
| 1788 | mask_x = this->controle_device->genCommitmentMask(AKout_x); |
| 1789 | #endif |
| 1790 | |
| 1791 | rct::key mask; |
| 1792 | int offset = set_command_header_noopt(INS_GEN_COMMITMENT_MASK); |
| 1793 | // AKout |
| 1794 | this->send_secret(AKout.bytes, offset); |
| 1795 | |
| 1796 | this->buffer_send[4] = offset-5; |
| 1797 | this->length_send = offset; |
| 1798 | this->exchange(); |
| 1799 | |
| 1800 | memmove(mask.bytes, &this->buffer_recv[0], 32); |
| 1801 | |
| 1802 | #ifdef DEBUG_HWDEVICE |
| 1803 | hw::ledger::check32("genCommitmentMask", "mask", (const char*)mask_x.bytes, (const char*)mask.bytes); |
| 1804 | #endif |
| 1805 | |
| 1806 | return mask; |
| 1807 | } |
| 1808 | |
| 1809 | bool device_ledger::ecdhEncode(rct::ecdhTuple & unmasked, const rct::key & AKout, bool short_amount) { |
| 1810 | AUTO_LOCK_CMD(); |
| 1811 | |
| 1812 | #ifdef DEBUG_HWDEVICE |
| 1813 | const rct::key AKout_x = hw::ledger::decrypt(AKout); |
| 1814 | rct::ecdhTuple unmasked_x = unmasked; |
| 1815 | this->controle_device->ecdhEncode(unmasked_x, AKout_x, short_amount); |
| 1816 | #endif |
| 1817 | |
| 1818 | int offset = set_command_header(INS_BLIND); |
| 1819 | //options |
| 1820 | this->buffer_send[offset] = short_amount?0x02:0x00; |
| 1821 | offset += 1; |
| 1822 | // AKout |
| 1823 | this->send_secret(AKout.bytes, offset); |
| 1824 | //mask k |
| 1825 | memmove(this->buffer_send+offset, unmasked.mask.bytes, 32); |
| 1826 | offset += 32; |
| 1827 | //value v |
| 1828 | memmove(this->buffer_send+offset, unmasked.amount.bytes, 32); |
| 1829 | offset += 32; |
| 1830 | |
| 1831 | this->buffer_send[4] = offset-5; |
| 1832 | this->length_send = offset; |
| 1833 | this->exchange(); |
| 1834 | |
| 1835 | memmove(unmasked.amount.bytes, &this->buffer_recv[0], 32); |
| 1836 | memmove(unmasked.mask.bytes, &this->buffer_recv[32], 32); |
| 1837 | |
| 1838 | #ifdef DEBUG_HWDEVICE |
| 1839 | MDEBUG("ecdhEncode: Akout: "<<AKout_x); |
| 1840 | hw::ledger::check32("ecdhEncode", "amount", (char*)unmasked_x.amount.bytes, (char*)unmasked.amount.bytes); |
| 1841 | hw::ledger::check32("ecdhEncode", "mask", (char*)unmasked_x.mask.bytes, (char*)unmasked.mask.bytes); |
| 1842 | |
| 1843 | log_hexbuffer("Blind AKV input", (char*)&this->buffer_recv[64], 3*32); |
| 1844 | #endif |
| 1845 | |
| 1846 | return true; |
| 1847 | } |
| 1848 | |
| 1849 | bool device_ledger::ecdhDecode(rct::ecdhTuple & masked, const rct::key & AKout, bool short_amount) { |
| 1850 | AUTO_LOCK_CMD(); |
| 1851 | |
| 1852 | #ifdef DEBUG_HWDEVICE |
| 1853 | const rct::key AKout_x = hw::ledger::decrypt(AKout); |
| 1854 | rct::ecdhTuple masked_x = masked; |
| 1855 | this->controle_device->ecdhDecode(masked_x, AKout_x, short_amount); |
| 1856 | #endif |
| 1857 | |
| 1858 | int offset = set_command_header(INS_UNBLIND); |
| 1859 | //options |
| 1860 | this->buffer_send[offset] = short_amount?0x02:0x00; |
| 1861 | offset += 1; |
| 1862 | // AKout |
| 1863 | this->send_secret(AKout.bytes, offset); |
| 1864 | //mask k |
| 1865 | memmove(this->buffer_send+offset, masked.mask.bytes, 32); |
| 1866 | offset += 32; |
| 1867 | //value v |
| 1868 | memmove(this->buffer_send+offset, masked.amount.bytes, 32); |
| 1869 | offset += 32; |
| 1870 | |
| 1871 | this->buffer_send[4] = offset-5; |
| 1872 | this->length_send = offset; |
| 1873 | this->exchange(); |
| 1874 | |
| 1875 | memmove(masked.amount.bytes, &this->buffer_recv[0], 32); |
| 1876 | memmove(masked.mask.bytes, &this->buffer_recv[32], 32); |
| 1877 | |
| 1878 | #ifdef DEBUG_HWDEVICE |
| 1879 | MDEBUG("ecdhEncode: Akout: "<<AKout_x); |
| 1880 | hw::ledger::check32("ecdhDecode", "amount", (char*)masked_x.amount.bytes, (char*)masked.amount.bytes); |
| 1881 | hw::ledger::check32("ecdhDecode", "mask", (char*)masked_x.mask.bytes,(char*) masked.mask.bytes); |
| 1882 | #endif |
| 1883 | |
| 1884 | return true; |
| 1885 | } |
| 1886 | |
| 1887 | bool device_ledger::mlsag_prehash(const std::string &blob, size_t inputs_size, size_t outputs_size, |
| 1888 | const rct::keyV &hashes, const rct::ctkeyV &outPk, |
| 1889 | rct::key &prehash) { |
| 1890 | AUTO_LOCK_CMD(); |
| 1891 | unsigned int data_offset, C_offset, kv_offset, i; |
| 1892 | const char *data; |
| 1893 | |
| 1894 | #ifdef DEBUG_HWDEVICE |
| 1895 | const std::string blob_x = blob; |
| 1896 | size_t inputs_size_x = inputs_size; |
| 1897 | size_t outputs_size_x = outputs_size; |
| 1898 | const rct::keyV hashes_x = hashes; |
| 1899 | const rct::ctkeyV outPk_x = outPk; |
| 1900 | rct::key prehash_x; |
| 1901 | this->controle_device->mlsag_prehash(blob_x, inputs_size_x, outputs_size_x, hashes_x, outPk_x, prehash_x); |
| 1902 | if (inputs_size) { |
| 1903 | log_message("mlsag_prehash", (std::string("inputs_size not null: ") + std::to_string(inputs_size)).c_str()); |
| 1904 | } |
| 1905 | this->key_map.log(); |
| 1906 | #endif |
| 1907 | |
| 1908 | data = blob.data(); |
| 1909 | |
| 1910 | // ====== u8 type, varint txnfee ====== |
| 1911 | int offset = set_command_header(INS_VALIDATE, 0x01, 0x01); |
| 1912 | //options |
| 1913 | this->buffer_send[offset] = (inputs_size == 0)?0x00:0x80; |
| 1914 | offset += 1; |
| 1915 | |
| 1916 | //type |
| 1917 | uint8_t type = data[0]; |
| 1918 | this->buffer_send[offset] = data[0]; |
| 1919 | offset += 1; |
| 1920 | |
| 1921 | //txnfee |
| 1922 | data_offset = 1; |
| 1923 | while (data[data_offset]&0x80) { |
| 1924 | this->buffer_send[offset] = data[data_offset]; |
| 1925 | offset += 1; |
| 1926 | data_offset += 1; |
| 1927 | } |
| 1928 | this->buffer_send[offset] = data[data_offset]; |
| 1929 | offset += 1; |
| 1930 | data_offset += 1; |
| 1931 | |
| 1932 | this->buffer_send[4] = offset-5; |
| 1933 | this->length_send = offset; |
| 1934 | // check fee user input |
| 1935 | CHECK_AND_ASSERT_THROW_MES(this->exchange_wait_on_input() == 0, "Fee denied on device."); |
| 1936 | |
| 1937 | //pseudoOuts |
| 1938 | if (type == rct::RCTTypeSimple) { |
| 1939 | for ( i = 0; i < inputs_size; i++) { |
| 1940 | offset = set_command_header(INS_VALIDATE, 0x01, i+2); |
| 1941 | //options |
| 1942 | this->buffer_send[offset] = (i==inputs_size-1)? 0x00:0x80; |
| 1943 | offset += 1; |
| 1944 | //pseudoOut |
| 1945 | memmove(this->buffer_send+offset, data+data_offset,32); |
| 1946 | offset += 32; |
| 1947 | data_offset += 32; |
| 1948 | |
| 1949 | this->buffer_send[4] = offset-5; |
| 1950 | this->length_send = offset; |
| 1951 | this->exchange(); |
| 1952 | } |
| 1953 | } |
| 1954 | |
| 1955 | // ====== Aout, Bout, AKout, C, v, k ====== |
| 1956 | kv_offset = data_offset; |
| 1957 | if (type==rct::RCTTypeBulletproof2 || type==rct::RCTTypeCLSAG || type==rct::RCTTypeBulletproofPlus) { |
| 1958 | C_offset = kv_offset+ (8)*outputs_size; |
| 1959 | } else { |
| 1960 | C_offset = kv_offset+ (32+32)*outputs_size; |
| 1961 | } |
| 1962 | for ( i = 0; i < outputs_size; i++) { |
| 1963 | ABPkeys outKeys; |
| 1964 | bool found; |
| 1965 | |
| 1966 | found = this->key_map.find(outPk[i].dest, outKeys); |
| 1967 | if (!found) { |
| 1968 | log_hexbuffer("Pout not found", (char*)outPk[i].dest.bytes, 32); |
| 1969 | CHECK_AND_ASSERT_THROW_MES(found, "Pout not found"); |
| 1970 | } |
| 1971 | offset = set_command_header(INS_VALIDATE, 0x02, i+1); |
| 1972 | //options |
| 1973 | this->buffer_send[offset] = (i==outputs_size-1)? 0x00:0x80 ; |
| 1974 | this->buffer_send[offset] |= (type==rct::RCTTypeBulletproof2 || type==rct::RCTTypeCLSAG || type==rct::RCTTypeBulletproofPlus)?0x02:0x00; |
| 1975 | offset += 1; |
| 1976 | //is_subaddress |
| 1977 | this->buffer_send[offset] = outKeys.is_subaddress; |
| 1978 | offset++; |
| 1979 | //is_change_address |
| 1980 | this->buffer_send[offset] = outKeys.is_change_address; |
| 1981 | offset++; |
| 1982 | //Aout |
| 1983 | memmove(this->buffer_send+offset, outKeys.Aout.bytes, 32); |
| 1984 | offset+=32; |
| 1985 | //Bout |
| 1986 | memmove(this->buffer_send+offset, outKeys.Bout.bytes, 32); |
| 1987 | offset+=32; |
| 1988 | //AKout |
| 1989 | this->send_secret(outKeys.AKout.bytes, offset); |
| 1990 | |
| 1991 | //C |
| 1992 | memmove(this->buffer_send+offset, data+C_offset,32); |
| 1993 | offset += 32; |
| 1994 | C_offset += 32; |
| 1995 | if (type==rct::RCTTypeBulletproof2 || type==rct::RCTTypeCLSAG || type==rct::RCTTypeBulletproofPlus) { |
| 1996 | //k |
| 1997 | memset(this->buffer_send+offset, 0, 32); |
| 1998 | offset += 32; |
| 1999 | //v |
| 2000 | memset(this->buffer_send+offset, 0, 32); |
| 2001 | memmove(this->buffer_send+offset, data+kv_offset,8); |
| 2002 | offset += 32; |
| 2003 | kv_offset += 8; |
| 2004 | } else { |
| 2005 | //k |
| 2006 | memmove(this->buffer_send+offset, data+kv_offset,32); |
| 2007 | offset += 32; |
| 2008 | kv_offset += 32; |
| 2009 | //v |
| 2010 | memmove(this->buffer_send+offset, data+kv_offset,32); |
| 2011 | offset += 32; |
| 2012 | kv_offset += 32; |
| 2013 | } |
| 2014 | |
| 2015 | this->buffer_send[4] = offset-5; |
| 2016 | this->length_send = offset; |
| 2017 | // check transaction user input |
| 2018 | CHECK_AND_ASSERT_THROW_MES(this->exchange_wait_on_input() == 0, "Transaction denied on device."); |
| 2019 | #ifdef DEBUG_HWDEVICE |
| 2020 | log_hexbuffer("Prehash AKV input", (char*)&this->buffer_recv[64], 3*32); |
| 2021 | #endif |
| 2022 | } |
| 2023 | |
| 2024 | // ====== C[], message, proof====== |
| 2025 | C_offset = kv_offset; |
| 2026 | for (i = 0; i < outputs_size; i++) { |
| 2027 | offset = set_command_header(INS_VALIDATE, 0x03, i+1); |
| 2028 | //options |
| 2029 | this->buffer_send[offset] = 0x80 ; |
| 2030 | offset += 1; |
| 2031 | //C |
| 2032 | memmove(this->buffer_send+offset, data+C_offset,32); |
| 2033 | offset += 32; |
| 2034 | C_offset += 32; |
| 2035 | |
| 2036 | this->buffer_send[4] = offset-5; |
| 2037 | this->length_send = offset; |
| 2038 | this->exchange(); |
| 2039 | |
| 2040 | } |
| 2041 | |
| 2042 | offset = set_command_header_noopt(INS_VALIDATE, 0x03, i+1); |
| 2043 | //message |
| 2044 | memmove(this->buffer_send+offset, hashes[0].bytes,32); |
| 2045 | offset += 32; |
| 2046 | //proof |
| 2047 | memmove(this->buffer_send+offset, hashes[2].bytes,32); |
| 2048 | offset += 32; |
| 2049 | |
| 2050 | this->buffer_send[4] = offset-5; |
| 2051 | this->length_send = offset; |
| 2052 | this->exchange(); |
| 2053 | |
| 2054 | memmove(prehash.bytes, this->buffer_recv, 32); |
| 2055 | |
| 2056 | #ifdef DEBUG_HWDEVICE |
| 2057 | hw::ledger::check32("mlsag_prehash", "prehash", (char*)prehash_x.bytes, (char*)prehash.bytes); |
| 2058 | #endif |
| 2059 | |
| 2060 | return true; |
| 2061 | } |
| 2062 | |
| 2063 | |
| 2064 | bool device_ledger::mlsag_prepare(const rct::key &H, const rct::key &xx, |
| 2065 | rct::key &a, rct::key &aG, rct::key &aHP, rct::key &II) { |
| 2066 | AUTO_LOCK_CMD(); |
| 2067 | |
| 2068 | #ifdef DEBUG_HWDEVICE |
| 2069 | const rct::key H_x = H; |
| 2070 | const rct::key xx_x = hw::ledger::decrypt(xx); |
| 2071 | rct::key a_x; |
| 2072 | rct::key aG_x; |
| 2073 | rct::key aHP_x; |
| 2074 | rct::key II_x; |
| 2075 | #endif |
| 2076 | |
| 2077 | int offset = set_command_header_noopt(INS_MLSAG, 0x01); |
| 2078 | //value H |
| 2079 | memmove(this->buffer_send+offset, H.bytes, 32); |
| 2080 | offset += 32; |
| 2081 | //mask xin |
| 2082 | this->send_secret(xx.bytes, offset); |
| 2083 | |
| 2084 | this->buffer_send[4] = offset-5; |
| 2085 | this->length_send = offset; |
| 2086 | this->exchange(); |
| 2087 | |
| 2088 | offset = 0; |
| 2089 | this->receive_secret(a.bytes, offset); |
| 2090 | memmove(aG.bytes, &this->buffer_recv[offset], 32); |
| 2091 | offset +=32; |
| 2092 | memmove(aHP.bytes, &this->buffer_recv[offset], 32); |
| 2093 | offset +=32; |
| 2094 | memmove(II.bytes, &this->buffer_recv[offset], 32); |
| 2095 | |
| 2096 | #ifdef DEBUG_HWDEVICE |
| 2097 | a_x = hw::ledger::decrypt(a); |
| 2098 | |
| 2099 | rct::scalarmultBase(aG_x, a_x); |
| 2100 | rct::scalarmultKey(aHP_x, H_x, a_x); |
| 2101 | rct::scalarmultKey(II_x, H_x, xx_x); |
| 2102 | hw::ledger::check32("mlsag_prepare", "AG", (char*)aG_x.bytes, (char*)aG.bytes); |
| 2103 | hw::ledger::check32("mlsag_prepare", "aHP", (char*)aHP_x.bytes, (char*)aHP.bytes); |
| 2104 | hw::ledger::check32("mlsag_prepare", "II", (char*)II_x.bytes, (char*)II.bytes); |
| 2105 | #endif |
| 2106 | |
| 2107 | return true; |
| 2108 | } |
| 2109 | |
| 2110 | bool device_ledger::mlsag_prepare(rct::key &a, rct::key &aG) { |
| 2111 | AUTO_LOCK_CMD(); |
| 2112 | int offset; |
| 2113 | |
| 2114 | #ifdef DEBUG_HWDEVICE |
| 2115 | rct::key a_x; |
| 2116 | rct::key aG_x; |
| 2117 | #endif |
| 2118 | |
| 2119 | send_simple(INS_MLSAG, 0x01); |
| 2120 | |
| 2121 | offset = 0; |
| 2122 | this->receive_secret(a.bytes, offset); |
| 2123 | memmove(aG.bytes, &this->buffer_recv[offset], 32); |
| 2124 | |
| 2125 | #ifdef DEBUG_HWDEVICE |
| 2126 | a_x = hw::ledger::decrypt(a); |
| 2127 | rct::scalarmultBase(aG_x, a_x); |
| 2128 | hw::ledger::check32("mlsag_prepare", "AG", (char*)aG_x.bytes, (char*)aG.bytes); |
| 2129 | #endif |
| 2130 | |
| 2131 | return true; |
| 2132 | } |
| 2133 | |
| 2134 | bool device_ledger::mlsag_hash(const rct::keyV &long_message, rct::key &c) { |
| 2135 | AUTO_LOCK_CMD(); |
| 2136 | size_t cnt; |
| 2137 | |
| 2138 | #ifdef DEBUG_HWDEVICE |
| 2139 | const rct::keyV long_message_x = long_message; |
| 2140 | rct::key c_x; |
| 2141 | this->controle_device->mlsag_hash(long_message_x, c_x); |
| 2142 | #endif |
| 2143 | |
| 2144 | cnt = long_message.size(); |
| 2145 | for (size_t i = 0; i<cnt; i++) { |
| 2146 | int offset = set_command_header(INS_MLSAG, 0x02, i+1); |
| 2147 | //options |
| 2148 | this->buffer_send[offset] = |
| 2149 | (i==(cnt-1))?0x00:0x80; //last |
| 2150 | offset += 1; |
| 2151 | //msg part |
| 2152 | memmove(this->buffer_send+offset, long_message[i].bytes, 32); |
| 2153 | offset += 32; |
| 2154 | |
| 2155 | this->buffer_send[4] = offset-5; |
| 2156 | this->length_send = offset; |
| 2157 | this->exchange(); |
| 2158 | } |
| 2159 | |
| 2160 | memmove(c.bytes, &this->buffer_recv[0], 32); |
| 2161 | |
| 2162 | #ifdef DEBUG_HWDEVICE |
| 2163 | hw::ledger::check32("mlsag_hash", "c", (char*)c_x.bytes, (char*)c.bytes); |
| 2164 | #endif |
| 2165 | |
| 2166 | return true; |
| 2167 | } |
| 2168 | |
| 2169 | 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) { |
| 2170 | AUTO_LOCK_CMD(); |
| 2171 | |
| 2172 | CHECK_AND_ASSERT_THROW_MES(dsRows<=rows, "dsRows greater than rows"); |
| 2173 | CHECK_AND_ASSERT_THROW_MES(xx.size() == rows, "xx size does not match rows"); |
| 2174 | CHECK_AND_ASSERT_THROW_MES(alpha.size() == rows, "alpha size does not match rows"); |
| 2175 | CHECK_AND_ASSERT_THROW_MES(ss.size() == rows, "ss size does not match rows"); |
| 2176 | |
| 2177 | #ifdef DEBUG_HWDEVICE |
| 2178 | const rct::key c_x = c; |
| 2179 | const rct::keyV xx_x = hw::ledger::decrypt(xx); |
| 2180 | const rct::keyV alpha_x = hw::ledger::decrypt(alpha); |
| 2181 | const int rows_x = rows; |
| 2182 | const int dsRows_x = dsRows; |
| 2183 | rct::keyV ss_x(ss.size()); |
| 2184 | this->controle_device->mlsag_sign(c_x, xx_x, alpha_x, rows_x, dsRows_x, ss_x); |
| 2185 | #endif |
| 2186 | |
| 2187 | for (size_t j = 0; j < dsRows; j++) { |
| 2188 | int offset = set_command_header(INS_MLSAG, 0x03, j+1); |
| 2189 | //options |
| 2190 | this->buffer_send[offset] = 0x00; |
| 2191 | if (j==(dsRows-1)) { |
| 2192 | this->buffer_send[offset] |= 0x80; //last |
| 2193 | } |
| 2194 | offset += 1; |
| 2195 | //xx |
| 2196 | this->send_secret(xx[j].bytes, offset); |
| 2197 | //alpa |
| 2198 | this->send_secret(alpha[j].bytes, offset); |
| 2199 | |
| 2200 | this->buffer_send[4] = offset-5; |
| 2201 | this->length_send = offset; |
| 2202 | this->exchange(); |
| 2203 | |
| 2204 | //ss |
| 2205 | memmove(ss[j].bytes, &this->buffer_recv[0], 32); |
| 2206 | } |
| 2207 | |
| 2208 | for (size_t j = dsRows; j < rows; j++) { |
| 2209 | sc_mulsub(ss[j].bytes, c.bytes, xx[j].bytes, alpha[j].bytes); |
| 2210 | } |
| 2211 | |
| 2212 | #ifdef DEBUG_HWDEVICE |
| 2213 | for (size_t j = 0; j < rows; j++) { |
| 2214 | hw::ledger::check32("mlsag_sign", "ss["+std::to_string(j)+"]", (char*)ss_x[j].bytes, (char*)ss[j].bytes); |
| 2215 | } |
| 2216 | #endif |
| 2217 | |
| 2218 | return true; |
| 2219 | } |
| 2220 | |
| 2221 | bool device_ledger::clsag_prepare(const rct::key &p, const rct::key &z, rct::key &I, rct::key &D, const rct::key &H, rct::key &a, rct::key &aG, rct::key &aH) { |
| 2222 | AUTO_LOCK_CMD(); |
| 2223 | #ifdef DEBUG_HWDEVICE |
| 2224 | const rct::key p_x = hw::ledger::decrypt(p); |
| 2225 | const rct::key z_x = z; |
| 2226 | rct::key I_x; |
| 2227 | rct::key D_x; |
| 2228 | const rct::key H_x = H; |
| 2229 | rct::key a_x; |
| 2230 | rct::key aG_x; |
| 2231 | rct::key aH_x; |
| 2232 | this->controle_device->clsag_prepare(p_x, z_x, I_x, D_x, H_x, a_x, aG_x, aH_x); |
| 2233 | #endif |
| 2234 | |
| 2235 | /* |
| 2236 | rct::skpkGen(a,aG); // aG = a*G |
| 2237 | rct::scalarmultKey(aH,H,a); // aH = a*H |
| 2238 | rct::scalarmultKey(I,H,p); // I = p*H |
| 2239 | rct::scalarmultKey(D,H,z); // D = z*H |
| 2240 | */ |
| 2241 | int offset = set_command_header_noopt(INS_CLSAG, 0x01); |
| 2242 | //p |
| 2243 | this->send_secret(p.bytes, offset); |
| 2244 | //z |
| 2245 | memmove(this->buffer_send+offset, z.bytes, 32); |
| 2246 | offset += 32; |
| 2247 | //H |
| 2248 | memmove(this->buffer_send+offset, H.bytes, 32); |
| 2249 | offset += 32; |
| 2250 | |
| 2251 | this->buffer_send[4] = offset-5; |
| 2252 | this->length_send = offset; |
| 2253 | this->exchange(); |
| 2254 | |
| 2255 | offset = 0; |
| 2256 | //a |
| 2257 | this->receive_secret(a.bytes, offset); |
| 2258 | //aG |
| 2259 | memmove(aG.bytes, this->buffer_recv+offset, 32); |
| 2260 | offset +=32; |
| 2261 | //aH |
| 2262 | memmove(aH.bytes, this->buffer_recv+offset, 32); |
| 2263 | offset +=32; |
| 2264 | //I = pH |
| 2265 | memmove(I.bytes, this->buffer_recv+offset, 32); |
| 2266 | offset +=32; |
| 2267 | //D = zH |
| 2268 | memmove(D.bytes, this->buffer_recv+offset, 32); |
| 2269 | offset +=32; |
| 2270 | |
| 2271 | #ifdef DEBUG_HWDEVICE |
| 2272 | hw::ledger::check32("clsag_prepare", "I", (char*)I_x.bytes, (char*)I.bytes); |
| 2273 | hw::ledger::check32("clsag_prepare", "D", (char*)D_x.bytes, (char*)D.bytes); |
| 2274 | hw::ledger::check32("clsag_prepare", "a", (char*)a_x.bytes, (char*)a.bytes); |
| 2275 | hw::ledger::check32("clsag_prepare", "aG", (char*)aG_x.bytes, (char*)aG.bytes); |
| 2276 | hw::ledger::check32("clsag_prepare", "aH", (char*)aH_x.bytes, (char*)aH.bytes); |
| 2277 | #endif |
| 2278 | |
| 2279 | return true; |
| 2280 | } |
| 2281 | |
| 2282 | bool device_ledger::clsag_hash(const rct::keyV &data, rct::key &hash) { |
| 2283 | AUTO_LOCK_CMD(); |
| 2284 | |
| 2285 | #ifdef DEBUG_HWDEVICE |
| 2286 | const rct::keyV data_x = data; |
| 2287 | rct::key hash_x; |
| 2288 | this->controle_device->mlsag_hash(data_x, hash_x); |
| 2289 | #endif |
| 2290 | |
| 2291 | size_t cnt; |
| 2292 | int offset; |
| 2293 | |
| 2294 | cnt = data.size(); |
| 2295 | for (size_t i = 0; i<cnt; i++) { |
| 2296 | offset = set_command_header(INS_CLSAG, 0x02, i+1); |
| 2297 | //options |
| 2298 | this->buffer_send[offset] = (i==(cnt-1))?0x00:0x80; //last |
| 2299 | offset += 1; |
| 2300 | //msg part |
| 2301 | memmove(this->buffer_send+offset, data[i].bytes, 32); |
| 2302 | offset += 32; |
| 2303 | |
| 2304 | this->buffer_send[4] = offset-5; |
| 2305 | this->length_send = offset; |
| 2306 | this->exchange(); |
| 2307 | } |
| 2308 | |
| 2309 | //c/hash |
| 2310 | memmove(hash.bytes, &this->buffer_recv[0], 32); |
| 2311 | |
| 2312 | #ifdef DEBUG_HWDEVICE |
| 2313 | hw::ledger::check32("mlsag_hash", "hash", (char*)hash_x.bytes, (char*)hash.bytes); |
| 2314 | #endif |
| 2315 | return true; |
| 2316 | } |
| 2317 | |
| 2318 | bool device_ledger::clsag_sign(const rct::key &c, const rct::key &a, const rct::key &p, const rct::key &z, const rct::key &mu_P, const rct::key &mu_C, rct::key &s) { |
| 2319 | AUTO_LOCK_CMD(); |
| 2320 | |
| 2321 | #ifdef DEBUG_HWDEVICE |
| 2322 | const rct::key c_x = c; |
| 2323 | const rct::key a_x = hw::ledger::decrypt(a); |
| 2324 | const rct::key p_x = hw::ledger::decrypt(p); |
| 2325 | const rct::key z_x = z; |
| 2326 | const rct::key mu_P_x = mu_P; |
| 2327 | const rct::key mu_C_x = mu_C; |
| 2328 | rct::key s_x; |
| 2329 | this->controle_device->clsag_sign(c_x, a_x, p_x, z_x, mu_P_x, mu_C_x, s_x); |
| 2330 | #endif |
| 2331 | |
| 2332 | /* |
| 2333 | rct::key s0_p_mu_P; |
| 2334 | sc_mul(s0_p_mu_P.bytes,mu_P.bytes,p.bytes); |
| 2335 | rct::key s0_add_z_mu_C; |
| 2336 | sc_muladd(s0_add_z_mu_C.bytes,mu_C.bytes,z.bytes,s0_p_mu_P.bytes); |
| 2337 | sc_mulsub(s.bytes,c.bytes,s0_add_z_mu_C.bytes,a.bytes); |
| 2338 | */ |
| 2339 | |
| 2340 | int offset = set_command_header_noopt(INS_CLSAG, 0x03); |
| 2341 | |
| 2342 | //c |
| 2343 | //discard, unse internal one |
| 2344 | //a |
| 2345 | this->send_secret(a.bytes, offset); |
| 2346 | //p |
| 2347 | this->send_secret(p.bytes, offset); |
| 2348 | //z |
| 2349 | memmove(this->buffer_send+offset, z.bytes, 32); |
| 2350 | offset += 32; |
| 2351 | //mu_P |
| 2352 | memmove(this->buffer_send+offset, mu_P.bytes, 32); |
| 2353 | offset += 32; |
| 2354 | //mu_C |
| 2355 | memmove(this->buffer_send+offset, mu_C.bytes, 32); |
| 2356 | offset += 32; |
| 2357 | |
| 2358 | this->buffer_send[4] = offset-5; |
| 2359 | this->length_send = offset; |
| 2360 | this->exchange(); |
| 2361 | |
| 2362 | offset = 0; |
| 2363 | //s |
| 2364 | memmove(s.bytes, this->buffer_recv+offset, 32); |
| 2365 | |
| 2366 | #ifdef DEBUG_HWDEVICE |
| 2367 | hw::ledger::check32("clsag_sign", "s", (char*)s_x.bytes, (char*)s.bytes); |
| 2368 | #endif |
| 2369 | |
| 2370 | return true; |
| 2371 | } |
| 2372 | |
| 2373 | |
| 2374 | bool device_ledger::close_tx() { |
| 2375 | AUTO_LOCK_CMD(); |
| 2376 | if (!hw_device.disconnected) { |
| 2377 | send_simple(INS_CLOSE_TX); // This can throw an exception if device is disconnected -> recursive mutex never gets unlocked |
| 2378 | } |
| 2379 | key_map.clear(); |
| 2380 | hmac_map.clear(); |
| 2381 | this->tx_in_progress = false; |
| 2382 | this->unlock(); |
| 2383 | return true; |
| 2384 | } |
| 2385 | |
| 2386 | /* ---------------------------------------------------------- */ |
| 2387 | |
| 2388 | static device_ledger *legder_device = NULL; |
| 2389 | void register_all(std::map<std::string, std::unique_ptr<device>> ®istry) { |
| 2390 | if (!legder_device) { |
| 2391 | legder_device = new device_ledger(); |
| 2392 | legder_device->set_name("Ledger"); |
| 2393 | } |
| 2394 | registry.insert(std::make_pair("Ledger", std::unique_ptr<device>(legder_device))); |
| 2395 | } |
| 2396 | |
| 2397 | #else //WITH_DEVICE_LEDGER |
| 2398 | |
| 2399 | void register_all(std::map<std::string, std::unique_ptr<device>> ®istry) { |
| 2400 | } |
| 2401 | |
| 2402 | #endif //WITH_DEVICE_LEDGER |
| 2403 | |
| 2404 | } |
| 2405 | } |
| 2406 |