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 }
72 
73 namespace hw {
74  namespace {
75  //device funcion not supported
76  #define dfns() \
77  throw std::runtime_error(std::string("device function not supported: ")+ std::string(__FUNCTION__) + \
78  std::string(" (device.hpp line ")+std::to_string(__LINE__)+std::string(").")); \
79  return false;
80  }
81 
82  class device {
83  protected:
84  std::string name;
85 
86  public:
87 
88  device() {}
89  device(const device &hwdev) {}
90  virtual ~device() {}
91 
92  explicit virtual operator bool() const = 0;
93  enum device_mode {
98  };
100  {
101  SOFTWARE = 0,
102  LEDGER = 1
103  };
104 
105 
106  /* ======================================================================= */
107  /* SETUP/TEARDOWN */
108  /* ======================================================================= */
109  virtual bool set_name(const std::string &name) = 0;
110  virtual const std::string get_name() const = 0;
111 
112  virtual bool init(void) = 0;
113  virtual bool release() = 0;
114 
115  virtual bool connect(void) = 0;
116  virtual bool disconnect(void) = 0;
117 
118  virtual bool set_mode(device_mode mode) = 0;
119 
120  virtual device_type get_type() const = 0;
121 
122 
123  /* ======================================================================= */
124  /* LOCKER */
125  /* ======================================================================= */
126  virtual void lock(void) = 0;
127  virtual void unlock(void) = 0;
128  virtual bool try_lock(void) = 0;
129 
130 
131  /* ======================================================================= */
132  /* WALLET & ADDRESS */
133  /* ======================================================================= */
134  virtual bool get_public_address(cryptonote::account_public_address &pubkey) = 0;
135  virtual bool get_secret_keys(crypto::secret_key &viewkey , crypto::secret_key &spendkey) = 0;
136  virtual bool generate_chacha_key(const cryptonote::account_keys &keys, crypto::chacha_key &key, uint64_t kdf_rounds) = 0;
137 
138  /* ======================================================================= */
139  /* SUB ADDRESS */
140  /* ======================================================================= */
141  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;
143  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;
146 
147  /* ======================================================================= */
148  /* DERIVATION & KEY */
149  /* ======================================================================= */
150  virtual bool verify_keys(const crypto::secret_key &secret_key, const crypto::public_key &public_key) = 0;
151  virtual bool scalarmultKey(rct::key & aP, const rct::key &P, const rct::key &a) = 0;
152  virtual bool scalarmultBase(rct::key &aG, const rct::key &a) = 0;
153  virtual bool sc_secret_add( crypto::secret_key &r, const crypto::secret_key &a, const crypto::secret_key &b) = 0;
154  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;
155  virtual bool generate_key_derivation(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_derivation &derivation) = 0;
156  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;
157  virtual bool derivation_to_scalar(const crypto::key_derivation &derivation, const size_t output_index, crypto::ec_scalar &res) = 0;
158  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;
159  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;
160  virtual bool secret_key_to_public_key(const crypto::secret_key &sec, crypto::public_key &pub) = 0;
161  virtual bool generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image) = 0;
162 
163  // alternative prototypes available in libringct
165  {
166  rct::key aP;
167  scalarmultKey(aP, P, a);
168  return aP;
169  }
170 
172  {
173  rct::key aG;
174  scalarmultBase(aG, a);
175  return aG;
176  }
177 
178  /* ======================================================================= */
179  /* TRANSACTION */
180  /* ======================================================================= */
181 
182  virtual bool open_tx(crypto::secret_key &tx_key) = 0;
183 
184  virtual bool encrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key) = 0;
186  {
187  // Encryption and decryption are the same operation (xor with a key)
188  return encrypt_payment_id(payment_id, public_key, secret_key);
189  }
190 
191  virtual bool ecdhEncode(rct::ecdhTuple & unmasked, const rct::key & sharedSec) = 0;
192  virtual bool ecdhDecode(rct::ecdhTuple & masked, const rct::key & sharedSec) = 0;
193 
194  virtual bool add_output_key_mapping(const crypto::public_key &Aout, const crypto::public_key &Bout, const bool is_subaddress, const size_t real_output_index,
195  const rct::key &amount_key, const crypto::public_key &out_eph_public_key) = 0;
196 
197 
198  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;
199  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;
200  virtual bool mlsag_prepare(rct::key &a, rct::key &aG) = 0;
201  virtual bool mlsag_hash(const rct::keyV &long_message, rct::key &c) = 0;
202  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;
203 
204  virtual bool close_tx(void) = 0;
205  } ;
206 
207  struct reset_mode {
209  reset_mode(hw::device& dev) : hwref(dev) { }
211  };
212 
214  private:
215  std::map<std::string, std::unique_ptr<device>> registry;
216 
217  public:
218  device_registry();
219  bool register_device(const std::string & device_name, device * hw_device);
220  device& get_device(const std::string & device_descriptor);
221  };
222 
223  device& get_device(const std::string & device_descriptor);
224  bool register_device(const std::string & device_name, device * hw_device);
225 }
226 
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:431
Definition: device.hpp:96
virtual bool verify_keys(const crypto::secret_key &secret_key, const crypto::public_key &public_key)=0
device_registry()
Definition: device.cpp:46
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
device()
Definition: device.hpp:88
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
device_mode
Definition: device.hpp:93
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:90
virtual void lock(void)=0
Definition: rctTypes.h:125
bool register_device(const std::string &device_name, device *hw_device)
Definition: device.cpp:53
device & get_device(const std::string &device_descriptor)
Definition: device.cpp:63
virtual void unlock(void)=0
virtual bool ecdhDecode(rct::ecdhTuple &masked, const rct::key &sharedSec)=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:208
std::vector< key > keyV
Definition: rctTypes.h:88
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:224
virtual bool add_output_key_mapping(const crypto::public_key &Aout, const crypto::public_key &Bout, const bool is_subaddress, const size_t real_output_index, const rct::key &amount_key, const crypto::public_key &out_eph_public_key)=0
std::string name
Definition: device.hpp:84
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:101
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:99
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:82
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:164
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:185
Definition: subaddress_index.h:38
Definition: device.hpp:213
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:95
Definition: device.hpp:82
virtual cryptonote::account_public_address get_subaddress(const cryptonote::account_keys &keys, const cryptonote::subaddress_index &index)=0
virtual ~device()
Definition: device.hpp:90
Definition: account.h:40
Definition: device.hpp:97
~reset_mode()
Definition: device.hpp:210
virtual bool close_tx(void)=0
POD_CLASS public_key
Definition: crypto.h:63
rct::key scalarmultBase(const rct::key &a)
Definition: device.hpp:171
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:209
string a
Definition: MakeCryptoOps.py:15
int bool
Definition: stdbool.h:35
virtual bool release()=0
virtual crypto::secret_key get_subaddress_secret_key(const crypto::secret_key &sec, const cryptonote::subaddress_index &index)=0
Definition: device.hpp:94
Definition: device.hpp:207
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:89
virtual const std::string get_name() const =0
Definition: device.hpp:102
virtual bool try_lock(void)=0
virtual bool init(void)=0
virtual bool ecdhEncode(rct::ecdhTuple &unmasked, const rct::key &sharedSec)=0
std::map< std::string, std::unique_ptr< device > > registry
Definition: device.hpp:215
int rows
Definition: crypto.h:73