From f3f6e62f1cc187657baedeabd56cdd5e4d117c4c Mon Sep 17 00:00:00 2001 From: Satoshi Otomakan Date: Mon, 30 Oct 2023 12:43:35 +0100 Subject: [PATCH] [Sec]: Zeroize mnemonic --- rust/coverage.stats | 2 +- src/HDWallet.cpp | 6 +++--- src/Keystore/StoredKey.cpp | 9 +++++++-- src/interface/TWString.cpp | 9 +++++++-- tests/chains/Bitcoin/TWSegwitAddressTests.cpp | 2 +- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/rust/coverage.stats b/rust/coverage.stats index 7d7ab43dc7c..8670b15529f 100644 --- a/rust/coverage.stats +++ b/rust/coverage.stats @@ -1 +1 @@ -92.0 \ No newline at end of file +91.0 \ No newline at end of file diff --git a/src/HDWallet.cpp b/src/HDWallet.cpp index 74d6e4585bd..b4674f56dcf 100644 --- a/src/HDWallet.cpp +++ b/src/HDWallet.cpp @@ -99,9 +99,9 @@ HDWallet::HDWallet(const Data& entropy, const std::string& passphrase) template HDWallet::~HDWallet() { - std::fill(seed.begin(), seed.end(), 0); - std::fill(mnemonic.begin(), mnemonic.end(), 0); - std::fill(passphrase.begin(), passphrase.end(), 0); + memzero(seed.data(), seed.size()); + memzero(mnemonic.data(), mnemonic.size()); + memzero(passphrase.data(), passphrase.size()); } template diff --git a/src/Keystore/StoredKey.cpp b/src/Keystore/StoredKey.cpp index 3e35bc7aa9b..a3fca3bb91f 100644 --- a/src/Keystore/StoredKey.cpp +++ b/src/Keystore/StoredKey.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -32,7 +33,9 @@ StoredKey StoredKey::createWithMnemonic(const std::string& name, const Data& pas } Data mnemonicData = TW::Data(mnemonic.begin(), mnemonic.end()); - return StoredKey(StoredKeyType::mnemonicPhrase, name, password, mnemonicData, encryptionLevel, encryption); + StoredKey key(StoredKeyType::mnemonicPhrase, name, password, mnemonicData, encryptionLevel, encryption); + memzero(mnemonicData.data(), mnemonic.size()); + return key; } StoredKey StoredKey::createWithMnemonicRandom(const std::string& name, const Data& password, TWStoredKeyEncryptionLevel encryptionLevel, TWStoredKeyEncryption encryption) { @@ -40,7 +43,9 @@ StoredKey StoredKey::createWithMnemonicRandom(const std::string& name, const Dat const auto& mnemonic = wallet.getMnemonic(); assert(Mnemonic::isValid(mnemonic)); Data mnemonicData = TW::Data(mnemonic.begin(), mnemonic.end()); - return StoredKey(StoredKeyType::mnemonicPhrase, name, password, mnemonicData, encryptionLevel, encryption); + StoredKey key(StoredKeyType::mnemonicPhrase, name, password, mnemonicData, encryptionLevel, encryption); + memzero(mnemonicData.data(), mnemonic.size()); + return key; } StoredKey StoredKey::createWithMnemonicAddDefaultAddress(const std::string& name, const Data& password, const std::string& mnemonic, TWCoinType coin, TWStoredKeyEncryption encryption) { diff --git a/src/interface/TWString.cpp b/src/interface/TWString.cpp index ccd0f267dfd..ac118e581a0 100644 --- a/src/interface/TWString.cpp +++ b/src/interface/TWString.cpp @@ -6,6 +6,7 @@ #include +#include #include TWString *_Nonnull TWStringCreateWithUTF8Bytes(const char *_Nonnull bytes) { @@ -34,8 +35,12 @@ const char *_Nonnull TWStringUTF8Bytes(TWString *_Nonnull string) { } void TWStringDelete(TWString *_Nonnull string) { - auto* s = reinterpret_cast(string); - delete s; + auto *sConst = reinterpret_cast(string); + // `const_cast` is safe here despite that the pointer to the string is const + // but `std::string` is not a constant value. + auto *s = const_cast(sConst); + memzero(s->data(), s->size()); + delete sConst; } bool TWStringEqual(TWString *_Nonnull lhs, TWString *_Nonnull rhs) { diff --git a/tests/chains/Bitcoin/TWSegwitAddressTests.cpp b/tests/chains/Bitcoin/TWSegwitAddressTests.cpp index 6b21a066205..9723244292c 100644 --- a/tests/chains/Bitcoin/TWSegwitAddressTests.cpp +++ b/tests/chains/Bitcoin/TWSegwitAddressTests.cpp @@ -60,7 +60,7 @@ TEST(TWSegwitAddress, InitWithAddress) { ASSERT_EQ(TWHRPBitcoin, TWSegwitAddressHRP(address.get())); - auto witness = WRAPS(TWSegwitAddressWitnessProgram(address.get())); + auto witness = WRAPD(TWSegwitAddressWitnessProgram(address.get())); ASSERT_EQ(TW::hex(TW::data(TWDataBytes(witness.get()), TWDataSize(witness.get()))), "751e76e8199196d454941c45d1b3a323f1433bd6"); }