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