Debian Codegraph @

src/device_trezor/trezor/protocol.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 #ifndef MONERO_PROTOCOL_H
31 #define MONERO_PROTOCOL_H
32
33 #include "trezor_defs.hpp"
34 #include "device/device_cold.hpp"
35 #include "messages_map.hpp"
36 #include "transport.hpp"
37 #include "wallet/wallet2.h"
38
39 namespace hw{
40 namespace trezor{
41 namespace protocol{
42
43 std::string key_to_string(const ::crypto::ec_point & key);
44 std::string key_to_string(const ::crypto::ec_scalar & key);
45 std::string key_to_string(const ::crypto::hash & key);
46 std::string key_to_string(const ::rct::key & key);
47
48 void string_to_key(::crypto::ec_scalar & key, const std::string & str);
49 void string_to_key(::crypto::ec_point & key, const std::string & str);
50 void string_to_key(::rct::key & key, const std::string & str);
51
52 template<class sub_t, class InputIterator>
53 void assign_to_repeatable(::google::protobuf::RepeatedField<sub_t> * dst, const InputIterator begin, const InputIterator end){
54 for (InputIterator it = begin; it != end; it++) {
55 auto s = dst->Add();
56 *s = *it;
57 }
58 }
59
60 template<class sub_t, class InputIterator>
61 void assign_from_repeatable(std::vector<sub_t> * dst, const InputIterator begin, const InputIterator end){
62 for (InputIterator it = begin; it != end; it++) {
63 dst->push_back(*it);
64 }
65 };
66
67 template<typename T>
68 bool cn_deserialize(const void * buff, size_t len, T & dst){
69 binary_archive<false> ba{{reinterpret_cast<const std::uint8_t*>(buff), len}};
70 bool r = ::serialization::serialize(ba, dst);
71 return r;
72 }
73
74 template<typename T>
75 bool cn_deserialize(const std::string & str, T & dst){
76 return cn_deserialize(str.data(), str.size(), dst);
77 }
78
79 template<typename T>
80 std::string cn_serialize(T & obj){
81 std::ostringstream oss;
82 binary_archive<true> oar(oss);
83 bool success = ::serialization::serialize(oar, obj);
84 if (!success){
85 throw exc::EncodingException("Could not CN serialize given object");
86 }
87 return oss.str();
88 }
89
90 // Crypto / encryption
91 namespace crypto {
92 namespace chacha {
93 // Constants as defined in RFC 7539.
94 const unsigned IV_SIZE = 12;
95 const unsigned TAG_SIZE = 16; // crypto_aead_chacha20poly1305_IETF_ABYTES;
96
97 /**
98 * Chacha20Poly1305 decryption with tag verification. RFC 7539.
99 */
100 void decrypt(const void* ciphertext, size_t length, const uint8_t* key, const uint8_t* iv, char* plaintext, size_t *plaintext_len=nullptr);
101
102 }
103 }
104
105
106 // Cold Key image sync
107 namespace ki {
108
109 using MoneroTransferDetails = messages::monero::MoneroKeyImageSyncStepRequest_MoneroTransferDetails;
110 using MoneroSubAddressIndicesList = messages::monero::MoneroKeyImageExportInitRequest_MoneroSubAddressIndicesList;
111 using MoneroExportedKeyImage = messages::monero::MoneroKeyImageSyncStepAck_MoneroExportedKeyImage;
112 using exported_key_image = hw::device_cold::exported_key_image;
113
114 /**
115 * Converts transfer details to the MoneroTransferDetails required for KI sync
116 */
117 bool key_image_data(wallet_shim * wallet,
118 const std::vector<tools::wallet2::transfer_details> & transfers,
119 std::vector<MoneroTransferDetails> & res);
120
121 /**
122 * Computes a hash over MoneroTransferDetails. Commitment used in the KI sync.
123 */
124 std::string compute_hash(const MoneroTransferDetails & rr);
125
126 /**
127 * Generates KI sync request with commitments computed.
128 */
129 void generate_commitment(std::vector<MoneroTransferDetails> & mtds,
130 const std::vector<tools::wallet2::transfer_details> & transfers,
131 std::shared_ptr<messages::monero::MoneroKeyImageExportInitRequest> & req);
132
133 /**
134 * Processes Live refresh step response, parses KI, checks the signature
135 */
136 void live_refresh_ack(const ::crypto::secret_key & view_key_priv,
137 const ::crypto::public_key& out_key,
138 const std::shared_ptr<messages::monero::MoneroLiveRefreshStepAck> & ack,
139 ::cryptonote::keypair& in_ephemeral,
140 ::crypto::key_image& ki);
141 }
142
143 // Cold transaction signing
144 namespace tx {
145 using TsxData = messages::monero::MoneroTransactionInitRequest_MoneroTransactionData;
146 using MoneroTransactionDestinationEntry = messages::monero::MoneroTransactionDestinationEntry;
147 using MoneroAccountPublicAddress = messages::monero::MoneroTransactionDestinationEntry_MoneroAccountPublicAddress;
148 using MoneroTransactionSourceEntry = messages::monero::MoneroTransactionSourceEntry;
149 using MoneroMultisigKLRki = messages::monero::MoneroTransactionSourceEntry_MoneroMultisigKLRki;
150 using MoneroOutputEntry = messages::monero::MoneroTransactionSourceEntry_MoneroOutputEntry;
151 using MoneroRctKey = messages::monero::MoneroTransactionSourceEntry_MoneroOutputEntry_MoneroRctKeyPublic;
152 using MoneroRsigData = messages::monero::MoneroTransactionRsigData;
153
154 using tx_construction_data = tools::wallet2::tx_construction_data;
155 using unsigned_tx_set = tools::wallet2::unsigned_tx_set;
156
157 void translate_address(MoneroAccountPublicAddress * dst, const cryptonote::account_public_address * src);
158 void translate_dst_entry(MoneroTransactionDestinationEntry * dst, const cryptonote::tx_destination_entry * src);
159 void translate_klrki(MoneroMultisigKLRki * dst, const rct::multisig_kLRki * src);
160 void translate_rct_key(MoneroRctKey * dst, const rct::ctkey * src);
161 std::string hash_addr(const MoneroAccountPublicAddress * addr, boost::optional<uint64_t> amount = boost::none, boost::optional<bool> is_subaddr = boost::none);
162 std::string hash_addr(const std::string & spend_key, const std::string & view_key, boost::optional<uint64_t> amount = boost::none, boost::optional<bool> is_subaddr = boost::none);
163 std::string hash_addr(const ::crypto::public_key * spend_key, const ::crypto::public_key * view_key, boost::optional<uint64_t> amount = boost::none, boost::optional<bool> is_subaddr = boost::none);
164 ::crypto::secret_key compute_enc_key(const ::crypto::secret_key & private_view_key, const std::string & aux, const std::string & salt);
165 std::string compute_sealing_key(const std::string & master_key, size_t idx, bool is_iv=false);
166
167 typedef boost::variant<rct::Bulletproof, rct::BulletproofPlus> rsig_v;
168
169 /**
170 * Transaction signer state holder.
171 */
172 class TData {
173 public:
174 TsxData tsx_data;
175 tx_construction_data tx_data;
176 cryptonote::transaction tx;
177 unsigned rsig_type;
178 int bp_version;
179 std::vector<uint64_t> grouping_vct;
180 std::shared_ptr<MoneroRsigData> rsig_param;
181 size_t cur_input_idx;
182 size_t cur_output_idx;
183 size_t cur_batch_idx;
184 size_t cur_output_in_batch_idx;
185
186 std::vector<std::string> tx_in_hmacs;
187 std::vector<std::string> tx_out_entr_hmacs;
188 std::vector<std::string> tx_out_hmacs;
189 std::vector<rsig_v> tx_out_rsigs;
190 std::vector<rct::ctkey> tx_out_pk;
191 std::vector<rct::ecdhTuple> tx_out_ecdh;
192 std::vector<size_t> source_permutation;
193 std::vector<std::string> alphas;
194 std::vector<std::string> spend_encs;
195 std::vector<std::string> pseudo_outs;
196 std::vector<std::string> pseudo_outs_hmac;
197 std::vector<std::string> couts;
198 std::vector<std::string> couts_dec;
199 std::vector<std::string> signatures;
200 std::vector<rct::key> rsig_gamma;
201 std::string tx_prefix_hash;
202 std::string enc_salt1;
203 std::string enc_salt2;
204 std::string enc_keys;
205
206 std::shared_ptr<rct::rctSig> rv;
207
208 TData();
209 };
210
211 class Signer {
212 private:
213 TData m_ct;
214 wallet_shim * m_wallet2;
215
216 size_t m_tx_idx;
217 const unsigned_tx_set * m_unsigned_tx;
218 hw::tx_aux_data * m_aux_data;
219
220 unsigned m_client_version;
221 bool m_multisig;
222
223 const tx_construction_data & cur_src_tx() const {
224 CHECK_AND_ASSERT_THROW_MES(m_tx_idx < m_unsigned_tx->txes.size(), "Invalid transaction index");
225 return m_unsigned_tx->txes[m_tx_idx];
226 }
227
228 const tx_construction_data & cur_tx() const {
229 return m_ct.tx_data;
230 }
231
232 const tools::wallet2::transfer_details & get_transfer(size_t idx) const {
233 CHECK_AND_ASSERT_THROW_MES(idx < std::get<2>(m_unsigned_tx->transfers).size() + std::get<0>(m_unsigned_tx->transfers) && idx >= std::get<0>(m_unsigned_tx->transfers), "Invalid transfer index");
234 return std::get<2>(m_unsigned_tx->transfers)[idx - std::get<0>(m_unsigned_tx->transfers)];
235 }
236
237 const tools::wallet2::transfer_details & get_source_transfer(size_t idx) const {
238 const auto & sel_transfers = cur_tx().selected_transfers;
239 CHECK_AND_ASSERT_THROW_MES(idx < m_ct.source_permutation.size(), "Invalid source index - permutation");
240 CHECK_AND_ASSERT_THROW_MES(m_ct.source_permutation[idx] < sel_transfers.size(), "Invalid source index");
241 return get_transfer(sel_transfers.at(m_ct.source_permutation[idx]));
242 }
243
244 void extract_payment_id();
245 void compute_integrated_indices(TsxData * tsx_data);
246 bool should_compute_bp_now() const;
247 void compute_bproof(messages::monero::MoneroTransactionRsigData & rsig_data);
248 void process_bproof(rsig_v & bproof);
249 void set_tx_input(MoneroTransactionSourceEntry * dst, size_t idx, bool need_ring_keys=false, bool need_ring_indices=false);
250
251 public:
252 Signer(wallet_shim * wallet2, const unsigned_tx_set * unsigned_tx, size_t tx_idx = 0, hw::tx_aux_data * aux_data = nullptr);
253
254 std::shared_ptr<messages::monero::MoneroTransactionInitRequest> step_init();
255 void step_init_ack(std::shared_ptr<const messages::monero::MoneroTransactionInitAck> ack);
256
257 std::shared_ptr<messages::monero::MoneroTransactionSetInputRequest> step_set_input(size_t idx);
258 void step_set_input_ack(std::shared_ptr<const messages::monero::MoneroTransactionSetInputAck> ack);
259
260 void sort_ki();
261
262 std::shared_ptr<messages::monero::MoneroTransactionInputViniRequest> step_set_vini_input(size_t idx);
263 void step_set_vini_input_ack(std::shared_ptr<const messages::monero::MoneroTransactionInputViniAck> ack);
264
265 std::shared_ptr<messages::monero::MoneroTransactionAllInputsSetRequest> step_all_inputs_set();
266 void step_all_inputs_set_ack(std::shared_ptr<const messages::monero::MoneroTransactionAllInputsSetAck> ack);
267
268 std::shared_ptr<messages::monero::MoneroTransactionSetOutputRequest> step_set_output(size_t idx);
269 void step_set_output_ack(std::shared_ptr<const messages::monero::MoneroTransactionSetOutputAck> ack);
270
271 std::shared_ptr<messages::monero::MoneroTransactionSetOutputRequest> step_rsig(size_t idx);
272 void step_set_rsig_ack(std::shared_ptr<const messages::monero::MoneroTransactionSetOutputAck> ack);
273
274 std::shared_ptr<messages::monero::MoneroTransactionAllOutSetRequest> step_all_outs_set();
275 void step_all_outs_set_ack(std::shared_ptr<const messages::monero::MoneroTransactionAllOutSetAck> ack, hw::device &hwdev);
276
277 std::shared_ptr<messages::monero::MoneroTransactionSignInputRequest> step_sign_input(size_t idx);
278 void step_sign_input_ack(std::shared_ptr<const messages::monero::MoneroTransactionSignInputAck> ack);
279
280 std::shared_ptr<messages::monero::MoneroTransactionFinalRequest> step_final();
281 void step_final_ack(std::shared_ptr<const messages::monero::MoneroTransactionFinalAck> ack);
282
283 std::string store_tx_aux_info();
284
285 unsigned client_version() const {
286 return m_client_version;
287 }
288
289 uint8_t get_rv_type() const {
290 if (!m_ct.rv){
291 throw std::invalid_argument("RV not initialized");
292 }
293 return m_ct.rv->type;
294 }
295
296 bool is_simple() const {
297 auto tp = get_rv_type();
298 return tp == rct::RCTTypeSimple;
299 }
300
301 bool is_req_bulletproof() const {
302 return m_ct.tx_data.rct_config.range_proof_type != rct::RangeProofBorromean;
303 }
304
305 bool is_req_clsag() const {
306 return is_req_bulletproof() && m_ct.tx_data.rct_config.bp_version >= 3;
307 }
308
309 bool is_req_bulletproof_plus() const {
310 return is_req_bulletproof() && m_ct.tx_data.rct_config.bp_version == 4; // rct::genRctSimple
311 }
312
313 bool is_bulletproof() const {
314 auto tp = get_rv_type();
315 return rct::is_rct_bulletproof(tp) || rct::is_rct_bulletproof_plus(tp);
316 }
317
318 bool is_bulletproof_plus() const {
319 auto tp = get_rv_type();
320 return rct::is_rct_bulletproof_plus(tp);
321 }
322
323 bool is_clsag() const {
324 auto tp = get_rv_type();
325 return rct::is_rct_clsag(tp);
326 }
327
328 bool is_offloading() const {
329 return m_ct.rsig_param && m_ct.rsig_param->offload_type() != 0;
330 }
331
332 size_t num_outputs() const {
333 return m_ct.tx_data.splitted_dsts.size();
334 }
335
336 size_t num_inputs() const {
337 return m_ct.tx_data.sources.size();
338 }
339
340 const TData & tdata() const {
341 return m_ct;
342 }
343 };
344
345 // TX Key decryption
346 void load_tx_key_data(hw::device_cold::tx_key_data_t & res, const std::string & data);
347
348 std::shared_ptr<messages::monero::MoneroGetTxKeyRequest> get_tx_key(
349 const hw::device_cold::tx_key_data_t & tx_data);
350
351 void get_tx_key_ack(
352 std::vector<::crypto::secret_key> & tx_keys,
353 const std::string & tx_prefix_hash,
354 const ::crypto::secret_key & view_key_priv,
355 std::shared_ptr<const messages::monero::MoneroGetTxKeyAck> ack
356 );
357 }
358
359 }
360 }
361 }
362
363
364 #endif //MONERO_PROTOCOL_H