Botan  2.10.0
Crypto and TLS for C++11
secmem.h
Go to the documentation of this file.
1 /*
2 * Secure Memory Buffers
3 * (C) 1999-2007,2012 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_SECURE_MEMORY_BUFFERS_H_
9 #define BOTAN_SECURE_MEMORY_BUFFERS_H_
10 
11 #include <botan/types.h> // IWYU pragma: export
12 #include <botan/mem_ops.h> // IWYU pragma: export
13 #include <vector> // IWYU pragma: export
14 #include <algorithm>
15 #include <deque>
16 #include <type_traits>
17 
18 namespace Botan {
19 
20 template<typename T>
22  {
23  public:
24  /*
25  * Assert exists to prevent someone from doing something that will
26  * probably crash anyway (like secure_vector<non_POD_t> where ~non_POD_t
27  * deletes a member pointer which was zeroed before it ran).
28  * MSVC in debug mode uses non-integral proxy types in container types
29  * like std::vector, thus we disable the check there.
30  */
31 #if !defined(_ITERATOR_DEBUG_LEVEL) || _ITERATOR_DEBUG_LEVEL == 0
32  static_assert(std::is_integral<T>::value, "secure_allocator supports only integer types");
33 #endif
34 
35  typedef T value_type;
36  typedef std::size_t size_type;
37 
38  secure_allocator() noexcept = default;
39  secure_allocator(const secure_allocator&) noexcept = default;
40  secure_allocator& operator=(const secure_allocator&) noexcept = default;
41  ~secure_allocator() noexcept = default;
42 
43  template<typename U>
44  secure_allocator(const secure_allocator<U>&) noexcept {}
45 
46  T* allocate(std::size_t n)
47  {
48  return static_cast<T*>(allocate_memory(n, sizeof(T)));
49  }
50 
51  void deallocate(T* p, std::size_t n)
52  {
53  deallocate_memory(p, n, sizeof(T));
54  }
55  };
56 
57 template<typename T, typename U> inline bool
59  { return true; }
60 
61 template<typename T, typename U> inline bool
63  { return false; }
64 
65 template<typename T> using secure_vector = std::vector<T, secure_allocator<T>>;
66 template<typename T> using secure_deque = std::deque<T, secure_allocator<T>>;
67 
68 // For better compatibility with 1.10 API
69 template<typename T> using SecureVector = secure_vector<T>;
70 
71 template<typename T>
72 std::vector<T> unlock(const secure_vector<T>& in)
73  {
74  std::vector<T> out(in.size());
75  copy_mem(out.data(), in.data(), in.size());
76  return out;
77  }
78 
79 template<typename T, typename Alloc>
80 size_t buffer_insert(std::vector<T, Alloc>& buf,
81  size_t buf_offset,
82  const T input[],
83  size_t input_length)
84  {
85  BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
86  const size_t to_copy = std::min(input_length, buf.size() - buf_offset);
87  if(to_copy > 0)
88  {
89  copy_mem(&buf[buf_offset], input, to_copy);
90  }
91  return to_copy;
92  }
93 
94 template<typename T, typename Alloc, typename Alloc2>
95 size_t buffer_insert(std::vector<T, Alloc>& buf,
96  size_t buf_offset,
97  const std::vector<T, Alloc2>& input)
98  {
99  BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
100  const size_t to_copy = std::min(input.size(), buf.size() - buf_offset);
101  if(to_copy > 0)
102  {
103  copy_mem(&buf[buf_offset], input.data(), to_copy);
104  }
105  return to_copy;
106  }
107 
108 template<typename T, typename Alloc, typename Alloc2>
109 std::vector<T, Alloc>&
110 operator+=(std::vector<T, Alloc>& out,
111  const std::vector<T, Alloc2>& in)
112  {
113  const size_t copy_offset = out.size();
114  out.resize(out.size() + in.size());
115  if(in.size() > 0)
116  {
117  copy_mem(&out[copy_offset], in.data(), in.size());
118  }
119  return out;
120  }
121 
122 template<typename T, typename Alloc>
123 std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, T in)
124  {
125  out.push_back(in);
126  return out;
127  }
128 
129 template<typename T, typename Alloc, typename L>
130 std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out,
131  const std::pair<const T*, L>& in)
132  {
133  const size_t copy_offset = out.size();
134  out.resize(out.size() + in.second);
135  if(in.second > 0)
136  {
137  copy_mem(&out[copy_offset], in.first, in.second);
138  }
139  return out;
140  }
141 
142 template<typename T, typename Alloc, typename L>
143 std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out,
144  const std::pair<T*, L>& in)
145  {
146  const size_t copy_offset = out.size();
147  out.resize(out.size() + in.second);
148  if(in.second > 0)
149  {
150  copy_mem(&out[copy_offset], in.first, in.second);
151  }
152  return out;
153  }
154 
155 /**
156 * Zeroise the values; length remains unchanged
157 * @param vec the vector to zeroise
158 */
159 template<typename T, typename Alloc>
160 void zeroise(std::vector<T, Alloc>& vec)
161  {
162  clear_mem(vec.data(), vec.size());
163  }
164 
165 /**
166 * Zeroise the values then free the memory
167 * @param vec the vector to zeroise and free
168 */
169 template<typename T, typename Alloc>
170 void zap(std::vector<T, Alloc>& vec)
171  {
172  zeroise(vec);
173  vec.clear();
174  vec.shrink_to_fit();
175  }
176 
177 }
178 
179 #endif
void deallocate(T *p, std::size_t n)
Definition: secmem.h:51
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:90
void zap(std::vector< T, Alloc > &vec)
Definition: secmem.h:170
std::deque< T, secure_allocator< T > > secure_deque
Definition: secmem.h:66
BOTAN_MALLOC_FN void * allocate_memory(size_t elems, size_t elem_size)
Definition: mem_ops.cpp:18
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:111
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:68
T * allocate(std::size_t n)
Definition: secmem.h:46
secure_allocator() noexcept=default
std::size_t size_type
Definition: secmem.h:36
secure_vector< T > SecureVector
Definition: secmem.h:69
void deallocate_memory(void *p, size_t elems, size_t elem_size)
Definition: mem_ops.cpp:31
std::vector< T, Alloc > & operator+=(std::vector< T, Alloc > &out, const std::vector< T, Alloc2 > &in)
Definition: secmem.h:110
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:122
Definition: alg_id.cpp:13
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:72
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
Definition: secmem.h:80
fe T
Definition: ge.cpp:37
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:75
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:160