Bitcoin Core  22.99.0
P2P Digital Currency
secure.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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 #ifndef BITCOIN_SUPPORT_ALLOCATORS_SECURE_H
7 #define BITCOIN_SUPPORT_ALLOCATORS_SECURE_H
8 
9 #include <support/lockedpool.h>
10 #include <support/cleanse.h>
11 
12 #include <memory>
13 #include <string>
14 
15 //
16 // Allocator that locks its contents from being paged
17 // out of memory and clears its contents before deletion.
18 //
19 template <typename T>
20 struct secure_allocator : public std::allocator<T> {
21  using base = std::allocator<T>;
22  using traits = std::allocator_traits<base>;
23  using size_type = typename traits::size_type;
24  using difference_type = typename traits::difference_type;
25  using pointer = typename traits::pointer;
26  using const_pointer = typename traits::const_pointer;
27  using value_type = typename traits::value_type;
28  secure_allocator() noexcept {}
29  secure_allocator(const secure_allocator& a) noexcept : base(a) {}
30  template <typename U>
31  secure_allocator(const secure_allocator<U>& a) noexcept : base(a)
32  {
33  }
34  ~secure_allocator() noexcept {}
35  template <typename _Other>
36  struct rebind {
38  };
39 
40  T* allocate(std::size_t n, const void* hint = 0)
41  {
42  T* allocation = static_cast<T*>(LockedPoolManager::Instance().alloc(sizeof(T) * n));
43  if (!allocation) {
44  throw std::bad_alloc();
45  }
46  return allocation;
47  }
48 
49  void deallocate(T* p, std::size_t n)
50  {
51  if (p != nullptr) {
52  memory_cleanse(p, sizeof(T) * n);
53  }
55  }
56 };
57 
58 // This is exactly like std::string, but with a custom allocator.
59 typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
60 
61 #endif // BITCOIN_SUPPORT_ALLOCATORS_SECURE_H
secure_allocator::secure_allocator
secure_allocator(const secure_allocator< U > &a) noexcept
Definition: secure.h:31
secure_allocator::secure_allocator
secure_allocator() noexcept
Definition: secure.h:28
secure_allocator::difference_type
typename traits::difference_type difference_type
Definition: secure.h:24
secure_allocator
Definition: secure.h:20
secure_allocator::~secure_allocator
~secure_allocator() noexcept
Definition: secure.h:34
secure_allocator::base
std::allocator< T > base
Definition: secure.h:21
LockedPoolManager::Instance
static LockedPoolManager & Instance()
Return the current instance, or create it once.
Definition: lockedpool.h:222
memory_cleanse
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
Definition: cleanse.cpp:14
secure_allocator::pointer
typename traits::pointer pointer
Definition: secure.h:25
SecureString
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:59
secure_allocator::traits
std::allocator_traits< base > traits
Definition: secure.h:22
LockedPool::alloc
void * alloc(size_t size)
Allocate size bytes from this arena.
Definition: lockedpool.cpp:294
cleanse.h
secure_allocator::value_type
typename traits::value_type value_type
Definition: secure.h:27
secure_allocator::rebind
Definition: secure.h:36
secure_allocator::const_pointer
typename traits::const_pointer const_pointer
Definition: secure.h:26
secure_allocator::rebind::other
secure_allocator< _Other > other
Definition: secure.h:37
secure_allocator::allocate
T * allocate(std::size_t n, const void *hint=0)
Definition: secure.h:40
LockedPool::free
void free(void *ptr)
Free a previously allocated chunk of memory.
Definition: lockedpool.cpp:316
secure_allocator::size_type
typename traits::size_type size_type
Definition: secure.h:23
lockedpool.h
secure_allocator::deallocate
void deallocate(T *p, std::size_t n)
Definition: secure.h:49
T
#define T(expected, seed, data)
secure_allocator::secure_allocator
secure_allocator(const secure_allocator &a) noexcept
Definition: secure.h:29