| 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 | |
| 31 | #pragma once |
| 32 | |
| 33 | #include <cstddef> |
| 34 | #include <string> |
| 35 | #include "device.hpp" |
| 36 | #include "log.hpp" |
| 37 | #include "device_io_hid.hpp" |
| 38 | #include <boost/thread/mutex.hpp> |
| 39 | #include <boost/thread/recursive_mutex.hpp> |
| 40 | |
| 41 | namespace hw { |
| 42 | |
| 43 | namespace ledger { |
| 44 | |
| 45 | /* Minimal supported version */ |
| 46 | #define MINIMAL_APP_VERSION_MAJOR 1 |
| 47 | #define MINIMAL_APP_VERSION_MINOR 8 |
| 48 | #define MINIMAL_APP_VERSION_MICRO 0 |
| 49 | |
| 50 | #define VERSION(M,m,u) ((M)<<16|(m)<<8|(u)) |
| 51 | #define VERSION_MAJOR(v) (((v)>>16)&0xFF) |
| 52 | #define VERSION_MINOR(v) (((v)>>8)&0xFF) |
| 53 | #define VERSION_MICRO(v) (((v)>>0)&0xFF) |
| 54 | |
| 55 | #define MINIMAL_APP_VERSION VERSION(MINIMAL_APP_VERSION_MAJOR, MINIMAL_APP_VERSION_MINOR, MINIMAL_APP_VERSION_MICRO) |
| 56 | |
| 57 | void register_all(std::map<std::string, std::unique_ptr<device>> ®istry); |
| 58 | |
| 59 | #ifdef WITH_DEVICE_LEDGER |
| 60 | |
| 61 | // Origin: https://github.com/LedgerHQ/ledger-app-monero/blob/master/src/monero_types.h |
| 62 | #define SW_OK 0x9000 |
| 63 | #define SW_WRONG_LENGTH 0x6700 |
| 64 | #define SW_SECURITY_PIN_LOCKED 0x6910 |
| 65 | #define SW_SECURITY_LOAD_KEY 0x6911 |
| 66 | #define SW_SECURITY_COMMITMENT_CONTROL 0x6912 |
| 67 | #define SW_SECURITY_AMOUNT_CHAIN_CONTROL 0x6913 |
| 68 | #define SW_SECURITY_COMMITMENT_CHAIN_CONTROL 0x6914 |
| 69 | #define SW_SECURITY_OUTKEYS_CHAIN_CONTROL 0x6915 |
| 70 | #define SW_SECURITY_MAXOUTPUT_REACHED 0x6916 |
| 71 | #define SW_SECURITY_HMAC 0x6917 |
| 72 | #define SW_SECURITY_RANGE_VALUE 0x6918 |
| 73 | #define SW_SECURITY_INTERNAL 0x6919 |
| 74 | #define SW_SECURITY_MAX_SIGNATURE_REACHED 0x691A |
| 75 | #define SW_SECURITY_PREFIX_HASH 0x691B |
| 76 | #define SW_SECURITY_LOCKED 0x69EE |
| 77 | #define SW_COMMAND_NOT_ALLOWED 0x6980 |
| 78 | #define SW_SUBCOMMAND_NOT_ALLOWED 0x6981 |
| 79 | #define SW_DENY 0x6982 |
| 80 | #define SW_KEY_NOT_SET 0x6983 |
| 81 | #define SW_WRONG_DATA 0x6984 |
| 82 | #define SW_WRONG_DATA_RANGE 0x6985 |
| 83 | #define SW_IO_FULL 0x6986 |
| 84 | #define SW_CLIENT_NOT_SUPPORTED 0x6A30 |
| 85 | #define SW_WRONG_P1P2 0x6b00 |
| 86 | #define SW_INS_NOT_SUPPORTED 0x6d00 |
| 87 | #define SW_PROTOCOL_NOT_SUPPORTED 0x6e00 |
| 88 | #define SW_UNKNOWN 0x6f00 |
| 89 | |
| 90 | namespace { |
| 91 | bool apdu_verbose =true; |
| 92 | } |
| 93 | |
| 94 | void set_apdu_verbose(bool verbose); |
| 95 | |
| 96 | class ABPkeys { |
| 97 | public: |
| 98 | rct::key Aout; |
| 99 | rct::key Bout; |
| 100 | bool is_subaddress; |
| 101 | bool is_change_address; |
| 102 | bool additional_key ; |
| 103 | size_t index; |
| 104 | rct::key Pout; |
| 105 | rct::key AKout; |
| 106 | ABPkeys(const rct::key& A, const rct::key& B, const bool is_subaddr, bool is_subaddress, bool is_change_address, size_t index, const rct::key& P,const rct::key& AK); |
| 107 | ABPkeys(const ABPkeys& keys) ; |
| 108 | ABPkeys() {index=0;is_subaddress=false;is_change_address=false;additional_key=false;} |
| 109 | ABPkeys &operator=(const ABPkeys &keys); |
| 110 | }; |
| 111 | |
| 112 | class Keymap { |
| 113 | public: |
| 114 | std::vector<ABPkeys> ABP; |
| 115 | |
| 116 | bool find(const rct::key& P, ABPkeys& keys) const; |
| 117 | void add(const ABPkeys& keys); |
| 118 | void clear(); |
| 119 | void log(); |
| 120 | }; |
| 121 | |
| 122 | class SecHMAC { |
| 123 | public: |
| 124 | uint32_t sec[32]; |
| 125 | uint32_t hmac[32]; |
| 126 | |
| 127 | SecHMAC(const uint8_t s[32], const uint8_t m[32]); |
| 128 | |
| 129 | }; |
| 130 | |
| 131 | class HMACmap { |
| 132 | public: |
| 133 | std::vector<SecHMAC> hmacs; |
| 134 | |
| 135 | void find_mac(const uint8_t sec[32], uint8_t hmac[32]) ; |
| 136 | void add_mac(const uint8_t sec[32], const uint8_t hmac[32]) ; |
| 137 | void clear() ; |
| 138 | }; |
| 139 | |
| 140 | |
| 141 | #define BUFFER_SEND_SIZE 262 |
| 142 | #define BUFFER_RECV_SIZE 262 |
| 143 | |
| 144 | class device_ledger : public hw::device { |
| 145 | private: |
| 146 | // Locker for concurrent access |
| 147 | mutable boost::recursive_mutex device_locker; |
| 148 | mutable boost::mutex command_locker; |
| 149 | |
| 150 | //IO |
| 151 | hw::io::device_io_hid hw_device; |
| 152 | unsigned int length_send; |
| 153 | unsigned char buffer_send[BUFFER_SEND_SIZE]; |
| 154 | unsigned int length_recv; |
| 155 | unsigned char buffer_recv[BUFFER_RECV_SIZE]; |
| 156 | unsigned int sw; |
| 157 | unsigned int id; |
| 158 | void logCMD(void); |
| 159 | void logRESP(void); |
| 160 | unsigned int exchange(unsigned int ok=SW_OK, unsigned int mask=0xFFFF); |
| 161 | unsigned int exchange_wait_on_input(unsigned int ok=SW_OK, unsigned int mask=0xFFFF); |
| 162 | void reset_buffer(void); |
| 163 | int set_command_header(unsigned char ins, unsigned char p1 = 0x00, unsigned char p2 = 0x00); |
| 164 | int set_command_header_noopt(unsigned char ins, unsigned char p1 = 0x00, unsigned char p2 = 0x00); |
| 165 | void send_simple(unsigned char ins, unsigned char p1 = 0x00); |
| 166 | void send_secret(const unsigned char sec[32], int &offset); |
| 167 | void receive_secret(unsigned char sec[32], int &offset); |
| 168 | |
| 169 | bool tx_in_progress; |
| 170 | |
| 171 | // map public destination key to ephemeral destination key |
| 172 | Keymap key_map; |
| 173 | bool add_output_key_mapping(const crypto::public_key &Aout, const crypto::public_key &Bout, const bool is_subaddress, const bool is_change, |
| 174 | const bool need_additional, const size_t real_output_index, |
| 175 | const rct::key &amount_key, const crypto::public_key &out_eph_public_key); |
| 176 | //hmac for some encrypted value |
| 177 | HMACmap hmac_map; |
| 178 | |
| 179 | // To speed up blockchain parsing the view key maybe handle here. |
| 180 | crypto::secret_key viewkey = crypto::null_skey; |
| 181 | bool has_view_key = false; |
| 182 | |
| 183 | device *controle_device; |
| 184 | |
| 185 | public: |
| 186 | device_ledger(); |
| 187 | ~device_ledger(); |
| 188 | |
| 189 | device_ledger(const device_ledger &device) = delete ; |
| 190 | device_ledger& operator=(const device_ledger &device) = delete; |
| 191 | |
| 192 | explicit operator bool() const override {return this->connected(); } |
| 193 | |
| 194 | bool reset(void); |
| 195 | |
| 196 | /* ======================================================================= */ |
| 197 | /* SETUP/TEARDOWN */ |
| 198 | /* ======================================================================= */ |
| 199 | bool set_name(const std::string &name) override; |
| 200 | |
| 201 | const std::string get_name() const override; |
| 202 | bool init(void) override; |
| 203 | bool release() override; |
| 204 | bool connect(void) override; |
| 205 | bool disconnect() override; |
| 206 | bool connected(void) const; |
| 207 | |
| 208 | bool set_mode(device_mode mode) override; |
| 209 | |
| 210 | device_type get_type() const override {return device_type::LEDGER;}; |
| 211 | device_protocol_t device_protocol() const override { return PROTOCOL_PROXY; }; |
| 212 | |
| 213 | /* ======================================================================= */ |
| 214 | /* LOCKER */ |
| 215 | /* ======================================================================= */ |
| 216 | void lock(void) override; |
| 217 | void unlock(void) override; |
| 218 | bool try_lock(void) override; |
| 219 | |
| 220 | /* ======================================================================= */ |
| 221 | /* WALLET & ADDRESS */ |
| 222 | /* ======================================================================= */ |
| 223 | bool get_public_address(cryptonote::account_public_address &pubkey) override; |
| 224 | bool get_secret_keys(crypto::secret_key &viewkey , crypto::secret_key &spendkey) override; |
| 225 | bool generate_chacha_key(const cryptonote::account_keys &keys, crypto::chacha_key &key, uint64_t kdf_rounds) override; |
| 226 | void display_address(const cryptonote::subaddress_index& index, const boost::optional<crypto::hash8> &payment_id) override; |
| 227 | |
| 228 | /* ======================================================================= */ |
| 229 | /* SUB ADDRESS */ |
| 230 | /* ======================================================================= */ |
| 231 | bool derive_subaddress_public_key(const crypto::public_key &pub, const crypto::key_derivation &derivation, const std::size_t output_index, crypto::public_key &derived_pub) override; |
| 232 | crypto::public_key get_subaddress_spend_public_key(const cryptonote::account_keys& keys, const cryptonote::subaddress_index& index) override; |
| 233 | std::vector<crypto::public_key> get_subaddress_spend_public_keys(const cryptonote::account_keys &keys, uint32_t account, uint32_t begin, uint32_t end) override; |
| 234 | cryptonote::account_public_address get_subaddress(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) override; |
| 235 | crypto::secret_key get_subaddress_secret_key(const crypto::secret_key &sec, const cryptonote::subaddress_index &index) override; |
| 236 | |
| 237 | /* ======================================================================= */ |
| 238 | /* DERIVATION & KEY */ |
| 239 | /* ======================================================================= */ |
| 240 | bool verify_keys(const crypto::secret_key &secret_key, const crypto::public_key &public_key) override; |
| 241 | bool scalarmultKey(rct::key & aP, const rct::key &P, const rct::key &a) override; |
| 242 | bool scalarmultBase(rct::key &aG, const rct::key &a) override; |
| 243 | bool sc_secret_add(crypto::secret_key &r, const crypto::secret_key &a, const crypto::secret_key &b) override; |
| 244 | crypto::secret_key generate_keys(crypto::public_key &pub, crypto::secret_key &sec, const crypto::secret_key& recovery_key = crypto::secret_key(), bool recover = false) override; |
| 245 | bool generate_key_derivation(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_derivation &derivation) override; |
| 246 | bool conceal_derivation(crypto::key_derivation &derivation, const crypto::public_key &tx_pub_key, const std::vector<crypto::public_key> &additional_tx_pub_keys, const crypto::key_derivation &main_derivation, const std::vector<crypto::key_derivation> &additional_derivations) override; |
| 247 | bool derivation_to_scalar(const crypto::key_derivation &derivation, const size_t output_index, crypto::ec_scalar &res) override; |
| 248 | bool derive_secret_key(const crypto::key_derivation &derivation, const std::size_t output_index, const crypto::secret_key &sec, crypto::secret_key &derived_sec) override; |
| 249 | bool derive_public_key(const crypto::key_derivation &derivation, const std::size_t output_index, const crypto::public_key &pub, crypto::public_key &derived_pub) override; |
| 250 | bool secret_key_to_public_key(const crypto::secret_key &sec, crypto::public_key &pub) override; |
| 251 | bool generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image) override; |
| 252 | bool derive_view_tag(const crypto::key_derivation &derivation, const size_t output_index, crypto::view_tag &view_tag) override; |
| 253 | |
| 254 | /* ======================================================================= */ |
| 255 | /* TRANSACTION */ |
| 256 | /* ======================================================================= */ |
| 257 | void generate_tx_proof(const crypto::hash &prefix_hash, |
| 258 | const crypto::public_key &R, const crypto::public_key &A, const boost::optional<crypto::public_key> &B, const crypto::public_key &D, const crypto::secret_key &r, |
| 259 | crypto::signature &sig) override; |
| 260 | |
| 261 | bool open_tx(crypto::secret_key &tx_key) override; |
| 262 | |
| 263 | void get_transaction_prefix_hash(const cryptonote::transaction_prefix& tx, crypto::hash& h) override; |
| 264 | |
| 265 | bool encrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key) override; |
| 266 | |
| 267 | rct::key genCommitmentMask(const rct::key &amount_key) override; |
| 268 | |
| 269 | bool ecdhEncode(rct::ecdhTuple & unmasked, const rct::key & sharedSec, bool short_format) override; |
| 270 | bool ecdhDecode(rct::ecdhTuple & masked, const rct::key & sharedSec, bool short_format) override; |
| 271 | |
| 272 | bool generate_output_ephemeral_keys(const size_t tx_version, const cryptonote::account_keys &sender_account_keys, const crypto::public_key &txkey_pub, const crypto::secret_key &tx_key, |
| 273 | const cryptonote::tx_destination_entry &dst_entr, const boost::optional<cryptonote::account_public_address> &change_addr, const size_t output_index, |
| 274 | const bool &need_additional_txkeys, const std::vector<crypto::secret_key> &additional_tx_keys, |
| 275 | std::vector<crypto::public_key> &additional_tx_public_keys, |
| 276 | std::vector<rct::key> &amount_keys, |
| 277 | crypto::public_key &out_eph_public_key, |
| 278 | const bool use_view_tags, crypto::view_tag &view_tag) override; |
| 279 | |
| 280 | bool mlsag_prehash(const std::string &blob, size_t inputs_size, size_t outputs_size, const rct::keyV &hashes, const rct::ctkeyV &outPk, rct::key &prehash) override; |
| 281 | bool mlsag_prepare(const rct::key &H, const rct::key &xx, rct::key &a, rct::key &aG, rct::key &aHP, rct::key &rvII) override; |
| 282 | bool mlsag_prepare(rct::key &a, rct::key &aG) override; |
| 283 | bool mlsag_hash(const rct::keyV &long_message, rct::key &c) override; |
| 284 | bool mlsag_sign( const rct::key &c, const rct::keyV &xx, const rct::keyV &alpha, const size_t rows, const size_t dsRows, rct::keyV &ss) override; |
| 285 | |
| 286 | bool clsag_prepare(const rct::key &p, const rct::key &z, rct::key &I, rct::key &D, const rct::key &H, rct::key &a, rct::key &aG, rct::key &aH) override; |
| 287 | bool clsag_hash(const rct::keyV &data, rct::key &hash) override; |
| 288 | bool clsag_sign(const rct::key &c, const rct::key &a, const rct::key &p, const rct::key &z, const rct::key &mu_P, const rct::key &mu_C, rct::key &s) override; |
| 289 | |
| 290 | |
| 291 | bool close_tx(void) override; |
| 292 | |
| 293 | }; |
| 294 | |
| 295 | |
| 296 | #ifdef DEBUG_HWDEVICE |
| 297 | extern crypto::secret_key dbg_viewkey; |
| 298 | extern crypto::secret_key dbg_spendkey; |
| 299 | #endif |
| 300 | |
| 301 | #endif //WITH_DEVICE_LEDGER |
| 302 | } |
| 303 | |
| 304 | } |
| 305 |