| 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 | #include "version.h" |
| 31 | #include "protocol.hpp" |
| 32 | #include <unordered_map> |
| 33 | #include <set> |
| 34 | #include <utility> |
| 35 | #include <boost/endian/conversion.hpp> |
| 36 | #include <common/apply_permutation.h> |
| 37 | #include <common/json_util.h> |
| 38 | #include <crypto/hmac-keccak.h> |
| 39 | #include <ringct/rctSigs.h> |
| 40 | #include <ringct/bulletproofs.h> |
| 41 | #include <ringct/bulletproofs_plus.h> |
| 42 | #include "cryptonote_config.h" |
| 43 | #include <sodium.h> |
| 44 | #include <sodium/crypto_verify_32.h> |
| 45 | #include <sodium/crypto_aead_chacha20poly1305.h> |
| 46 | |
| 47 | #define GET_FIELD_STRING(name, type, jtype) field_##name = std::string(json[#name].GetString(), json[#name].GetStringLength()) |
| 48 | #define GET_FIELD_OTHER(name, type, jtype) field_##name = static_cast<type>(json[#name].Get##jtype()) |
| 49 | |
| 50 | #define GET_STRING_FROM_JSON(json, name, type, mandatory, def) \ |
| 51 | GET_FIELD_FROM_JSON_EX(json, name, type, String, mandatory, def, GET_FIELD_STRING) |
| 52 | |
| 53 | #define GET_FIELD_FROM_JSON(json, name, type, jtype, mandatory, def) \ |
| 54 | GET_FIELD_FROM_JSON_EX(json, name, type, jtype, mandatory, def, GET_FIELD_OTHER) |
| 55 | |
| 56 | #define GET_FIELD_FROM_JSON_EX(json, name, type, jtype, mandatory, def, VAL) \ |
| 57 | type field_##name = static_cast<type>(def); \ |
| 58 | bool field_##name##_found = false; \ |
| 59 | (void)field_##name##_found; \ |
| 60 | do if (json.HasMember(#name)) \ |
| 61 | { \ |
| 62 | if (json[#name].Is##jtype()) \ |
| 63 | { \ |
| 64 | VAL(name, type, jtype); \ |
| 65 | field_##name##_found = true; \ |
| 66 | } \ |
| 67 | else \ |
| 68 | { \ |
| 69 | throw std::invalid_argument("Field " #name " found in JSON, but not " #jtype); \ |
| 70 | } \ |
| 71 | } \ |
| 72 | else if (mandatory) \ |
| 73 | { \ |
| 74 | throw std::invalid_argument("Field " #name " not found in JSON");\ |
| 75 | } while(0) |
| 76 | |
| 77 | |
| 78 | namespace hw{ |
| 79 | namespace trezor{ |
| 80 | namespace protocol{ |
| 81 | |
| 82 | std::string key_to_string(const ::crypto::ec_point & key){ |
| 83 | return std::string(key.data, sizeof(key.data)); |
| 84 | } |
| 85 | |
| 86 | std::string key_to_string(const ::crypto::ec_scalar & key){ |
| 87 | return std::string(key.data, sizeof(key.data)); |
| 88 | } |
| 89 | |
| 90 | std::string key_to_string(const ::crypto::hash & key){ |
| 91 | return std::string(key.data, sizeof(key.data)); |
| 92 | } |
| 93 | |
| 94 | std::string key_to_string(const ::rct::key & key){ |
| 95 | return std::string(reinterpret_cast<const char*>(key.bytes), sizeof(key.bytes)); |
| 96 | } |
| 97 | |
| 98 | void string_to_key(::crypto::ec_scalar & key, const std::string & str){ |
| 99 | if (str.size() != sizeof(key.data)){ |
| 100 | throw std::invalid_argument(std::string("Key has to have ") + std::to_string(sizeof(key.data)) + " B"); |
| 101 | } |
| 102 | memcpy(key.data, str.data(), sizeof(key.data)); |
| 103 | } |
| 104 | |
| 105 | void string_to_key(::crypto::ec_point & key, const std::string & str){ |
| 106 | if (str.size() != sizeof(key.data)){ |
| 107 | throw std::invalid_argument(std::string("Key has to have ") + std::to_string(sizeof(key.data)) + " B"); |
| 108 | } |
| 109 | memcpy(key.data, str.data(), sizeof(key.data)); |
| 110 | } |
| 111 | |
| 112 | void string_to_key(::rct::key & key, const std::string & str){ |
| 113 | if (str.size() != sizeof(key.bytes)){ |
| 114 | throw std::invalid_argument(std::string("Key has to have ") + std::to_string(sizeof(key.bytes)) + " B"); |
| 115 | } |
| 116 | memcpy(key.bytes, str.data(), sizeof(key.bytes)); |
| 117 | } |
| 118 | |
| 119 | namespace crypto { |
| 120 | namespace chacha { |
| 121 | |
| 122 | void decrypt(const void* ciphertext, size_t length, const uint8_t* key, const uint8_t* iv, char* plaintext, size_t *plaintext_len){ |
| 123 | CHECK_AND_ASSERT_THROW_MES(length >= TAG_SIZE, "Ciphertext length too small"); |
| 124 | CHECK_AND_ASSERT_THROW_MES(!plaintext_len || *plaintext_len >= (length - TAG_SIZE), "Plaintext length too small"); |
| 125 | |
| 126 | unsigned long long int res_len = plaintext_len ? *plaintext_len : length; |
| 127 | auto r = crypto_aead_chacha20poly1305_ietf_decrypt( |
| 128 | reinterpret_cast<unsigned char *>(plaintext), &res_len, nullptr, |
| 129 | static_cast<const unsigned char *>(ciphertext), length, nullptr, 0, iv, key); |
| 130 | |
| 131 | if (r != 0){ |
| 132 | throw exc::Poly1305TagInvalid(); |
| 133 | } |
| 134 | |
| 135 | if (plaintext_len){ |
| 136 | *plaintext_len = (size_t) res_len; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |
| 144 | // Cold Key image sync |
| 145 | namespace ki { |
| 146 | |
| 147 | bool key_image_data(wallet_shim * wallet, |
| 148 | const std::vector<tools::wallet2::transfer_details> & transfers, |
| 149 | std::vector<MoneroTransferDetails> & res) |
| 150 | { |
| 151 | for(auto & td : transfers){ |
| 152 | ::crypto::public_key tx_pub_key = wallet->get_tx_pub_key_from_received_outs(td); |
| 153 | const std::vector<::crypto::public_key> additional_tx_pub_keys = cryptonote::get_additional_tx_pub_keys_from_extra(td.m_tx); |
| 154 | |
| 155 | res.emplace_back(); |
| 156 | auto & cres = res.back(); |
| 157 | cres.set_out_key(key_to_string(td.get_public_key())); |
| 158 | cres.set_tx_pub_key(key_to_string(tx_pub_key)); |
| 159 | cres.set_internal_output_index(td.m_internal_output_index); |
| 160 | cres.set_sub_addr_major(td.m_subaddr_index.major); |
| 161 | cres.set_sub_addr_minor(td.m_subaddr_index.minor); |
| 162 | if (!additional_tx_pub_keys.empty() && additional_tx_pub_keys.size() > td.m_internal_output_index) { |
| 163 | cres.add_additional_tx_pub_keys(key_to_string(additional_tx_pub_keys[td.m_internal_output_index])); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | std::string compute_hash(const MoneroTransferDetails & rr){ |
| 171 | KECCAK_CTX kck; |
| 172 | uint8_t md[32]; |
| 173 | |
| 174 | CHECK_AND_ASSERT_THROW_MES(rr.out_key().size() == 32, "Invalid out_key size"); |
| 175 | CHECK_AND_ASSERT_THROW_MES(rr.tx_pub_key().size() == 32, "Invalid tx_pub_key size"); |
| 176 | |
| 177 | keccak_init(&kck); |
| 178 | keccak_update(&kck, reinterpret_cast<const uint8_t *>(rr.out_key().data()), 32); |
| 179 | keccak_update(&kck, reinterpret_cast<const uint8_t *>(rr.tx_pub_key().data()), 32); |
| 180 | for (const auto &aux : rr.additional_tx_pub_keys()){ |
| 181 | CHECK_AND_ASSERT_THROW_MES(aux.size() == 32, "Invalid aux size"); |
| 182 | keccak_update(&kck, reinterpret_cast<const uint8_t *>(aux.data()), 32); |
| 183 | } |
| 184 | |
| 185 | auto index_serialized = tools::get_varint_data(rr.internal_output_index()); |
| 186 | keccak_update(&kck, reinterpret_cast<const uint8_t *>(index_serialized.data()), index_serialized.size()); |
| 187 | keccak_finish(&kck, md); |
| 188 | return std::string(reinterpret_cast<const char*>(md), sizeof(md)); |
| 189 | } |
| 190 | |
| 191 | void generate_commitment(std::vector<MoneroTransferDetails> & mtds, |
| 192 | const std::vector<tools::wallet2::transfer_details> & transfers, |
| 193 | std::shared_ptr<messages::monero::MoneroKeyImageExportInitRequest> & req) |
| 194 | { |
| 195 | req = std::make_shared<messages::monero::MoneroKeyImageExportInitRequest>(); |
| 196 | |
| 197 | KECCAK_CTX kck; |
| 198 | uint8_t final_hash[32]; |
| 199 | keccak_init(&kck); |
| 200 | |
| 201 | for(auto &cur : mtds){ |
| 202 | auto hash = compute_hash(cur); |
| 203 | keccak_update(&kck, reinterpret_cast<const uint8_t *>(hash.data()), hash.size()); |
| 204 | } |
| 205 | keccak_finish(&kck, final_hash); |
| 206 | |
| 207 | req = std::make_shared<messages::monero::MoneroKeyImageExportInitRequest>(); |
| 208 | req->set_hash(std::string(reinterpret_cast<const char*>(final_hash), 32)); |
| 209 | req->set_num(transfers.size()); |
| 210 | |
| 211 | std::unordered_map<uint32_t, std::set<uint32_t>> sub_indices; |
| 212 | for (auto &cur : transfers){ |
| 213 | auto search = sub_indices.emplace(cur.m_subaddr_index.major, std::set<uint32_t>()); |
| 214 | auto & st = search.first->second; |
| 215 | st.insert(cur.m_subaddr_index.minor); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void live_refresh_ack(const ::crypto::secret_key & view_key_priv, |
| 220 | const ::crypto::public_key& out_key, |
| 221 | const std::shared_ptr<messages::monero::MoneroLiveRefreshStepAck> & ack, |
| 222 | ::cryptonote::keypair& in_ephemeral, |
| 223 | ::crypto::key_image& ki) |
| 224 | { |
| 225 | std::string str_out_key(out_key.data, sizeof(out_key.data)); |
| 226 | auto enc_key = protocol::tx::compute_enc_key(view_key_priv, str_out_key, ack->salt()); |
| 227 | |
| 228 | const size_t len_ciphertext = ack->key_image().size(); // IV || keys |
| 229 | CHECK_AND_ASSERT_THROW_MES(len_ciphertext > crypto::chacha::IV_SIZE + crypto::chacha::TAG_SIZE, "Invalid size"); |
| 230 | |
| 231 | size_t ki_len = len_ciphertext - crypto::chacha::IV_SIZE - crypto::chacha::TAG_SIZE; |
| 232 | std::unique_ptr<uint8_t[]> plaintext(new uint8_t[ki_len]); |
| 233 | uint8_t * buff = plaintext.get(); |
| 234 | |
| 235 | protocol::crypto::chacha::decrypt( |
| 236 | ack->key_image().data() + crypto::chacha::IV_SIZE, |
| 237 | len_ciphertext - crypto::chacha::IV_SIZE, |
| 238 | reinterpret_cast<const uint8_t *>(enc_key.data), |
| 239 | reinterpret_cast<const uint8_t *>(ack->key_image().data()), |
| 240 | reinterpret_cast<char *>(buff), &ki_len); |
| 241 | |
| 242 | CHECK_AND_ASSERT_THROW_MES(ki_len == 3*32, "Invalid size"); |
| 243 | ::crypto::signature sig{}; |
| 244 | memcpy(ki.data, buff, 32); |
| 245 | memcpy(sig.c.data, buff + 32, 32); |
| 246 | memcpy(sig.r.data, buff + 64, 32); |
| 247 | in_ephemeral.pub = out_key; |
| 248 | in_ephemeral.sec = ::crypto::null_skey; |
| 249 | |
| 250 | // Verification |
| 251 | std::vector<const ::crypto::public_key*> pkeys; |
| 252 | pkeys.push_back(&out_key); |
| 253 | |
| 254 | CHECK_AND_ASSERT_THROW_MES(rct::scalarmultKey(rct::ki2rct(ki), rct::curveOrder()) == rct::identity(), |
| 255 | "Key image out of validity domain: key image " << epee::string_tools::pod_to_hex(ki)); |
| 256 | |
| 257 | CHECK_AND_ASSERT_THROW_MES(::crypto::check_ring_signature((const ::crypto::hash&)ki, ki, pkeys, &sig), |
| 258 | "Signature failed for key image " << epee::string_tools::pod_to_hex(ki) |
| 259 | << ", signature " + epee::string_tools::pod_to_hex(sig) |
| 260 | << ", pubkey " + epee::string_tools::pod_to_hex(*pkeys[0])); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // Cold transaction signing |
| 265 | namespace tx { |
| 266 | |
| 267 | void translate_address(MoneroAccountPublicAddress * dst, const cryptonote::account_public_address * src){ |
| 268 | dst->set_view_public_key(key_to_string(src->m_view_public_key)); |
| 269 | dst->set_spend_public_key(key_to_string(src->m_spend_public_key)); |
| 270 | } |
| 271 | |
| 272 | void translate_dst_entry(MoneroTransactionDestinationEntry * dst, const cryptonote::tx_destination_entry * src){ |
| 273 | dst->set_amount(src->amount); |
| 274 | dst->set_is_subaddress(src->is_subaddress); |
| 275 | dst->set_is_integrated(src->is_integrated); |
| 276 | dst->set_original(src->original); |
| 277 | translate_address(dst->mutable_addr(), &(src->addr)); |
| 278 | } |
| 279 | |
| 280 | void translate_klrki(MoneroMultisigKLRki * dst, const rct::multisig_kLRki * src){ |
| 281 | dst->set_k(key_to_string(src->k)); |
| 282 | dst->set_l(key_to_string(src->L)); |
| 283 | dst->set_r(key_to_string(src->R)); |
| 284 | dst->set_ki(key_to_string(src->ki)); |
| 285 | } |
| 286 | |
| 287 | void translate_rct_key(MoneroRctKey * dst, const rct::ctkey * src){ |
| 288 | dst->set_dest(key_to_string(src->dest)); |
| 289 | dst->set_commitment(key_to_string(src->mask)); |
| 290 | } |
| 291 | |
| 292 | std::string hash_addr(const MoneroAccountPublicAddress * addr, boost::optional<uint64_t> amount, boost::optional<bool> is_subaddr){ |
| 293 | return hash_addr(addr->spend_public_key(), addr->view_public_key(), amount, is_subaddr); |
| 294 | } |
| 295 | |
| 296 | std::string hash_addr(const std::string & spend_key, const std::string & view_key, boost::optional<uint64_t> amount, boost::optional<bool> is_subaddr){ |
| 297 | ::crypto::public_key spend{}, view{}; |
| 298 | if (spend_key.size() != 32 || view_key.size() != 32){ |
| 299 | throw std::invalid_argument("Public keys have invalid sizes"); |
| 300 | } |
| 301 | |
| 302 | memcpy(spend.data, spend_key.data(), 32); |
| 303 | memcpy(view.data, view_key.data(), 32); |
| 304 | return hash_addr(&spend, &view, amount, is_subaddr); |
| 305 | } |
| 306 | |
| 307 | std::string hash_addr(const ::crypto::public_key * spend_key, const ::crypto::public_key * view_key, boost::optional<uint64_t> amount, boost::optional<bool> is_subaddr){ |
| 308 | char buff[64+8+1]; |
| 309 | size_t offset = 0; |
| 310 | |
| 311 | memcpy(buff + offset, spend_key->data, 32); offset += 32; |
| 312 | memcpy(buff + offset, view_key->data, 32); offset += 32; |
| 313 | |
| 314 | if (amount){ |
| 315 | memcpy(buff + offset, (uint8_t*) &(amount.get()), sizeof(amount.get())); offset += sizeof(amount.get()); |
| 316 | } |
| 317 | |
| 318 | if (is_subaddr){ |
| 319 | buff[offset] = is_subaddr.get(); |
| 320 | offset += 1; |
| 321 | } |
| 322 | |
| 323 | return std::string(buff, offset); |
| 324 | } |
| 325 | |
| 326 | ::crypto::secret_key compute_enc_key(const ::crypto::secret_key & private_view_key, const std::string & aux, const std::string & salt) |
| 327 | { |
| 328 | uint8_t hash[32]; |
| 329 | KECCAK_CTX ctx; |
| 330 | ::crypto::secret_key res; |
| 331 | |
| 332 | keccak_init(&ctx); |
| 333 | keccak_update(&ctx, (const uint8_t *) private_view_key.data, sizeof(private_view_key.data)); |
| 334 | if (!aux.empty()){ |
| 335 | keccak_update(&ctx, (const uint8_t *) aux.data(), aux.size()); |
| 336 | } |
| 337 | keccak_finish(&ctx, hash); |
| 338 | keccak(hash, sizeof(hash), hash, sizeof(hash)); |
| 339 | |
| 340 | hmac_keccak_hash(hash, (const uint8_t *) salt.data(), salt.size(), hash, sizeof(hash)); |
| 341 | memcpy(res.data, hash, sizeof(hash)); |
| 342 | memwipe(hash, sizeof(hash)); |
| 343 | return res; |
| 344 | } |
| 345 | |
| 346 | std::string compute_sealing_key(const std::string & master_key, size_t idx, bool is_iv) |
| 347 | { |
| 348 | // master-key-32B || domain-sep-12B || index-4B |
| 349 | uint8_t hash[32] = {0}; |
| 350 | KECCAK_CTX ctx; |
| 351 | std::string sep = is_iv ? "sig-iv" : "sig-key"; |
| 352 | std::string idx_data = tools::get_varint_data(idx); |
| 353 | if (idx_data.size() > 4){ |
| 354 | throw std::invalid_argument("index is too big"); |
| 355 | } |
| 356 | |
| 357 | keccak_init(&ctx); |
| 358 | keccak_update(&ctx, (const uint8_t *) master_key.data(), master_key.size()); |
| 359 | keccak_update(&ctx, (const uint8_t *) sep.data(), sep.size()); |
| 360 | keccak_update(&ctx, hash, 12 - sep.size()); |
| 361 | keccak_update(&ctx, (const uint8_t *) idx_data.data(), idx_data.size()); |
| 362 | if (idx_data.size() < 4) { |
| 363 | keccak_update(&ctx, hash, 4 - idx_data.size()); |
| 364 | } |
| 365 | |
| 366 | keccak_finish(&ctx, hash); |
| 367 | keccak(hash, sizeof(hash), hash, sizeof(hash)); |
| 368 | return std::string((const char*) hash, 32); |
| 369 | } |
| 370 | |
| 371 | TData::TData() { |
| 372 | rsig_type = 0; |
| 373 | bp_version = 0; |
| 374 | cur_input_idx = 0; |
| 375 | cur_output_idx = 0; |
| 376 | cur_batch_idx = 0; |
| 377 | cur_output_in_batch_idx = 0; |
| 378 | } |
| 379 | |
| 380 | Signer::Signer(wallet_shim *wallet2, const unsigned_tx_set * unsigned_tx, size_t tx_idx, hw::tx_aux_data * aux_data) { |
| 381 | m_wallet2 = wallet2; |
| 382 | m_unsigned_tx = unsigned_tx; |
| 383 | m_aux_data = aux_data; |
| 384 | m_tx_idx = tx_idx; |
| 385 | m_ct.tx_data = cur_src_tx(); |
| 386 | m_multisig = false; |
| 387 | m_client_version = 3; |
| 388 | } |
| 389 | |
| 390 | void Signer::extract_payment_id(){ |
| 391 | const std::vector<uint8_t>& tx_extra = cur_tx().extra; |
| 392 | m_ct.tsx_data.set_payment_id(""); |
| 393 | |
| 394 | std::vector<cryptonote::tx_extra_field> tx_extra_fields; |
| 395 | cryptonote::parse_tx_extra(tx_extra, tx_extra_fields); // ok if partially parsed |
| 396 | cryptonote::tx_extra_nonce extra_nonce; |
| 397 | |
| 398 | ::crypto::hash payment_id{}; |
| 399 | if (find_tx_extra_field_by_type(tx_extra_fields, extra_nonce)) |
| 400 | { |
| 401 | ::crypto::hash8 payment_id8{}; |
| 402 | if(cryptonote::get_encrypted_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id8)) |
| 403 | { |
| 404 | m_ct.tsx_data.set_payment_id(std::string(payment_id8.data, 8)); |
| 405 | } |
| 406 | else if (cryptonote::get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id)) |
| 407 | { |
| 408 | m_ct.tsx_data.set_payment_id(std::string(payment_id.data, 32)); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | static unsigned get_rsig_type(const rct::RCTConfig &rct_config, size_t num_outputs){ |
| 414 | if (rct_config.range_proof_type == rct::RangeProofBorromean){ |
| 415 | return rct::RangeProofBorromean; |
| 416 | } else if (num_outputs > BULLETPROOF_MAX_OUTPUTS){ |
| 417 | return rct::RangeProofMultiOutputBulletproof; |
| 418 | } else { |
| 419 | return rct::RangeProofPaddedBulletproof; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | static void generate_rsig_batch_sizes(std::vector<uint64_t> &batches, unsigned rsig_type, size_t num_outputs){ |
| 424 | size_t amount_batched = 0; |
| 425 | |
| 426 | while(amount_batched < num_outputs){ |
| 427 | if (rsig_type == rct::RangeProofBorromean || rsig_type == rct::RangeProofBulletproof) { |
| 428 | batches.push_back(1); |
| 429 | amount_batched += 1; |
| 430 | |
| 431 | } else if (rsig_type == rct::RangeProofPaddedBulletproof){ |
| 432 | if (num_outputs > BULLETPROOF_MAX_OUTPUTS){ |
| 433 | throw std::invalid_argument("BP padded can support only BULLETPROOF_MAX_OUTPUTS statements"); |
| 434 | } |
| 435 | batches.push_back(num_outputs); |
| 436 | amount_batched += num_outputs; |
| 437 | |
| 438 | } else if (rsig_type == rct::RangeProofMultiOutputBulletproof){ |
| 439 | size_t batch_size = 1; |
| 440 | while (batch_size * 2 + amount_batched <= num_outputs && batch_size * 2 <= BULLETPROOF_MAX_OUTPUTS){ |
| 441 | batch_size *= 2; |
| 442 | } |
| 443 | batch_size = std::min(batch_size, num_outputs - amount_batched); |
| 444 | batches.push_back(batch_size); |
| 445 | amount_batched += batch_size; |
| 446 | |
| 447 | } else { |
| 448 | throw std::invalid_argument("Unknown rsig type"); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | void Signer::set_tx_input(MoneroTransactionSourceEntry * dst, size_t idx, bool need_ring_keys, bool need_ring_indices){ |
| 454 | const cryptonote::tx_source_entry & src = cur_tx().sources[idx]; |
| 455 | const tools::wallet2::transfer_details & transfer = get_source_transfer(idx); |
| 456 | |
| 457 | dst->set_real_output(src.real_output); |
| 458 | for(size_t i = 0; i < src.outputs.size(); ++i){ |
| 459 | auto & cur = src.outputs[i]; |
| 460 | auto out = dst->add_outputs(); |
| 461 | |
| 462 | if (i == src.real_output || need_ring_indices) { |
| 463 | out->set_idx(cur.first); |
| 464 | } |
| 465 | if (i == src.real_output || need_ring_keys) { |
| 466 | translate_rct_key(out->mutable_key(), &(cur.second)); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | dst->set_real_out_tx_key(key_to_string(src.real_out_tx_key)); |
| 471 | dst->set_real_output_in_tx_index(src.real_output_in_tx_index); |
| 472 | if (!src.real_out_additional_tx_keys.empty()) { |
| 473 | dst->add_real_out_additional_tx_keys(key_to_string(src.real_out_additional_tx_keys.at(src.real_output_in_tx_index))); |
| 474 | } |
| 475 | dst->set_amount(src.amount); |
| 476 | dst->set_rct(src.rct); |
| 477 | dst->set_mask(key_to_string(src.mask)); |
| 478 | translate_klrki(dst->mutable_multisig_klrki(), &(src.multisig_kLRki)); |
| 479 | dst->set_subaddr_minor(transfer.m_subaddr_index.minor); |
| 480 | } |
| 481 | |
| 482 | void Signer::compute_integrated_indices(TsxData * tsx_data){ |
| 483 | auto & chg = tsx_data->change_dts(); |
| 484 | std::string change_hash = hash_addr(&chg.addr(), chg.amount(), chg.is_subaddress()); |
| 485 | std::vector<uint32_t> integrated_indices; |
| 486 | |
| 487 | ssize_t idx = -1; |
| 488 | for (auto & cur : tsx_data->outputs()){ |
| 489 | idx += 1; |
| 490 | |
| 491 | std::string c_hash = hash_addr(&cur.addr(), cur.amount(), cur.is_subaddress()); |
| 492 | if (c_hash == change_hash || cur.is_subaddress()){ |
| 493 | continue; |
| 494 | } |
| 495 | |
| 496 | if (cur.is_integrated()){ |
| 497 | integrated_indices.push_back((uint32_t)idx); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | if (!integrated_indices.empty()){ |
| 502 | assign_to_repeatable(tsx_data->mutable_integrated_indices(), integrated_indices.begin(), integrated_indices.end()); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | std::shared_ptr<messages::monero::MoneroTransactionInitRequest> Signer::step_init(){ |
| 507 | // extract payment ID from construction data |
| 508 | auto & tsx_data = m_ct.tsx_data; |
| 509 | auto & tx = cur_tx(); |
| 510 | const size_t input_size = tx.sources.size(); |
| 511 | |
| 512 | m_ct.tx.version = 2; |
| 513 | m_ct.tx.unlock_time = tx.unlock_time; |
| 514 | m_client_version = (m_aux_data->client_version ? m_aux_data->client_version.get() : 3); |
| 515 | |
| 516 | tsx_data.set_version(1); |
| 517 | tsx_data.set_client_version(client_version()); |
| 518 | tsx_data.set_unlock_time(tx.unlock_time); |
| 519 | tsx_data.set_num_inputs(static_cast<google::protobuf::uint32>(input_size)); |
| 520 | tsx_data.set_mixin(static_cast<google::protobuf::uint32>(tx.sources[0].outputs.size() - 1)); |
| 521 | tsx_data.set_account(tx.subaddr_account); |
| 522 | tsx_data.set_monero_version(std::string(MONERO_VERSION) + "|" + MONERO_VERSION_TAG); |
| 523 | tsx_data.set_hard_fork(m_aux_data->hard_fork ? m_aux_data->hard_fork.get() : 0); |
| 524 | |
| 525 | // Rsig decision |
| 526 | auto rsig_data = tsx_data.mutable_rsig_data(); |
| 527 | m_ct.rsig_type = get_rsig_type(tx.rct_config, tx.splitted_dsts.size()); |
| 528 | rsig_data->set_rsig_type(m_ct.rsig_type); |
| 529 | CHECK_AND_ASSERT_THROW_MES(tx.rct_config.range_proof_type != rct::RangeProofBorromean, "Borromean rsig not supported"); |
| 530 | m_ct.bp_version = (m_aux_data->bp_version ? m_aux_data->bp_version.get() : 1); |
| 531 | rsig_data->set_bp_version((uint32_t) m_ct.bp_version); |
| 532 | |
| 533 | generate_rsig_batch_sizes(m_ct.grouping_vct, m_ct.rsig_type, tx.splitted_dsts.size()); |
| 534 | assign_to_repeatable(rsig_data->mutable_grouping(), m_ct.grouping_vct.begin(), m_ct.grouping_vct.end()); |
| 535 | |
| 536 | translate_dst_entry(tsx_data.mutable_change_dts(), &(tx.change_dts)); |
| 537 | for(auto & cur : tx.splitted_dsts){ |
| 538 | auto dst = tsx_data.mutable_outputs()->Add(); |
| 539 | translate_dst_entry(dst, &cur); |
| 540 | } |
| 541 | |
| 542 | m_ct.source_permutation.clear(); |
| 543 | for (size_t n = 0; n < input_size; ++n){ |
| 544 | m_ct.source_permutation.push_back(n); |
| 545 | } |
| 546 | |
| 547 | compute_integrated_indices(&tsx_data); |
| 548 | |
| 549 | int64_t fee = 0; |
| 550 | for(auto & cur_in : tx.sources){ |
| 551 | fee += cur_in.amount; |
| 552 | } |
| 553 | for(auto & cur_out : tx.splitted_dsts){ |
| 554 | fee -= cur_out.amount; |
| 555 | } |
| 556 | if (fee < 0){ |
| 557 | throw std::invalid_argument("Fee cannot be negative"); |
| 558 | } |
| 559 | |
| 560 | tsx_data.set_fee(static_cast<google::protobuf::uint64>(fee)); |
| 561 | this->extract_payment_id(); |
| 562 | |
| 563 | auto init_req = std::make_shared<messages::monero::MoneroTransactionInitRequest>(); |
| 564 | init_req->set_version(0); |
| 565 | init_req->mutable_tsx_data()->CopyFrom(tsx_data); |
| 566 | return init_req; |
| 567 | } |
| 568 | |
| 569 | void Signer::step_init_ack(std::shared_ptr<const messages::monero::MoneroTransactionInitAck> ack){ |
| 570 | if (ack->has_rsig_data()){ |
| 571 | m_ct.rsig_param = std::make_shared<MoneroRsigData>(ack->rsig_data()); |
| 572 | } |
| 573 | |
| 574 | assign_from_repeatable(&(m_ct.tx_out_entr_hmacs), ack->hmacs().begin(), ack->hmacs().end()); |
| 575 | } |
| 576 | |
| 577 | std::shared_ptr<messages::monero::MoneroTransactionSetInputRequest> Signer::step_set_input(size_t idx){ |
| 578 | CHECK_AND_ASSERT_THROW_MES(idx < cur_tx().sources.size(), "Invalid source index"); |
| 579 | m_ct.cur_input_idx = idx; |
| 580 | auto res = std::make_shared<messages::monero::MoneroTransactionSetInputRequest>(); |
| 581 | set_tx_input(res->mutable_src_entr(), idx, false, true); |
| 582 | return res; |
| 583 | } |
| 584 | |
| 585 | void Signer::step_set_input_ack(std::shared_ptr<const messages::monero::MoneroTransactionSetInputAck> ack){ |
| 586 | auto & vini_str = ack->vini(); |
| 587 | |
| 588 | cryptonote::txin_v vini; |
| 589 | if (!cn_deserialize(vini_str.data(), vini_str.size(), vini)){ |
| 590 | throw exc::ProtocolException("Cannot deserialize vin[i]"); |
| 591 | } |
| 592 | |
| 593 | m_ct.tx.vin.emplace_back(vini); |
| 594 | m_ct.tx_in_hmacs.push_back(ack->vini_hmac()); |
| 595 | m_ct.pseudo_outs.push_back(ack->pseudo_out()); |
| 596 | m_ct.pseudo_outs_hmac.push_back(ack->pseudo_out_hmac()); |
| 597 | m_ct.alphas.push_back(ack->pseudo_out_alpha()); |
| 598 | m_ct.spend_encs.push_back(ack->spend_key()); |
| 599 | } |
| 600 | |
| 601 | void Signer::sort_ki(){ |
| 602 | const size_t input_size = cur_tx().sources.size(); |
| 603 | |
| 604 | CHECK_AND_ASSERT_THROW_MES(m_ct.tx.vin.size() == input_size, "Invalid vector size"); |
| 605 | std::sort(m_ct.source_permutation.begin(), m_ct.source_permutation.end(), [&](const size_t i0, const size_t i1) { |
| 606 | const cryptonote::txin_to_key &tk0 = boost::get<cryptonote::txin_to_key>(m_ct.tx.vin[i0]); |
| 607 | const cryptonote::txin_to_key &tk1 = boost::get<cryptonote::txin_to_key>(m_ct.tx.vin[i1]); |
| 608 | return memcmp(&tk0.k_image, &tk1.k_image, sizeof(tk0.k_image)) > 0; |
| 609 | }); |
| 610 | |
| 611 | CHECK_AND_ASSERT_THROW_MES(m_ct.tx_in_hmacs.size() == input_size, "Invalid vector size"); |
| 612 | CHECK_AND_ASSERT_THROW_MES(m_ct.pseudo_outs.size() == input_size, "Invalid vector size"); |
| 613 | CHECK_AND_ASSERT_THROW_MES(m_ct.pseudo_outs_hmac.size() == input_size, "Invalid vector size"); |
| 614 | CHECK_AND_ASSERT_THROW_MES(m_ct.alphas.size() == input_size, "Invalid vector size"); |
| 615 | CHECK_AND_ASSERT_THROW_MES(m_ct.spend_encs.size() == input_size, "Invalid vector size"); |
| 616 | CHECK_AND_ASSERT_THROW_MES(m_ct.tx_data.sources.size() == input_size, "Invalid vector size"); |
| 617 | |
| 618 | tools::apply_permutation(m_ct.source_permutation, [&](size_t i0, size_t i1){ |
| 619 | std::swap(m_ct.tx.vin[i0], m_ct.tx.vin[i1]); |
| 620 | std::swap(m_ct.tx_in_hmacs[i0], m_ct.tx_in_hmacs[i1]); |
| 621 | std::swap(m_ct.pseudo_outs[i0], m_ct.pseudo_outs[i1]); |
| 622 | std::swap(m_ct.pseudo_outs_hmac[i0], m_ct.pseudo_outs_hmac[i1]); |
| 623 | std::swap(m_ct.alphas[i0], m_ct.alphas[i1]); |
| 624 | std::swap(m_ct.spend_encs[i0], m_ct.spend_encs[i1]); |
| 625 | std::swap(m_ct.tx_data.sources[i0], m_ct.tx_data.sources[i1]); |
| 626 | }); |
| 627 | } |
| 628 | |
| 629 | std::shared_ptr<messages::monero::MoneroTransactionInputViniRequest> Signer::step_set_vini_input(size_t idx){ |
| 630 | CHECK_AND_ASSERT_THROW_MES(idx < m_ct.tx_data.sources.size(), "Invalid transaction index"); |
| 631 | CHECK_AND_ASSERT_THROW_MES(idx < m_ct.tx.vin.size(), "Invalid transaction index"); |
| 632 | CHECK_AND_ASSERT_THROW_MES(idx < m_ct.tx_in_hmacs.size(), "Invalid transaction index"); |
| 633 | |
| 634 | m_ct.cur_input_idx = idx; |
| 635 | auto tx = m_ct.tx_data; |
| 636 | auto res = std::make_shared<messages::monero::MoneroTransactionInputViniRequest>(); |
| 637 | auto & vini = m_ct.tx.vin[idx]; |
| 638 | set_tx_input(res->mutable_src_entr(), idx, false, false); |
| 639 | res->set_vini(cryptonote::t_serializable_object_to_blob(vini)); |
| 640 | res->set_vini_hmac(m_ct.tx_in_hmacs[idx]); |
| 641 | res->set_orig_idx(m_ct.source_permutation[idx]); |
| 642 | return res; |
| 643 | } |
| 644 | |
| 645 | void Signer::step_set_vini_input_ack(std::shared_ptr<const messages::monero::MoneroTransactionInputViniAck> ack){ |
| 646 | |
| 647 | } |
| 648 | |
| 649 | std::shared_ptr<messages::monero::MoneroTransactionAllInputsSetRequest> Signer::step_all_inputs_set(){ |
| 650 | return std::make_shared<messages::monero::MoneroTransactionAllInputsSetRequest>(); |
| 651 | } |
| 652 | |
| 653 | void Signer::step_all_inputs_set_ack(std::shared_ptr<const messages::monero::MoneroTransactionAllInputsSetAck> ack){ |
| 654 | } |
| 655 | |
| 656 | std::shared_ptr<messages::monero::MoneroTransactionSetOutputRequest> Signer::step_set_output(size_t idx){ |
| 657 | CHECK_AND_ASSERT_THROW_MES(idx < m_ct.tx_data.splitted_dsts.size(), "Invalid transaction index"); |
| 658 | CHECK_AND_ASSERT_THROW_MES(idx < m_ct.tx_out_entr_hmacs.size(), "Invalid transaction index"); |
| 659 | CHECK_AND_ASSERT_THROW_MES(is_req_bulletproof(), "Borromean rsig not supported"); |
| 660 | |
| 661 | m_ct.cur_output_idx = idx; |
| 662 | m_ct.cur_output_in_batch_idx += 1; // assumes sequential call to step_set_output() |
| 663 | |
| 664 | auto res = std::make_shared<messages::monero::MoneroTransactionSetOutputRequest>(); |
| 665 | auto & cur_dst = m_ct.tx_data.splitted_dsts[idx]; |
| 666 | translate_dst_entry(res->mutable_dst_entr(), &cur_dst); |
| 667 | res->set_dst_entr_hmac(m_ct.tx_out_entr_hmacs[idx]); |
| 668 | return res; |
| 669 | } |
| 670 | |
| 671 | void Signer::step_set_output_ack(std::shared_ptr<const messages::monero::MoneroTransactionSetOutputAck> ack){ |
| 672 | CHECK_AND_ASSERT_THROW_MES(is_req_bulletproof(), "Borromean rsig not supported"); |
| 673 | cryptonote::tx_out tx_out; |
| 674 | rct::Bulletproof bproof{}; |
| 675 | rct::BulletproofPlus bproof_plus{}; |
| 676 | rct::ctkey out_pk{}; |
| 677 | rct::ecdhTuple ecdh{}; |
| 678 | |
| 679 | bool has_rsig = false; |
| 680 | std::string rsig_buff; |
| 681 | |
| 682 | if (ack->has_rsig_data()){ |
| 683 | auto & rsig_data = ack->rsig_data(); |
| 684 | |
| 685 | if (rsig_data.has_rsig() && !rsig_data.rsig().empty()){ |
| 686 | has_rsig = true; |
| 687 | rsig_buff = rsig_data.rsig(); |
| 688 | } |
| 689 | |
| 690 | if (rsig_data.has_mask()){ |
| 691 | rct::key cmask{}; |
| 692 | string_to_key(cmask, rsig_data.mask()); |
| 693 | m_ct.rsig_gamma.emplace_back(cmask); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | if (!cn_deserialize(ack->tx_out(), tx_out)){ |
| 698 | throw exc::ProtocolException("Cannot deserialize vout[i]"); |
| 699 | } |
| 700 | |
| 701 | if (!cn_deserialize(ack->out_pk(), out_pk)){ |
| 702 | throw exc::ProtocolException("Cannot deserialize out_pk"); |
| 703 | } |
| 704 | |
| 705 | if (m_ct.bp_version <= 1) { |
| 706 | if (!cn_deserialize(ack->ecdh_info(), ecdh)){ |
| 707 | throw exc::ProtocolException("Cannot deserialize ecdhtuple"); |
| 708 | } |
| 709 | } else { |
| 710 | CHECK_AND_ASSERT_THROW_MES(8 == ack->ecdh_info().size(), "Invalid ECDH.amount size"); |
| 711 | memcpy(ecdh.amount.bytes, ack->ecdh_info().data(), 8); |
| 712 | } |
| 713 | |
| 714 | m_ct.tx.vout.emplace_back(tx_out); |
| 715 | m_ct.tx_out_hmacs.push_back(ack->vouti_hmac()); |
| 716 | m_ct.tx_out_pk.emplace_back(out_pk); |
| 717 | m_ct.tx_out_ecdh.emplace_back(ecdh); |
| 718 | |
| 719 | rsig_v bp_obj{}; |
| 720 | if (has_rsig) { |
| 721 | bool deserialize_success; |
| 722 | if (is_req_bulletproof_plus()) { |
| 723 | deserialize_success = cn_deserialize(rsig_buff, bproof_plus); |
| 724 | bp_obj = bproof_plus; |
| 725 | } else { |
| 726 | deserialize_success = cn_deserialize(rsig_buff, bproof); |
| 727 | bp_obj = bproof; |
| 728 | } |
| 729 | if (!deserialize_success) { |
| 730 | throw exc::ProtocolException("Cannot deserialize bulletproof rangesig"); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | // Generates BP after all masks in the current batch are generated |
| 735 | if (!has_rsig || is_offloading()){ |
| 736 | return; |
| 737 | } |
| 738 | |
| 739 | process_bproof(bp_obj); |
| 740 | m_ct.cur_batch_idx += 1; |
| 741 | m_ct.cur_output_in_batch_idx = 0; |
| 742 | } |
| 743 | |
| 744 | bool Signer::should_compute_bp_now() const { |
| 745 | CHECK_AND_ASSERT_THROW_MES(m_ct.cur_batch_idx < m_ct.grouping_vct.size(), "Invalid batch index"); |
| 746 | return m_ct.grouping_vct[m_ct.cur_batch_idx] <= m_ct.cur_output_in_batch_idx; |
| 747 | } |
| 748 | |
| 749 | void Signer::compute_bproof(messages::monero::MoneroTransactionRsigData & rsig_data){ |
| 750 | auto batch_size = m_ct.grouping_vct[m_ct.cur_batch_idx]; |
| 751 | std::vector<uint64_t> amounts; |
| 752 | rct::keyV masks; |
| 753 | CHECK_AND_ASSERT_THROW_MES(m_ct.cur_output_idx + 1 >= batch_size, "Invalid index for batching"); |
| 754 | |
| 755 | for(size_t i = 0; i < batch_size; ++i){ |
| 756 | const size_t bidx = 1 + m_ct.cur_output_idx - batch_size + i; |
| 757 | CHECK_AND_ASSERT_THROW_MES(bidx < m_ct.tx_data.splitted_dsts.size(), "Invalid gamma index"); |
| 758 | CHECK_AND_ASSERT_THROW_MES(bidx < m_ct.rsig_gamma.size(), "Invalid gamma index"); |
| 759 | |
| 760 | amounts.push_back(m_ct.tx_data.splitted_dsts[bidx].amount); |
| 761 | masks.push_back(m_ct.rsig_gamma[bidx]); |
| 762 | } |
| 763 | |
| 764 | std::string serRsig; |
| 765 | if (is_req_bulletproof_plus()) { |
| 766 | auto bp = bulletproof_plus_PROVE(amounts, masks); |
| 767 | serRsig = cn_serialize(bp); |
| 768 | m_ct.tx_out_rsigs.emplace_back(bp); |
| 769 | } else { |
| 770 | auto bp = bulletproof_PROVE(amounts, masks); |
| 771 | serRsig = cn_serialize(bp); |
| 772 | m_ct.tx_out_rsigs.emplace_back(bp); |
| 773 | } |
| 774 | |
| 775 | rsig_data.set_rsig(serRsig); |
| 776 | } |
| 777 | |
| 778 | void Signer::process_bproof(rsig_v & bproof){ |
| 779 | CHECK_AND_ASSERT_THROW_MES(m_ct.cur_batch_idx < m_ct.grouping_vct.size(), "Invalid batch index"); |
| 780 | auto batch_size = m_ct.grouping_vct[m_ct.cur_batch_idx]; |
| 781 | for (size_t i = 0; i < batch_size; ++i){ |
| 782 | const size_t bidx = 1 + m_ct.cur_output_idx - batch_size + i; |
| 783 | CHECK_AND_ASSERT_THROW_MES(bidx < m_ct.tx_out_pk.size(), "Invalid out index"); |
| 784 | |
| 785 | rct::key commitment = m_ct.tx_out_pk[bidx].mask; |
| 786 | commitment = rct::scalarmultKey(commitment, rct::INV_EIGHT); |
| 787 | if (is_req_bulletproof_plus()) { |
| 788 | boost::get<rct::BulletproofPlus>(bproof).V.push_back(commitment); |
| 789 | } else { |
| 790 | boost::get<rct::Bulletproof>(bproof).V.push_back(commitment); |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | m_ct.tx_out_rsigs.emplace_back(bproof); |
| 795 | if (is_req_bulletproof_plus()) { |
| 796 | if (!rct::bulletproof_plus_VERIFY(boost::get<rct::BulletproofPlus>(m_ct.tx_out_rsigs.back()))) { |
| 797 | throw exc::ProtocolException("Returned range signature is invalid"); |
| 798 | } |
| 799 | } else { |
| 800 | if (!rct::bulletproof_VERIFY(boost::get<rct::Bulletproof>(m_ct.tx_out_rsigs.back()))) { |
| 801 | throw exc::ProtocolException("Returned range signature is invalid"); |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | std::shared_ptr<messages::monero::MoneroTransactionSetOutputRequest> Signer::step_rsig(size_t idx){ |
| 807 | if (!is_offloading() || !should_compute_bp_now()){ |
| 808 | return nullptr; |
| 809 | } |
| 810 | |
| 811 | auto res = std::make_shared<messages::monero::MoneroTransactionSetOutputRequest>(); |
| 812 | auto & cur_dst = m_ct.tx_data.splitted_dsts[idx]; |
| 813 | translate_dst_entry(res->mutable_dst_entr(), &cur_dst); |
| 814 | res->set_dst_entr_hmac(m_ct.tx_out_entr_hmacs[idx]); |
| 815 | |
| 816 | compute_bproof(*(res->mutable_rsig_data())); |
| 817 | res->set_is_offloaded_bp(true); |
| 818 | return res; |
| 819 | } |
| 820 | |
| 821 | void Signer::step_set_rsig_ack(std::shared_ptr<const messages::monero::MoneroTransactionSetOutputAck> ack){ |
| 822 | m_ct.cur_batch_idx += 1; |
| 823 | m_ct.cur_output_in_batch_idx = 0; |
| 824 | } |
| 825 | |
| 826 | std::shared_ptr<messages::monero::MoneroTransactionAllOutSetRequest> Signer::step_all_outs_set(){ |
| 827 | return std::make_shared<messages::monero::MoneroTransactionAllOutSetRequest>(); |
| 828 | } |
| 829 | |
| 830 | void Signer::step_all_outs_set_ack(std::shared_ptr<const messages::monero::MoneroTransactionAllOutSetAck> ack, hw::device &hwdev){ |
| 831 | CHECK_AND_ASSERT_THROW_MES(is_req_bulletproof(), "Borromean rsig not supported"); |
| 832 | m_ct.rv = std::make_shared<rct::rctSig>(); |
| 833 | m_ct.rv->txnFee = ack->rv().txn_fee(); |
| 834 | m_ct.rv->type = static_cast<uint8_t>(ack->rv().rv_type()); |
| 835 | string_to_key(m_ct.rv->message, ack->rv().message()); |
| 836 | |
| 837 | // Extra copy |
| 838 | m_ct.tx.extra.clear(); |
| 839 | auto extra = ack->extra(); |
| 840 | auto extra_data = extra.data(); |
| 841 | m_ct.tx.extra.reserve(extra.size()); |
| 842 | for(size_t i = 0; i < extra.size(); ++i){ |
| 843 | m_ct.tx.extra.push_back(static_cast<uint8_t>(extra_data[i])); |
| 844 | } |
| 845 | |
| 846 | ::crypto::hash tx_prefix_hash{}; |
| 847 | cryptonote::get_transaction_prefix_hash(m_ct.tx, tx_prefix_hash); |
| 848 | m_ct.tx_prefix_hash = key_to_string(tx_prefix_hash); |
| 849 | if (crypto_verify_32(reinterpret_cast<const unsigned char *>(tx_prefix_hash.data), |
| 850 | reinterpret_cast<const unsigned char *>(ack->tx_prefix_hash().data()))){ |
| 851 | throw exc::proto::SecurityException("Transaction prefix has does not match to the computed value"); |
| 852 | } |
| 853 | |
| 854 | // RctSig |
| 855 | auto num_sources = m_ct.tx_data.sources.size(); |
| 856 | auto dst = &m_ct.rv->p.pseudoOuts; |
| 857 | dst->clear(); |
| 858 | for (const auto &pseudo_out : m_ct.pseudo_outs) { |
| 859 | dst->emplace_back(); |
| 860 | string_to_key(dst->back(), pseudo_out); |
| 861 | } |
| 862 | |
| 863 | m_ct.rv->mixRing.resize(num_sources); |
| 864 | |
| 865 | CHECK_AND_ASSERT_THROW_MES(m_ct.tx_out_pk.size() == m_ct.tx_out_ecdh.size(), "Invalid vector sizes"); |
| 866 | for(size_t i = 0; i < m_ct.tx_out_ecdh.size(); ++i){ |
| 867 | m_ct.rv->outPk.push_back(m_ct.tx_out_pk[i]); |
| 868 | m_ct.rv->ecdhInfo.push_back(m_ct.tx_out_ecdh[i]); |
| 869 | } |
| 870 | |
| 871 | for(size_t i = 0; i < m_ct.tx_out_rsigs.size(); ++i){ |
| 872 | if (is_req_bulletproof_plus()) { |
| 873 | m_ct.rv->p.bulletproofs_plus.push_back(boost::get<rct::BulletproofPlus>(m_ct.tx_out_rsigs[i])); |
| 874 | } else { |
| 875 | m_ct.rv->p.bulletproofs.push_back(boost::get<rct::Bulletproof>(m_ct.tx_out_rsigs[i])); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | rct::key hash_computed = rct::get_pre_mlsag_hash(*(m_ct.rv), hwdev); |
| 880 | auto & hash = ack->full_message_hash(); |
| 881 | |
| 882 | if (hash.size() != 32){ |
| 883 | throw exc::ProtocolException("Returned mlsag hash has invalid size"); |
| 884 | } |
| 885 | |
| 886 | if (crypto_verify_32(reinterpret_cast<const unsigned char *>(hash_computed.bytes), |
| 887 | reinterpret_cast<const unsigned char *>(hash.data()))){ |
| 888 | throw exc::proto::SecurityException("Computed MLSAG does not match"); |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | std::shared_ptr<messages::monero::MoneroTransactionSignInputRequest> Signer::step_sign_input(size_t idx){ |
| 893 | m_ct.cur_input_idx = idx; |
| 894 | |
| 895 | CHECK_AND_ASSERT_THROW_MES(idx < m_ct.tx_data.sources.size(), "Invalid transaction index"); |
| 896 | CHECK_AND_ASSERT_THROW_MES(idx < m_ct.tx.vin.size(), "Invalid transaction index"); |
| 897 | CHECK_AND_ASSERT_THROW_MES(idx < m_ct.tx_in_hmacs.size(), "Invalid transaction index"); |
| 898 | CHECK_AND_ASSERT_THROW_MES(idx < m_ct.alphas.size(), "Invalid transaction index"); |
| 899 | CHECK_AND_ASSERT_THROW_MES(idx < m_ct.spend_encs.size(), "Invalid transaction index"); |
| 900 | |
| 901 | auto res = std::make_shared<messages::monero::MoneroTransactionSignInputRequest>(); |
| 902 | set_tx_input(res->mutable_src_entr(), idx, true, true); |
| 903 | res->set_vini(cryptonote::t_serializable_object_to_blob(m_ct.tx.vin[idx])); |
| 904 | res->set_vini_hmac(m_ct.tx_in_hmacs[idx]); |
| 905 | res->set_pseudo_out_alpha(m_ct.alphas[idx]); |
| 906 | res->set_spend_key(m_ct.spend_encs[idx]); |
| 907 | res->set_orig_idx(m_ct.source_permutation[idx]); |
| 908 | |
| 909 | CHECK_AND_ASSERT_THROW_MES(idx < m_ct.pseudo_outs.size(), "Invalid transaction index"); |
| 910 | CHECK_AND_ASSERT_THROW_MES(idx < m_ct.pseudo_outs_hmac.size(), "Invalid transaction index"); |
| 911 | res->set_pseudo_out(m_ct.pseudo_outs[idx]); |
| 912 | res->set_pseudo_out_hmac(m_ct.pseudo_outs_hmac[idx]); |
| 913 | return res; |
| 914 | } |
| 915 | |
| 916 | void Signer::step_sign_input_ack(std::shared_ptr<const messages::monero::MoneroTransactionSignInputAck> ack){ |
| 917 | m_ct.signatures.push_back(ack->signature()); |
| 918 | |
| 919 | // Sync updated pseudo_outputs |
| 920 | if (ack->has_pseudo_out()){ |
| 921 | CHECK_AND_ASSERT_THROW_MES(m_ct.cur_input_idx < m_ct.pseudo_outs.size(), "Invalid pseudo-out index"); |
| 922 | m_ct.pseudo_outs[m_ct.cur_input_idx] = ack->pseudo_out(); |
| 923 | if (is_bulletproof()){ |
| 924 | CHECK_AND_ASSERT_THROW_MES(m_ct.cur_input_idx < m_ct.rv->p.pseudoOuts.size(), "Invalid pseudo-out index"); |
| 925 | string_to_key(m_ct.rv->p.pseudoOuts[m_ct.cur_input_idx], ack->pseudo_out()); |
| 926 | } else { |
| 927 | CHECK_AND_ASSERT_THROW_MES(m_ct.cur_input_idx < m_ct.rv->pseudoOuts.size(), "Invalid pseudo-out index"); |
| 928 | string_to_key(m_ct.rv->pseudoOuts[m_ct.cur_input_idx], ack->pseudo_out()); |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | std::shared_ptr<messages::monero::MoneroTransactionFinalRequest> Signer::step_final(){ |
| 934 | return std::make_shared<messages::monero::MoneroTransactionFinalRequest>(); |
| 935 | } |
| 936 | |
| 937 | void Signer::step_final_ack(std::shared_ptr<const messages::monero::MoneroTransactionFinalAck> ack){ |
| 938 | CHECK_AND_ASSERT_THROW_MES(is_clsag(), "Only CLSAGs signatures are supported"); |
| 939 | |
| 940 | if (m_multisig){ |
| 941 | auto & cout_key = ack->cout_key(); |
| 942 | for(auto & cur : m_ct.couts){ |
| 943 | if (cur.size() != crypto::chacha::IV_SIZE + 32){ |
| 944 | throw std::invalid_argument("Encrypted cout has invalid length"); |
| 945 | } |
| 946 | |
| 947 | char buff[32]; |
| 948 | auto data = cur.data(); |
| 949 | |
| 950 | crypto::chacha::decrypt(data + crypto::chacha::IV_SIZE, 32, reinterpret_cast<const uint8_t *>(cout_key.data()), reinterpret_cast<const uint8_t *>(data), buff); |
| 951 | m_ct.couts_dec.emplace_back(buff, 32); |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | m_ct.enc_salt1 = ack->salt(); |
| 956 | m_ct.enc_salt2 = ack->rand_mult(); |
| 957 | m_ct.enc_keys = ack->tx_enc_keys(); |
| 958 | |
| 959 | // Opening the sealed signatures |
| 960 | if(!ack->has_opening_key()){ |
| 961 | throw exc::ProtocolException("Client version 3+ requires sealed signatures"); |
| 962 | } |
| 963 | |
| 964 | for(size_t i = 0; i < m_ct.signatures.size(); ++i){ |
| 965 | CHECK_AND_ASSERT_THROW_MES(m_ct.signatures[i].size() > crypto::chacha::TAG_SIZE, "Invalid signature size"); |
| 966 | std::string nonce = compute_sealing_key(ack->opening_key(), i, true); |
| 967 | std::string key = compute_sealing_key(ack->opening_key(), i, false); |
| 968 | size_t plen = m_ct.signatures[i].size() - crypto::chacha::TAG_SIZE; |
| 969 | std::unique_ptr<uint8_t[]> plaintext(new uint8_t[plen]); |
| 970 | uint8_t * buff = plaintext.get(); |
| 971 | |
| 972 | protocol::crypto::chacha::decrypt( |
| 973 | m_ct.signatures[i].data(), |
| 974 | m_ct.signatures[i].size(), |
| 975 | reinterpret_cast<const uint8_t *>(key.data()), |
| 976 | reinterpret_cast<const uint8_t *>(nonce.data()), |
| 977 | reinterpret_cast<char *>(buff), &plen); |
| 978 | m_ct.signatures[i].assign(reinterpret_cast<const char *>(buff), plen); |
| 979 | } |
| 980 | |
| 981 | m_ct.rv->p.CLSAGs.reserve(m_ct.signatures.size()); |
| 982 | for (size_t i = 0; i < m_ct.signatures.size(); ++i) { |
| 983 | rct::clsag clsag; |
| 984 | if (!cn_deserialize(m_ct.signatures[i], clsag)) { |
| 985 | throw exc::ProtocolException("Cannot deserialize clsag[i]"); |
| 986 | } |
| 987 | m_ct.rv->p.CLSAGs.push_back(clsag); |
| 988 | } |
| 989 | |
| 990 | m_ct.tx.rct_signatures = *(m_ct.rv); |
| 991 | } |
| 992 | |
| 993 | std::string Signer::store_tx_aux_info(){ |
| 994 | rapidjson::StringBuffer sb; |
| 995 | rapidjson::Writer<rapidjson::StringBuffer> writer(sb); |
| 996 | |
| 997 | rapidjson::Document json; |
| 998 | json.SetObject(); |
| 999 | |
| 1000 | rapidjson::Value valueS(rapidjson::kStringType); |
| 1001 | rapidjson::Value valueI(rapidjson::kNumberType); |
| 1002 | |
| 1003 | valueI.SetInt(1); |
| 1004 | json.AddMember("version", valueI, json.GetAllocator()); |
| 1005 | |
| 1006 | valueS.SetString(m_ct.enc_salt1.c_str(), m_ct.enc_salt1.size()); |
| 1007 | json.AddMember("salt1", valueS, json.GetAllocator()); |
| 1008 | |
| 1009 | valueS.SetString(m_ct.enc_salt2.c_str(), m_ct.enc_salt2.size()); |
| 1010 | json.AddMember("salt2", valueS, json.GetAllocator()); |
| 1011 | |
| 1012 | valueS.SetString(m_ct.tx_prefix_hash.c_str(), m_ct.tx_prefix_hash.size()); |
| 1013 | json.AddMember("tx_prefix_hash", valueS, json.GetAllocator()); |
| 1014 | |
| 1015 | valueS.SetString(m_ct.enc_keys.c_str(), m_ct.enc_keys.size()); |
| 1016 | json.AddMember("enc_keys", valueS, json.GetAllocator()); |
| 1017 | |
| 1018 | json.Accept(writer); |
| 1019 | return sb.GetString(); |
| 1020 | } |
| 1021 | |
| 1022 | void load_tx_key_data(hw::device_cold::tx_key_data_t & res, const std::string & data) |
| 1023 | { |
| 1024 | rapidjson::Document json; |
| 1025 | |
| 1026 | // The contents should be JSON if the wallet follows the new format. |
| 1027 | if (json.Parse(data.c_str()).HasParseError()) |
| 1028 | { |
| 1029 | throw std::invalid_argument("Data parsing error"); |
| 1030 | } |
| 1031 | else if(!json.IsObject()) |
| 1032 | { |
| 1033 | throw std::invalid_argument("Data parsing error - not an object"); |
| 1034 | } |
| 1035 | |
| 1036 | GET_FIELD_FROM_JSON(json, version, int, Int, true, -1); |
| 1037 | GET_STRING_FROM_JSON(json, salt1, std::string, true, std::string()); |
| 1038 | GET_STRING_FROM_JSON(json, salt2, std::string, true, std::string()); |
| 1039 | GET_STRING_FROM_JSON(json, enc_keys, std::string, true, std::string()); |
| 1040 | GET_STRING_FROM_JSON(json, tx_prefix_hash, std::string, false, std::string()); |
| 1041 | |
| 1042 | if (field_version != 1) |
| 1043 | { |
| 1044 | throw std::invalid_argument("Unknown version"); |
| 1045 | } |
| 1046 | |
| 1047 | res.salt1 = field_salt1; |
| 1048 | res.salt2 = field_salt2; |
| 1049 | res.tx_enc_keys = field_enc_keys; |
| 1050 | res.tx_prefix_hash = field_tx_prefix_hash; |
| 1051 | } |
| 1052 | |
| 1053 | std::shared_ptr<messages::monero::MoneroGetTxKeyRequest> get_tx_key( |
| 1054 | const hw::device_cold::tx_key_data_t & tx_data) |
| 1055 | { |
| 1056 | auto req = std::make_shared<messages::monero::MoneroGetTxKeyRequest>(); |
| 1057 | req->set_salt1(tx_data.salt1); |
| 1058 | req->set_salt2(tx_data.salt2); |
| 1059 | req->set_tx_enc_keys(tx_data.tx_enc_keys); |
| 1060 | req->set_tx_prefix_hash(tx_data.tx_prefix_hash); |
| 1061 | req->set_reason(0); |
| 1062 | |
| 1063 | return req; |
| 1064 | } |
| 1065 | |
| 1066 | void get_tx_key_ack( |
| 1067 | std::vector<::crypto::secret_key> & tx_keys, |
| 1068 | const std::string & tx_prefix_hash, |
| 1069 | const ::crypto::secret_key & view_key_priv, |
| 1070 | std::shared_ptr<const messages::monero::MoneroGetTxKeyAck> ack |
| 1071 | ) |
| 1072 | { |
| 1073 | auto enc_key = protocol::tx::compute_enc_key(view_key_priv, tx_prefix_hash, ack->salt()); |
| 1074 | auto & encrypted_keys = ack->has_tx_derivations() ? ack->tx_derivations() : ack->tx_keys(); |
| 1075 | |
| 1076 | const size_t len_ciphertext = encrypted_keys.size(); // IV || keys || TAG |
| 1077 | CHECK_AND_ASSERT_THROW_MES(len_ciphertext > crypto::chacha::IV_SIZE + crypto::chacha::TAG_SIZE, "Invalid size"); |
| 1078 | |
| 1079 | size_t keys_len = len_ciphertext - crypto::chacha::IV_SIZE - crypto::chacha::TAG_SIZE; |
| 1080 | std::unique_ptr<uint8_t[]> plaintext(new uint8_t[keys_len]); |
| 1081 | |
| 1082 | protocol::crypto::chacha::decrypt( |
| 1083 | encrypted_keys.data() + crypto::chacha::IV_SIZE, |
| 1084 | len_ciphertext - crypto::chacha::IV_SIZE, |
| 1085 | reinterpret_cast<const uint8_t *>(enc_key.data), |
| 1086 | reinterpret_cast<const uint8_t *>(encrypted_keys.data()), |
| 1087 | reinterpret_cast<char *>(plaintext.get()), &keys_len); |
| 1088 | |
| 1089 | CHECK_AND_ASSERT_THROW_MES(keys_len % 32 == 0, "Invalid size"); |
| 1090 | tx_keys.resize(keys_len / 32); |
| 1091 | |
| 1092 | for(unsigned i = 0; i < keys_len / 32; ++i) |
| 1093 | { |
| 1094 | memcpy(tx_keys[i].data, plaintext.get() + 32 * i, 32); |
| 1095 | } |
| 1096 | memwipe(plaintext.get(), keys_len); |
| 1097 | } |
| 1098 | |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | } |