2015-01-20 16:23:25 -08:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2022-05-11 16:29:59 -07:00
|
|
|
// Copyright (c) 2019-2022 The Zcash developers
|
2015-01-20 16:23:25 -08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2019-07-18 07:16:09 -07:00
|
|
|
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
2015-01-20 16:23:25 -08:00
|
|
|
|
|
|
|
#include "cleanse.h"
|
|
|
|
|
2017-08-29 22:26:12 -07:00
|
|
|
#include <cstring>
|
2015-01-20 16:23:25 -08:00
|
|
|
|
2017-11-09 12:06:49 -08:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#include <Windows.h> // For SecureZeroMemory.
|
|
|
|
#endif
|
|
|
|
|
2015-01-20 16:23:25 -08:00
|
|
|
void memory_cleanse(void *ptr, size_t len)
|
|
|
|
{
|
2019-06-05 13:43:28 -07:00
|
|
|
#if defined(_MSC_VER)
|
2019-06-05 13:44:04 -07:00
|
|
|
/* SecureZeroMemory is guaranteed not to be optimized out by MSVC. */
|
2019-06-05 13:43:28 -07:00
|
|
|
SecureZeroMemory(ptr, len);
|
|
|
|
#else
|
2017-08-29 22:26:12 -07:00
|
|
|
std::memset(ptr, 0, len);
|
|
|
|
|
2020-04-30 16:00:49 -07:00
|
|
|
/* In order to prevent the compiler from optimizing out the memset, this uses an
|
|
|
|
* unremovable (see https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Volatile )
|
2021-11-12 02:02:19 -08:00
|
|
|
* asm block that the compiler must assume could access arbitrary memory, including
|
2020-04-30 16:00:49 -07:00
|
|
|
* the zero bytes written by std::memset.
|
2019-06-05 13:44:04 -07:00
|
|
|
*
|
|
|
|
* 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.
|
2020-04-30 16:00:49 -07:00
|
|
|
* This method is used by memzero_explicit() in the Linux kernel, too. Its advantage is that it
|
2019-06-05 13:44:04 -07:00
|
|
|
* 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.
|
|
|
|
*/
|
2017-08-29 22:26:12 -07:00
|
|
|
__asm__ __volatile__("" : : "r"(ptr) : "memory");
|
|
|
|
#endif
|
2015-01-20 16:23:25 -08:00
|
|
|
}
|