| 1 | // Copyright (c) 2021-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 "multisig_account.h" |
| 30 | |
| 31 | #include "crypto/crypto.h" |
| 32 | #include "cryptonote_config.h" |
| 33 | #include "include_base_utils.h" |
| 34 | #include "multisig.h" |
| 35 | #include "multisig_kex_msg.h" |
| 36 | #include "ringct/rctOps.h" |
| 37 | #include "ringct/rctTypes.h" |
| 38 | |
| 39 | #include <cstdint> |
| 40 | #include <utility> |
| 41 | #include <vector> |
| 42 | |
| 43 | |
| 44 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 45 | #define MONERO_DEFAULT_LOG_CATEGORY "multisig" |
| 46 | |
| 47 | namespace multisig |
| 48 | { |
| 49 | //---------------------------------------------------------------------------------------------------------------------- |
| 50 | // multisig_account: EXTERNAL |
| 51 | //---------------------------------------------------------------------------------------------------------------------- |
| 52 | multisig_account::multisig_account(const crypto::secret_key &base_privkey, |
| 53 | const crypto::secret_key &base_common_privkey) : |
| 54 | m_base_privkey{base_privkey}, |
| 55 | m_base_common_privkey{base_common_privkey}, |
| 56 | m_multisig_pubkey{rct::rct2pk(rct::identity())}, |
| 57 | m_common_pubkey{rct::rct2pk(rct::identity())}, |
| 58 | m_kex_rounds_complete{0}, |
| 59 | m_next_round_kex_message{multisig_kex_msg{1, base_privkey, std::vector<crypto::public_key>{}, base_common_privkey}.get_msg()} |
| 60 | { |
| 61 | CHECK_AND_ASSERT_THROW_MES(crypto::secret_key_to_public_key(m_base_privkey, m_base_pubkey), |
| 62 | "Failed to derive public key"); |
| 63 | } |
| 64 | //---------------------------------------------------------------------------------------------------------------------- |
| 65 | // multisig_account: EXTERNAL |
| 66 | //---------------------------------------------------------------------------------------------------------------------- |
| 67 | multisig_account::multisig_account(const std::uint32_t threshold, |
| 68 | std::vector<crypto::public_key> signers, |
| 69 | const crypto::secret_key &base_privkey, |
| 70 | const crypto::secret_key &base_common_privkey, |
| 71 | std::vector<crypto::secret_key> multisig_privkeys, |
| 72 | const crypto::secret_key &common_privkey, |
| 73 | const crypto::public_key &multisig_pubkey, |
| 74 | const crypto::public_key &common_pubkey, |
| 75 | const std::uint32_t kex_rounds_complete, |
| 76 | multisig_keyset_map_memsafe_t kex_origins_map, |
| 77 | std::string next_round_kex_message) : |
| 78 | m_base_privkey{base_privkey}, |
| 79 | m_base_common_privkey{base_common_privkey}, |
| 80 | m_multisig_privkeys{std::move(multisig_privkeys)}, |
| 81 | m_common_privkey{common_privkey}, |
| 82 | m_multisig_pubkey{multisig_pubkey}, |
| 83 | m_common_pubkey{common_pubkey}, |
| 84 | m_kex_rounds_complete{kex_rounds_complete}, |
| 85 | m_kex_keys_to_origins_map{std::move(kex_origins_map)}, |
| 86 | m_next_round_kex_message{std::move(next_round_kex_message)} |
| 87 | { |
| 88 | CHECK_AND_ASSERT_THROW_MES(kex_rounds_complete > 0, "multisig account: can't reconstruct account if its kex wasn't initialized"); |
| 89 | CHECK_AND_ASSERT_THROW_MES(crypto::secret_key_to_public_key(m_base_privkey, m_base_pubkey), |
| 90 | "Failed to derive public key"); |
| 91 | set_multisig_config(threshold, std::move(signers)); |
| 92 | |
| 93 | // kex rounds should not exceed post-kex verification round |
| 94 | const std::uint32_t kex_rounds_required{multisig_kex_rounds_required(m_signers.size(), m_threshold)}; |
| 95 | CHECK_AND_ASSERT_THROW_MES(m_kex_rounds_complete <= kex_rounds_required + 1, |
| 96 | "multisig account: tried to reconstruct account, but kex rounds complete counter is invalid."); |
| 97 | |
| 98 | // once an account is done with kex, the 'next kex msg' is always the post-kex verification message |
| 99 | // i.e. the multisig account pubkey signed by the signer's privkey AND the common pubkey |
| 100 | if (main_kex_rounds_done()) |
| 101 | { |
| 102 | m_next_round_kex_message = multisig_kex_msg{kex_rounds_required + 1, |
| 103 | m_base_privkey, |
| 104 | std::vector<crypto::public_key>{m_multisig_pubkey, m_common_pubkey}}.get_msg(); |
| 105 | } |
| 106 | } |
| 107 | //---------------------------------------------------------------------------------------------------------------------- |
| 108 | // multisig_account: EXTERNAL |
| 109 | //---------------------------------------------------------------------------------------------------------------------- |
| 110 | bool multisig_account::account_is_active() const |
| 111 | { |
| 112 | return m_kex_rounds_complete > 0; |
| 113 | } |
| 114 | //---------------------------------------------------------------------------------------------------------------------- |
| 115 | // multisig_account: EXTERNAL |
| 116 | //---------------------------------------------------------------------------------------------------------------------- |
| 117 | bool multisig_account::main_kex_rounds_done() const |
| 118 | { |
| 119 | if (account_is_active()) |
| 120 | return m_kex_rounds_complete >= multisig_kex_rounds_required(m_signers.size(), m_threshold); |
| 121 | else |
| 122 | return false; |
| 123 | } |
| 124 | //---------------------------------------------------------------------------------------------------------------------- |
| 125 | // multisig_account: EXTERNAL |
| 126 | //---------------------------------------------------------------------------------------------------------------------- |
| 127 | bool multisig_account::multisig_is_ready() const |
| 128 | { |
| 129 | if (main_kex_rounds_done()) |
| 130 | return m_kex_rounds_complete >= multisig_setup_rounds_required(m_signers.size(), m_threshold); |
| 131 | else |
| 132 | return false; |
| 133 | } |
| 134 | //---------------------------------------------------------------------------------------------------------------------- |
| 135 | // multisig_account: INTERNAL |
| 136 | //---------------------------------------------------------------------------------------------------------------------- |
| 137 | void multisig_account::set_multisig_config(const std::size_t threshold, std::vector<crypto::public_key> signers) |
| 138 | { |
| 139 | // validate |
| 140 | CHECK_AND_ASSERT_THROW_MES(threshold > 0 && threshold <= signers.size(), "multisig account: tried to set invalid threshold."); |
| 141 | CHECK_AND_ASSERT_THROW_MES(signers.size() >= 2 && signers.size() <= config::MULTISIG_MAX_SIGNERS, |
| 142 | "multisig account: tried to set invalid number of signers."); |
| 143 | |
| 144 | for (auto signer_it = signers.begin(); signer_it != signers.end(); ++signer_it) |
| 145 | { |
| 146 | // signer pubkeys must be in main subgroup, and not identity |
| 147 | CHECK_AND_ASSERT_THROW_MES(rct::isInMainSubgroup(rct::pk2rct(*signer_it)) && !(*signer_it == rct::rct2pk(rct::identity())), |
| 148 | "multisig account: tried to set signers, but a signer pubkey is invalid."); |
| 149 | } |
| 150 | |
| 151 | // own pubkey should be in signers list |
| 152 | CHECK_AND_ASSERT_THROW_MES(std::find(signers.begin(), signers.end(), m_base_pubkey) != signers.end(), |
| 153 | "multisig account: tried to set signers, but did not find the account's base pubkey in signer list."); |
| 154 | |
| 155 | // sort signers |
| 156 | std::sort(signers.begin(), signers.end()); |
| 157 | |
| 158 | // signers should all be unique |
| 159 | CHECK_AND_ASSERT_THROW_MES(std::adjacent_find(signers.begin(), signers.end()) == signers.end(), |
| 160 | "multisig account: tried to set signers, but there are duplicate signers unexpectedly."); |
| 161 | |
| 162 | // set |
| 163 | m_threshold = threshold; |
| 164 | m_signers = std::move(signers); |
| 165 | } |
| 166 | //---------------------------------------------------------------------------------------------------------------------- |
| 167 | // multisig_account: EXTERNAL |
| 168 | //---------------------------------------------------------------------------------------------------------------------- |
| 169 | void multisig_account::initialize_kex(const std::uint32_t threshold, |
| 170 | std::vector<crypto::public_key> signers, |
| 171 | const std::vector<multisig_kex_msg> &expanded_msgs_rnd1) |
| 172 | { |
| 173 | CHECK_AND_ASSERT_THROW_MES(!account_is_active(), "multisig account: tried to initialize kex, but already initialized"); |
| 174 | |
| 175 | // only mutate account if update succeeds |
| 176 | multisig_account temp_account{*this}; |
| 177 | temp_account.set_multisig_config(threshold, std::move(signers)); |
| 178 | temp_account.kex_update_impl(expanded_msgs_rnd1, false); |
| 179 | *this = std::move(temp_account); |
| 180 | } |
| 181 | //---------------------------------------------------------------------------------------------------------------------- |
| 182 | // multisig_account: EXTERNAL |
| 183 | //---------------------------------------------------------------------------------------------------------------------- |
| 184 | void multisig_account::kex_update(const std::vector<multisig_kex_msg> &expanded_msgs, |
| 185 | const bool force_update_use_with_caution /*= false*/) |
| 186 | { |
| 187 | CHECK_AND_ASSERT_THROW_MES(account_is_active(), "multisig account: tried to update kex, but kex isn't initialized yet."); |
| 188 | CHECK_AND_ASSERT_THROW_MES(!multisig_is_ready(), "multisig account: tried to update kex, but kex is already complete."); |
| 189 | |
| 190 | multisig_account temp_account{*this}; |
| 191 | temp_account.kex_update_impl(expanded_msgs, force_update_use_with_caution); |
| 192 | *this = std::move(temp_account); |
| 193 | } |
| 194 | //---------------------------------------------------------------------------------------------------------------------- |
| 195 | // EXTERNAL |
| 196 | //---------------------------------------------------------------------------------------------------------------------- |
| 197 | std::uint32_t multisig_kex_rounds_required(const std::uint32_t num_signers, const std::uint32_t threshold) |
| 198 | { |
| 199 | CHECK_AND_ASSERT_THROW_MES(num_signers >= threshold, "num_signers must be >= threshold"); |
| 200 | CHECK_AND_ASSERT_THROW_MES(threshold >= 1, "threshold must be >= 1"); |
| 201 | return num_signers - threshold + 1; |
| 202 | } |
| 203 | //---------------------------------------------------------------------------------------------------------------------- |
| 204 | // EXTERNAL |
| 205 | //---------------------------------------------------------------------------------------------------------------------- |
| 206 | std::uint32_t multisig_setup_rounds_required(const std::uint32_t num_signers, const std::uint32_t threshold) |
| 207 | { |
| 208 | return multisig_kex_rounds_required(num_signers, threshold) + 1; |
| 209 | } |
| 210 | //---------------------------------------------------------------------------------------------------------------------- |
| 211 | } //namespace multisig |