Bitcoin ABC 0.30.7
P2P Digital Currency
cleanse.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2015 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#include <support/cleanse.h>
7
8#include <cstring>
9
10#if defined(_MSC_VER)
11#include <Windows.h> // For SecureZeroMemory.
12#endif
13
14void memory_cleanse(void *ptr, size_t len) {
15#if defined(_MSC_VER)
16 /* SecureZeroMemory is guaranteed not to be optimized out by MSVC. */
17 SecureZeroMemory(ptr, len);
18#else
19 std::memset(ptr, 0, len);
20
21 /*
22 * Memory barrier that scares the compiler away from optimizing out the
23 * memset.
24 *
25 * Quoting Adam Langley <[email protected]> in commit
26 * ad1907fe73334d6c696c8539646c21b11178f20f in BoringSSL (ISC License):
27 * As best as we can tell, this is sufficient to break any optimisations
28 * that might try to eliminate "superfluous" memsets.
29 * This method is used in memzero_explicit() the Linux kernel, too. Its
30 * advantage is that it is pretty efficient because the compiler can still
31 * implement the memset() efficiently, just not remove it entirely. See
32 * "Dead Store Elimination (Still) Considered Harmful" by Yang et al.
33 * (USENIX Security 2017) for more background.
34 */
35 __asm__ __volatile__("" : : "r"(ptr) : "memory");
36#endif
37}
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
Definition: cleanse.cpp:14