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