Bitcoin ABC  0.29.4
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 <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> struct secure_allocator : public std::allocator<T> {
20  using base = std::allocator<T>;
21  using traits = std::allocator_traits<base>;
22  using size_type = typename traits::size_type;
23  using difference_type = typename traits::difference_type;
24  using pointer = typename traits::pointer;
25  using const_pointer = typename traits::const_pointer;
26  using value_type = typename traits::value_type;
27  secure_allocator() noexcept {}
28  secure_allocator(const secure_allocator &a) noexcept : base(a) {}
29  template <typename U>
30  secure_allocator(const secure_allocator<U> &a) noexcept : base(a) {}
31  ~secure_allocator() noexcept {}
32  template <typename _Other> struct rebind {
34  };
35 
36  T *allocate(std::size_t n, const void *hint = 0) {
37  T *allocation = static_cast<T *>(
38  LockedPoolManager::Instance().alloc(sizeof(T) * n));
39  if (!allocation) {
40  throw std::bad_alloc();
41  }
42  return allocation;
43  }
44 
45  void deallocate(T *p, std::size_t n) {
46  if (p != nullptr) {
47  memory_cleanse(p, sizeof(T) * n);
48  }
50  }
51 };
52 
53 // This is exactly like std::string, but with a custom allocator.
54 typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char>>
56 
57 #endif // BITCOIN_SUPPORT_ALLOCATORS_SECURE_H
void free(void *ptr)
Free a previously allocated chunk of memory.
Definition: lockedpool.cpp:318
void * alloc(size_t size)
Allocate size bytes from this arena.
Definition: lockedpool.cpp:296
static LockedPoolManager & Instance()
Return the current instance, or create it once.
Definition: lockedpool.h:236
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:55
secure_allocator< _Other > other
Definition: secure.h:33
std::allocator_traits< base > traits
Definition: secure.h:21
std::allocator< T > base
Definition: secure.h:20
typename traits::const_pointer const_pointer
Definition: secure.h:25
typename traits::value_type value_type
Definition: secure.h:26
typename traits::pointer pointer
Definition: secure.h:24
typename traits::difference_type difference_type
Definition: secure.h:23
secure_allocator(const secure_allocator< U > &a) noexcept
Definition: secure.h:30
~secure_allocator() noexcept
Definition: secure.h:31
typename traits::size_type size_type
Definition: secure.h:22
secure_allocator() noexcept
Definition: secure.h:27
secure_allocator(const secure_allocator &a) noexcept
Definition: secure.h:28
T * allocate(std::size_t n, const void *hint=0)
Definition: secure.h:36
void deallocate(T *p, std::size_t n)
Definition: secure.h:45