Bitcoin Core  23.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-2021 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 = nullptr)
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
void free(void *ptr)
Free a previously allocated chunk of memory.
Definition: lockedpool.cpp:309
void * alloc(size_t size)
Allocate size bytes from this arena.
Definition: lockedpool.cpp:287
static LockedPoolManager & Instance()
Return the current instance, or create it once.
Definition: lockedpool.h:222
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
Definition: cleanse.cpp:14
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:59
secure_allocator< _Other > other
Definition: secure.h:37
std::allocator_traits< base > traits
Definition: secure.h:22
std::allocator< T > base
Definition: secure.h:21
typename traits::const_pointer const_pointer
Definition: secure.h:26
typename traits::value_type value_type
Definition: secure.h:27
typename traits::pointer pointer
Definition: secure.h:25
typename traits::difference_type difference_type
Definition: secure.h:24
secure_allocator(const secure_allocator< U > &a) noexcept
Definition: secure.h:31
T * allocate(std::size_t n, const void *hint=nullptr)
Definition: secure.h:40
~secure_allocator() noexcept
Definition: secure.h:34
typename traits::size_type size_type
Definition: secure.h:23
secure_allocator() noexcept
Definition: secure.h:28
secure_allocator(const secure_allocator &a) noexcept
Definition: secure.h:29
void deallocate(T *p, std::size_t n)
Definition: secure.h:49