| 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_kex_msg.h" |
| 30 | #include "multisig_kex_msg_serialization.h" |
| 31 | |
| 32 | #include "common/base58.h" |
| 33 | #include "crypto/crypto.h" |
| 34 | extern "C" |
| 35 | { |
| 36 | #include "crypto/crypto-ops.h" |
| 37 | } |
| 38 | #include "cryptonote_basic/cryptonote_format_utils.h" |
| 39 | #include "include_base_utils.h" |
| 40 | #include "ringct/rctOps.h" |
| 41 | #include "serialization/binary_archive.h" |
| 42 | #include "serialization/serialization.h" |
| 43 | |
| 44 | #include <boost/utility/string_ref.hpp> |
| 45 | |
| 46 | #include <sstream> |
| 47 | #include <utility> |
| 48 | #include <vector> |
| 49 | |
| 50 | |
| 51 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 52 | #define MONERO_DEFAULT_LOG_CATEGORY "multisig" |
| 53 | |
| 54 | const boost::string_ref MULTISIG_KEX_V1_MAGIC{"MultisigV1"}; |
| 55 | const boost::string_ref MULTISIG_KEX_MSG_V1_MAGIC{"MultisigxV1"}; |
| 56 | const boost::string_ref MULTISIG_KEX_MSG_V2_MAGIC_1{"MultisigxV2R1"}; //round 1 |
| 57 | const boost::string_ref MULTISIG_KEX_MSG_V2_MAGIC_N{"MultisigxV2Rn"}; //round n > 1 |
| 58 | |
| 59 | namespace multisig |
| 60 | { |
| 61 | //---------------------------------------------------------------------------------------------------------------------- |
| 62 | // multisig_kex_msg: EXTERNAL |
| 63 | //---------------------------------------------------------------------------------------------------------------------- |
| 64 | multisig_kex_msg::multisig_kex_msg(const std::uint32_t round, |
| 65 | const crypto::secret_key &signing_privkey, |
| 66 | std::vector<crypto::public_key> msg_pubkeys, |
| 67 | const crypto::secret_key &msg_privkey) : |
| 68 | m_kex_round{round} |
| 69 | { |
| 70 | CHECK_AND_ASSERT_THROW_MES(round > 0, "Kex round must be > 0."); |
| 71 | CHECK_AND_ASSERT_THROW_MES(sc_check((const unsigned char*)&signing_privkey) == 0 && |
| 72 | signing_privkey != crypto::null_skey, "Invalid msg signing key."); |
| 73 | |
| 74 | if (round == 1) |
| 75 | { |
| 76 | CHECK_AND_ASSERT_THROW_MES(sc_check((const unsigned char*)&msg_privkey) == 0 && |
| 77 | msg_privkey != crypto::null_skey, "Invalid msg privkey."); |
| 78 | |
| 79 | m_msg_privkey = msg_privkey; |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | for (const auto &pubkey : msg_pubkeys) |
| 84 | { |
| 85 | CHECK_AND_ASSERT_THROW_MES(pubkey != crypto::null_pkey && pubkey != rct::rct2pk(rct::identity()), |
| 86 | "Pubkey for message was invalid."); |
| 87 | CHECK_AND_ASSERT_THROW_MES((rct::scalarmultKey(rct::pk2rct(pubkey), rct::curveOrder()) == rct::identity()), |
| 88 | "Pubkey for message was not in prime subgroup."); |
| 89 | } |
| 90 | |
| 91 | m_msg_pubkeys = std::move(msg_pubkeys); |
| 92 | } |
| 93 | CHECK_AND_ASSERT_THROW_MES(crypto::secret_key_to_public_key(signing_privkey, m_signing_pubkey), |
| 94 | "Failed to derive public key"); |
| 95 | |
| 96 | // sets message and signing pub key |
| 97 | construct_msg(signing_privkey); |
| 98 | } |
| 99 | //---------------------------------------------------------------------------------------------------------------------- |
| 100 | // multisig_kex_msg: EXTERNAL |
| 101 | //---------------------------------------------------------------------------------------------------------------------- |
| 102 | multisig_kex_msg::multisig_kex_msg(std::string msg) : m_msg{std::move(msg)} |
| 103 | { |
| 104 | parse_and_validate_msg(); |
| 105 | } |
| 106 | //---------------------------------------------------------------------------------------------------------------------- |
| 107 | // multisig_kex_msg: INTERNAL |
| 108 | //---------------------------------------------------------------------------------------------------------------------- |
| 109 | crypto::hash multisig_kex_msg::get_msg_to_sign() const |
| 110 | { |
| 111 | //// |
| 112 | // msg_content = kex_round | signing_pubkey | expand(msg_pubkeys) | OPTIONAL msg_privkey |
| 113 | // sign_msg = versioning-domain-sep | msg_content |
| 114 | /// |
| 115 | |
| 116 | std::string data; |
| 117 | CHECK_AND_ASSERT_THROW_MES(MULTISIG_KEX_MSG_V2_MAGIC_1.size() == MULTISIG_KEX_MSG_V2_MAGIC_N.size(), |
| 118 | "Multisig kex msg magic inconsistency."); |
| 119 | data.reserve(MULTISIG_KEX_MSG_V2_MAGIC_1.size() + 4 + 32*(1 + (m_kex_round == 1 ? 1 : 0) + m_msg_pubkeys.size())); |
| 120 | |
| 121 | // versioning domain-sep |
| 122 | if (m_kex_round == 1) |
| 123 | data.append(MULTISIG_KEX_MSG_V2_MAGIC_1.data(), MULTISIG_KEX_MSG_V2_MAGIC_1.size()); |
| 124 | else |
| 125 | data.append(MULTISIG_KEX_MSG_V2_MAGIC_N.data(), MULTISIG_KEX_MSG_V2_MAGIC_N.size()); |
| 126 | |
| 127 | // kex_round as little-endian bytes |
| 128 | for (std::size_t i{0}; i < 4; ++i) |
| 129 | { |
| 130 | data += static_cast<char>(m_kex_round >> i*8); |
| 131 | } |
| 132 | |
| 133 | // signing pubkey |
| 134 | data.append((const char *)&m_signing_pubkey, sizeof(crypto::public_key)); |
| 135 | |
| 136 | // add msg privkey if kex_round == 1 |
| 137 | if (m_kex_round == 1) |
| 138 | data.append((const char *)&m_msg_privkey, sizeof(crypto::secret_key)); |
| 139 | else |
| 140 | { |
| 141 | // only add pubkeys if not round 1 |
| 142 | |
| 143 | // msg pubkeys |
| 144 | for (const auto &key : m_msg_pubkeys) |
| 145 | data.append((const char *)&key, sizeof(crypto::public_key)); |
| 146 | } |
| 147 | |
| 148 | // message to sign |
| 149 | crypto::hash hash; |
| 150 | crypto::cn_fast_hash(data.data(), data.size(), hash); |
| 151 | |
| 152 | return hash; |
| 153 | } |
| 154 | //---------------------------------------------------------------------------------------------------------------------- |
| 155 | // multisig_kex_msg: INTERNAL |
| 156 | //---------------------------------------------------------------------------------------------------------------------- |
| 157 | void multisig_kex_msg::construct_msg(const crypto::secret_key &signing_privkey) |
| 158 | { |
| 159 | //// |
| 160 | // msg_content = kex_round | signing_pubkey | expand(msg_pubkeys) | OPTIONAL msg_privkey |
| 161 | // sign_msg = versioning-domain-sep | msg_content |
| 162 | // msg = versioning-domain-sep | serialize(msg_content | crypto_sig[signing_privkey](sign_msg)) |
| 163 | /// |
| 164 | |
| 165 | // sign the message |
| 166 | crypto::signature msg_signature; |
| 167 | crypto::hash msg_to_sign{get_msg_to_sign()}; |
| 168 | crypto::generate_signature(msg_to_sign, m_signing_pubkey, signing_privkey, msg_signature); |
| 169 | |
| 170 | // assemble the message |
| 171 | m_msg.clear(); |
| 172 | |
| 173 | std::stringstream serialized_msg_ss; |
| 174 | binary_archive<true> b_archive(serialized_msg_ss); |
| 175 | |
| 176 | if (m_kex_round == 1) |
| 177 | { |
| 178 | m_msg.append(MULTISIG_KEX_MSG_V2_MAGIC_1.data(), MULTISIG_KEX_MSG_V2_MAGIC_1.size()); |
| 179 | |
| 180 | multisig_kex_msg_serializable_round1 msg_serializable; |
| 181 | msg_serializable.msg_privkey = m_msg_privkey; |
| 182 | msg_serializable.signing_pubkey = m_signing_pubkey; |
| 183 | msg_serializable.signature = msg_signature; |
| 184 | |
| 185 | CHECK_AND_ASSERT_THROW_MES(::serialization::serialize(b_archive, msg_serializable), |
| 186 | "Failed to serialize multisig kex msg"); |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | m_msg.append(MULTISIG_KEX_MSG_V2_MAGIC_N.data(), MULTISIG_KEX_MSG_V2_MAGIC_N.size()); |
| 191 | |
| 192 | multisig_kex_msg_serializable_general msg_serializable; |
| 193 | msg_serializable.kex_round = m_kex_round; |
| 194 | msg_serializable.msg_pubkeys = m_msg_pubkeys; |
| 195 | msg_serializable.signing_pubkey = m_signing_pubkey; |
| 196 | msg_serializable.signature = msg_signature; |
| 197 | |
| 198 | CHECK_AND_ASSERT_THROW_MES(::serialization::serialize(b_archive, msg_serializable), |
| 199 | "Failed to serialize multisig kex msg"); |
| 200 | } |
| 201 | |
| 202 | m_msg.append(tools::base58::encode(serialized_msg_ss.str())); |
| 203 | } |
| 204 | //---------------------------------------------------------------------------------------------------------------------- |
| 205 | // multisig_kex_msg: INTERNAL |
| 206 | //---------------------------------------------------------------------------------------------------------------------- |
| 207 | void multisig_kex_msg::parse_and_validate_msg() |
| 208 | { |
| 209 | CHECK_AND_ASSERT_THROW_MES(MULTISIG_KEX_MSG_V2_MAGIC_1.size() == MULTISIG_KEX_MSG_V2_MAGIC_N.size(), |
| 210 | "Multisig kex msg magic inconsistency."); |
| 211 | CHECK_AND_ASSERT_THROW_MES(MULTISIG_KEX_MSG_V2_MAGIC_1.size() >= MULTISIG_KEX_V1_MAGIC.size(), |
| 212 | "Multisig kex msg magic inconsistency."); |
| 213 | |
| 214 | // check message type |
| 215 | CHECK_AND_ASSERT_THROW_MES(m_msg.size() >= MULTISIG_KEX_MSG_V2_MAGIC_1.size(), "Kex message unexpectedly small."); |
| 216 | CHECK_AND_ASSERT_THROW_MES(m_msg.substr(0, MULTISIG_KEX_V1_MAGIC.size()) != MULTISIG_KEX_V1_MAGIC, |
| 217 | "V1 multisig kex messages are deprecated (unsafe)."); |
| 218 | CHECK_AND_ASSERT_THROW_MES(m_msg.substr(0, MULTISIG_KEX_MSG_V1_MAGIC.size()) != MULTISIG_KEX_MSG_V1_MAGIC, |
| 219 | "V1 multisig kex messages are deprecated (unsafe)."); |
| 220 | |
| 221 | // deserialize the message |
| 222 | std::string msg_no_magic; |
| 223 | CHECK_AND_ASSERT_THROW_MES(tools::base58::decode(m_msg.substr(MULTISIG_KEX_MSG_V2_MAGIC_1.size()), msg_no_magic), |
| 224 | "Multisig kex msg decoding error."); |
| 225 | binary_archive<false> b_archive{epee::strspan<std::uint8_t>(msg_no_magic)}; |
| 226 | crypto::signature msg_signature; |
| 227 | |
| 228 | if (m_msg.substr(0, MULTISIG_KEX_MSG_V2_MAGIC_1.size()) == MULTISIG_KEX_MSG_V2_MAGIC_1) |
| 229 | { |
| 230 | // try round 1 message |
| 231 | multisig_kex_msg_serializable_round1 kex_msg_rnd1; |
| 232 | |
| 233 | if (::serialization::serialize(b_archive, kex_msg_rnd1)) |
| 234 | { |
| 235 | // in round 1 the message stores a private ancillary key component for the multisig account |
| 236 | // that will be shared by all participants (e.g. a shared private view key) |
| 237 | m_kex_round = 1; |
| 238 | m_msg_privkey = kex_msg_rnd1.msg_privkey; |
| 239 | m_signing_pubkey = kex_msg_rnd1.signing_pubkey; |
| 240 | msg_signature = kex_msg_rnd1.signature; |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | CHECK_AND_ASSERT_THROW_MES(false, "Deserializing kex msg failed."); |
| 245 | } |
| 246 | } |
| 247 | else if (m_msg.substr(0, MULTISIG_KEX_MSG_V2_MAGIC_N.size()) == MULTISIG_KEX_MSG_V2_MAGIC_N) |
| 248 | { |
| 249 | // try general message |
| 250 | multisig_kex_msg_serializable_general kex_msg_general; |
| 251 | |
| 252 | if (::serialization::serialize(b_archive, kex_msg_general)) |
| 253 | { |
| 254 | m_kex_round = kex_msg_general.kex_round; |
| 255 | m_msg_privkey = crypto::null_skey; |
| 256 | m_msg_pubkeys = std::move(kex_msg_general.msg_pubkeys); |
| 257 | m_signing_pubkey = kex_msg_general.signing_pubkey; |
| 258 | msg_signature = kex_msg_general.signature; |
| 259 | |
| 260 | CHECK_AND_ASSERT_THROW_MES(m_kex_round > 1, "Invalid kex message round (must be > 1 for the general msg type)."); |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | CHECK_AND_ASSERT_THROW_MES(false, "Deserializing kex msg failed."); |
| 265 | } |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | // unknown message type |
| 270 | CHECK_AND_ASSERT_THROW_MES(false, "Only v2 multisig kex messages are supported."); |
| 271 | } |
| 272 | |
| 273 | // checks |
| 274 | for (const auto &pubkey: m_msg_pubkeys) |
| 275 | { |
| 276 | CHECK_AND_ASSERT_THROW_MES(pubkey != crypto::null_pkey && pubkey != rct::rct2pk(rct::identity()), |
| 277 | "Pubkey from message was invalid."); |
| 278 | CHECK_AND_ASSERT_THROW_MES(rct::isInMainSubgroup(rct::pk2rct(pubkey)), |
| 279 | "Pubkey from message was not in prime subgroup."); |
| 280 | } |
| 281 | |
| 282 | CHECK_AND_ASSERT_THROW_MES(m_signing_pubkey != crypto::null_pkey && m_signing_pubkey != rct::rct2pk(rct::identity()), |
| 283 | "Message signing key was invalid."); |
| 284 | CHECK_AND_ASSERT_THROW_MES(rct::isInMainSubgroup(rct::pk2rct(m_signing_pubkey)), |
| 285 | "Message signing key was not in prime subgroup."); |
| 286 | |
| 287 | // validate signature |
| 288 | crypto::hash signed_msg{get_msg_to_sign()}; |
| 289 | CHECK_AND_ASSERT_THROW_MES(crypto::check_signature(signed_msg, m_signing_pubkey, msg_signature), |
| 290 | "Multisig kex msg signature invalid."); |
| 291 | } |
| 292 | //---------------------------------------------------------------------------------------------------------------------- |
| 293 | } //namespace multisig |