/src/botan/build/include/botan/secmem.h
Line | Count | Source (jump to first uncovered line) |
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> |
21 | | class secure_allocator |
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 | 83.6M | { |
48 | 83.6M | return static_cast<T*>(allocate_memory(n, sizeof(T))); |
49 | 83.6M | } Botan::secure_allocator<unsigned long>::allocate(unsigned long) Line | Count | Source | 47 | 80.0M | { | 48 | 80.0M | return static_cast<T*>(allocate_memory(n, sizeof(T))); | 49 | 80.0M | } |
Botan::secure_allocator<unsigned char>::allocate(unsigned long) Line | Count | Source | 47 | 3.48M | { | 48 | 3.48M | return static_cast<T*>(allocate_memory(n, sizeof(T))); | 49 | 3.48M | } |
Botan::secure_allocator<unsigned int>::allocate(unsigned long) Line | Count | Source | 47 | 127k | { | 48 | 127k | return static_cast<T*>(allocate_memory(n, sizeof(T))); | 49 | 127k | } |
Unexecuted instantiation: Botan::secure_allocator<unsigned short>::allocate(unsigned long) |
50 | | |
51 | | void deallocate(T* p, std::size_t n) |
52 | 83.6M | { |
53 | 83.6M | deallocate_memory(p, n, sizeof(T)); |
54 | 83.6M | } Botan::secure_allocator<unsigned long>::deallocate(unsigned long*, unsigned long) Line | Count | Source | 52 | 80.0M | { | 53 | 80.0M | deallocate_memory(p, n, sizeof(T)); | 54 | 80.0M | } |
Botan::secure_allocator<unsigned char>::deallocate(unsigned char*, unsigned long) Line | Count | Source | 52 | 3.48M | { | 53 | 3.48M | deallocate_memory(p, n, sizeof(T)); | 54 | 3.48M | } |
Botan::secure_allocator<unsigned int>::deallocate(unsigned int*, unsigned long) Line | Count | Source | 52 | 127k | { | 53 | 127k | deallocate_memory(p, n, sizeof(T)); | 54 | 127k | } |
Unexecuted instantiation: Botan::secure_allocator<unsigned short>::deallocate(unsigned short*, unsigned long) |
55 | | }; |
56 | | |
57 | | template<typename T, typename U> inline bool |
58 | | operator==(const secure_allocator<T>&, const secure_allocator<U>&) |
59 | | { return true; } |
60 | | |
61 | | template<typename T, typename U> inline bool |
62 | | operator!=(const secure_allocator<T>&, const secure_allocator<U>&) |
63 | 462k | { return false; } bool Botan::operator!=<unsigned char, unsigned char>(Botan::secure_allocator<unsigned char> const&, Botan::secure_allocator<unsigned char> const&) Line | Count | Source | 63 | 462k | { return false; } |
Unexecuted instantiation: bool Botan::operator!=<unsigned short, unsigned short>(Botan::secure_allocator<unsigned short> const&, Botan::secure_allocator<unsigned short> const&) |
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 | 1.65k | { |
74 | 1.65k | return std::vector<T>(in.begin(), in.end()); |
75 | 1.65k | } |
76 | | |
77 | | template<typename T, typename Alloc, typename Alloc2> |
78 | | std::vector<T, Alloc>& |
79 | | operator+=(std::vector<T, Alloc>& out, |
80 | | const std::vector<T, Alloc2>& in) |
81 | 341k | { |
82 | 341k | out.reserve(out.size() + in.size()); |
83 | 341k | out.insert(out.end(), in.begin(), in.end()); |
84 | 341k | return out; |
85 | 341k | } 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 | 81 | 2.14k | { | 82 | 2.14k | out.reserve(out.size() + in.size()); | 83 | 2.14k | out.insert(out.end(), in.begin(), in.end()); | 84 | 2.14k | return out; | 85 | 2.14k | } |
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&) Line | Count | Source | 81 | 1.15k | { | 82 | 1.15k | out.reserve(out.size() + in.size()); | 83 | 1.15k | out.insert(out.end(), in.begin(), in.end()); | 84 | 1.15k | return out; | 85 | 1.15k | } |
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&) Line | Count | Source | 81 | 4.30k | { | 82 | 4.30k | out.reserve(out.size() + in.size()); | 83 | 4.30k | out.insert(out.end(), in.begin(), in.end()); | 84 | 4.30k | return out; | 85 | 4.30k | } |
std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >& Botan::operator+=<unsigned char, std::__1::allocator<unsigned char>, std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&) Line | Count | Source | 81 | 333k | { | 82 | 333k | out.reserve(out.size() + in.size()); | 83 | 333k | out.insert(out.end(), in.begin(), in.end()); | 84 | 333k | return out; | 85 | 333k | } |
|
86 | | |
87 | | template<typename T, typename Alloc> |
88 | | std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, T in) |
89 | | { |
90 | | out.push_back(in); |
91 | | return out; |
92 | | } |
93 | | |
94 | | template<typename T, typename Alloc, typename L> |
95 | | std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, |
96 | | const std::pair<const T*, L>& in) |
97 | 235k | { |
98 | 235k | out.reserve(out.size() + in.second); |
99 | 235k | out.insert(out.end(), in.first, in.first + in.second); |
100 | 235k | return out; |
101 | 235k | } 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 | 97 | 227k | { | 98 | 227k | out.reserve(out.size() + in.second); | 99 | 227k | out.insert(out.end(), in.first, in.first + in.second); | 100 | 227k | return out; | 101 | 227k | } |
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 | 97 | 7.91k | { | 98 | 7.91k | out.reserve(out.size() + in.second); | 99 | 7.91k | out.insert(out.end(), in.first, in.first + in.second); | 100 | 7.91k | return out; | 101 | 7.91k | } |
|
102 | | |
103 | | template<typename T, typename Alloc, typename L> |
104 | | std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, |
105 | | const std::pair<T*, L>& in) |
106 | 250k | { |
107 | 250k | out.reserve(out.size() + in.second); |
108 | 250k | out.insert(out.end(), in.first, in.first + in.second); |
109 | 250k | return out; |
110 | 250k | } |
111 | | |
112 | | /** |
113 | | * Zeroise the values; length remains unchanged |
114 | | * @param vec the vector to zeroise |
115 | | */ |
116 | | template<typename T, typename Alloc> |
117 | | void zeroise(std::vector<T, Alloc>& vec) |
118 | 566k | { |
119 | 566k | clear_mem(vec.data(), vec.size()); |
120 | 566k | } void Botan::zeroise<unsigned int, Botan::secure_allocator<unsigned int> >(std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> >&) Line | Count | Source | 118 | 52.3k | { | 119 | 52.3k | clear_mem(vec.data(), vec.size()); | 120 | 52.3k | } |
Unexecuted instantiation: void Botan::zeroise<unsigned long, Botan::secure_allocator<unsigned long> >(std::__1::vector<unsigned long, Botan::secure_allocator<unsigned long> >&) void Botan::zeroise<unsigned char, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&) Line | Count | Source | 118 | 512k | { | 119 | 512k | clear_mem(vec.data(), vec.size()); | 120 | 512k | } |
Unexecuted instantiation: void Botan::zeroise<unsigned short, Botan::secure_allocator<unsigned short> >(std::__1::vector<unsigned short, Botan::secure_allocator<unsigned short> >&) void Botan::zeroise<unsigned char, std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&) Line | Count | Source | 118 | 2.10k | { | 119 | 2.10k | clear_mem(vec.data(), vec.size()); | 120 | 2.10k | } |
|
121 | | |
122 | | /** |
123 | | * Zeroise the values then free the memory |
124 | | * @param vec the vector to zeroise and free |
125 | | */ |
126 | | template<typename T, typename Alloc> |
127 | | void zap(std::vector<T, Alloc>& vec) |
128 | 0 | { |
129 | 0 | zeroise(vec); |
130 | 0 | vec.clear(); |
131 | 0 | vec.shrink_to_fit(); |
132 | 0 | } Unexecuted instantiation: void Botan::zap<unsigned int, Botan::secure_allocator<unsigned int> >(std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> >&) Unexecuted instantiation: void Botan::zap<unsigned long, Botan::secure_allocator<unsigned long> >(std::__1::vector<unsigned long, Botan::secure_allocator<unsigned long> >&) Unexecuted instantiation: void Botan::zap<unsigned char, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&) Unexecuted instantiation: void Botan::zap<unsigned short, Botan::secure_allocator<unsigned short> >(std::__1::vector<unsigned short, Botan::secure_allocator<unsigned short> >&) Unexecuted instantiation: void Botan::zap<unsigned char, std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&) |
133 | | |
134 | | } |
135 | | |
136 | | #endif |