Monero
device.hpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2018, 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 /* Note about debug:
32  * To debug Device you can def the following :
33  * #define DEBUG_HWDEVICE
34  * Activate debug mechanism:
35  * - Add more trace
36  * - All computation done by device are checked by default device.
37  * Required IODUMMYCRYPT_HWDEVICE or IONOCRYPT_HWDEVICE for fully working
38  * #define IODUMMYCRYPT_HWDEVICE 1
39  * - It assumes sensitive data encryption is is off on device side. a XOR with 0x55. This allow Ledger Class to make check on clear value
40  * #define IONOCRYPT_HWDEVICE 1
41  * - It assumes sensitive data encryption is off on device side.
42  */
43 
44 
45 #pragma once
46 
47 #include "crypto/crypto.h"
48 #include "crypto/chacha.h"
49 #include "ringct/rctTypes.h"
50 
51 
52 #ifndef USE_DEVICE_LEDGER
53 #define USE_DEVICE_LEDGER 1
54 #endif
55 
56 #if !defined(HAVE_HIDAPI)
57 #undef USE_DEVICE_LEDGER
58 #define USE_DEVICE_LEDGER 0
59 #endif
60 
61 #if USE_DEVICE_LEDGER
62 #define WITH_DEVICE_LEDGER
63 #endif
64 
65 // forward declaration needed because this header is included by headers in libcryptonote_basic which depends on libdevice
66 namespace cryptonote
67 {
68  struct account_public_address;
69  struct account_keys;
70  struct subaddress_index;
71  struct tx_destination_entry;
72 }
73 
74 namespace hw {
75  namespace {
76  //device funcion not supported
77  #define dfns() \
78  throw std::runtime_error(std::string("device function not supported: ")+ std::string(__FUNCTION__) + \
79  std::string(" (device.hpp line ")+std::to_string(__LINE__)+std::string(").")); \
80  return false;
81  }
82 
83  class device {
84  protected:
85  std::string name;
86 
87  public:
88 
89  device() {}
90  device(const device &hwdev) {}
91  virtual ~device() {}
92 
93  explicit virtual operator bool() const = 0;
94  enum device_mode {
99  };
101  {
102  SOFTWARE = 0,
103  LEDGER = 1
104  };
105 
106 
107  /* ======================================================================= */
108  /* SETUP/TEARDOWN */
109  /* ======================================================================= */
110  virtual bool set_name(const std::string &name) = 0;
111  virtual const std::string get_name() const = 0;
112 
113  virtual bool init(void) = 0;
114  virtual bool release() = 0;
115 
116  virtual bool connect(void) = 0;
117  virtual bool disconnect(void) = 0;
118 
119  virtual bool set_mode(device_mode mode) = 0;
120 
121  virtual device_type get_type() const = 0;
122 
123 
124  /* ======================================================================= */
125  /* LOCKER */
126  /* ======================================================================= */
127  virtual void lock(void) = 0;
128  virtual void unlock(void) = 0;
129  virtual bool try_lock(void) = 0;
130 
131 
132  /* ======================================================================= */
133  /* WALLET & ADDRESS */
134  /* ======================================================================= */
135  virtual bool get_public_address(cryptonote::account_public_address &pubkey) = 0;
136  virtual bool get_secret_keys(crypto::secret_key &viewkey , crypto::secret_key &spendkey) = 0;
137  virtual bool generate_chacha_key(const cryptonote::account_keys &keys, crypto::chacha_key &key, uint64_t kdf_rounds) = 0;
138 
139  /* ======================================================================= */
140  /* SUB ADDRESS */
141  /* ======================================================================= */
142  virtual 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) = 0;
144  virtual std::vector<crypto::public_key> get_subaddress_spend_public_keys(const cryptonote::account_keys &keys, uint32_t account, uint32_t begin, uint32_t end) = 0;
147 
148  /* ======================================================================= */
149  /* DERIVATION & KEY */
150  /* ======================================================================= */
151  virtual bool verify_keys(const crypto::secret_key &secret_key, const crypto::public_key &public_key) = 0;
152  virtual bool scalarmultKey(rct::key & aP, const rct::key &P, const rct::key &a) = 0;
153  virtual bool scalarmultBase(rct::key &aG, const rct::key &a) = 0;
154  virtual bool sc_secret_add( crypto::secret_key &r, const crypto::secret_key &a, const crypto::secret_key &b) = 0;
155  virtual 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) = 0;
156  virtual bool generate_key_derivation(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_derivation &derivation) = 0;
157  virtual 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) = 0;
158  virtual bool derivation_to_scalar(const crypto::key_derivation &derivation, const size_t output_index, crypto::ec_scalar &res) = 0;
159  virtual 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) = 0;
160  virtual 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) = 0;
161  virtual bool secret_key_to_public_key(const crypto::secret_key &sec, crypto::public_key &pub) = 0;
162  virtual bool generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image) = 0;
163 
164  // alternative prototypes available in libringct
166  {
167  rct::key aP;
168  scalarmultKey(aP, P, a);
169  return aP;
170  }
171 
173  {
174  rct::key aG;
175  scalarmultBase(aG, a);
176  return aG;
177  }
178 
179  /* ======================================================================= */
180  /* TRANSACTION */
181  /* ======================================================================= */
182 
183  virtual bool open_tx(crypto::secret_key &tx_key) = 0;
184 
185  virtual bool encrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key) = 0;
187  {
188  // Encryption and decryption are the same operation (xor with a key)
189  return encrypt_payment_id(payment_id, public_key, secret_key);
190  }
191 
192  virtual rct::key genCommitmentMask(const rct::key &amount_key) = 0;
193 
194  virtual bool ecdhEncode(rct::ecdhTuple & unmasked, const rct::key & sharedSec, bool short_amount) = 0;
195  virtual bool ecdhDecode(rct::ecdhTuple & masked, const rct::key & sharedSec, bool short_amount) = 0;
196 
197  virtual 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,
198  const cryptonote::tx_destination_entry &dst_entr, const boost::optional<cryptonote::account_public_address> &change_addr, const size_t output_index,
199  const bool &need_additional_txkeys, const std::vector<crypto::secret_key> &additional_tx_keys,
200  std::vector<crypto::public_key> &additional_tx_public_keys,
201  std::vector<rct::key> &amount_keys,
202  crypto::public_key &out_eph_public_key) = 0;
203 
204  virtual 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) = 0;
205  virtual bool mlsag_prepare(const rct::key &H, const rct::key &xx, rct::key &a, rct::key &aG, rct::key &aHP, rct::key &rvII) = 0;
206  virtual bool mlsag_prepare(rct::key &a, rct::key &aG) = 0;
207  virtual bool mlsag_hash(const rct::keyV &long_message, rct::key &c) = 0;
208  virtual 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) = 0;
209 
210  virtual bool close_tx(void) = 0;
211  } ;
212 
213  struct reset_mode {
215  reset_mode(hw::device& dev) : hwref(dev) { }
217  };
218 
220  private:
221  std::map<std::string, std::unique_ptr<device>> registry;
222 
223  public:
224  device_registry();
225  bool register_device(const std::string & device_name, device * hw_device);
226  device& get_device(const std::string & device_descriptor);
227  };
228 
229  device& get_device(const std::string & device_descriptor);
230  bool register_device(const std::string & device_name, device * hw_device);
231 }
232 
virtual device_type get_type() const =0
virtual bool encrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key)=0
virtual bool secret_key_to_public_key(const crypto::secret_key &sec, crypto::public_key &pub)=0
virtual bool scalarmultKey(rct::key &aP, const rct::key &P, const rct::key &a)=0
static const key H
Definition: rctTypes.h:451
Definition: device.hpp:97
virtual bool verify_keys(const crypto::secret_key &secret_key, const crypto::public_key &public_key)=0
device_registry()
Definition: device.cpp:58
virtual bool generate_chacha_key(const cryptonote::account_keys &keys, crypto::chacha_key &key, uint64_t kdf_rounds)=0
virtual bool sc_secret_add(crypto::secret_key &r, const crypto::secret_key &a, const crypto::secret_key &b)=0
virtual rct::key genCommitmentMask(const rct::key &amount_key)=0
device()
Definition: device.hpp:89
POD_CLASS key_derivation
Definition: crypto.h:85
virtual bool mlsag_prepare(const rct::key &H, const rct::key &xx, rct::key &a, rct::key &aG, rct::key &aHP, rct::key &rvII)=0
virtual bool ecdhEncode(rct::ecdhTuple &unmasked, const rct::key &sharedSec, bool short_amount)=0
device_mode
Definition: device.hpp:94
epee::mlocked< tools::scrubbed< ec_scalar > > secret_key
Definition: crypto.h:69
bool register_device(const std::string &device_name, device *hw_device)
Definition: device.cpp:100
virtual void lock(void)=0
Definition: rctTypes.h:125
bool register_device(const std::string &device_name, device *hw_device)
Definition: device.cpp:66
device & get_device(const std::string &device_descriptor)
Definition: device.cpp:76
virtual void unlock(void)=0
virtual bool set_name(const std::string &name)=0
virtual bool disconnect(void)=0
virtual bool generate_key_derivation(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_derivation &derivation)=0
device & hwref
Definition: device.hpp:214
std::vector< key > keyV
Definition: rctTypes.h:88
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:224
std::string name
Definition: device.hpp:85
virtual 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)=0
Definition: device.hpp:102
virtual 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)=0
virtual 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)=0
device_type
Definition: device.hpp:100
POD_CLASS ec_scalar
Definition: crypto.h:61
virtual bool get_public_address(cryptonote::account_public_address &pubkey)=0
device & get_device(const std::string &device_descriptor)
Definition: device.cpp:95
std::vector< ctkey > ctkeyV
Definition: rctTypes.h:100
Definition: rctTypes.h:78
rct::key scalarmultKey(const rct::key &P, const rct::key &a)
Definition: device.hpp:165
virtual 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)=0
int b
Definition: base.py:1
bool decrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key)
Definition: device.hpp:186
Definition: subaddress_index.h:38
virtual 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, const cryptonote::tx_destination_entry &dst_entr, const boost::optional< cryptonote::account_public_address > &change_addr, const size_t output_index, const bool &need_additional_txkeys, const std::vector< crypto::secret_key > &additional_tx_keys, std::vector< crypto::public_key > &additional_tx_public_keys, std::vector< rct::key > &amount_keys, crypto::public_key &out_eph_public_key)=0
Definition: device.hpp:219
virtual bool generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image)=0
Definition: device.cpp:38
virtual bool open_tx(crypto::secret_key &tx_key)=0
virtual 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)=0
Definition: device.hpp:96
Definition: device.hpp:83
virtual cryptonote::account_public_address get_subaddress(const cryptonote::account_keys &keys, const cryptonote::subaddress_index &index)=0
virtual ~device()
Definition: device.hpp:91
Definition: account.h:40
Definition: device.hpp:98
~reset_mode()
Definition: device.hpp:216
virtual bool close_tx(void)=0
POD_CLASS public_key
Definition: crypto.h:63
rct::key scalarmultBase(const rct::key &a)
Definition: device.hpp:172
virtual 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)=0
virtual bool mlsag_hash(const rct::keyV &long_message, rct::key &c)=0
virtual std::vector< crypto::public_key > get_subaddress_spend_public_keys(const cryptonote::account_keys &keys, uint32_t account, uint32_t begin, uint32_t end)=0
Definition: cryptonote_basic.h:400
POD_CLASS hash8
Definition: hash.h:52
virtual bool get_secret_keys(crypto::secret_key &viewkey, crypto::secret_key &spendkey)=0
virtual bool scalarmultBase(rct::key &aG, const rct::key &a)=0
POD_CLASS key_image
Definition: crypto.h:89
virtual bool derivation_to_scalar(const crypto::key_derivation &derivation, const size_t output_index, crypto::ec_scalar &res)=0
reset_mode(hw::device &dev)
Definition: device.hpp:215
string a
Definition: MakeCryptoOps.py:15
int bool
Definition: stdbool.h:35
virtual bool release()=0
Definition: cryptonote_tx_utils.h:74
virtual crypto::secret_key get_subaddress_secret_key(const crypto::secret_key &sec, const cryptonote::subaddress_index &index)=0
Definition: device.hpp:95
Definition: device.hpp:213
virtual bool set_mode(device_mode mode)=0
virtual crypto::public_key get_subaddress_spend_public_key(const cryptonote::account_keys &keys, const cryptonote::subaddress_index &index)=0
virtual 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)=0
virtual bool connect(void)=0
device(const device &hwdev)
Definition: device.hpp:90
virtual const std::string get_name() const =0
Definition: device.hpp:103
virtual bool try_lock(void)=0
virtual bool init(void)=0
std::map< std::string, std::unique_ptr< device > > registry
Definition: device.hpp:221
int rows
Definition: crypto.h:73
virtual bool ecdhDecode(rct::ecdhTuple &masked, const rct::key &sharedSec, bool short_amount)=0