Replace OPENSSL_cleanse
This commit is contained in:
@@ -3,11 +3,33 @@
|
||||
// Distributed under the GPLv3 software license, see the accompanying
|
||||
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
||||
|
||||
#include "cleanse.h"
|
||||
#include <support/cleanse.h>
|
||||
|
||||
#include <openssl/crypto.h>
|
||||
#include <cstring>
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#include <Windows.h> // For SecureZeroMemory.
|
||||
#endif
|
||||
|
||||
void memory_cleanse(void *ptr, size_t len)
|
||||
{
|
||||
OPENSSL_cleanse(ptr, len);
|
||||
#if defined(_MSC_VER)
|
||||
/* SecureZeroMemory is guaranteed not to be optimized out by MSVC. */
|
||||
SecureZeroMemory(ptr, len);
|
||||
#else
|
||||
std::memset(ptr, 0, len);
|
||||
|
||||
/* Memory barrier that scares the compiler away from optimizing out the memset.
|
||||
*
|
||||
* Quoting Adam Langley <agl@google.com> in commit ad1907fe73334d6c696c8539646c21b11178f20f
|
||||
* in BoringSSL (ISC License):
|
||||
* As best as we can tell, this is sufficient to break any optimisations that
|
||||
* might try to eliminate "superfluous" memsets.
|
||||
* This method is used in memzero_explicit() the Linux kernel, too. Its advantage is that it
|
||||
* is pretty efficient because the compiler can still implement the memset() efficiently,
|
||||
* just not remove it entirely. See "Dead Store Elimination (Still) Considered Harmful" by
|
||||
* Yang et al. (USENIX Security 2017) for more background.
|
||||
*/
|
||||
__asm__ __volatile__("" : : "r"(ptr) : "memory");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/** Secure overwrite a buffer (possibly containing secret data) with zero-bytes. The write
|
||||
* operation will not be optimized out by the compiler. */
|
||||
void memory_cleanse(void *ptr, size_t len);
|
||||
|
||||
#endif // BITCOIN_SUPPORT_CLEANSE_H
|
||||
|
||||
Reference in New Issue
Block a user