| 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 | #include "crypto/crypto.h" |
| 30 | #include "multisig/multisig_account.h" |
| 31 | #include "multisig/multisig_kex_msg.h" |
| 32 | #include "ringct/rctOps.h" |
| 33 | #include "wallet/wallet2.h" |
| 34 | |
| 35 | #include "gtest/gtest.h" |
| 36 | |
| 37 | #include <cstdint> |
| 38 | |
| 39 | static const struct |
| 40 | { |
| 41 | const char *address; |
| 42 | const char *spendkey; |
| 43 | } test_addresses[] = |
| 44 | { |
| 45 | { |
| 46 | "9uvjbU54ZJb8j7Dcq1h3F1DnBRkxXdYUX4pbJ7mE3ghM8uF3fKzqRKRNAKYZXcNLqMg7MxjVVD2wKC2PALUwEveGSC3YSWD", |
| 47 | "2dd6e34a234c3e8b5d29a371789e4601e96dee4ea6f7ef79224d1a2d91164c01" |
| 48 | }, |
| 49 | { |
| 50 | "9ywDBAyDbb6QKFiZxDJ4hHZqZEQXXCR5EaYNcndUpqPDeE7rEgs6neQdZnhcDrWbURYK8xUjhuG2mVjJdmknrZbcG7NnbaB", |
| 51 | "fac47aecc948ce9d3531aa042abb18235b1df632087c55a361b632ffdd6ede0c" |
| 52 | }, |
| 53 | { |
| 54 | "9t6Hn946u3eah5cuncH1hB5hGzsTUoevtf4SY7MHN5NgJZh2SFWsyVt3vUhuHyRKyrCQvr71Lfc1AevG3BXE11PQFoXDtD8", |
| 55 | "bbd3175ef9fd9f5eefdc43035f882f74ad14c4cf1799d8b6f9001bc197175d02" |
| 56 | }, |
| 57 | { |
| 58 | "9zmAWoNyNPbgnYSm3nJNpAKHm6fCcs3MR94gBWxp9MCDUiMUhyYFfyQETUDLPF7DP6ZsmNo6LRxwPP9VmhHNxKrER9oGigT", |
| 59 | "f2efae45bef1917a7430cda8fcffc4ee010e3178761aa41d4628e23b1fe2d501" |
| 60 | }, |
| 61 | { |
| 62 | "9ue8NJMg3WzKxTtmjeXzWYF5KmU6dC7LHEt9wvYdPn2qMmoFUa8hJJHhSHvJ46UEwpDyy5jSboNMRaDBKwU54NT42YcNUp5", |
| 63 | "a4cef54ed3fd61cd78a2ceb82ecf85a903ad2db9a86fb77ff56c35c56016280a" |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | static const size_t KEYS_COUNT = 5; |
| 68 | |
| 69 | static void make_wallet(unsigned int idx, tools::wallet2 &wallet) |
| 70 | { |
| 71 | ASSERT_TRUE(idx < sizeof(test_addresses) / sizeof(test_addresses[0])); |
| 72 | |
| 73 | crypto::secret_key spendkey; |
| 74 | epee::string_tools::hex_to_pod(test_addresses[idx].spendkey, spendkey); |
| 75 | |
| 76 | try |
| 77 | { |
| 78 | wallet.init("", boost::none, "", 0, true, epee::net_utils::ssl_support_t::e_ssl_support_disabled); |
| 79 | wallet.set_subaddress_lookahead(1, 1); |
| 80 | wallet.generate("", "", spendkey, true, false); |
| 81 | ASSERT_TRUE(test_addresses[idx].address == wallet.get_account().get_public_address_str(cryptonote::TESTNET)); |
| 82 | wallet.decrypt_keys(""); |
| 83 | ASSERT_TRUE(test_addresses[idx].spendkey == epee::string_tools::pod_to_hex(unwrap(unwrap(wallet.get_account().get_keys().m_spend_secret_key)))); |
| 84 | wallet.encrypt_keys(""); |
| 85 | } |
| 86 | catch (const std::exception &e) |
| 87 | { |
| 88 | MFATAL("Error creating test wallet: " << e.what()); |
| 89 | ASSERT_TRUE(0); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static std::vector<std::string> exchange_round(std::vector<tools::wallet2>& wallets, const std::vector<std::string>& infos) |
| 94 | { |
| 95 | std::vector<std::string> new_infos; |
| 96 | new_infos.reserve(infos.size()); |
| 97 | |
| 98 | for (size_t i = 0; i < wallets.size(); ++i) |
| 99 | new_infos.push_back(wallets[i].exchange_multisig_keys("", infos)); |
| 100 | |
| 101 | return new_infos; |
| 102 | } |
| 103 | |
| 104 | static std::vector<std::string> exchange_round_force_update(std::vector<tools::wallet2>& wallets, |
| 105 | const std::vector<std::string>& infos, |
| 106 | const std::size_t round_in_progress) |
| 107 | { |
| 108 | EXPECT_TRUE(wallets.size() == infos.size()); |
| 109 | std::vector<std::string> new_infos; |
| 110 | std::vector<std::string> temp_force_update_infos; |
| 111 | new_infos.reserve(infos.size()); |
| 112 | |
| 113 | // when force-updating, we only need at most 'num_signers - 1 - (round - 1)' messages from other signers |
| 114 | size_t num_other_messages_required{wallets.size() - 1 - (round_in_progress - 1)}; |
| 115 | if (num_other_messages_required > wallets.size()) |
| 116 | num_other_messages_required = 0; //overflow case for post-kex verification round of 1-of-N |
| 117 | |
| 118 | for (size_t i = 0; i < wallets.size(); ++i) |
| 119 | { |
| 120 | temp_force_update_infos.clear(); |
| 121 | temp_force_update_infos.reserve(num_other_messages_required + 1); |
| 122 | temp_force_update_infos.push_back(infos[i]); //always include the local signer's message for this round |
| 123 | |
| 124 | size_t infos_collected{0}; |
| 125 | for (size_t wallet_index = 0; wallet_index < wallets.size(); ++wallet_index) |
| 126 | { |
| 127 | // skip the local signer's message |
| 128 | if (wallet_index == i) |
| 129 | continue; |
| 130 | |
| 131 | temp_force_update_infos.push_back(infos[wallet_index]); |
| 132 | ++infos_collected; |
| 133 | |
| 134 | if (infos_collected == num_other_messages_required) |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | new_infos.push_back(wallets[i].exchange_multisig_keys("", temp_force_update_infos, true)); |
| 139 | } |
| 140 | |
| 141 | return new_infos; |
| 142 | } |
| 143 | |
| 144 | static void check_results(const std::vector<std::string> &intermediate_infos, |
| 145 | std::vector<tools::wallet2>& wallets, |
| 146 | const std::uint32_t M) |
| 147 | { |
| 148 | // check results |
| 149 | std::unordered_set<crypto::secret_key> unique_privkeys; |
| 150 | rct::key composite_pubkey = rct::identity(); |
| 151 | |
| 152 | wallets[0].decrypt_keys(""); |
| 153 | crypto::public_key spend_pubkey = wallets[0].get_account().get_keys().m_account_address.m_spend_public_key; |
| 154 | crypto::secret_key view_privkey = wallets[0].get_account().get_keys().m_view_secret_key; |
| 155 | crypto::public_key view_pubkey; |
| 156 | EXPECT_TRUE(crypto::secret_key_to_public_key(view_privkey, view_pubkey)); |
| 157 | wallets[0].encrypt_keys(""); |
| 158 | |
| 159 | for (size_t i = 0; i < wallets.size(); ++i) |
| 160 | { |
| 161 | EXPECT_TRUE(!intermediate_infos[i].empty()); |
| 162 | bool ready; |
| 163 | uint32_t threshold, total; |
| 164 | EXPECT_TRUE(wallets[i].multisig(&ready, &threshold, &total)); |
| 165 | EXPECT_TRUE(ready); |
| 166 | EXPECT_TRUE(threshold == M); |
| 167 | EXPECT_TRUE(total == wallets.size()); |
| 168 | |
| 169 | wallets[i].decrypt_keys(""); |
| 170 | |
| 171 | if (i != 0) |
| 172 | { |
| 173 | // "equals" is transitive relation so we need only to compare first wallet's address to each others' addresses. |
| 174 | // no need to compare 0's address with itself. |
| 175 | EXPECT_TRUE(wallets[0].get_account().get_public_address_str(cryptonote::TESTNET) == |
| 176 | wallets[i].get_account().get_public_address_str(cryptonote::TESTNET)); |
| 177 | |
| 178 | EXPECT_EQ(spend_pubkey, wallets[i].get_account().get_keys().m_account_address.m_spend_public_key); |
| 179 | EXPECT_EQ(view_privkey, wallets[i].get_account().get_keys().m_view_secret_key); |
| 180 | EXPECT_EQ(view_pubkey, wallets[i].get_account().get_keys().m_account_address.m_view_public_key); |
| 181 | } |
| 182 | |
| 183 | // sum together unique multisig keys |
| 184 | for (const auto &privkey : wallets[i].get_account().get_keys().m_multisig_keys) |
| 185 | { |
| 186 | EXPECT_NE(privkey, crypto::null_skey); |
| 187 | |
| 188 | if (unique_privkeys.find(privkey) == unique_privkeys.end()) |
| 189 | { |
| 190 | unique_privkeys.insert(privkey); |
| 191 | crypto::public_key pubkey; |
| 192 | crypto::secret_key_to_public_key(privkey, pubkey); |
| 193 | EXPECT_NE(privkey, crypto::null_skey); |
| 194 | EXPECT_NE(pubkey, crypto::null_pkey); |
| 195 | EXPECT_NE(pubkey, rct::rct2pk(rct::identity())); |
| 196 | rct::addKeys(composite_pubkey, composite_pubkey, rct::pk2rct(pubkey)); |
| 197 | } |
| 198 | } |
| 199 | wallets[i].encrypt_keys(""); |
| 200 | } |
| 201 | |
| 202 | // final key via sums should equal the wallets' public spend key |
| 203 | wallets[0].decrypt_keys(""); |
| 204 | EXPECT_EQ(wallets[0].get_account().get_keys().m_account_address.m_spend_public_key, rct::rct2pk(composite_pubkey)); |
| 205 | wallets[0].encrypt_keys(""); |
| 206 | } |
| 207 | |
| 208 | static void make_wallets(const unsigned int M, const unsigned int N, const bool force_update) |
| 209 | { |
| 210 | std::vector<tools::wallet2> wallets(N); |
| 211 | ASSERT_TRUE(wallets.size() > 1 && wallets.size() <= KEYS_COUNT); |
| 212 | ASSERT_TRUE(M <= wallets.size()); |
| 213 | std::uint32_t total_rounds_required = multisig::multisig_setup_rounds_required(wallets.size(), M); |
| 214 | std::uint32_t rounds_complete{0}; |
| 215 | |
| 216 | // initialize wallets, get first round multisig kex msgs |
| 217 | std::vector<std::string> initial_infos(wallets.size()); |
| 218 | |
| 219 | for (size_t i = 0; i < wallets.size(); ++i) |
| 220 | { |
| 221 | make_wallet(i, wallets[i]); |
| 222 | |
| 223 | wallets[i].decrypt_keys(""); |
| 224 | initial_infos[i] = wallets[i].get_multisig_first_kex_msg(); |
| 225 | wallets[i].encrypt_keys(""); |
| 226 | } |
| 227 | |
| 228 | // wallets should not be multisig yet |
| 229 | for (const auto &wallet: wallets) |
| 230 | { |
| 231 | ASSERT_FALSE(wallet.multisig()); |
| 232 | } |
| 233 | |
| 234 | // make wallets multisig, get second round kex messages (if appropriate) |
| 235 | std::vector<std::string> intermediate_infos(wallets.size()); |
| 236 | |
| 237 | for (size_t i = 0; i < wallets.size(); ++i) |
| 238 | { |
| 239 | intermediate_infos[i] = wallets[i].make_multisig("", initial_infos, M); |
| 240 | } |
| 241 | |
| 242 | ++rounds_complete; |
| 243 | |
| 244 | // perform kex rounds until kex is complete |
| 245 | bool ready; |
| 246 | wallets[0].multisig(&ready); |
| 247 | while (!ready) |
| 248 | { |
| 249 | if (force_update) |
| 250 | intermediate_infos = exchange_round_force_update(wallets, intermediate_infos, rounds_complete + 1); |
| 251 | else |
| 252 | intermediate_infos = exchange_round(wallets, intermediate_infos); |
| 253 | |
| 254 | wallets[0].multisig(&ready); |
| 255 | ++rounds_complete; |
| 256 | } |
| 257 | |
| 258 | EXPECT_EQ(total_rounds_required, rounds_complete); |
| 259 | |
| 260 | check_results(intermediate_infos, wallets, M); |
| 261 | } |
| 262 | |
| 263 | TEST(multisig, make_1_2) |
| 264 | { |
| 265 | make_wallets(1, 2, false); |
| 266 | make_wallets(1, 2, true); |
| 267 | } |
| 268 | |
| 269 | TEST(multisig, make_1_3) |
| 270 | { |
| 271 | make_wallets(1, 3, false); |
| 272 | make_wallets(1, 3, true); |
| 273 | } |
| 274 | |
| 275 | TEST(multisig, make_2_2) |
| 276 | { |
| 277 | make_wallets(2, 2, false); |
| 278 | make_wallets(2, 2, true); |
| 279 | } |
| 280 | |
| 281 | TEST(multisig, make_3_3) |
| 282 | { |
| 283 | make_wallets(3, 3, false); |
| 284 | make_wallets(3, 3, true); |
| 285 | } |
| 286 | |
| 287 | TEST(multisig, make_2_3) |
| 288 | { |
| 289 | make_wallets(2, 3, false); |
| 290 | make_wallets(2, 3, true); |
| 291 | } |
| 292 | |
| 293 | TEST(multisig, make_2_4) |
| 294 | { |
| 295 | make_wallets(2, 4, false); |
| 296 | make_wallets(2, 4, true); |
| 297 | } |
| 298 | |
| 299 | TEST(multisig, multisig_kex_msg) |
| 300 | { |
| 301 | using namespace multisig; |
| 302 | |
| 303 | crypto::public_key pubkey1; |
| 304 | crypto::public_key pubkey2; |
| 305 | crypto::public_key pubkey3; |
| 306 | crypto::secret_key_to_public_key(rct::rct2sk(rct::skGen()), pubkey1); |
| 307 | crypto::secret_key_to_public_key(rct::rct2sk(rct::skGen()), pubkey2); |
| 308 | crypto::secret_key_to_public_key(rct::rct2sk(rct::skGen()), pubkey3); |
| 309 | |
| 310 | crypto::secret_key signing_skey = rct::rct2sk(rct::skGen()); |
| 311 | crypto::public_key signing_pubkey; |
| 312 | while(!crypto::secret_key_to_public_key(signing_skey, signing_pubkey)) |
| 313 | { |
| 314 | signing_skey = rct::rct2sk(rct::skGen()); |
| 315 | } |
| 316 | |
| 317 | const crypto::secret_key ancillary_skey{rct::rct2sk(rct::skGen())}; |
| 318 | |
| 319 | // misc. edge cases |
| 320 | EXPECT_NO_THROW((multisig_kex_msg{})); |
| 321 | EXPECT_ANY_THROW((multisig_kex_msg{multisig_kex_msg{}.get_msg()})); |
| 322 | EXPECT_ANY_THROW((multisig_kex_msg{"abc"})); |
| 323 | EXPECT_ANY_THROW((multisig_kex_msg{0, crypto::null_skey, std::vector<crypto::public_key>{}, crypto::null_skey})); |
| 324 | EXPECT_ANY_THROW((multisig_kex_msg{1, crypto::null_skey, std::vector<crypto::public_key>{}, crypto::null_skey})); |
| 325 | EXPECT_ANY_THROW((multisig_kex_msg{1, signing_skey, std::vector<crypto::public_key>{}, crypto::null_skey})); |
| 326 | EXPECT_ANY_THROW((multisig_kex_msg{1, crypto::null_skey, std::vector<crypto::public_key>{}, ancillary_skey})); |
| 327 | |
| 328 | // test that messages are both constructible and reversible |
| 329 | |
| 330 | // round 1 |
| 331 | EXPECT_NO_THROW((multisig_kex_msg{ |
| 332 | multisig_kex_msg{1, signing_skey, std::vector<crypto::public_key>{}, ancillary_skey}.get_msg() |
| 333 | })); |
| 334 | EXPECT_NO_THROW((multisig_kex_msg{ |
| 335 | multisig_kex_msg{1, signing_skey, std::vector<crypto::public_key>{pubkey1}, ancillary_skey}.get_msg() |
| 336 | })); |
| 337 | |
| 338 | // round 2 |
| 339 | EXPECT_NO_THROW((multisig_kex_msg{ |
| 340 | multisig_kex_msg{2, signing_skey, std::vector<crypto::public_key>{pubkey1}, ancillary_skey}.get_msg() |
| 341 | })); |
| 342 | EXPECT_NO_THROW((multisig_kex_msg{ |
| 343 | multisig_kex_msg{2, signing_skey, std::vector<crypto::public_key>{pubkey1}, crypto::null_skey}.get_msg() |
| 344 | })); |
| 345 | EXPECT_NO_THROW((multisig_kex_msg{ |
| 346 | multisig_kex_msg{2, signing_skey, std::vector<crypto::public_key>{pubkey1, pubkey2}, ancillary_skey}.get_msg() |
| 347 | })); |
| 348 | EXPECT_NO_THROW((multisig_kex_msg{ |
| 349 | multisig_kex_msg{2, signing_skey, std::vector<crypto::public_key>{pubkey1, pubkey2, pubkey3}, crypto::null_skey}.get_msg() |
| 350 | })); |
| 351 | |
| 352 | // test that keys can be recovered if stored in a message and the message's reverse |
| 353 | |
| 354 | // round 1 |
| 355 | const multisig_kex_msg msg_rnd1{1, signing_skey, std::vector<crypto::public_key>{pubkey1}, ancillary_skey}; |
| 356 | const multisig_kex_msg msg_rnd1_reverse{msg_rnd1.get_msg()}; |
| 357 | EXPECT_EQ(msg_rnd1.get_round(), 1); |
| 358 | EXPECT_EQ(msg_rnd1.get_round(), msg_rnd1_reverse.get_round()); |
| 359 | EXPECT_EQ(msg_rnd1.get_signing_pubkey(), signing_pubkey); |
| 360 | EXPECT_EQ(msg_rnd1.get_signing_pubkey(), msg_rnd1_reverse.get_signing_pubkey()); |
| 361 | EXPECT_EQ(msg_rnd1.get_msg_pubkeys().size(), 0); |
| 362 | EXPECT_EQ(msg_rnd1.get_msg_pubkeys().size(), msg_rnd1_reverse.get_msg_pubkeys().size()); |
| 363 | EXPECT_EQ(msg_rnd1.get_msg_privkey(), ancillary_skey); |
| 364 | EXPECT_EQ(msg_rnd1.get_msg_privkey(), msg_rnd1_reverse.get_msg_privkey()); |
| 365 | |
| 366 | // round 2 |
| 367 | const multisig_kex_msg msg_rnd2{2, signing_skey, std::vector<crypto::public_key>{pubkey1, pubkey2}, ancillary_skey}; |
| 368 | const multisig_kex_msg msg_rnd2_reverse{msg_rnd2.get_msg()}; |
| 369 | EXPECT_EQ(msg_rnd2.get_round(), 2); |
| 370 | EXPECT_EQ(msg_rnd2.get_round(), msg_rnd2_reverse.get_round()); |
| 371 | EXPECT_EQ(msg_rnd2.get_signing_pubkey(), signing_pubkey); |
| 372 | EXPECT_EQ(msg_rnd2.get_signing_pubkey(), msg_rnd2_reverse.get_signing_pubkey()); |
| 373 | ASSERT_EQ(msg_rnd2.get_msg_pubkeys().size(), 2); |
| 374 | ASSERT_EQ(msg_rnd2.get_msg_pubkeys().size(), msg_rnd2_reverse.get_msg_pubkeys().size()); |
| 375 | EXPECT_EQ(msg_rnd2.get_msg_pubkeys()[0], pubkey1); |
| 376 | EXPECT_EQ(msg_rnd2.get_msg_pubkeys()[1], pubkey2); |
| 377 | EXPECT_EQ(msg_rnd2.get_msg_pubkeys()[0], msg_rnd2_reverse.get_msg_pubkeys()[0]); |
| 378 | EXPECT_EQ(msg_rnd2.get_msg_pubkeys()[1], msg_rnd2_reverse.get_msg_pubkeys()[1]); |
| 379 | EXPECT_EQ(msg_rnd2.get_msg_privkey(), crypto::null_skey); |
| 380 | EXPECT_EQ(msg_rnd2.get_msg_privkey(), msg_rnd2_reverse.get_msg_privkey()); |
| 381 | } |