Debian Codegraph @

src/device/device_ledger.cpp

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