Debian Codegraph @

src/device/device_default.cpp

raw symbols SCIP
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
32
33 #include "device_default.hpp"
34 #include "int-util.h"
35 #include "crypto/wallet/crypto.h"
36 #include "cryptonote_basic/account.h"
37 #include "cryptonote_basic/subaddress_index.h"
38 #include "cryptonote_core/cryptonote_tx_utils.h"
39 #include "ringct/rctOps.h"
40 #include "cryptonote_config.h"
41
42 namespace hw {
43
44 namespace core {
45
46 device_default::device_default() { }
47
48 device_default::~device_default() { }
49
50 /* ===================================================================== */
51 /* === Misc ==== */
52 /* ===================================================================== */
53 static inline unsigned char *operator &(crypto::ec_scalar &scalar) {
54 return &reinterpret_cast<unsigned char &>(scalar);
55 }
56 static inline const unsigned char *operator &(const crypto::ec_scalar &scalar) {
57 return &reinterpret_cast<const unsigned char &>(scalar);
58 }
59
60 /* ======================================================================= */
61 /* SETUP/TEARDOWN */
62 /* ======================================================================= */
63 bool device_default::set_name(const std::string &name) {
64 this->name = name;
65 return true;
66 }
67 const std::string device_default::get_name() const {
68 return this->name;
69 }
70
71 bool device_default::init(void) {
72 return true;
73 }
74 bool device_default::release() {
75 return true;
76 }
77
78 bool device_default::connect(void) {
79 return true;
80 }
81 bool device_default::disconnect() {
82 return true;
83 }
84
85 bool device_default::set_mode(device_mode mode) {
86 return device::set_mode(mode);
87 }
88
89 /* ======================================================================= */
90 /* LOCKER */
91 /* ======================================================================= */
92
93 void device_default::lock() { }
94
95 bool device_default::try_lock() { return true; }
96
97 void device_default::unlock() { }
98
99 /* ======================================================================= */
100 /* WALLET & ADDRESS */
101 /* ======================================================================= */
102
103 bool device_default::generate_chacha_key(const cryptonote::account_keys &keys, crypto::chacha_key &key, uint64_t kdf_rounds) {
104 const crypto::secret_key &view_key = keys.m_view_secret_key;
105 const crypto::secret_key &spend_key = keys.m_spend_secret_key;
106 epee::mlocked<tools::scrubbed_arr<char, sizeof(view_key) + sizeof(spend_key) + 1>> data;
107 memcpy(data.data(), &view_key, sizeof(view_key));
108 memcpy(data.data() + sizeof(view_key), &spend_key, sizeof(spend_key));
109 data[sizeof(data) - 1] = config::HASH_KEY_WALLET;
110 crypto::generate_chacha_key(data.data(), sizeof(data), key, kdf_rounds);
111 return true;
112 }
113 bool device_default::get_public_address(cryptonote::account_public_address &pubkey) {
114 dfns();
115 }
116 bool device_default::get_secret_keys(crypto::secret_key &viewkey , crypto::secret_key &spendkey) {
117 dfns();
118 }
119 /* ======================================================================= */
120 /* SUB ADDRESS */
121 /* ======================================================================= */
122
123 bool device_default::derive_subaddress_public_key(const crypto::public_key &out_key, const crypto::key_derivation &derivation, const std::size_t output_index, crypto::public_key &derived_key) {
124 return crypto::wallet::derive_subaddress_public_key(out_key, derivation, output_index,derived_key);
125 }
126
127 crypto::public_key device_default::get_subaddress_spend_public_key(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) {
128 if (index.is_zero())
129 return keys.m_account_address.m_spend_public_key;
130
131 // m = Hs(a || index_major || index_minor)
132 crypto::secret_key m = get_subaddress_secret_key(keys.m_view_secret_key, index);
133
134 // M = m*G
135 crypto::public_key M;
136 crypto::secret_key_to_public_key(m, M);
137
138 // D = B + M
139 crypto::public_key D = rct::rct2pk(rct::addKeys(rct::pk2rct(keys.m_account_address.m_spend_public_key), rct::pk2rct(M)));
140 return D;
141 }
142
143 std::vector<crypto::public_key> device_default::get_subaddress_spend_public_keys(const cryptonote::account_keys &keys, uint32_t account, uint32_t begin, uint32_t end) {
144 CHECK_AND_ASSERT_THROW_MES(begin <= end, "begin > end");
145
146 std::vector<crypto::public_key> pkeys;
147 pkeys.reserve(end - begin);
148 cryptonote::subaddress_index index = {account, begin};
149
150 ge_p3 p3;
151 ge_cached cached;
152 CHECK_AND_ASSERT_THROW_MES(ge_frombytes_vartime(&p3, (const unsigned char*)keys.m_account_address.m_spend_public_key.data) == 0,
153 "ge_frombytes_vartime failed to convert spend public key");
154 ge_p3_to_cached(&cached, &p3);
155
156 for (uint32_t idx = begin; idx < end; ++idx)
157 {
158 index.minor = idx;
159 if (index.is_zero())
160 {
161 pkeys.push_back(keys.m_account_address.m_spend_public_key);
162 continue;
163 }
164 crypto::secret_key m = get_subaddress_secret_key(keys.m_view_secret_key, index);
165
166 // M = m*G
167 ge_scalarmult_base(&p3, (const unsigned char*)m.data);
168
169 // D = B + M
170 crypto::public_key D;
171 ge_p1p1 p1p1;
172 ge_add(&p1p1, &p3, &cached);
173 ge_p1p1_to_p3(&p3, &p1p1);
174 ge_p3_tobytes((unsigned char*)D.data, &p3);
175
176 pkeys.push_back(D);
177 }
178 return pkeys;
179 }
180
181 cryptonote::account_public_address device_default::get_subaddress(const cryptonote::account_keys& keys, const cryptonote::subaddress_index &index) {
182 if (index.is_zero())
183 return keys.m_account_address;
184
185 crypto::public_key D = get_subaddress_spend_public_key(keys, index);
186
187 // C = a*D
188 crypto::public_key C = rct::rct2pk(rct::scalarmultKey(rct::pk2rct(D), rct::sk2rct(keys.m_view_secret_key)));
189
190 // result: (C, D)
191 cryptonote::account_public_address address;
192 address.m_view_public_key = C;
193 address.m_spend_public_key = D;
194 return address;
195 }
196
197 crypto::secret_key device_default::get_subaddress_secret_key(const crypto::secret_key &a, const cryptonote::subaddress_index &index) {
198 char data[sizeof(config::HASH_KEY_SUBADDRESS) + sizeof(crypto::secret_key) + 2 * sizeof(uint32_t)];
199 memcpy(data, config::HASH_KEY_SUBADDRESS, sizeof(config::HASH_KEY_SUBADDRESS));
200 memcpy(data + sizeof(config::HASH_KEY_SUBADDRESS), &a, sizeof(crypto::secret_key));
201 uint32_t idx = SWAP32LE(index.major);
202 memcpy(data + sizeof(config::HASH_KEY_SUBADDRESS) + sizeof(crypto::secret_key), &idx, sizeof(uint32_t));
203 idx = SWAP32LE(index.minor);
204 memcpy(data + sizeof(config::HASH_KEY_SUBADDRESS) + sizeof(crypto::secret_key) + sizeof(uint32_t), &idx, sizeof(uint32_t));
205 crypto::secret_key m;
206 crypto::hash_to_scalar(data, sizeof(data), m);
207 return m;
208 }
209
210 /* ======================================================================= */
211 /* DERIVATION & KEY */
212 /* ======================================================================= */
213
214 bool device_default::verify_keys(const crypto::secret_key &secret_key, const crypto::public_key &public_key) {
215 crypto::public_key calculated_pub;
216 bool r = crypto::secret_key_to_public_key(secret_key, calculated_pub);
217 return r && public_key == calculated_pub;
218 }
219
220 bool device_default::scalarmultKey(rct::key & aP, const rct::key &P, const rct::key &a) {
221 rct::scalarmultKey(aP, P,a);
222 return true;
223 }
224
225 bool device_default::scalarmultBase(rct::key &aG, const rct::key &a) {
226 rct::scalarmultBase(aG,a);
227 return true;
228 }
229
230 bool device_default::sc_secret_add(crypto::secret_key &r, const crypto::secret_key &a, const crypto::secret_key &b) {
231 sc_add(&r, &a, &b);
232 return true;
233 }
234
235 crypto::secret_key device_default::generate_keys(crypto::public_key &pub, crypto::secret_key &sec, const crypto::secret_key& recovery_key, bool recover) {
236 return crypto::generate_keys(pub, sec, recovery_key, recover);
237 }
238
239 bool device_default::generate_key_derivation(const crypto::public_key &key1, const crypto::secret_key &key2, crypto::key_derivation &derivation) {
240 return crypto::wallet::generate_key_derivation(key1, key2, derivation);
241 }
242
243 bool device_default::derivation_to_scalar(const crypto::key_derivation &derivation, const size_t output_index, crypto::ec_scalar &res){
244 crypto::derivation_to_scalar(derivation,output_index, res);
245 return true;
246 }
247
248 bool device_default::derive_secret_key(const crypto::key_derivation &derivation, const std::size_t output_index, const crypto::secret_key &base, crypto::secret_key &derived_key){
249 crypto::derive_secret_key(derivation, output_index, base, derived_key);
250 return true;
251 }
252
253 bool device_default::derive_public_key(const crypto::key_derivation &derivation, const std::size_t output_index, const crypto::public_key &base, crypto::public_key &derived_key){
254 return crypto::derive_public_key(derivation, output_index, base, derived_key);
255 }
256
257 bool device_default::secret_key_to_public_key(const crypto::secret_key &sec, crypto::public_key &pub) {
258 return crypto::secret_key_to_public_key(sec,pub);
259 }
260
261 bool device_default::generate_key_image(const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image){
262 crypto::generate_key_image(pub, sec,image);
263 return true;
264 }
265
266 bool device_default::derive_view_tag(const crypto::key_derivation &derivation, const std::size_t output_index, crypto::view_tag &view_tag) {
267 crypto::derive_view_tag(derivation, output_index, view_tag);
268 return true;
269 }
270
271 bool device_default::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){
272 return true;
273 }
274
275 /* ======================================================================= */
276 /* TRANSACTION */
277 /* ======================================================================= */
278 void device_default::generate_tx_proof(const crypto::hash &prefix_hash,
279 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,
280 crypto::signature &sig) {
281 crypto::generate_tx_proof(prefix_hash, R, A, B, D, r, sig);
282 }
283
284 bool device_default::open_tx(crypto::secret_key &tx_key) {
285 cryptonote::keypair txkey = cryptonote::keypair::generate(*this);
286 tx_key = txkey.sec;
287 return true;
288 }
289
290 void device_default::get_transaction_prefix_hash(const cryptonote::transaction_prefix& tx, crypto::hash& h) {
291 cryptonote::get_transaction_prefix_hash(tx, h);
292 }
293
294 bool device_default::generate_output_ephemeral_keys(const size_t tx_version,
295 const cryptonote::account_keys &sender_account_keys, const crypto::public_key &txkey_pub, const crypto::secret_key &tx_key,
296 const cryptonote::tx_destination_entry &dst_entr, const boost::optional<cryptonote::account_public_address> &change_addr, const size_t output_index,
297 const bool &need_additional_txkeys, const std::vector<crypto::secret_key> &additional_tx_keys,
298 std::vector<crypto::public_key> &additional_tx_public_keys,
299 std::vector<rct::key> &amount_keys, crypto::public_key &out_eph_public_key,
300 const bool use_view_tags, crypto::view_tag &view_tag) {
301
302 crypto::key_derivation derivation;
303
304 // make additional tx pubkey if necessary
305 cryptonote::keypair additional_txkey;
306 if (need_additional_txkeys)
307 {
308 additional_txkey.sec = additional_tx_keys[output_index];
309 if (dst_entr.is_subaddress)
310 additional_txkey.pub = rct::rct2pk(rct::scalarmultKey(rct::pk2rct(dst_entr.addr.m_spend_public_key), rct::sk2rct(additional_txkey.sec)));
311 else
312 additional_txkey.pub = rct::rct2pk(rct::scalarmultBase(rct::sk2rct(additional_txkey.sec)));
313 }
314
315 bool r;
316 if (change_addr && dst_entr.addr == *change_addr)
317 {
318 // sending change to yourself; derivation = a*R
319 r = generate_key_derivation(txkey_pub, sender_account_keys.m_view_secret_key, derivation);
320 CHECK_AND_ASSERT_MES(r, false, "at creation outs: failed to generate_key_derivation(" << txkey_pub << ", <viewkey>)");
321 }
322 else
323 {
324 // sending to the recipient; derivation = r*A (or s*C in the subaddress scheme)
325 const crypto::secret_key &tx_privkey{dst_entr.is_subaddress && need_additional_txkeys ? additional_txkey.sec : tx_key};
326 r = generate_key_derivation(dst_entr.addr.m_view_public_key, tx_privkey, derivation);
327 CHECK_AND_ASSERT_MES(r, false, "at creation outs: failed to generate_key_derivation("
328 << dst_entr.addr.m_view_public_key << ", " << crypto::secret_key_explicit_print_ref{tx_privkey} << ")");
329 }
330
331 if (need_additional_txkeys)
332 {
333 additional_tx_public_keys.push_back(additional_txkey.pub);
334 }
335
336 if (tx_version > 1)
337 {
338 crypto::secret_key scalar1;
339 derivation_to_scalar(derivation, output_index, scalar1);
340 amount_keys.push_back(rct::sk2rct(scalar1));
341 }
342
343 if (use_view_tags)
344 {
345 derive_view_tag(derivation, output_index, view_tag);
346 }
347
348 r = derive_public_key(derivation, output_index, dst_entr.addr.m_spend_public_key, out_eph_public_key);
349 CHECK_AND_ASSERT_MES(r, false, "at creation outs: failed to derive_public_key(" << derivation << ", " << output_index << ", "<< dst_entr.addr.m_spend_public_key << ")");
350
351 return r;
352 }
353
354 bool device_default::encrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key) {
355 crypto::key_derivation derivation;
356 crypto::hash hash;
357 char data[33]; /* A hash, and an extra byte */
358
359 if (!generate_key_derivation(public_key, secret_key, derivation))
360 return false;
361
362 memcpy(data, &derivation, 32);
363 data[32] = config::HASH_KEY_ENCRYPTED_PAYMENT_ID;
364 cn_fast_hash(data, 33, hash);
365
366 for (size_t b = 0; b < 8; ++b)
367 payment_id.data[b] ^= hash.data[b];
368
369 return true;
370 }
371
372 rct::key device_default::genCommitmentMask(const rct::key &amount_key) {
373 return rct::genCommitmentMask(amount_key);
374 }
375
376 bool device_default::ecdhEncode(rct::ecdhTuple & unmasked, const rct::key & sharedSec, bool short_amount) {
377 rct::ecdhEncode(unmasked, sharedSec, short_amount);
378 return true;
379 }
380
381 bool device_default::ecdhDecode(rct::ecdhTuple & masked, const rct::key & sharedSec, bool short_amount) {
382 rct::ecdhDecode(masked, sharedSec, short_amount);
383 return true;
384 }
385
386 bool device_default::mlsag_prepare(const rct::key &H, const rct::key &xx,
387 rct::key &a, rct::key &aG, rct::key &aHP, rct::key &II) {
388 rct::skpkGen(a, aG);
389 rct::scalarmultKey(aHP, H, a);
390 rct::scalarmultKey(II, H, xx);
391 return true;
392 }
393 bool device_default::mlsag_prepare(rct::key &a, rct::key &aG) {
394 rct::skpkGen(a, aG);
395 return true;
396 }
397 bool device_default::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) {
398 prehash = rct::cn_fast_hash(hashes);
399 return true;
400 }
401
402
403 bool device_default::mlsag_hash(const rct::keyV &toHash, rct::key &c_old) {
404 c_old = rct::hash_to_scalar(toHash);
405 return true;
406 }
407
408 bool device_default::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 ) {
409 CHECK_AND_ASSERT_THROW_MES(dsRows<=rows, "dsRows greater than rows");
410 CHECK_AND_ASSERT_THROW_MES(xx.size() == rows, "xx size does not match rows");
411 CHECK_AND_ASSERT_THROW_MES(alpha.size() == rows, "alpha size does not match rows");
412 CHECK_AND_ASSERT_THROW_MES(ss.size() == rows, "ss size does not match rows");
413 for (size_t j = 0; j < rows; j++) {
414 sc_mulsub(ss[j].bytes, c.bytes, xx[j].bytes, alpha[j].bytes);
415 }
416 return true;
417 }
418
419 bool device_default::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) {
420 rct::skpkGen(a,aG); // aG = a*G
421 rct::scalarmultKey(aH,H,a); // aH = a*H
422 rct::scalarmultKey(I,H,p); // I = p*H
423 rct::scalarmultKey(D,H,z); // D = z*H
424 return true;
425 }
426
427 bool device_default::clsag_hash(const rct::keyV &data, rct::key &hash) {
428 hash = rct::hash_to_scalar(data);
429 return true;
430 }
431
432 bool device_default::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) {
433 rct::key s0_p_mu_P;
434 sc_mul(s0_p_mu_P.bytes,mu_P.bytes,p.bytes);
435 rct::key s0_add_z_mu_C;
436 sc_muladd(s0_add_z_mu_C.bytes,mu_C.bytes,z.bytes,s0_p_mu_P.bytes);
437 sc_mulsub(s.bytes,c.bytes,s0_add_z_mu_C.bytes,a.bytes);
438
439 return true;
440 }
441
442 bool device_default::close_tx() {
443 return true;
444 }
445
446
447 /* ---------------------------------------------------------- */
448 static device_default *default_core_device = NULL;
449 void register_all(std::map<std::string, std::unique_ptr<device>> &registry) {
450 if (!default_core_device) {
451 default_core_device = new device_default();
452 default_core_device->set_name("default_core_device");
453
454 }
455 registry.insert(std::make_pair("default", std::unique_ptr<device>(default_core_device)));
456 }
457
458
459 }
460
461 }