Monero
device_ledger.hpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2018, 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 
31 #pragma once
32 
33 #include <cstddef>
34 #include <string>
35 #include "device.hpp"
36 #include "device_io_hid.hpp"
37 #include <boost/thread/mutex.hpp>
38 #include <boost/thread/recursive_mutex.hpp>
39 
40 namespace hw {
41 
42  namespace ledger {
43 
44  void register_all(std::map<std::string, std::unique_ptr<device>> &registry);
45 
46  #ifdef WITH_DEVICE_LEDGER
47 
48  namespace {
49  bool apdu_verbose =true;
50  }
51 
52  void set_apdu_verbose(bool verbose);
53 
54  class ABPkeys {
55  public:
56  rct::key Aout;
57  rct::key Bout;
58  bool is_subaddress;
59  bool is_change_address;
60  bool additional_key ;
61  size_t index;
62  rct::key Pout;
63  rct::key AKout;
64  ABPkeys(const rct::key& A, const rct::key& B, const bool is_subaddr, bool is_subaddress, bool is_change_address, size_t index, const rct::key& P,const rct::key& AK);
65  ABPkeys(const ABPkeys& keys) ;
66  ABPkeys() {index=0;is_subaddress=false;is_subaddress=false;is_change_address=false;}
67  };
68 
69  class Keymap {
70  public:
71  std::vector<ABPkeys> ABP;
72 
73  bool find(const rct::key& P, ABPkeys& keys) const;
74  void add(const ABPkeys& keys);
75  void clear();
76  void log();
77  };
78 
79  #define BUFFER_SEND_SIZE 262
80  #define BUFFER_RECV_SIZE 262
81 
82  class device_ledger : public hw::device {
83  private:
84  // Locker for concurrent access
85  mutable boost::recursive_mutex device_locker;
86  mutable boost::mutex command_locker;
87 
88  //IO
89  hw::io::device_io_hid hw_device;
90  std::string full_name;
91  unsigned int length_send;
92  unsigned char buffer_send[BUFFER_SEND_SIZE];
93  unsigned int length_recv;
94  unsigned char buffer_recv[BUFFER_RECV_SIZE];
95  unsigned int sw;
96  unsigned int id;
97  void logCMD(void);
98  void logRESP(void);
99  unsigned int exchange(unsigned int ok=0x9000, unsigned int mask=0xFFFF);
100  void reset_buffer(void);
101  int set_command_header(unsigned char ins, unsigned char p1 = 0x00, unsigned char p2 = 0x00);
102  int set_command_header_noopt(unsigned char ins, unsigned char p1 = 0x00, unsigned char p2 = 0x00);
103  void send_simple(unsigned char ins, unsigned char p1 = 0x00);
104 
105 
106  // hw running mode
107  device_mode mode;
108  // map public destination key to ephemeral destination key
109  Keymap key_map;
110  bool add_output_key_mapping(const crypto::public_key &Aout, const crypto::public_key &Bout, const bool is_subaddress, const bool is_change,
111  const bool need_additional, const size_t real_output_index,
112  const rct::key &amount_key, const crypto::public_key &out_eph_public_key);
113  // To speed up blockchain parsing the view key maybe handle here.
114  crypto::secret_key viewkey;
115  bool has_view_key;
116 
117  //extra debug
118  #ifdef DEBUG_HWDEVICE
119  device *controle_device;
120  #endif
121 
122  public:
123  device_ledger();
124  ~device_ledger();
125 
126  device_ledger(const device_ledger &device) = delete ;
127  device_ledger& operator=(const device_ledger &device) = delete;
128 
129  explicit operator bool() const override {return this->connected(); }
130 
131  bool reset(void);
132 
133  /* ======================================================================= */
134  /* SETUP/TEARDOWN */
135  /* ======================================================================= */
136  bool set_name(const std::string &name) override;
137 
138  const std::string get_name() const override;
139  bool init(void) override;
140  bool release() override;
141  bool connect(void) override;
142  bool disconnect() override;
143  bool connected(void) const;
144 
145  bool set_mode(device_mode mode) override;
146 
147  device_type get_type() const override {return device_type::LEDGER;};
148 
149  /* ======================================================================= */
150  /* LOCKER */
151  /* ======================================================================= */
152  void lock(void) override;
153  void unlock(void) override;
154  bool try_lock(void) override;
155 
156  /* ======================================================================= */
157  /* WALLET & ADDRESS */
158  /* ======================================================================= */
159  bool get_public_address(cryptonote::account_public_address &pubkey) override;
160  bool get_secret_keys(crypto::secret_key &viewkey , crypto::secret_key &spendkey) override;
161  bool generate_chacha_key(const cryptonote::account_keys &keys, crypto::chacha_key &key, uint64_t kdf_rounds) override;
162 
163 
164  /* ======================================================================= */
165  /* SUB ADDRESS */
166  /* ======================================================================= */
167  bool 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) override;
168  crypto::public_key get_subaddress_spend_public_key(const cryptonote::account_keys& keys, const cryptonote::subaddress_index& index) override;
169  std::vector<crypto::public_key> get_subaddress_spend_public_keys(const cryptonote::account_keys &keys, uint32_t account, uint32_t begin, uint32_t end) override;
170  cryptonote::account_public_address get_subaddress(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) override;
171  crypto::secret_key get_subaddress_secret_key(const crypto::secret_key &sec, const cryptonote::subaddress_index &index) override;
172 
173  /* ======================================================================= */
174  /* DERIVATION & KEY */
175  /* ======================================================================= */
176  bool verify_keys(const crypto::secret_key &secret_key, const crypto::public_key &public_key) override;
177  bool scalarmultKey(rct::key & aP, const rct::key &P, const rct::key &a) override;
178  bool scalarmultBase(rct::key &aG, const rct::key &a) override;
179  bool sc_secret_add(crypto::secret_key &r, const crypto::secret_key &a, const crypto::secret_key &b) override;
180  crypto::secret_key generate_keys(crypto::public_key &pub, crypto::secret_key &sec, const crypto::secret_key& recovery_key = crypto::secret_key(), bool recover = false) override;
181  bool generate_key_derivation(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_derivation &derivation) override;
182  bool 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) override;
183  bool derivation_to_scalar(const crypto::key_derivation &derivation, const size_t output_index, crypto::ec_scalar &res) override;
184  bool derive_secret_key(const crypto::key_derivation &derivation, const std::size_t output_index, const crypto::secret_key &sec, crypto::secret_key &derived_sec) override;
185  bool derive_public_key(const crypto::key_derivation &derivation, const std::size_t output_index, const crypto::public_key &pub, crypto::public_key &derived_pub) override;
186  bool secret_key_to_public_key(const crypto::secret_key &sec, crypto::public_key &pub) override;
187  bool generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image) override;
188 
189  /* ======================================================================= */
190  /* TRANSACTION */
191  /* ======================================================================= */
192 
193  bool open_tx(crypto::secret_key &tx_key) override;
194 
195  bool encrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key) override;
196 
197  rct::key genCommitmentMask(const rct::key &amount_key) override;
198 
199  bool ecdhEncode(rct::ecdhTuple & unmasked, const rct::key & sharedSec, bool short_format) override;
200  bool ecdhDecode(rct::ecdhTuple & masked, const rct::key & sharedSec, bool short_format) override;
201 
202  bool 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,
203  const cryptonote::tx_destination_entry &dst_entr, const boost::optional<cryptonote::account_public_address> &change_addr, const size_t output_index,
204  const bool &need_additional_txkeys, const std::vector<crypto::secret_key> &additional_tx_keys,
205  std::vector<crypto::public_key> &additional_tx_public_keys,
206  std::vector<rct::key> &amount_keys,
207  crypto::public_key &out_eph_public_key) override;
208 
209  bool mlsag_prehash(const std::string &blob, size_t inputs_size, size_t outputs_size, const rct::keyV &hashes, const rct::ctkeyV &outPk, rct::key &prehash) override;
210  bool mlsag_prepare(const rct::key &H, const rct::key &xx, rct::key &a, rct::key &aG, rct::key &aHP, rct::key &rvII) override;
211  bool mlsag_prepare(rct::key &a, rct::key &aG) override;
212  bool mlsag_hash(const rct::keyV &long_message, rct::key &c) override;
213  bool 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) override;
214 
215  bool close_tx(void) override;
216 
217  };
218 
219 
220  #ifdef DEBUG_HWDEVICE
221  extern crypto::secret_key dbg_viewkey;
222  extern crypto::secret_key dbg_spendkey;
223  #endif
224 
225  #endif //WITH_DEVICE_LEDGER
226  }
227 
228 }
229 
void derivation_to_scalar(const key_derivation &derivation, size_t output_index, ec_scalar &res)
Definition: crypto.h:199
static const key H
Definition: rctTypes.h:451
void derive_secret_key(const key_derivation &derivation, std::size_t output_index, const secret_key &base, secret_key &derived_key)
Definition: crypto.h:202
static void init(std::string cache_filename)
Definition: blockchain_blackball.cpp:223
list B
Definition: base.py:26
POD_CLASS key_derivation
Definition: crypto.h:85
epee::mlocked< tools::scrubbed< ec_scalar > > secret_key
Definition: crypto.h:69
void register_all(std::map< std::string, std::unique_ptr< device >> &registry)
Definition: device_ledger.cpp:1761
static rct::key scalarmultKey(const ge_p3 &P, const rct::key &a)
Definition: bulletproofs.cc:130
Definition: rctTypes.h:125
void ecdhEncode(ecdhTuple &unmasked, const key &sharedSec, bool v2)
Definition: rctOps.cpp:514
key genCommitmentMask(const key &sk)
Definition: rctOps.cpp:504
bool generate_key_derivation(const public_key &key1, const secret_key &key2, key_derivation &derivation)
Definition: crypto.h:192
std::vector< key > keyV
Definition: rctTypes.h:88
void generate_key_image(const public_key &pub, const secret_key &sec, key_image &image)
Definition: crypto.h:236
static void add(ge_p3 &p3, const ge_cached &other)
Definition: multiexp.cc:124
cryptonote::simple_wallet sw
Definition: simplewallet.cpp:82
POD_CLASS ec_scalar
Definition: crypto.h:61
bool derive_subaddress_public_key(const public_key &out_key, const key_derivation &derivation, std::size_t output_index, public_key &result)
Definition: crypto.h:206
std::vector< ctkey > ctkeyV
Definition: rctTypes.h:100
Definition: rctTypes.h:78
int b
Definition: base.py:1
Definition: subaddress_index.h:38
secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key &recovery_key=secret_key(), bool recover=false)
Definition: crypto.h:170
Definition: device.cpp:38
Definition: device.hpp:83
Definition: account.h:40
POD_CLASS public_key
Definition: crypto.h:63
void ecdhDecode(ecdhTuple &masked, const key &sharedSec, bool v2)
Definition: rctOps.cpp:529
void scalarmultBase(key &aG, const key &a)
Definition: rctOps.cpp:167
Definition: cryptonote_basic.h:400
POD_CLASS hash8
Definition: hash.h:52
POD_CLASS key_image
Definition: crypto.h:89
bool derive_public_key(const key_derivation &derivation, std::size_t output_index, const public_key &base, public_key &derived_key)
Definition: crypto.h:195
string a
Definition: MakeCryptoOps.py:15
int bool
Definition: stdbool.h:35
Definition: cryptonote_tx_utils.h:74
bool 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, const cryptonote::tx_destination_entry &dst_entr, const boost::optional< cryptonote::account_public_address > &change_addr, const size_t output_index, const bool &need_additional_txkeys, const std::vector< crypto::secret_key > &additional_tx_keys, std::vector< crypto::public_key > &additional_tx_public_keys, std::vector< rct::key > &amount_keys, crypto::public_key &out_eph_public_key)
bool secret_key_to_public_key(const secret_key &sec, public_key &pub)
Definition: crypto.h:182
const char * name
Definition: simplewallet.cpp:284
int rows
Definition: crypto.h:73