/src/Botan-3.13.0/build/include/public/botan/secmem.h
Line | Count | Source |
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/allocator.h> |
12 | | #include <botan/types.h> // IWYU pragma: export |
13 | | #include <span> |
14 | | #include <type_traits> |
15 | | #include <vector> // IWYU pragma: export |
16 | | |
17 | | #if !defined(BOTAN_IS_BEING_BUILT) && !defined(BOTAN_DISABLE_DEPRECATED_FEATURES) |
18 | | // TODO(Botan4) remove this |
19 | | #include <deque> |
20 | | #endif |
21 | | |
22 | | namespace Botan { |
23 | | |
24 | | template <typename T> |
25 | | #if !defined(_ITERATOR_DEBUG_LEVEL) || _ITERATOR_DEBUG_LEVEL == 0 |
26 | | /* |
27 | | * Check exists to prevent someone from doing something that will |
28 | | * probably crash anyway (like secure_vector<non_POD_t> where ~non_POD_t |
29 | | * deletes a member pointer which was zeroed before it ran). |
30 | | * MSVC in debug mode uses non-integral proxy types in container types |
31 | | * like std::vector, thus we disable the check there. |
32 | | */ |
33 | | requires std::is_integral_v<T> || std::is_enum_v<T> |
34 | | #endif |
35 | | |
36 | | /** |
37 | | * An allocator which zeroizes memory before releasing it |
38 | | * |
39 | | * Restricted to integral and enum types, since a non-trivial destructor |
40 | | * would run after the object had already been zeroized. |
41 | | */ |
42 | | class secure_allocator { |
43 | | |
44 | | public: |
45 | | /** |
46 | | * The type being allocated |
47 | | */ |
48 | | typedef T value_type; |
49 | | |
50 | | /** |
51 | | * The type used to express allocation sizes |
52 | | */ |
53 | | typedef std::size_t size_type; |
54 | | |
55 | | /** |
56 | | * Default constructor |
57 | | */ |
58 | | secure_allocator() noexcept = default; |
59 | | |
60 | | /** |
61 | | * Copy constructor |
62 | | */ |
63 | | secure_allocator(const secure_allocator&) noexcept = default; |
64 | | |
65 | | /** |
66 | | * Copy assignment |
67 | | * @return reference to this |
68 | | */ |
69 | | secure_allocator& operator=(const secure_allocator&) noexcept = default; |
70 | | |
71 | | /** |
72 | | * Move constructor |
73 | | */ |
74 | | secure_allocator(secure_allocator&&) noexcept = default; |
75 | | |
76 | | /** |
77 | | * Move assignment |
78 | | * @return reference to this |
79 | | */ |
80 | | secure_allocator& operator=(secure_allocator&&) noexcept = default; |
81 | | |
82 | | ~secure_allocator() noexcept = default; |
83 | | |
84 | | /** |
85 | | * Convert an allocator for a different type |
86 | | */ |
87 | | template <typename U> |
88 | | explicit secure_allocator(const secure_allocator<U>& /*other*/) noexcept {} |
89 | | |
90 | | /** |
91 | | * Allocate storage for n objects |
92 | | * @param n the number of objects |
93 | | * @return a pointer to the allocated storage |
94 | | */ |
95 | 11.4M | T* allocate(std::size_t n) { return static_cast<T*>(allocate_memory(n, sizeof(T))); }Botan::secure_allocator<unsigned char>::allocate(unsigned long) Line | Count | Source | 95 | 3.97M | T* allocate(std::size_t n) { return static_cast<T*>(allocate_memory(n, sizeof(T))); } |
Botan::secure_allocator<unsigned long>::allocate(unsigned long) Line | Count | Source | 95 | 6.82M | T* allocate(std::size_t n) { return static_cast<T*>(allocate_memory(n, sizeof(T))); } |
Botan::secure_allocator<unsigned int>::allocate(unsigned long) Line | Count | Source | 95 | 657k | T* allocate(std::size_t n) { return static_cast<T*>(allocate_memory(n, sizeof(T))); } |
Botan::secure_allocator<unsigned short>::allocate(unsigned long) Line | Count | Source | 95 | 12.3k | T* allocate(std::size_t n) { return static_cast<T*>(allocate_memory(n, sizeof(T))); } |
|
96 | | |
97 | | /** |
98 | | * Zeroize and release storage previously returned by allocate |
99 | | * @param p the pointer to release |
100 | | * @param n the number of objects p was allocated for |
101 | | */ |
102 | 11.4M | void deallocate(T* p, std::size_t n) { deallocate_memory(p, n, sizeof(T)); }Botan::secure_allocator<unsigned char>::deallocate(unsigned char*, unsigned long) Line | Count | Source | 102 | 3.97M | void deallocate(T* p, std::size_t n) { deallocate_memory(p, n, sizeof(T)); } |
Botan::secure_allocator<unsigned long>::deallocate(unsigned long*, unsigned long) Line | Count | Source | 102 | 6.82M | void deallocate(T* p, std::size_t n) { deallocate_memory(p, n, sizeof(T)); } |
Botan::secure_allocator<unsigned int>::deallocate(unsigned int*, unsigned long) Line | Count | Source | 102 | 657k | void deallocate(T* p, std::size_t n) { deallocate_memory(p, n, sizeof(T)); } |
Botan::secure_allocator<unsigned short>::deallocate(unsigned short*, unsigned long) Line | Count | Source | 102 | 12.3k | void deallocate(T* p, std::size_t n) { deallocate_memory(p, n, sizeof(T)); } |
|
103 | | }; |
104 | | |
105 | | /** |
106 | | * Compare two secure allocators |
107 | | * |
108 | | * All instances are interchangeable, so this is always true. |
109 | | * @return always true |
110 | | */ |
111 | | template <typename T, typename U> |
112 | | inline bool operator==(const secure_allocator<T>& /*a*/, const secure_allocator<U>& /*b*/) { |
113 | | return true; |
114 | | } |
115 | | |
116 | | /** |
117 | | * Compare two secure allocators |
118 | | * |
119 | | * All instances are interchangeable, so this is always false. |
120 | | * @return always false |
121 | | */ |
122 | | template <typename T, typename U> |
123 | 878k | inline bool operator!=(const secure_allocator<T>& /*a*/, const secure_allocator<U>& /*b*/) { |
124 | 878k | return false; |
125 | 878k | } |
126 | | |
127 | | template <typename T> |
128 | | using secure_vector = std::vector<T, secure_allocator<T>>; |
129 | | |
130 | | #if !defined(BOTAN_IS_BEING_BUILT) && !defined(BOTAN_DISABLE_DEPRECATED_FEATURES) |
131 | | template <typename T> |
132 | | using secure_deque = std::deque<T, secure_allocator<T>>; |
133 | | #endif |
134 | | |
135 | | // For better compatibility with 1.10 API |
136 | | template <typename T> |
137 | | using SecureVector = secure_vector<T>; |
138 | | |
139 | | /** |
140 | | * Copy a vector into a secure_vector |
141 | | * @param in the vector to copy |
142 | | * @return a secure_vector holding the same contents |
143 | | */ |
144 | | template <typename T> |
145 | | secure_vector<T> lock(const std::vector<T>& in) { |
146 | | return secure_vector<T>(in.begin(), in.end()); |
147 | | } |
148 | | |
149 | | /** |
150 | | * Copy a secure_vector into an ordinary vector |
151 | | * @param in the vector to copy |
152 | | * @return a std::vector holding the same contents |
153 | | */ |
154 | | template <typename T> |
155 | 0 | std::vector<T> unlock(const secure_vector<T>& in) { |
156 | 0 | return std::vector<T>(in.begin(), in.end()); |
157 | 0 | } |
158 | | |
159 | | // TODO(Botan4) remove these += operators entirely |
160 | | |
161 | | /** |
162 | | * Append the contents of one vector to another |
163 | | * @param out the vector to append to |
164 | | * @param in the vector to append |
165 | | * @return reference to out |
166 | | */ |
167 | | template <typename T, typename Alloc, typename Alloc2> |
168 | 14 | std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::vector<T, Alloc2>& in) { |
169 | 14 | out.insert(out.end(), in.begin(), in.end()); |
170 | 14 | return out; |
171 | 14 | } std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >& Botan::operator+=<unsigned char, Botan::secure_allocator<unsigned char>, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&, std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > const&) Line | Count | Source | 168 | 14 | std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::vector<T, Alloc2>& in) { | 169 | 14 | out.insert(out.end(), in.begin(), in.end()); | 170 | 14 | return out; | 171 | 14 | } |
Unexecuted instantiation: std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >& Botan::operator+=<unsigned char, Botan::secure_allocator<unsigned char>, std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&) |
172 | | |
173 | | /** |
174 | | * Append the contents of a span to a vector |
175 | | * @param out the vector to append to |
176 | | * @param in the elements to append |
177 | | * @return reference to out |
178 | | */ |
179 | | template <typename T, typename Alloc> |
180 | | std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, std::span<const T> in) { |
181 | | out.insert(out.end(), in.begin(), in.end()); |
182 | | return out; |
183 | | } |
184 | | |
185 | | /** |
186 | | * Append a single element to a vector |
187 | | * @param out the vector to append to |
188 | | * @param in the element to append |
189 | | * @return reference to out |
190 | | */ |
191 | | template <typename T, typename Alloc> |
192 | | std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, T in) { |
193 | | out.push_back(in); |
194 | | return out; |
195 | | } |
196 | | |
197 | | /** |
198 | | * Append a (pointer, length) pair to a vector |
199 | | * @param out the vector to append to |
200 | | * @param in the elements to append |
201 | | * @return reference to out |
202 | | */ |
203 | | template <typename T, typename Alloc, typename L> |
204 | 133k | std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<const T*, L>& in) { |
205 | 133k | if(in.second > 0) { |
206 | 133k | out.insert(out.end(), in.first, in.first + in.second); |
207 | 133k | } |
208 | 133k | return out; |
209 | 133k | } Unexecuted instantiation: std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >& Botan::operator+=<unsigned char, Botan::secure_allocator<unsigned char>, unsigned long>(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&, std::__1::pair<unsigned char const*, unsigned long> const&) std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >& Botan::operator+=<unsigned char, std::__1::allocator<unsigned char>, unsigned long>(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&, std::__1::pair<unsigned char const*, unsigned long> const&) Line | Count | Source | 204 | 133k | std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<const T*, L>& in) { | 205 | 133k | if(in.second > 0) { | 206 | 133k | out.insert(out.end(), in.first, in.first + in.second); | 207 | 133k | } | 208 | 133k | return out; | 209 | 133k | } |
|
210 | | |
211 | | /** |
212 | | * Append a (pointer, length) pair to a vector |
213 | | * @param out the vector to append to |
214 | | * @param in the elements to append |
215 | | * @return reference to out |
216 | | */ |
217 | | template <typename T, typename Alloc, typename L> |
218 | 458k | std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<T*, L>& in) { |
219 | 458k | if(in.second > 0) { |
220 | 458k | out.insert(out.end(), in.first, in.first + in.second); |
221 | 458k | } |
222 | 458k | return out; |
223 | 458k | } |
224 | | |
225 | | /** |
226 | | * Zeroise the values; length remains unchanged |
227 | | * |
228 | | * Note this is not intended for cases where the compiler might elide |
229 | | * the writes as being without side-effects; use secure_scrub_memory |
230 | | * for that. |
231 | | * |
232 | | * TODO(Botan4): make these not-inlined and only for secure_vector, eg declare |
233 | | * void zeroize(secure_vector<uint8_t>& v); |
234 | | * void zeroize(secure_vector<uint16_t>& v); |
235 | | * void zeroize(secure_vector<uint32_t>& v); |
236 | | * void zeroize(secure_vector<uint64_t>& v); |
237 | | * |
238 | | * @param vec the vector to zeroise |
239 | | */ |
240 | | template <typename T, typename Alloc> |
241 | 5.35M | void zeroise(std::vector<T, Alloc>& vec) { |
242 | 226M | for(size_t i = 0; i != vec.size(); ++i) { |
243 | 221M | vec[i] = static_cast<T>(0); |
244 | 221M | } |
245 | 5.35M | } void Botan::zeroise<unsigned char, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&) Line | Count | Source | 241 | 4.99M | void zeroise(std::vector<T, Alloc>& vec) { | 242 | 220M | for(size_t i = 0; i != vec.size(); ++i) { | 243 | 215M | vec[i] = static_cast<T>(0); | 244 | 215M | } | 245 | 4.99M | } |
void Botan::zeroise<unsigned int, Botan::secure_allocator<unsigned int> >(std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> >&) Line | Count | Source | 241 | 5.53k | void zeroise(std::vector<T, Alloc>& vec) { | 242 | 5.53k | for(size_t i = 0; i != vec.size(); ++i) { | 243 | 0 | vec[i] = static_cast<T>(0); | 244 | 0 | } | 245 | 5.53k | } |
void Botan::zeroise<unsigned long, Botan::secure_allocator<unsigned long> >(std::__1::vector<unsigned long, Botan::secure_allocator<unsigned long> >&) Line | Count | Source | 241 | 1.42k | void zeroise(std::vector<T, Alloc>& vec) { | 242 | 1.42k | for(size_t i = 0; i != vec.size(); ++i) { | 243 | 0 | vec[i] = static_cast<T>(0); | 244 | 0 | } | 245 | 1.42k | } |
void Botan::zeroise<unsigned short, Botan::secure_allocator<unsigned short> >(std::__1::vector<unsigned short, Botan::secure_allocator<unsigned short> >&) Line | Count | Source | 241 | 1.62k | void zeroise(std::vector<T, Alloc>& vec) { | 242 | 1.62k | for(size_t i = 0; i != vec.size(); ++i) { | 243 | 0 | vec[i] = static_cast<T>(0); | 244 | 0 | } | 245 | 1.62k | } |
void Botan::zeroise<unsigned char, std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&) Line | Count | Source | 241 | 355k | void zeroise(std::vector<T, Alloc>& vec) { | 242 | 6.02M | for(size_t i = 0; i != vec.size(); ++i) { | 243 | 5.66M | vec[i] = static_cast<T>(0); | 244 | 5.66M | } | 245 | 355k | } |
|
246 | | |
247 | | /** |
248 | | * Zeroise the values then free the memory |
249 | | * |
250 | | * TODO(Botan4): make these not-inlined and only for secure_vector, eg declare |
251 | | * void zap(secure_vector<uint8_t>& v); |
252 | | * void zap(secure_vector<uint16_t>& v); |
253 | | * void zap(secure_vector<uint32_t>& v); |
254 | | * void zap(secure_vector<uint64_t>& v); |
255 | | * |
256 | | * [And maybe rename as well] |
257 | | * |
258 | | * @param vec the vector to zeroise and free |
259 | | */ |
260 | | template <typename T, typename Alloc> |
261 | 9.35k | void zap(std::vector<T, Alloc>& vec) { |
262 | 9.35k | zeroise(vec); |
263 | 9.35k | vec.clear(); |
264 | 9.35k | vec.shrink_to_fit(); |
265 | 9.35k | } void Botan::zap<unsigned int, Botan::secure_allocator<unsigned int> >(std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> >&) Line | Count | Source | 261 | 5.53k | void zap(std::vector<T, Alloc>& vec) { | 262 | 5.53k | zeroise(vec); | 263 | 5.53k | vec.clear(); | 264 | 5.53k | vec.shrink_to_fit(); | 265 | 5.53k | } |
void Botan::zap<unsigned long, Botan::secure_allocator<unsigned long> >(std::__1::vector<unsigned long, Botan::secure_allocator<unsigned long> >&) Line | Count | Source | 261 | 1.42k | void zap(std::vector<T, Alloc>& vec) { | 262 | 1.42k | zeroise(vec); | 263 | 1.42k | vec.clear(); | 264 | 1.42k | vec.shrink_to_fit(); | 265 | 1.42k | } |
void Botan::zap<unsigned char, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&) Line | Count | Source | 261 | 765 | void zap(std::vector<T, Alloc>& vec) { | 262 | 765 | zeroise(vec); | 263 | 765 | vec.clear(); | 264 | 765 | vec.shrink_to_fit(); | 265 | 765 | } |
void Botan::zap<unsigned short, Botan::secure_allocator<unsigned short> >(std::__1::vector<unsigned short, Botan::secure_allocator<unsigned short> >&) Line | Count | Source | 261 | 1.62k | void zap(std::vector<T, Alloc>& vec) { | 262 | 1.62k | zeroise(vec); | 263 | 1.62k | vec.clear(); | 264 | 1.62k | vec.shrink_to_fit(); | 265 | 1.62k | } |
Unexecuted instantiation: void Botan::zap<unsigned char, std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&) |
266 | | |
267 | | } // namespace Botan |
268 | | |
269 | | #endif |