Debian Codegraph @

src/cryptonote_basic/account.cpp

raw symbols SCIP
1 // Copyright (c) 2014-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 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31 #include <fstream>
32
33 #include "include_base_utils.h"
34 #include "account.h"
35 #include "warnings.h"
36 #include "crypto/crypto.h"
37 extern "C"
38 {
39 #include "crypto/keccak.h"
40 }
41 #include "cryptonote_basic_impl.h"
42 #include "cryptonote_format_utils.h"
43 #include "cryptonote_config.h"
44
45 #undef MONERO_DEFAULT_LOG_CATEGORY
46 #define MONERO_DEFAULT_LOG_CATEGORY "account"
47
48 using namespace std;
49
50 DISABLE_VS_WARNINGS(4244 4345)
51
52 namespace cryptonote
53 {
54
55 //-----------------------------------------------------------------
56 hw::device& account_keys::get_device() const {
57 return *m_device;
58 }
59 //-----------------------------------------------------------------
60 void account_keys::set_device( hw::device &hwdev) {
61 m_device = &hwdev;
62 MCDEBUG("device", "account_keys::set_device device type: "<<typeid(hwdev).name());
63 }
64 //-----------------------------------------------------------------
65 static void derive_key(const crypto::chacha_key &base_key, crypto::chacha_key &key)
66 {
67 static_assert(sizeof(base_key) == sizeof(crypto::hash), "chacha key and hash should be the same size");
68 epee::mlocked<tools::scrubbed_arr<char, sizeof(base_key)+1>> data;
69 memcpy(data.data(), &base_key, sizeof(base_key));
70 data[sizeof(base_key)] = config::HASH_KEY_MEMORY;
71 crypto::generate_chacha_key(data.data(), sizeof(data), key, 1);
72 }
73 //-----------------------------------------------------------------
74 static epee::wipeable_string get_key_stream(const crypto::chacha_key &base_key, const crypto::chacha_iv &iv, size_t bytes)
75 {
76 // derive a new key
77 crypto::chacha_key key;
78 derive_key(base_key, key);
79
80 // chacha
81 epee::wipeable_string buffer0(std::string(bytes, '\0'));
82 epee::wipeable_string buffer1 = buffer0;
83 crypto::chacha20(buffer0.data(), buffer0.size(), key, iv, buffer1.data());
84 return buffer1;
85 }
86 //-----------------------------------------------------------------
87 void account_keys::xor_with_key_stream(const crypto::chacha_key &key)
88 {
89 // encrypt a large enough byte stream with chacha20
90 epee::wipeable_string key_stream = get_key_stream(key, m_encryption_iv, sizeof(crypto::secret_key) * (2 + m_multisig_keys.size()));
91 const char *ptr = key_stream.data();
92 for (size_t i = 0; i < sizeof(crypto::secret_key); ++i)
93 m_spend_secret_key.data[i] ^= *ptr++;
94 for (size_t i = 0; i < sizeof(crypto::secret_key); ++i)
95 m_view_secret_key.data[i] ^= *ptr++;
96 for (crypto::secret_key &k: m_multisig_keys)
97 {
98 for (size_t i = 0; i < sizeof(crypto::secret_key); ++i)
99 k.data[i] ^= *ptr++;
100 }
101 }
102 //-----------------------------------------------------------------
103 void account_keys::encrypt(const crypto::chacha_key &key)
104 {
105 m_encryption_iv = crypto::rand<crypto::chacha_iv>();
106 xor_with_key_stream(key);
107 }
108 //-----------------------------------------------------------------
109 void account_keys::decrypt(const crypto::chacha_key &key)
110 {
111 xor_with_key_stream(key);
112 }
113 //-----------------------------------------------------------------
114 void account_keys::encrypt_viewkey(const crypto::chacha_key &key)
115 {
116 // encrypt a large enough byte stream with chacha20
117 epee::wipeable_string key_stream = get_key_stream(key, m_encryption_iv, sizeof(crypto::secret_key) * 2);
118 const char *ptr = key_stream.data();
119 ptr += sizeof(crypto::secret_key);
120 for (size_t i = 0; i < sizeof(crypto::secret_key); ++i)
121 m_view_secret_key.data[i] ^= *ptr++;
122 }
123 //-----------------------------------------------------------------
124 void account_keys::decrypt_viewkey(const crypto::chacha_key &key)
125 {
126 encrypt_viewkey(key);
127 }
128 //-----------------------------------------------------------------
129 account_base::account_base()
130 {
131 set_null();
132 }
133 //-----------------------------------------------------------------
134 void account_base::set_null()
135 {
136 m_keys = account_keys();
137 m_creation_timestamp = 0;
138 }
139 //-----------------------------------------------------------------
140 void account_base::deinit()
141 {
142 try{
143 m_keys.get_device().disconnect();
144 } catch (const std::exception &e){
145 MERROR("Device disconnect exception: " << e.what());
146 }
147 }
148 //-----------------------------------------------------------------
149 void account_base::forget_spend_key()
150 {
151 m_keys.m_spend_secret_key = crypto::secret_key();
152 m_keys.m_multisig_keys.clear();
153 }
154 //-----------------------------------------------------------------
155 void account_base::set_spend_key(const crypto::secret_key& spend_secret_key)
156 {
157 // make sure derived spend public key matches saved public spend key
158 crypto::public_key spend_public_key;
159 crypto::secret_key_to_public_key(spend_secret_key, spend_public_key);
160 CHECK_AND_ASSERT_THROW_MES(m_keys.m_account_address.m_spend_public_key == spend_public_key,
161 "Unexpected derived public spend key");
162
163 m_keys.m_spend_secret_key = spend_secret_key;
164 }
165 //-----------------------------------------------------------------
166 crypto::secret_key account_base::generate(const crypto::secret_key& recovery_key, bool recover, bool two_random)
167 {
168 crypto::secret_key first = generate_keys(m_keys.m_account_address.m_spend_public_key, m_keys.m_spend_secret_key, recovery_key, recover);
169
170 // rng for generating second set of keys is hash of first rng. means only one set of electrum-style words needed for recovery
171 crypto::secret_key second;
172 keccak((uint8_t *)&m_keys.m_spend_secret_key, sizeof(crypto::secret_key), (uint8_t *)&second, sizeof(crypto::secret_key));
173
174 generate_keys(m_keys.m_account_address.m_view_public_key, m_keys.m_view_secret_key, second, two_random ? false : true);
175
176 struct tm timestamp = {0};
177 timestamp.tm_year = 2014 - 1900; // year 2014
178 timestamp.tm_mon = 6 - 1; // month june
179 timestamp.tm_mday = 8; // 8th of june
180 timestamp.tm_hour = 0;
181 timestamp.tm_min = 0;
182 timestamp.tm_sec = 0;
183
184 if (recover)
185 {
186 m_creation_timestamp = mktime(&timestamp);
187 if (m_creation_timestamp == (uint64_t)-1) // failure
188 m_creation_timestamp = 0; // lowest value
189 }
190 else
191 {
192 m_creation_timestamp = time(NULL);
193 }
194 return first;
195 }
196 //-----------------------------------------------------------------
197 void account_base::create_from_keys(const cryptonote::account_public_address& address, const crypto::secret_key& spendkey, const crypto::secret_key& viewkey)
198 {
199 m_keys.m_account_address = address;
200 m_keys.m_spend_secret_key = spendkey;
201 m_keys.m_view_secret_key = viewkey;
202
203 struct tm timestamp = {0};
204 timestamp.tm_year = 2014 - 1900; // year 2014
205 timestamp.tm_mon = 4 - 1; // month april
206 timestamp.tm_mday = 15; // 15th of april
207 timestamp.tm_hour = 0;
208 timestamp.tm_min = 0;
209 timestamp.tm_sec = 0;
210
211 m_creation_timestamp = mktime(&timestamp);
212 if (m_creation_timestamp == (uint64_t)-1) // failure
213 m_creation_timestamp = 0; // lowest value
214 }
215
216 //-----------------------------------------------------------------
217 void account_base::create_from_device(const std::string &device_name)
218 {
219 hw::device &hwdev = hw::get_device(device_name);
220 hwdev.set_name(device_name);
221 create_from_device(hwdev);
222 }
223
224 void account_base::create_from_device(hw::device &hwdev)
225 {
226 m_keys.set_device(hwdev);
227 MCDEBUG("device", "device type: "<<typeid(hwdev).name());
228 CHECK_AND_ASSERT_THROW_MES(hwdev.init(), "Device init failed");
229 CHECK_AND_ASSERT_THROW_MES(hwdev.connect(), "Device connect failed");
230 try {
231 CHECK_AND_ASSERT_THROW_MES(hwdev.get_public_address(m_keys.m_account_address), "Cannot get a device address");
232 CHECK_AND_ASSERT_THROW_MES(hwdev.get_secret_keys(m_keys.m_view_secret_key, m_keys.m_spend_secret_key), "Cannot get device secret");
233 } catch (const std::exception &e){
234 hwdev.disconnect();
235 throw;
236 }
237 struct tm timestamp = {0};
238 timestamp.tm_year = 2014 - 1900; // year 2014
239 timestamp.tm_mon = 4 - 1; // month april
240 timestamp.tm_mday = 15; // 15th of april
241 timestamp.tm_hour = 0;
242 timestamp.tm_min = 0;
243 timestamp.tm_sec = 0;
244
245 m_creation_timestamp = mktime(&timestamp);
246 if (m_creation_timestamp == (uint64_t)-1) // failure
247 m_creation_timestamp = 0; // lowest value
248 }
249
250 //-----------------------------------------------------------------
251 void account_base::create_from_viewkey(const cryptonote::account_public_address& address, const crypto::secret_key& viewkey)
252 {
253 crypto::secret_key fake;
254 memset(&unwrap(unwrap(fake)), 0, sizeof(fake));
255 create_from_keys(address, fake, viewkey);
256 }
257 //-----------------------------------------------------------------
258 bool account_base::make_multisig(const crypto::secret_key &view_secret_key, const crypto::secret_key &spend_secret_key, const crypto::public_key &spend_public_key, const std::vector<crypto::secret_key> &multisig_keys)
259 {
260 m_keys.m_account_address.m_spend_public_key = spend_public_key;
261 m_keys.m_view_secret_key = view_secret_key;
262 m_keys.m_spend_secret_key = spend_secret_key;
263 m_keys.m_multisig_keys = multisig_keys;
264 return crypto::secret_key_to_public_key(view_secret_key, m_keys.m_account_address.m_view_public_key);
265 }
266 //-----------------------------------------------------------------
267 const account_keys& account_base::get_keys() const
268 {
269 return m_keys;
270 }
271 //-----------------------------------------------------------------
272 std::string account_base::get_public_address_str(network_type nettype) const
273 {
274 //TODO: change this code into base 58
275 return get_account_address_as_str(nettype, false, m_keys.m_account_address);
276 }
277 //-----------------------------------------------------------------
278 std::string account_base::get_public_integrated_address_str(const crypto::hash8 &payment_id, network_type nettype) const
279 {
280 //TODO: change this code into base 58
281 return get_account_integrated_address_as_str(nettype, m_keys.m_account_address, payment_id);
282 }
283 //-----------------------------------------------------------------
284 }