/src/botan/build/include/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/mem_ops.h> // IWYU pragma: export |
12 | | #include <botan/types.h> // IWYU pragma: export |
13 | | #include <algorithm> |
14 | | #include <deque> |
15 | | #include <type_traits> |
16 | | #include <vector> // IWYU pragma: export |
17 | | |
18 | | namespace Botan { |
19 | | |
20 | | template <typename T> |
21 | | #if !defined(_ITERATOR_DEBUG_LEVEL) || _ITERATOR_DEBUG_LEVEL == 0 |
22 | | /* |
23 | | * Assert exists to prevent someone from doing something that will |
24 | | * probably crash anyway (like secure_vector<non_POD_t> where ~non_POD_t |
25 | | * deletes a member pointer which was zeroed before it ran). |
26 | | * MSVC in debug mode uses non-integral proxy types in container types |
27 | | * like std::vector, thus we disable the check there. |
28 | | */ |
29 | | requires std::is_integral<T>::value |
30 | | #endif |
31 | | class secure_allocator { |
32 | | |
33 | | public: |
34 | | typedef T value_type; |
35 | | typedef std::size_t size_type; |
36 | | |
37 | | secure_allocator() noexcept = default; |
38 | | secure_allocator(const secure_allocator&) noexcept = default; |
39 | | secure_allocator& operator=(const secure_allocator&) noexcept = default; |
40 | | ~secure_allocator() noexcept = default; |
41 | | |
42 | | template <typename U> |
43 | | secure_allocator(const secure_allocator<U>&) noexcept {} |
44 | | |
45 | 14.2M | 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 | 45 | 296k | 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 | 45 | 13.8M | 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 | 45 | 92.6k | 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 | 45 | 258 | T* allocate(std::size_t n) { return static_cast<T*>(allocate_memory(n, sizeof(T))); } |
|
46 | | |
47 | 14.2M | 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 | 47 | 13.8M | 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 | 47 | 296k | 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 | 47 | 92.6k | 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 | 47 | 258 | void deallocate(T* p, std::size_t n) { deallocate_memory(p, n, sizeof(T)); } |
|
48 | | }; |
49 | | |
50 | | template <typename T, typename U> |
51 | | inline bool operator==(const secure_allocator<T>&, const secure_allocator<U>&) { |
52 | | return true; |
53 | | } |
54 | | |
55 | | template <typename T, typename U> |
56 | 2.35k | inline bool operator!=(const secure_allocator<T>&, const secure_allocator<U>&) { |
57 | 2.35k | return false; |
58 | 2.35k | } |
59 | | |
60 | | template <typename T> |
61 | | using secure_vector = std::vector<T, secure_allocator<T>>; |
62 | | template <typename T> |
63 | | using secure_deque = std::deque<T, secure_allocator<T>>; |
64 | | |
65 | | // For better compatibility with 1.10 API |
66 | | template <typename T> |
67 | | using SecureVector = secure_vector<T>; |
68 | | |
69 | | template <typename T> |
70 | | secure_vector<T> lock(const std::vector<T>& in) { |
71 | | return secure_vector<T>(in.begin(), in.end()); |
72 | | } |
73 | | |
74 | | template <typename T> |
75 | 405 | std::vector<T> unlock(const secure_vector<T>& in) { |
76 | 405 | return std::vector<T>(in.begin(), in.end()); |
77 | 405 | } |
78 | | |
79 | | template <typename T, typename Alloc, typename Alloc2> |
80 | 1.68k | std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::vector<T, Alloc2>& in) { |
81 | 1.68k | out.insert(out.end(), in.begin(), in.end()); |
82 | 1.68k | return out; |
83 | 1.68k | } 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 | 80 | 1.68k | std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::vector<T, Alloc2>& in) { | 81 | 1.68k | out.insert(out.end(), in.begin(), in.end()); | 82 | 1.68k | return out; | 83 | 1.68k | } |
Unexecuted instantiation: std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >& Botan::operator+=<unsigned char, std::__1::allocator<unsigned char>, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&, std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > const&) 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&) |
84 | | |
85 | | template <typename T, typename Alloc> |
86 | | std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, T in) { |
87 | | out.push_back(in); |
88 | | return out; |
89 | | } |
90 | | |
91 | | template <typename T, typename Alloc, typename L> |
92 | 1.31k | std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<const T*, L>& in) { |
93 | 1.31k | out.insert(out.end(), in.first, in.first + in.second); |
94 | 1.31k | return out; |
95 | 1.31k | } 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&) Line | Count | Source | 92 | 644 | std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<const T*, L>& in) { | 93 | 644 | out.insert(out.end(), in.first, in.first + in.second); | 94 | 644 | return out; | 95 | 644 | } |
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 | 92 | 672 | std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<const T*, L>& in) { | 93 | 672 | out.insert(out.end(), in.first, in.first + in.second); | 94 | 672 | return out; | 95 | 672 | } |
|
96 | | |
97 | | template <typename T, typename Alloc, typename L> |
98 | 901 | std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<T*, L>& in) { |
99 | 901 | out.insert(out.end(), in.first, in.first + in.second); |
100 | 901 | return out; |
101 | 901 | } |
102 | | |
103 | | /** |
104 | | * Zeroise the values; length remains unchanged |
105 | | * @param vec the vector to zeroise |
106 | | */ |
107 | | template <typename T, typename Alloc> |
108 | 1.07M | void zeroise(std::vector<T, Alloc>& vec) { |
109 | 1.07M | clear_mem(vec.data(), vec.size()); |
110 | 1.07M | } void Botan::zeroise<unsigned char, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&) Line | Count | Source | 108 | 552k | void zeroise(std::vector<T, Alloc>& vec) { | 109 | 552k | clear_mem(vec.data(), vec.size()); | 110 | 552k | } |
void Botan::zeroise<unsigned long, Botan::secure_allocator<unsigned long> >(std::__1::vector<unsigned long, Botan::secure_allocator<unsigned long> >&) Line | Count | Source | 108 | 391k | void zeroise(std::vector<T, Alloc>& vec) { | 109 | 391k | clear_mem(vec.data(), vec.size()); | 110 | 391k | } |
void Botan::zeroise<unsigned int, Botan::secure_allocator<unsigned int> >(std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> >&) Line | Count | Source | 108 | 127k | void zeroise(std::vector<T, Alloc>& vec) { | 109 | 127k | clear_mem(vec.data(), vec.size()); | 110 | 127k | } |
void Botan::zeroise<unsigned char, std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&) Line | Count | Source | 108 | 4.52k | void zeroise(std::vector<T, Alloc>& vec) { | 109 | 4.52k | clear_mem(vec.data(), vec.size()); | 110 | 4.52k | } |
void Botan::zeroise<unsigned short, Botan::secure_allocator<unsigned short> >(std::__1::vector<unsigned short, Botan::secure_allocator<unsigned short> >&) Line | Count | Source | 108 | 250 | void zeroise(std::vector<T, Alloc>& vec) { | 109 | 250 | clear_mem(vec.data(), vec.size()); | 110 | 250 | } |
|
111 | | |
112 | | /** |
113 | | * Zeroise the values then free the memory |
114 | | * @param vec the vector to zeroise and free |
115 | | */ |
116 | | template <typename T, typename Alloc> |
117 | 72.5k | void zap(std::vector<T, Alloc>& vec) { |
118 | 72.5k | zeroise(vec); |
119 | 72.5k | vec.clear(); |
120 | 72.5k | vec.shrink_to_fit(); |
121 | 72.5k | } void Botan::zap<unsigned char, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&) Line | Count | Source | 117 | 54 | void zap(std::vector<T, Alloc>& vec) { | 118 | 54 | zeroise(vec); | 119 | 54 | vec.clear(); | 120 | 54 | vec.shrink_to_fit(); | 121 | 54 | } |
void Botan::zap<unsigned long, Botan::secure_allocator<unsigned long> >(std::__1::vector<unsigned long, Botan::secure_allocator<unsigned long> >&) Line | Count | Source | 117 | 374 | void zap(std::vector<T, Alloc>& vec) { | 118 | 374 | zeroise(vec); | 119 | 374 | vec.clear(); | 120 | 374 | vec.shrink_to_fit(); | 121 | 374 | } |
Unexecuted instantiation: void Botan::zap<unsigned char, std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&) void Botan::zap<unsigned int, Botan::secure_allocator<unsigned int> >(std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> >&) Line | Count | Source | 117 | 71.8k | void zap(std::vector<T, Alloc>& vec) { | 118 | 71.8k | zeroise(vec); | 119 | 71.8k | vec.clear(); | 120 | 71.8k | vec.shrink_to_fit(); | 121 | 71.8k | } |
void Botan::zap<unsigned short, Botan::secure_allocator<unsigned short> >(std::__1::vector<unsigned short, Botan::secure_allocator<unsigned short> >&) Line | Count | Source | 117 | 250 | void zap(std::vector<T, Alloc>& vec) { | 118 | 250 | zeroise(vec); | 119 | 250 | vec.clear(); | 120 | 250 | vec.shrink_to_fit(); | 121 | 250 | } |
|
122 | | |
123 | | } // namespace Botan |
124 | | |
125 | | #endif |