From 18a2c79d848ce45e8fbd3707a5b8db0b70a5ce33 Mon Sep 17 00:00:00 2001 From: iamunick Date: Tue, 20 May 2014 14:12:40 -0400 Subject: [PATCH] use openssl_cleanse instead of memset for crit mem spaces --- src/allocators.h | 5 +++-- src/base58.h | 2 +- src/crypter.cpp | 4 ++-- src/crypter.h | 4 ++-- src/util.cpp | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/allocators.h b/src/allocators.h index 99afa10..eb2aed6 100644 --- a/src/allocators.h +++ b/src/allocators.h @@ -9,6 +9,7 @@ #include #include #include +#include // for OPENSSL_cleanse() #ifdef WIN32 #ifdef _WIN32_WINNT @@ -212,7 +213,7 @@ struct secure_allocator : public std::allocator { if (p != NULL) { - memset(p, 0, sizeof(T) * n); + OPENSSL_cleanse(p, sizeof(T) * n); LockedPageManager::instance.UnlockRange(p, sizeof(T) * n); } std::allocator::deallocate(p, n); @@ -246,7 +247,7 @@ struct zero_after_free_allocator : public std::allocator void deallocate(T* p, std::size_t n) { if (p != NULL) - memset(p, 0, sizeof(T) * n); + OPENSSL_cleanse(p, sizeof(T) * n); std::allocator::deallocate(p, n); } }; diff --git a/src/base58.h b/src/base58.h index 969d6a7..5865d9f 100644 --- a/src/base58.h +++ b/src/base58.h @@ -221,7 +221,7 @@ class CBase58Data vchData.resize(vchTemp.size() - 1); if (!vchData.empty()) memcpy(&vchData[0], &vchTemp[1], vchData.size()); - memset(&vchTemp[0], 0, vchTemp.size()); + OPENSSL_cleanse(&vchTemp[0], vchData.size()); return true; } diff --git a/src/crypter.cpp b/src/crypter.cpp index 181b8fa..a2b62a8 100644 --- a/src/crypter.cpp +++ b/src/crypter.cpp @@ -24,8 +24,8 @@ bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::v if (i != (int)WALLET_CRYPTO_KEY_SIZE) { - memset(&chKey, 0, sizeof chKey); - memset(&chIV, 0, sizeof chIV); + OPENSSL_cleanse(chKey, sizeof(chKey)); + OPENSSL_cleanse(chIV, sizeof(chIV)); return false; } diff --git a/src/crypter.h b/src/crypter.h index 04538a3..6f75170 100644 --- a/src/crypter.h +++ b/src/crypter.h @@ -76,8 +76,8 @@ class CCrypter void CleanKey() { - memset(&chKey, 0, sizeof chKey); - memset(&chIV, 0, sizeof chIV); + OPENSSL_cleanse(chKey, sizeof(chKey)); + OPENSSL_cleanse(chIV, sizeof(chIV)); fKeySet = false; } diff --git a/src/util.cpp b/src/util.cpp index 21bb817..c3c42f8 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -160,7 +160,7 @@ void RandAddSeedPerfmon() if (ret == ERROR_SUCCESS) { RAND_add(pdata, nSize, nSize/100.0); - memset(pdata, 0, nSize); + OPENSSL_cleanse(pdata, nSize); printf("RandAddSeed() %lu bytes\n", nSize); } #endif