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  size_t index;
60  rct::key Pout;
61  rct::key AKout;
62  ABPkeys(const rct::key& A, const rct::key& B, const bool is_subaddr, size_t index, const rct::key& P,const rct::key& AK);
63  ABPkeys(const ABPkeys& keys) ;
64  ABPkeys() {index=0;is_subaddress=false;}
65  };
66 
67  class Keymap {
68  public:
69  std::vector<ABPkeys> ABP;
70 
71  bool find(const rct::key& P, ABPkeys& keys) const;
72  void add(const ABPkeys& keys);
73  void clear();
74  void log();
75  };
76 
77  #define BUFFER_SEND_SIZE 262
78  #define BUFFER_RECV_SIZE 262
79 
80  class device_ledger : public hw::device {
81  private:
82  // Locker for concurrent access
83  mutable boost::recursive_mutex device_locker;
84  mutable boost::mutex command_locker;
85 
86  //IO
87  hw::io::device_io_hid hw_device;
88  std::string full_name;
89  unsigned int length_send;
90  unsigned char buffer_send[BUFFER_SEND_SIZE];
91  unsigned int length_recv;
92  unsigned char buffer_recv[BUFFER_RECV_SIZE];
93  unsigned int sw;
94  unsigned int id;
95  void logCMD(void);
96  void logRESP(void);
97  unsigned int exchange(unsigned int ok=0x9000, unsigned int mask=0xFFFF);
98  void reset_buffer(void);
99  int set_command_header(unsigned char ins, unsigned char p1 = 0x00, unsigned char p2 = 0x00);
100  int set_command_header_noopt(unsigned char ins, unsigned char p1 = 0x00, unsigned char p2 = 0x00);
101  void send_simple(unsigned char ins, unsigned char p1 = 0x00);
102 
103 
104  // hw running mode
105  device_mode mode;
106  // map public destination key to ephemeral destination key
107  Keymap key_map;
108 
109  // To speed up blockchain parsing the view key maybe handle here.
110  crypto::secret_key viewkey;
111  bool has_view_key;
112 
113  //extra debug
114  #ifdef DEBUG_HWDEVICE
115  device *controle_device;
116  #endif
117 
118  public:
119  device_ledger();
120  ~device_ledger();
121 
122  device_ledger(const device_ledger &device) = delete ;
123  device_ledger& operator=(const device_ledger &device) = delete;
124 
125  explicit operator bool() const override {return this->connected(); }
126 
127  bool reset(void);
128 
129  /* ======================================================================= */
130  /* SETUP/TEARDOWN */
131  /* ======================================================================= */
132  bool set_name(const std::string &name) override;
133 
134  const std::string get_name() const override;
135  bool init(void) override;
136  bool release() override;
137  bool connect(void) override;
138  bool disconnect() override;
139  bool connected(void) const;
140 
141  bool set_mode(device_mode mode) override;
142 
143  device_type get_type() const override {return device_type::LEDGER;};
144 
145  /* ======================================================================= */
146  /* LOCKER */
147  /* ======================================================================= */
148  void lock(void) override;
149  void unlock(void) override;
150  bool try_lock(void) override;
151 
152  /* ======================================================================= */
153  /* WALLET & ADDRESS */
154  /* ======================================================================= */
155  bool get_public_address(cryptonote::account_public_address &pubkey) override;
156  bool get_secret_keys(crypto::secret_key &viewkey , crypto::secret_key &spendkey) override;
157  bool generate_chacha_key(const cryptonote::account_keys &keys, crypto::chacha_key &key, uint64_t kdf_rounds) override;
158 
159 
160  /* ======================================================================= */
161  /* SUB ADDRESS */
162  /* ======================================================================= */
163  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;
164  crypto::public_key get_subaddress_spend_public_key(const cryptonote::account_keys& keys, const cryptonote::subaddress_index& index) override;
165  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;
166  cryptonote::account_public_address get_subaddress(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) override;
167  crypto::secret_key get_subaddress_secret_key(const crypto::secret_key &sec, const cryptonote::subaddress_index &index) override;
168 
169  /* ======================================================================= */
170  /* DERIVATION & KEY */
171  /* ======================================================================= */
172  bool verify_keys(const crypto::secret_key &secret_key, const crypto::public_key &public_key) override;
173  bool scalarmultKey(rct::key & aP, const rct::key &P, const rct::key &a) override;
174  bool scalarmultBase(rct::key &aG, const rct::key &a) override;
175  bool sc_secret_add(crypto::secret_key &r, const crypto::secret_key &a, const crypto::secret_key &b) override;
176  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;
177  bool generate_key_derivation(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_derivation &derivation) override;
178  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;
179  bool derivation_to_scalar(const crypto::key_derivation &derivation, const size_t output_index, crypto::ec_scalar &res) override;
180  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;
181  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;
182  bool secret_key_to_public_key(const crypto::secret_key &sec, crypto::public_key &pub) override;
183  bool generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image) override;
184 
185  /* ======================================================================= */
186  /* TRANSACTION */
187  /* ======================================================================= */
188 
189  bool open_tx(crypto::secret_key &tx_key) override;
190 
191  bool encrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key) override;
192 
193  bool ecdhEncode(rct::ecdhTuple & unmasked, const rct::key & sharedSec) override;
194  bool ecdhDecode(rct::ecdhTuple & masked, const rct::key & sharedSec) override;
195 
196  bool add_output_key_mapping(const crypto::public_key &Aout, const crypto::public_key &Bout, const bool is_subaddress, const size_t real_output_index,
197  const rct::key &amount_key, const crypto::public_key &out_eph_public_key) override;
198 
199 
200  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;
201  bool mlsag_prepare(const rct::key &H, const rct::key &xx, rct::key &a, rct::key &aG, rct::key &aHP, rct::key &rvII) override;
202  bool mlsag_prepare(rct::key &a, rct::key &aG) override;
203  bool mlsag_hash(const rct::keyV &long_message, rct::key &c) override;
204  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;
205 
206  bool close_tx(void) override;
207 
208  };
209 
210 
211  #ifdef DEBUG_HWDEVICE
212  extern crypto::secret_key dbg_viewkey;
213  extern crypto::secret_key dbg_spendkey;
214  #endif
215 
216  #endif //WITH_DEVICE_LEDGER
217  }
218 
219 }
220 
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:431
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
void ecdhEncode(ecdhTuple &unmasked, const key &sharedSec)
Definition: rctOps.cpp:490
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:1565
static rct::key scalarmultKey(const ge_p3 &P, const rct::key &a)
Definition: bulletproofs.cc:130
Definition: rctTypes.h:125
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
static std::unique_ptr< device_registry > registry
Definition: device.cpp:44
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:82
Definition: account.h:40
POD_CLASS public_key
Definition: crypto.h:63
void scalarmultBase(key &aG, const key &a)
Definition: rctOps.cpp:167
Definition: cryptonote_basic.h:400
POD_CLASS hash8
Definition: hash.h:52
void ecdhDecode(ecdhTuple &masked, const key &sharedSec)
Definition: rctOps.cpp:497
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
bool secret_key_to_public_key(const secret_key &sec, public_key &pub)
Definition: crypto.h:182
const char * name
Definition: simplewallet.cpp:319
int rows
Definition: crypto.h:73