Debian Codegraph @

monero/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::set_view_key(const crypto::secret_key &view_secret_key)
150 {
151 m_keys.m_view_secret_key = view_secret_key;
152 }
153 //-----------------------------------------------------------------
154 void account_base::forget_view_key()
155 {
156 m_keys.m_view_secret_key = crypto::null_skey;
157 }
158 //-----------------------------------------------------------------
159 void account_base::forget_spend_key()
160 {
161 m_keys.m_spend_secret_key = crypto::secret_key();
162 m_keys.m_multisig_keys.clear();
163 }
164 //-----------------------------------------------------------------
165 void account_base::set_spend_key(const crypto::secret_key& spend_secret_key)
166 {
167 // make sure derived spend public key matches saved public spend key
168 crypto::public_key spend_public_key;
169 crypto::secret_key_to_public_key(spend_secret_key, spend_public_key);
170 CHECK_AND_ASSERT_THROW_MES(m_keys.m_account_address.m_spend_public_key == spend_public_key,
171 "Unexpected derived public spend key");
172
173 m_keys.m_spend_secret_key = spend_secret_key;
174 }
175 //-----------------------------------------------------------------
176 crypto::secret_key account_base::generate(const crypto::secret_key& recovery_key, bool recover, bool two_random)
177 {
178 crypto::secret_key first = generate_keys(m_keys.m_account_address.m_spend_public_key, m_keys.m_spend_secret_key, recovery_key, recover);
179
180 // rng for generating second set of keys is hash of first rng. means only one set of electrum-style words needed for recovery
181 crypto::secret_key second;
182 keccak((uint8_t *)&m_keys.m_spend_secret_key, sizeof(crypto::secret_key), (uint8_t *)&second, sizeof(crypto::secret_key));
183
184 generate_keys(m_keys.m_account_address.m_view_public_key, m_keys.m_view_secret_key, second, two_random ? false : true);
185
186 struct tm timestamp = {0};
187 timestamp.tm_year = 2014 - 1900; // year 2014
188 timestamp.tm_mon = 6 - 1; // month june
189 timestamp.tm_mday = 8; // 8th of june
190 timestamp.tm_hour = 0;
191 timestamp.tm_min = 0;
192 timestamp.tm_sec = 0;
193
194 if (recover)
195 {
196 m_creation_timestamp = mktime(&timestamp);
197 if (m_creation_timestamp == (uint64_t)-1) // failure
198 m_creation_timestamp = 0; // lowest value
199 }
200 else
201 {
202 m_creation_timestamp = time(NULL);
203 }
204 return first;
205 }
206 //-----------------------------------------------------------------
207 void account_base::create_from_keys(const cryptonote::account_public_address& address, const crypto::secret_key& spendkey, const crypto::secret_key& viewkey)
208 {
209 m_keys.m_account_address = address;
210 m_keys.m_spend_secret_key = spendkey;
211 m_keys.m_view_secret_key = viewkey;
212
213 struct tm timestamp = {0};
214 timestamp.tm_year = 2014 - 1900; // year 2014
215 timestamp.tm_mon = 4 - 1; // month april
216 timestamp.tm_mday = 15; // 15th of april
217 timestamp.tm_hour = 0;
218 timestamp.tm_min = 0;
219 timestamp.tm_sec = 0;
220
221 m_creation_timestamp = mktime(&timestamp);
222 if (m_creation_timestamp == (uint64_t)-1) // failure
223 m_creation_timestamp = 0; // lowest value
224 }
225
226 //-----------------------------------------------------------------
227 void account_base::create_from_device(const std::string &device_name)
228 {
229 hw::device &hwdev = hw::get_device(device_name);
230 hwdev.set_name(device_name);
231 create_from_device(hwdev);
232 }
233
234 void account_base::create_from_device(hw::device &hwdev)
235 {
236 m_keys.set_device(hwdev);
237 MCDEBUG("device", "device type: "<<typeid(hwdev).name());
238 CHECK_AND_ASSERT_THROW_MES(hwdev.init(), "Device init failed");
239 CHECK_AND_ASSERT_THROW_MES(hwdev.connect(), "Device connect failed");
240 try {
241 CHECK_AND_ASSERT_THROW_MES(hwdev.get_public_address(m_keys.m_account_address), "Cannot get a device address");
242 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");
243 } catch (const std::exception &e){
244 hwdev.disconnect();
245 throw;
246 }
247 struct tm timestamp = {0};
248 timestamp.tm_year = 2014 - 1900; // year 2014
249 timestamp.tm_mon = 4 - 1; // month april
250 timestamp.tm_mday = 15; // 15th of april
251 timestamp.tm_hour = 0;
252 timestamp.tm_min = 0;
253 timestamp.tm_sec = 0;
254
255 m_creation_timestamp = mktime(&timestamp);
256 if (m_creation_timestamp == (uint64_t)-1) // failure
257 m_creation_timestamp = 0; // lowest value
258 }
259
260 //-----------------------------------------------------------------
261 void account_base::create_from_viewkey(const cryptonote::account_public_address& address, const crypto::secret_key& viewkey)
262 {
263 crypto::secret_key fake;
264 memset(&unwrap(unwrap(fake)), 0, sizeof(fake));
265 create_from_keys(address, fake, viewkey);
266 }
267 //-----------------------------------------------------------------
268 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)
269 {
270 m_keys.m_account_address.m_spend_public_key = spend_public_key;
271 m_keys.m_view_secret_key = view_secret_key;
272 m_keys.m_spend_secret_key = spend_secret_key;
273 m_keys.m_multisig_keys = multisig_keys;
274 return crypto::secret_key_to_public_key(view_secret_key, m_keys.m_account_address.m_view_public_key);
275 }
276 //-----------------------------------------------------------------
277 const account_keys& account_base::get_keys() const
278 {
279 return m_keys;
280 }
281 //-----------------------------------------------------------------
282 std::string account_base::get_public_address_str(network_type nettype) const
283 {
284 //TODO: change this code into base 58
285 return get_account_address_as_str(nettype, false, m_keys.m_account_address);
286 }
287 //-----------------------------------------------------------------
288 std::string account_base::get_public_integrated_address_str(const crypto::hash8 &payment_id, network_type nettype) const
289 {
290 //TODO: change this code into base 58
291 return get_account_integrated_address_as_str(nettype, m_keys.m_account_address, payment_id);
292 }
293 //-----------------------------------------------------------------
294 }