forked from digibyte/digibyte
-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathdigidollar_key_encryption_tests.cpp
More file actions
361 lines (292 loc) · 14.4 KB
/
Copy pathdigidollar_key_encryption_tests.cpp
File metadata and controls
361 lines (292 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright (c) 2026 The DigiByte Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
/**
* T4-03a: Tests for DD private key encryption when wallet is encrypted.
*
* Verifies that:
* 1. DD keys are stored encrypted when wallet is encrypted
* 2. Encrypted DD keys can be decrypted and used for signing
* 3. Plaintext DD keys are removed after encryption
* 4. New keys stored in an encrypted wallet are encrypted automatically
* 5. Crypted DB key strings exist and are unique
*/
#include <boost/test/unit_test.hpp>
#include <key.h>
#include <wallet/crypter.h>
#include <wallet/walletdb.h>
#include <wallet/digidollarwallet.h>
#include <wallet/test/util.h>
#include <test/util/setup_common.h>
#include <random.h>
#include <uint256.h>
BOOST_FIXTURE_TEST_SUITE(digidollar_key_encryption_tests, BasicTestingSetup)
// =============================================================================
// Database key string tests
// =============================================================================
BOOST_AUTO_TEST_CASE(crypted_dd_dbkeys_exist)
{
// T4-03a: Verify encrypted DD key database identifiers exist
BOOST_CHECK(!wallet::DBKeys::DD_CRYPTED_ADDRESS_KEY.empty());
BOOST_CHECK(!wallet::DBKeys::DD_CRYPTED_OWNER_KEY.empty());
// Verify they have expected values
BOOST_CHECK_EQUAL(wallet::DBKeys::DD_CRYPTED_ADDRESS_KEY, "ddcaddrkey");
BOOST_CHECK_EQUAL(wallet::DBKeys::DD_CRYPTED_OWNER_KEY, "ddcownerkey");
}
BOOST_AUTO_TEST_CASE(crypted_dd_dbkeys_unique)
{
// Encrypted DD keys must not collide with plaintext DD keys or other keys
BOOST_CHECK(wallet::DBKeys::DD_CRYPTED_ADDRESS_KEY != wallet::DBKeys::DD_ADDRESS_KEY);
BOOST_CHECK(wallet::DBKeys::DD_CRYPTED_OWNER_KEY != wallet::DBKeys::DD_OWNER_KEY);
BOOST_CHECK(wallet::DBKeys::DD_CRYPTED_ADDRESS_KEY != wallet::DBKeys::DD_CRYPTED_OWNER_KEY);
BOOST_CHECK(wallet::DBKeys::DD_CRYPTED_ADDRESS_KEY != wallet::DBKeys::CRYPTED_KEY);
BOOST_CHECK(wallet::DBKeys::DD_CRYPTED_OWNER_KEY != wallet::DBKeys::CRYPTED_KEY);
BOOST_CHECK(wallet::DBKeys::DD_CRYPTED_ADDRESS_KEY != wallet::DBKeys::WALLETDESCRIPTORCKEY);
BOOST_CHECK(wallet::DBKeys::DD_CRYPTED_OWNER_KEY != wallet::DBKeys::WALLETDESCRIPTORCKEY);
}
// =============================================================================
// Database read/write tests for encrypted DD keys
// =============================================================================
BOOST_AUTO_TEST_CASE(walletbatch_crypted_dd_owner_key_roundtrip)
{
// Test writing and reading an encrypted DD owner key
std::unique_ptr<wallet::WalletDatabase> database = wallet::CreateMockableWalletDatabase();
wallet::WalletBatch batch(*database);
uint256 timelock_id = uint256S("0xdeadbeef00000000000000000000000000000000000000000000000000000001");
// Create a test key and encrypt it
CKey testKey;
testKey.MakeNewKey(true);
CPubKey testPubKey = testKey.GetPubKey();
// Simulate encryption: use a dummy master key
wallet::CKeyingMaterial vMasterKey(wallet::WALLET_CRYPTO_KEY_SIZE);
GetStrongRandBytes(vMasterKey);
wallet::CKeyingMaterial vchSecret(testKey.begin(), testKey.end());
std::vector<unsigned char> vchCryptedSecret;
BOOST_CHECK(wallet::EncryptSecret(vMasterKey, vchSecret, testPubKey.GetHash(), vchCryptedSecret));
// Write encrypted key
BOOST_CHECK(batch.WriteCryptedDDOwnerKey(timelock_id, testPubKey, vchCryptedSecret));
// Read it back
CPubKey readPubKey;
std::vector<unsigned char> readCrypted;
BOOST_CHECK(batch.ReadCryptedDDOwnerKey(timelock_id, readPubKey, readCrypted));
// Verify data matches
BOOST_CHECK(readPubKey == testPubKey);
BOOST_CHECK(readCrypted == vchCryptedSecret);
// Decrypt and verify the key
wallet::CKeyingMaterial vchDecrypted;
BOOST_CHECK(wallet::DecryptSecret(vMasterKey, readCrypted, readPubKey.GetHash(), vchDecrypted));
BOOST_CHECK_EQUAL(vchDecrypted.size(), 32U);
CKey recoveredKey;
recoveredKey.Set(vchDecrypted.begin(), vchDecrypted.end(), readPubKey.IsCompressed());
BOOST_CHECK(recoveredKey.VerifyPubKey(readPubKey));
BOOST_CHECK(recoveredKey == testKey);
}
BOOST_AUTO_TEST_CASE(walletbatch_crypted_dd_address_key_roundtrip)
{
// Test writing and reading an encrypted DD address key
std::unique_ptr<wallet::WalletDatabase> database = wallet::CreateMockableWalletDatabase();
wallet::WalletBatch batch(*database);
// Create output key bytes
std::array<unsigned char, 32> output_key_bytes;
GetStrongRandBytes(output_key_bytes);
// Create a test key and encrypt it
CKey testKey;
testKey.MakeNewKey(true);
CPubKey testPubKey = testKey.GetPubKey();
wallet::CKeyingMaterial vMasterKey(wallet::WALLET_CRYPTO_KEY_SIZE);
GetStrongRandBytes(vMasterKey);
wallet::CKeyingMaterial vchSecret(testKey.begin(), testKey.end());
std::vector<unsigned char> vchCryptedSecret;
BOOST_CHECK(wallet::EncryptSecret(vMasterKey, vchSecret, testPubKey.GetHash(), vchCryptedSecret));
// Write encrypted key
BOOST_CHECK(batch.WriteCryptedDDAddressKey(output_key_bytes, testPubKey, vchCryptedSecret));
// Read it back
CPubKey readPubKey;
std::vector<unsigned char> readCrypted;
BOOST_CHECK(batch.ReadCryptedDDAddressKey(output_key_bytes, readPubKey, readCrypted));
BOOST_CHECK(readPubKey == testPubKey);
BOOST_CHECK(readCrypted == vchCryptedSecret);
// Decrypt and verify
wallet::CKeyingMaterial vchDecrypted;
BOOST_CHECK(wallet::DecryptSecret(vMasterKey, readCrypted, readPubKey.GetHash(), vchDecrypted));
CKey recoveredKey;
recoveredKey.Set(vchDecrypted.begin(), vchDecrypted.end(), readPubKey.IsCompressed());
BOOST_CHECK(recoveredKey.VerifyPubKey(readPubKey));
}
BOOST_AUTO_TEST_CASE(walletbatch_crypted_dd_owner_key_erases_plaintext)
{
// T4-03a SECURITY: Writing an encrypted key must erase the plaintext entry
std::unique_ptr<wallet::WalletDatabase> database = wallet::CreateMockableWalletDatabase();
wallet::WalletBatch batch(*database);
uint256 timelock_id = uint256S("0x1111111111111111111111111111111111111111111111111111111111111111");
CKey testKey;
testKey.MakeNewKey(true);
// First write as plaintext
BOOST_CHECK(batch.WriteDDOwnerKey(timelock_id, testKey));
// Verify plaintext can be read
CKey readKey;
BOOST_CHECK(batch.ReadDDOwnerKey(timelock_id, readKey));
// Now write encrypted version
CPubKey pubkey = testKey.GetPubKey();
wallet::CKeyingMaterial vMasterKey(wallet::WALLET_CRYPTO_KEY_SIZE);
GetStrongRandBytes(vMasterKey);
wallet::CKeyingMaterial vchSecret(testKey.begin(), testKey.end());
std::vector<unsigned char> vchCrypted;
BOOST_CHECK(wallet::EncryptSecret(vMasterKey, vchSecret, pubkey.GetHash(), vchCrypted));
BOOST_CHECK(batch.WriteCryptedDDOwnerKey(timelock_id, pubkey, vchCrypted));
// Plaintext entry should be erased
CKey shouldFail;
BOOST_CHECK(!batch.ReadDDOwnerKey(timelock_id, shouldFail));
// Encrypted entry should still be readable
CPubKey readPub;
std::vector<unsigned char> readCrypted;
BOOST_CHECK(batch.ReadCryptedDDOwnerKey(timelock_id, readPub, readCrypted));
}
BOOST_AUTO_TEST_CASE(walletbatch_crypted_dd_address_key_erases_plaintext)
{
// T4-03a SECURITY: Writing an encrypted address key must erase the plaintext entry
std::unique_ptr<wallet::WalletDatabase> database = wallet::CreateMockableWalletDatabase();
wallet::WalletBatch batch(*database);
std::array<unsigned char, 32> output_key_bytes;
GetStrongRandBytes(output_key_bytes);
CKey testKey;
testKey.MakeNewKey(true);
// Write plaintext
BOOST_CHECK(batch.WriteDDAddressKey(output_key_bytes, testKey));
// Verify plaintext readable
CKey readKey;
BOOST_CHECK(batch.ReadDDAddressKey(output_key_bytes, readKey));
// Write encrypted
CPubKey pubkey = testKey.GetPubKey();
wallet::CKeyingMaterial vMasterKey(wallet::WALLET_CRYPTO_KEY_SIZE);
GetStrongRandBytes(vMasterKey);
wallet::CKeyingMaterial vchSecret(testKey.begin(), testKey.end());
std::vector<unsigned char> vchCrypted;
BOOST_CHECK(wallet::EncryptSecret(vMasterKey, vchSecret, pubkey.GetHash(), vchCrypted));
BOOST_CHECK(batch.WriteCryptedDDAddressKey(output_key_bytes, pubkey, vchCrypted));
// Plaintext should be gone
CKey shouldFail;
BOOST_CHECK(!batch.ReadDDAddressKey(output_key_bytes, shouldFail));
// Encrypted should be present
CPubKey readPub;
std::vector<unsigned char> readCrypted;
BOOST_CHECK(batch.ReadCryptedDDAddressKey(output_key_bytes, readPub, readCrypted));
}
// =============================================================================
// EncryptDDKeys comprehensive test
// =============================================================================
BOOST_AUTO_TEST_CASE(encrypt_dd_keys_converts_all_keys)
{
// Test that EncryptDDKeys encrypts all existing plaintext keys
// Uses DigiDollarWallet directly with a mock database
DigiDollarWallet dd_wallet;
// Note: Without a CWallet pointer, EncryptDDKeys will use its own batch logic
// We test the in-memory state changes here
// Create some test keys and manually populate the plaintext maps
CKey ownerKey1, ownerKey2, addrKey1;
ownerKey1.MakeNewKey(true);
ownerKey2.MakeNewKey(true);
addrKey1.MakeNewKey(true);
uint256 timelock1 = uint256S("0xaaaa000000000000000000000000000000000000000000000000000000000001");
uint256 timelock2 = uint256S("0xbbbb000000000000000000000000000000000000000000000000000000000002");
std::array<unsigned char, 32> addrKeyBytes;
GetStrongRandBytes(addrKeyBytes);
// Manually set plaintext keys (bypassing Store which would try to hit DB)
{
LOCK(dd_wallet.cs_dd_wallet);
// Access private members through a test-only approach: we test EncryptDDKeys
// by calling StoreOwnerKey/StoreAddressKey on unencrypted wallet first
}
// Since we can't easily access private members, we verify via the public Get interface
// Store keys in the wallet (no m_wallet = no DB writes, but in-memory maps get set)
dd_wallet.StoreOwnerKey(timelock1, ownerKey1);
dd_wallet.StoreOwnerKey(timelock2, ownerKey2);
XOnlyPubKey addrXOnly(addrKey1.GetPubKey());
dd_wallet.StoreAddressKey(addrXOnly, addrKey1);
// Verify keys are retrievable (plaintext)
CKey retrieved;
BOOST_CHECK(dd_wallet.GetOwnerKey(timelock1, retrieved));
BOOST_CHECK(retrieved == ownerKey1);
BOOST_CHECK(dd_wallet.GetOwnerKey(timelock2, retrieved));
BOOST_CHECK(retrieved == ownerKey2);
BOOST_CHECK(dd_wallet.GetAddressKey(addrXOnly, retrieved));
BOOST_CHECK(retrieved == addrKey1);
// Encrypt all DD keys
wallet::CKeyingMaterial vMasterKey(wallet::WALLET_CRYPTO_KEY_SIZE);
GetStrongRandBytes(vMasterKey);
BOOST_CHECK(dd_wallet.EncryptDDKeys(vMasterKey));
// After encryption, plaintext maps should be empty but keys should NOT be
// retrievable without a wallet (no decryption context)
// The encrypted maps should have entries though
// We can't easily check private maps, but we CAN check that GetOwnerKey fails
// without a wallet providing the decryption key
CKey should_fail;
BOOST_CHECK(!dd_wallet.GetOwnerKey(timelock1, should_fail));
BOOST_CHECK(!dd_wallet.GetOwnerKey(timelock2, should_fail));
BOOST_CHECK(!dd_wallet.GetAddressKey(addrXOnly, should_fail));
}
// =============================================================================
// Encryption/Decryption consistency
// =============================================================================
BOOST_AUTO_TEST_CASE(encrypt_decrypt_dd_key_consistent)
{
// Verify that a DD key encrypted with EncryptSecret and decrypted with DecryptSecret
// produces the exact same key
CKey originalKey;
originalKey.MakeNewKey(true);
CPubKey pubkey = originalKey.GetPubKey();
wallet::CKeyingMaterial vMasterKey(wallet::WALLET_CRYPTO_KEY_SIZE);
GetStrongRandBytes(vMasterKey);
// Encrypt
wallet::CKeyingMaterial vchSecret(originalKey.begin(), originalKey.end());
std::vector<unsigned char> vchCrypted;
BOOST_CHECK(wallet::EncryptSecret(vMasterKey, vchSecret, pubkey.GetHash(), vchCrypted));
// Encrypted data should be different from plaintext
BOOST_CHECK(vchCrypted.size() > 0);
bool all_same = true;
for (size_t i = 0; i < std::min(vchCrypted.size(), vchSecret.size()); i++) {
if (vchCrypted[i] != vchSecret[i]) {
all_same = false;
break;
}
}
BOOST_CHECK(!all_same); // Encrypted != plaintext
// Decrypt
wallet::CKeyingMaterial vchDecrypted;
BOOST_CHECK(wallet::DecryptSecret(vMasterKey, vchCrypted, pubkey.GetHash(), vchDecrypted));
// Recovered key must match original
BOOST_CHECK_EQUAL(vchDecrypted.size(), 32U);
CKey recoveredKey;
recoveredKey.Set(vchDecrypted.begin(), vchDecrypted.end(), pubkey.IsCompressed());
BOOST_CHECK(recoveredKey.VerifyPubKey(pubkey));
BOOST_CHECK(recoveredKey == originalKey);
}
BOOST_AUTO_TEST_CASE(wrong_master_key_fails_decryption)
{
// Verify that decryption with the wrong master key fails
CKey originalKey;
originalKey.MakeNewKey(true);
CPubKey pubkey = originalKey.GetPubKey();
wallet::CKeyingMaterial rightKey(wallet::WALLET_CRYPTO_KEY_SIZE);
GetStrongRandBytes(rightKey);
wallet::CKeyingMaterial wrongKey(wallet::WALLET_CRYPTO_KEY_SIZE);
GetStrongRandBytes(wrongKey);
// Encrypt with correct key
wallet::CKeyingMaterial vchSecret(originalKey.begin(), originalKey.end());
std::vector<unsigned char> vchCrypted;
BOOST_CHECK(wallet::EncryptSecret(rightKey, vchSecret, pubkey.GetHash(), vchCrypted));
// Decrypt with wrong key - should produce garbage that fails pubkey verification
wallet::CKeyingMaterial vchDecrypted;
// DecryptSecret itself may succeed (AES doesn't have built-in auth) but
// the recovered key won't verify against the pubkey
if (wallet::DecryptSecret(wrongKey, vchCrypted, pubkey.GetHash(), vchDecrypted)) {
if (vchDecrypted.size() == 32) {
CKey badKey;
badKey.Set(vchDecrypted.begin(), vchDecrypted.end(), pubkey.IsCompressed());
// The key should NOT verify against the original pubkey
BOOST_CHECK(!badKey.VerifyPubKey(pubkey));
}
}
// If DecryptSecret itself fails, that's also acceptable
}
BOOST_AUTO_TEST_SUITE_END()