Monero
device_ledger.hpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2020, 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 "log.hpp"
37 #include "device_io_hid.hpp"
38 #include <boost/thread/mutex.hpp>
39 #include <boost/thread/recursive_mutex.hpp>
40 
41 namespace hw {
42 
43  namespace ledger {
44 
45  /* Minimal supported version */
46  #define MINIMAL_APP_VERSION_MAJOR 1
47  #define MINIMAL_APP_VERSION_MINOR 6
48  #define MINIMAL_APP_VERSION_MICRO 0
49 
50  #define VERSION(M,m,u) ((M)<<16|(m)<<8|(u))
51  #define VERSION_MAJOR(v) (((v)>>16)&0xFF)
52  #define VERSION_MINOR(v) (((v)>>8)&0xFF)
53  #define VERSION_MICRO(v) (((v)>>0)&0xFF)
54 
55  #define MINIMAL_APP_VERSION VERSION(MINIMAL_APP_VERSION_MAJOR, MINIMAL_APP_VERSION_MINOR, MINIMAL_APP_VERSION_MICRO)
56 
57  void register_all(std::map<std::string, std::unique_ptr<device>> &registry);
58 
59  #ifdef WITH_DEVICE_LEDGER
60 
61  // Origin: https://github.com/LedgerHQ/ledger-app-monero/blob/master/src/monero_types.h
62  #define SW_BYTES_REMAINING_00 0x6100
63  #define SW_WARNING_STATE_UNCHANGED 0x6200
64  #define SW_STATE_TERMINATED 0x6285
65  #define SW_MORE_DATA_AVAILABLE 0x6310
66  #define SW_WRONG_LENGTH 0x6700
67  #define SW_LOGICAL_CHANNEL_NOT_SUPPORTED 0x6881
68  #define SW_SECURE_MESSAGING_NOT_SUPPORTED 0x6882
69  #define SW_LAST_COMMAND_EXPECTED 0x6883
70  #define SW_COMMAND_CHAINING_NOT_SUPPORTED 0x6884
71  #define SW_SECURITY_LOAD_KEY 0x6900
72  #define SW_SECURITY_COMMITMENT_CONTROL 0x6911
73  #define SW_SECURITY_AMOUNT_CHAIN_CONTROL 0x6912
74  #define SW_SECURITY_COMMITMENT_CHAIN_CONTROL 0x6913
75  #define SW_SECURITY_OUTKEYS_CHAIN_CONTROL 0x6914
76  #define SW_SECURITY_MAXOUTPUT_REACHED 0x6915
77  #define SW_SECURITY_TRUSTED_INPUT 0x6916
78  #define SW_CLIENT_NOT_SUPPORTED 0x6930
79  #define SW_SECURITY_STATUS_NOT_SATISFIED 0x6982
80  #define SW_FILE_INVALID 0x6983
81  #define SW_PIN_BLOCKED 0x6983
82  #define SW_DATA_INVALID 0x6984
83  #define SW_CONDITIONS_NOT_SATISFIED 0x6985
84  #define SW_COMMAND_NOT_ALLOWED 0x6986
85  #define SW_APPLET_SELECT_FAILED 0x6999
86  #define SW_WRONG_DATA 0x6a80
87  #define SW_FUNC_NOT_SUPPORTED 0x6a81
88  #define SW_FILE_NOT_FOUND 0x6a82
89  #define SW_RECORD_NOT_FOUND 0x6a83
90  #define SW_FILE_FULL 0x6a84
91  #define SW_INCORRECT_P1P2 0x6a86
92  #define SW_REFERENCED_DATA_NOT_FOUND 0x6a88
93  #define SW_WRONG_P1P2 0x6b00
94  #define SW_CORRECT_LENGTH_00 0x6c00
95  #define SW_INS_NOT_SUPPORTED 0x6d00
96  #define SW_CLA_NOT_SUPPORTED 0x6e00
97  #define SW_UNKNOWN 0x6f00
98  #define SW_OK 0x9000
99  #define SW_ALGORITHM_UNSUPPORTED 0x9484
100 
101  namespace {
102  bool apdu_verbose =true;
103  }
104 
105  void set_apdu_verbose(bool verbose);
106 
107  class ABPkeys {
108  public:
109  rct::key Aout;
110  rct::key Bout;
111  bool is_subaddress;
112  bool is_change_address;
113  bool additional_key ;
114  size_t index;
115  rct::key Pout;
116  rct::key AKout;
117  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);
118  ABPkeys(const ABPkeys& keys) ;
119  ABPkeys() {index=0;is_subaddress=false;is_change_address=false;additional_key=false;}
120  ABPkeys &operator=(const ABPkeys &keys);
121  };
122 
123  class Keymap {
124  public:
125  std::vector<ABPkeys> ABP;
126 
127  bool find(const rct::key& P, ABPkeys& keys) const;
128  void add(const ABPkeys& keys);
129  void clear();
130  void log();
131  };
132 
133  class SecHMAC {
134  public:
135  uint32_t sec[32];
136  uint32_t hmac[32];
137 
138  SecHMAC(const uint8_t s[32], const uint8_t m[32]);
139 
140  };
141 
142  class HMACmap {
143  public:
144  std::vector<SecHMAC> hmacs;
145 
146  void find_mac(const uint8_t sec[32], uint8_t hmac[32]) ;
147  void add_mac(const uint8_t sec[32], const uint8_t hmac[32]) ;
148  void clear() ;
149  };
150 
151 
152  #define BUFFER_SEND_SIZE 262
153  #define BUFFER_RECV_SIZE 262
154 
155  class device_ledger : public hw::device {
156  private:
157  // Locker for concurrent access
158  mutable boost::recursive_mutex device_locker;
159  mutable boost::mutex command_locker;
160 
161  //IO
162  hw::io::device_io_hid hw_device;
163  unsigned int length_send;
164  unsigned char buffer_send[BUFFER_SEND_SIZE];
165  unsigned int length_recv;
166  unsigned char buffer_recv[BUFFER_RECV_SIZE];
167  unsigned int sw;
168  unsigned int id;
169  void logCMD(void);
170  void logRESP(void);
171  unsigned int exchange(unsigned int ok=SW_OK, unsigned int mask=0xFFFF);
172  unsigned int exchange_wait_on_input(unsigned int ok=SW_OK, unsigned int mask=0xFFFF);
173  void reset_buffer(void);
174  int set_command_header(unsigned char ins, unsigned char p1 = 0x00, unsigned char p2 = 0x00);
175  int set_command_header_noopt(unsigned char ins, unsigned char p1 = 0x00, unsigned char p2 = 0x00);
176  void send_simple(unsigned char ins, unsigned char p1 = 0x00);
177  void send_secret(const unsigned char sec[32], int &offset);
178  void receive_secret(unsigned char sec[32], int &offset);
179 
180  // hw running mode
181  device_mode mode;
182  bool tx_in_progress;
183 
184  // map public destination key to ephemeral destination key
185  Keymap key_map;
186  bool add_output_key_mapping(const crypto::public_key &Aout, const crypto::public_key &Bout, const bool is_subaddress, const bool is_change,
187  const bool need_additional, const size_t real_output_index,
188  const rct::key &amount_key, const crypto::public_key &out_eph_public_key);
189  //hmac for some encrypted value
190  HMACmap hmac_map;
191 
192  // To speed up blockchain parsing the view key maybe handle here.
193  crypto::secret_key viewkey;
194  bool has_view_key;
195 
196  //extra debug
197  #ifdef DEBUG_HWDEVICE
198  device *controle_device;
199  #endif
200 
201  public:
202  device_ledger();
203  ~device_ledger();
204 
205  device_ledger(const device_ledger &device) = delete ;
206  device_ledger& operator=(const device_ledger &device) = delete;
207 
208  explicit operator bool() const override {return this->connected(); }
209 
210  bool reset(void);
211 
212  /* ======================================================================= */
213  /* SETUP/TEARDOWN */
214  /* ======================================================================= */
215  bool set_name(const std::string &name) override;
216 
217  const std::string get_name() const override;
218  bool init(void) override;
219  bool release() override;
220  bool connect(void) override;
221  bool disconnect() override;
222  bool connected(void) const;
223 
224  bool set_mode(device_mode mode) override;
225 
226  device_type get_type() const override {return device_type::LEDGER;};
227  device_protocol_t device_protocol() const override { return PROTOCOL_PROXY; };
228 
229  /* ======================================================================= */
230  /* LOCKER */
231  /* ======================================================================= */
232  void lock(void) override;
233  void unlock(void) override;
234  bool try_lock(void) override;
235 
236  /* ======================================================================= */
237  /* WALLET & ADDRESS */
238  /* ======================================================================= */
239  bool get_public_address(cryptonote::account_public_address &pubkey) override;
240  bool get_secret_keys(crypto::secret_key &viewkey , crypto::secret_key &spendkey) override;
241  bool generate_chacha_key(const cryptonote::account_keys &keys, crypto::chacha_key &key, uint64_t kdf_rounds) override;
242  void display_address(const cryptonote::subaddress_index& index, const boost::optional<crypto::hash8> &payment_id) override;
243 
244  /* ======================================================================= */
245  /* SUB ADDRESS */
246  /* ======================================================================= */
247  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;
248  crypto::public_key get_subaddress_spend_public_key(const cryptonote::account_keys& keys, const cryptonote::subaddress_index& index) override;
249  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;
250  cryptonote::account_public_address get_subaddress(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) override;
251  crypto::secret_key get_subaddress_secret_key(const crypto::secret_key &sec, const cryptonote::subaddress_index &index) override;
252 
253  /* ======================================================================= */
254  /* DERIVATION & KEY */
255  /* ======================================================================= */
256  bool verify_keys(const crypto::secret_key &secret_key, const crypto::public_key &public_key) override;
257  bool scalarmultKey(rct::key & aP, const rct::key &P, const rct::key &a) override;
258  bool scalarmultBase(rct::key &aG, const rct::key &a) override;
259  bool sc_secret_add(crypto::secret_key &r, const crypto::secret_key &a, const crypto::secret_key &b) override;
260  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;
261  bool generate_key_derivation(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_derivation &derivation) override;
262  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;
263  bool derivation_to_scalar(const crypto::key_derivation &derivation, const size_t output_index, crypto::ec_scalar &res) override;
264  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;
265  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;
266  bool secret_key_to_public_key(const crypto::secret_key &sec, crypto::public_key &pub) override;
267  bool generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image) override;
268 
269  /* ======================================================================= */
270  /* TRANSACTION */
271  /* ======================================================================= */
272  void generate_tx_proof(const crypto::hash &prefix_hash,
273  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,
274  crypto::signature &sig) override;
275 
276  bool open_tx(crypto::secret_key &tx_key) override;
277 
279 
280  bool encrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key) override;
281 
282  rct::key genCommitmentMask(const rct::key &amount_key) override;
283 
284  bool ecdhEncode(rct::ecdhTuple & unmasked, const rct::key & sharedSec, bool short_format) override;
285  bool ecdhDecode(rct::ecdhTuple & masked, const rct::key & sharedSec, bool short_format) override;
286 
287  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,
288  const cryptonote::tx_destination_entry &dst_entr, const boost::optional<cryptonote::account_public_address> &change_addr, const size_t output_index,
289  const bool &need_additional_txkeys, const std::vector<crypto::secret_key> &additional_tx_keys,
290  std::vector<crypto::public_key> &additional_tx_public_keys,
291  std::vector<rct::key> &amount_keys,
292  crypto::public_key &out_eph_public_key) override;
293 
294  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;
295  bool mlsag_prepare(const rct::key &H, const rct::key &xx, rct::key &a, rct::key &aG, rct::key &aHP, rct::key &rvII) override;
296  bool mlsag_prepare(rct::key &a, rct::key &aG) override;
297  bool mlsag_hash(const rct::keyV &long_message, rct::key &c) override;
298  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;
299 
300  bool 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) override;
301  bool clsag_hash(const rct::keyV &data, rct::key &hash) override;
302  bool 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) override;
303 
304 
305  bool close_tx(void) override;
306 
307  };
308 
309 
310  #ifdef DEBUG_HWDEVICE
311  extern crypto::secret_key dbg_viewkey;
312  extern crypto::secret_key dbg_spendkey;
313  #endif
314 
315  #endif //WITH_DEVICE_LEDGER
316  }
317 
318 }
319 
#define s(x, c)
Definition: aesb.c:47
static void init(std::string cache_filename)
Definition: blockchain_blackball.cpp:221
Definition: cryptonote_basic.h:160
Definition: device.hpp:87
string a
Definition: MakeCryptoOps.py:15
const char * name
Definition: simplewallet.cpp:417
list B
Definition: base.py:26
def I
Definition: base.py:15
int b
Definition: base.py:1
void generate_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const boost::optional< public_key > &B, const public_key &D, const secret_key &r, signature &sig)
Definition: crypto.h:250
POD_CLASS signature
Definition: crypto.h:93
epee::mlocked< tools::scrubbed< ec_scalar > > secret_key
Definition: crypto.h:67
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:233
POD_CLASS hash8
Definition: hash.h:51
POD_CLASS key_derivation
Definition: crypto.h:83
secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key &recovery_key=secret_key(), bool recover=false)
Definition: crypto.h:197
void derive_secret_key(const key_derivation &derivation, std::size_t output_index, const secret_key &base, secret_key &derived_key)
Definition: crypto.h:229
bool generate_key_derivation(const public_key &key1, const secret_key &key2, key_derivation &derivation)
Definition: crypto.h:219
POD_CLASS public_key
Definition: crypto.h:61
bool derive_public_key(const key_derivation &derivation, std::size_t output_index, const public_key &base, public_key &derived_key)
Definition: crypto.h:222
POD_CLASS key_image
Definition: crypto.h:87
POD_CLASS ec_scalar
Definition: crypto.h:59
bool secret_key_to_public_key(const secret_key &sec, public_key &pub)
Definition: crypto.h:209
int rows
Definition: crypto.h:71
POD_CLASS hash
Definition: hash.h:48
void generate_key_image(const public_key &pub, const secret_key &sec, key_image &image)
Definition: crypto.h:266
void derivation_to_scalar(const key_derivation &derivation, size_t output_index, ec_scalar &res)
Definition: crypto.h:226
void get_transaction_prefix_hash(const transaction_prefix &tx, crypto::hash &h, hw::device &hwdev)
Definition: cryptonote_format_utils.cpp:129
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)
void register_all(std::map< std::string, std::unique_ptr< device >> &registry)
Definition: device_ledger.cpp:2301
Definition: device.cpp:38
void scalarmultBase(key &aG, const key &a)
Definition: rctOps.cpp:350
static const key H
Definition: rctTypes.h:556
void ecdhEncode(ecdhTuple &unmasked, const key &sharedSec, bool v2)
Definition: rctOps.cpp:698
std::vector< key > keyV
Definition: rctTypes.h:89
void scalarmultKey(key &aP, const key &P, const key &a)
Definition: rctOps.cpp:368
std::vector< ctkey > ctkeyV
Definition: rctTypes.h:101
void ecdhDecode(ecdhTuple &masked, const key &sharedSec, bool v2)
Definition: rctOps.cpp:713
static void add(ge_p3 &p3, const ge_cached &other)
Definition: multiexp.cc:143
key genCommitmentMask(const key &sk)
Definition: rctOps.cpp:688
cryptonote::simple_wallet sw
Definition: simplewallet.cpp:94
int bool
Definition: stdbool.h:35
Definition: account.h:41
Definition: cryptonote_basic.h:501
Definition: subaddress_index.h:39
Definition: cryptonote_tx_utils.h:75
Definition: rctTypes.h:132
Definition: rctTypes.h:79