Skip to content

Commit

Permalink
use openssl_cleanse instead of memset for crit mem spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
iamunick committed May 20, 2014
1 parent b42211e commit 18a2c79
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/allocators.h
Expand Up @@ -9,6 +9,7 @@
#include <string>
#include <boost/thread/mutex.hpp>
#include <map>
#include <openssl/crypto.h> // for OPENSSL_cleanse()

#ifdef WIN32
#ifdef _WIN32_WINNT
Expand Down Expand Up @@ -212,7 +213,7 @@ struct secure_allocator : public std::allocator<T>
{
if (p != NULL)
{
memset(p, 0, sizeof(T) * n);
OPENSSL_cleanse(p, sizeof(T) * n);
LockedPageManager::instance.UnlockRange(p, sizeof(T) * n);
}
std::allocator<T>::deallocate(p, n);
Expand Down Expand Up @@ -246,7 +247,7 @@ struct zero_after_free_allocator : public std::allocator<T>
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<T>::deallocate(p, n);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/base58.h
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/crypter.cpp
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/crypter.h
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/util.cpp
Expand Up @@ -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
Expand Down

0 comments on commit 18a2c79

Please sign in to comment.