What changed, and why it matters
This change adds a password check inside the wallet's key-encryption routine so that the wallet refuses to encrypt keys if the caller supplies the wrong password. Before, supplying an incorrect password could still trigger encryption, potentially leaving the wallet file in a state where the stored keys do not match the password the user actually knows. The patch is small and includes a new test that confirms the wrong-password case now throws an error.
Review callers of encrypt_keys() to confirm no legitimate use cases require encryption with a non-current password; otherwise merge the patch and run the new unit test. Consider whether any existing wallet files may already be in an inconsistent encrypted state from prior wrong-password calls and whether a recovery or migration path is needed.
Security signals we found
Missing authorization check before sensitive cryptographic operation
Potential key-state inconsistency if wrong password used during encryption
Defensive password verification added to key encryption routine
Unit test added for wrong-password encryption failure
Evidence from the diff
In wallet2::encrypt_keys(), the patch inserts verify_password_with_cached_key(key) before m_account.encrypt_keys(key) and m_account.decrypt_viewkey(key). This ensures the provided chacha key is validated against the wallet’s cached key before any key material is re-encrypted. A unit test encrypt_with_wrong_password verifies that calling encrypt_keys() with a mismatched password throws tools::error::invalid_password. The change is defensive: it prevents the wallet from entering an inconsistent encrypted state when an incorrect password/key is supplied.
Changed components
src/wallet/wallet2.cpptests/unit_tests/wallet_storage.cppInspect captured patch +12 / −0
### src/wallet/wallet2.cpp
@@ -5717,6 +5717,8 @@ bool wallet2::is_key_encryption_enabled() const
void wallet2::encrypt_keys(const crypto::chacha_key &key)
{
+ verify_password_with_cached_key(key);
+
m_account.encrypt_keys(key);
m_account.decrypt_viewkey(key);
}
### tests/unit_tests/wallet_storage.cpp
@@ -618,6 +618,16 @@ TEST(wallet_storage, change_export_format)
EXPECT_EQ(primary_address_1, primary_address_2);
}
+TEST(wallet_manual_encrypt, encrypt_with_wrong_password)
+{
+ const epee::wipeable_string password1("Beleza pura, malandro!");
+ const epee::wipeable_string password2("correct horse battery staple");
+ tools::wallet2 w;
+ w.generate("", password1);
+ ASSERT_TRUE(w.is_key_encryption_enabled());
+ EXPECT_THROW(w.encrypt_keys(password2), tools::error::invalid_password);
+}
+
#define OLD_WALLET_KEYS_UNLOCKER 0
#if OLD_WALLET_KEYS_UNLOCKER
#define WALLET_KEYS_UNLOCKER_CTOR(wal, p_pwd) tools::wallet_keys_unlocker(wal, \Why this scored 59/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.