Debian Codegraph @

monero/src/device/device_ledger.hpp

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
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 <mutex>
39
40 namespace hw {
41
42 namespace ledger {
43
44 /* Minimal supported version */
45 #define MINIMAL_APP_VERSION_MAJOR 1
46 #define MINIMAL_APP_VERSION_MINOR 8
47 #define MINIMAL_APP_VERSION_MICRO 0
48
49 #define VERSION(M,m,u) ((M)<<16|(m)<<8|(u))
50 #define VERSION_MAJOR(v) (((v)>>16)&0xFF)
51 #define VERSION_MINOR(v) (((v)>>8)&0xFF)
52 #define VERSION_MICRO(v) (((v)>>0)&0xFF)
53
54 #define MINIMAL_APP_VERSION VERSION(MINIMAL_APP_VERSION_MAJOR, MINIMAL_APP_VERSION_MINOR, MINIMAL_APP_VERSION_MICRO)
55
56 void register_all(std::map<std::string, std::unique_ptr<device>> &registry);
57
58 #ifdef WITH_DEVICE_LEDGER
59
60 // Origin: https://github.com/LedgerHQ/ledger-app-monero/blob/master/src/monero_types.h
61 #define SW_OK 0x9000
62 #define SW_WRONG_LENGTH 0x6700
63 #define SW_SECURITY_PIN_LOCKED 0x6910
64 #define SW_SECURITY_LOAD_KEY 0x6911
65 #define SW_SECURITY_COMMITMENT_CONTROL 0x6912
66 #define SW_SECURITY_AMOUNT_CHAIN_CONTROL 0x6913
67 #define SW_SECURITY_COMMITMENT_CHAIN_CONTROL 0x6914
68 #define SW_SECURITY_OUTKEYS_CHAIN_CONTROL 0x6915
69 #define SW_SECURITY_MAXOUTPUT_REACHED 0x6916
70 #define SW_SECURITY_HMAC 0x6917
71 #define SW_SECURITY_RANGE_VALUE 0x6918
72 #define SW_SECURITY_INTERNAL 0x6919
73 #define SW_SECURITY_MAX_SIGNATURE_REACHED 0x691A
74 #define SW_SECURITY_PREFIX_HASH 0x691B
75 #define SW_SECURITY_LOCKED 0x69EE
76 #define SW_COMMAND_NOT_ALLOWED 0x6980
77 #define SW_SUBCOMMAND_NOT_ALLOWED 0x6981
78 #define SW_DENY 0x6982
79 #define SW_KEY_NOT_SET 0x6983
80 #define SW_WRONG_DATA 0x6984
81 #define SW_WRONG_DATA_RANGE 0x6985
82 #define SW_IO_FULL 0x6986
83 #define SW_CLIENT_NOT_SUPPORTED 0x6A30
84 #define SW_WRONG_P1P2 0x6b00
85 #define SW_INS_NOT_SUPPORTED 0x6d00
86 #define SW_PROTOCOL_NOT_SUPPORTED 0x6e00
87 #define SW_UNKNOWN 0x6f00
88 #define SW_UNKNOWN_PROBABLY_LOCKED 0x5515
89
90 namespace {
91 bool apdu_verbose =true;
92 }
93
94 void set_apdu_verbose(bool verbose);
95
96 class ABPkeys {
97 public:
98 rct::key Aout;
99 rct::key Bout;
100 bool is_subaddress;
101 bool is_change_address;
102 bool additional_key ;
103 size_t index;
104 rct::key Pout;
105 rct::key AKout;
106 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);
107 ABPkeys(const ABPkeys& keys) ;
108 ABPkeys() {index=0;is_subaddress=false;is_change_address=false;additional_key=false;}
109 ABPkeys &operator=(const ABPkeys &keys);
110 };
111
112 class Keymap {
113 public:
114 std::vector<ABPkeys> ABP;
115
116 bool find(const rct::key& P, ABPkeys& keys) const;
117 void add(const ABPkeys& keys);
118 void clear();
119 void log();
120 };
121
122 class SecHMAC {
123 public:
124 uint32_t sec[32];
125 uint32_t hmac[32];
126
127 SecHMAC(const uint8_t s[32], const uint8_t m[32]);
128
129 };
130
131 class HMACmap {
132 public:
133 std::vector<SecHMAC> hmacs;
134
135 void find_mac(const uint8_t sec[32], uint8_t hmac[32]) ;
136 void add_mac(const uint8_t sec[32], const uint8_t hmac[32]) ;
137 void clear() ;
138 };
139
140
141 #define BUFFER_SEND_SIZE 262
142 #define BUFFER_RECV_SIZE 262
143
144 class device_ledger : public hw::device {
145 private:
146 // Locker for concurrent access
147 mutable std::recursive_mutex device_locker;
148 mutable std::mutex command_locker;
149
150 i_device_callback * m_callback;
151
152 //IO
153 hw::io::device_io_hid hw_device;
154 unsigned int length_send;
155 unsigned char buffer_send[BUFFER_SEND_SIZE];
156 unsigned int length_recv;
157 unsigned char buffer_recv[BUFFER_RECV_SIZE];
158 unsigned int sw;
159 unsigned int id;
160 void logCMD(void);
161 void logRESP(void);
162 unsigned int exchange(unsigned int ok=SW_OK, unsigned int mask=0xFFFF);
163 unsigned int exchange_wait_on_input(unsigned int ok=SW_OK, unsigned int mask=0xFFFF);
164 void reset_buffer(void);
165 int set_command_header(unsigned char ins, unsigned char p1 = 0x00, unsigned char p2 = 0x00);
166 int set_command_header_noopt(unsigned char ins, unsigned char p1 = 0x00, unsigned char p2 = 0x00);
167 void send_simple(unsigned char ins, unsigned char p1 = 0x00);
168 void send_secret(const unsigned char sec[32], int &offset);
169 void receive_secret(unsigned char sec[32], int &offset);
170
171 bool tx_in_progress;
172
173 // map public destination key to ephemeral destination key
174 Keymap key_map;
175 bool add_output_key_mapping(const crypto::public_key &Aout, const crypto::public_key &Bout, const bool is_subaddress, const bool is_change,
176 const bool need_additional, const size_t real_output_index,
177 const rct::key &amount_key, const crypto::public_key &out_eph_public_key);
178 //hmac for some encrypted value
179 HMACmap hmac_map;
180
181 // To speed up blockchain parsing the view key maybe handle here.
182 crypto::secret_key viewkey;
183 bool has_view_key;
184
185 device *controle_device;
186
187 public:
188 device_ledger();
189 ~device_ledger();
190
191 device_ledger(const device_ledger &device) = delete ;
192 device_ledger& operator=(const device_ledger &device) = delete;
193
194 explicit operator bool() const override {return this->connected(); }
195
196 bool reset(void);
197
198 /* ======================================================================= */
199 /* SETUP/TEARDOWN */
200 /* ======================================================================= */
201 bool set_name(const std::string &name) override;
202
203 const std::string get_name() const override;
204 bool init(void) override;
205 bool release() override;
206 bool connect(void) override;
207 bool disconnect() override;
208 bool connected(void) const;
209 bool disconnected() override;
210
211 bool set_mode(device_mode mode) override;
212
213 device_type get_type() const override {return device_type::LEDGER;};
214 device_protocol_t device_protocol() const override { return PROTOCOL_PROXY; };
215
216 void set_callback(i_device_callback * callback) override {
217 m_callback = callback;
218 }
219
220 /* ======================================================================= */
221 /* LOCKER */
222 /* ======================================================================= */
223 void lock(void) override;
224 void unlock(void) override;
225 bool try_lock(void) override;
226
227 /* ======================================================================= */
228 /* WALLET & ADDRESS */
229 /* ======================================================================= */
230 bool get_public_address(cryptonote::account_public_address &pubkey) override;
231 bool get_secret_keys(crypto::secret_key &viewkey , crypto::secret_key &spendkey) override;
232 bool set_secret_view_key(const crypto::secret_key &viewkey) override;
233 bool generate_chacha_key(const cryptonote::account_keys &keys, crypto::chacha_key &key, uint64_t kdf_rounds) override;
234 void display_address(const cryptonote::subaddress_index& index, const boost::optional<crypto::hash8> &payment_id) override;
235
236 /* ======================================================================= */
237 /* SUB ADDRESS */
238 /* ======================================================================= */
239 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;
240 crypto::public_key get_subaddress_spend_public_key(const cryptonote::account_keys& keys, const cryptonote::subaddress_index& index) override;
241 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;
242 cryptonote::account_public_address get_subaddress(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) override;
243 crypto::secret_key get_subaddress_secret_key(const crypto::secret_key &sec, const cryptonote::subaddress_index &index) override;
244
245 /* ======================================================================= */
246 /* DERIVATION & KEY */
247 /* ======================================================================= */
248 bool verify_keys(const crypto::secret_key &secret_key, const crypto::public_key &public_key) override;
249 bool scalarmultKey(rct::key & aP, const rct::key &P, const rct::key &a) override;
250 bool scalarmultBase(rct::key &aG, const rct::key &a) override;
251 bool sc_secret_add(crypto::secret_key &r, const crypto::secret_key &a, const crypto::secret_key &b) override;
252 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;
253 bool generate_key_derivation(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_derivation &derivation) override;
254 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;
255 bool derivation_to_scalar(const crypto::key_derivation &derivation, const size_t output_index, crypto::ec_scalar &res) override;
256 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;
257 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;
258 bool secret_key_to_public_key(const crypto::secret_key &sec, crypto::public_key &pub) override;
259 bool generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image) override;
260 bool derive_view_tag(const crypto::key_derivation &derivation, const size_t output_index, crypto::view_tag &view_tag) override;
261
262 /* ======================================================================= */
263 /* TRANSACTION */
264 /* ======================================================================= */
265 void generate_tx_proof(const crypto::hash &prefix_hash,
266 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,
267 crypto::signature &sig) override;
268
269 bool open_tx(crypto::secret_key &tx_key) override;
270
271 void get_transaction_prefix_hash(const cryptonote::transaction_prefix& tx, crypto::hash& h) override;
272
273 bool encrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key) override;
274
275 rct::key genCommitmentMask(const rct::key &amount_key) override;
276
277 bool ecdhEncode(rct::ecdhTuple & unmasked, const rct::key & sharedSec, bool short_format) override;
278 bool ecdhDecode(rct::ecdhTuple & masked, const rct::key & sharedSec, bool short_format) override;
279
280 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,
281 const cryptonote::tx_destination_entry &dst_entr, const boost::optional<cryptonote::account_public_address> &change_addr, const size_t output_index,
282 const bool &need_additional_txkeys, const std::vector<crypto::secret_key> &additional_tx_keys,
283 std::vector<crypto::public_key> &additional_tx_public_keys,
284 std::vector<rct::key> &amount_keys,
285 crypto::public_key &out_eph_public_key,
286 const bool use_view_tags, crypto::view_tag &view_tag) override;
287
288 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;
289 bool mlsag_prepare(const rct::key &H, const rct::key &xx, rct::key &a, rct::key &aG, rct::key &aHP, rct::key &rvII) override;
290 bool mlsag_prepare(rct::key &a, rct::key &aG) override;
291 bool mlsag_hash(const rct::keyV &long_message, rct::key &c) override;
292 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;
293
294 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;
295 bool clsag_hash(const rct::keyV &data, rct::key &hash) override;
296 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;
297
298
299 bool close_tx(void) override;
300
301 };
302
303
304 #ifdef DEBUG_HWDEVICE
305 extern crypto::secret_key dbg_viewkey;
306 extern crypto::secret_key dbg_spendkey;
307 #endif
308
309 #endif //WITH_DEVICE_LEDGER
310 }
311
312 }
313