Coverage Report

Created: 2026-09-14 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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 <algorithm>
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
  * Assert 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<T>::value || std::is_enum<T>::value
34
#endif
35
class secure_allocator {
36
37
   public:
38
      typedef T value_type;
39
      typedef std::size_t size_type;
40
41
      secure_allocator() noexcept = default;
42
      secure_allocator(const secure_allocator&) noexcept = default;
43
      secure_allocator& operator=(const secure_allocator&) noexcept = default;
44
      ~secure_allocator() noexcept = default;
45
46
      template <typename U>
47
      secure_allocator(const secure_allocator<U>&) noexcept {}
48
49
991k
      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
49
19.5k
      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
49
970k
      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
49
1.74k
      T* allocate(std::size_t n) { return static_cast<T*>(allocate_memory(n, sizeof(T))); }
Unexecuted instantiation: Botan::secure_allocator<unsigned short>::allocate(unsigned long)
50
51
991k
      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
51
970k
      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
51
19.5k
      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
51
1.74k
      void deallocate(T* p, std::size_t n) { deallocate_memory(p, n, sizeof(T)); }
Unexecuted instantiation: Botan::secure_allocator<unsigned short>::deallocate(unsigned short*, unsigned long)
52
};
53
54
template <typename T, typename U>
55
inline bool operator==(const secure_allocator<T>&, const secure_allocator<U>&) {
56
   return true;
57
}
58
59
template <typename T, typename U>
60
871
inline bool operator!=(const secure_allocator<T>&, const secure_allocator<U>&) {
61
871
   return false;
62
871
}
63
64
template <typename T>
65
using secure_vector = std::vector<T, secure_allocator<T>>;
66
67
#if !defined(BOTAN_IS_BEING_BUILT) && !defined(BOTAN_DISABLE_DEPRECATED_FEATURES)
68
template <typename T>
69
using secure_deque = std::deque<T, secure_allocator<T>>;
70
#endif
71
72
// For better compatibility with 1.10 API
73
template <typename T>
74
using SecureVector = secure_vector<T>;
75
76
template <typename T>
77
secure_vector<T> lock(const std::vector<T>& in) {
78
   return secure_vector<T>(in.begin(), in.end());
79
}
80
81
template <typename T>
82
0
std::vector<T> unlock(const secure_vector<T>& in) {
83
0
   return std::vector<T>(in.begin(), in.end());
84
0
}
85
86
template <typename T, typename Alloc, typename Alloc2>
87
0
std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::vector<T, Alloc2>& in) {
88
0
   out.insert(out.end(), in.begin(), in.end());
89
0
   return out;
90
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&)
91
92
template <typename T, typename Alloc>
93
std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, T in) {
94
   out.push_back(in);
95
   return out;
96
}
97
98
template <typename T, typename Alloc, typename L>
99
2.65k
std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<const T*, L>& in) {
100
2.65k
   out.insert(out.end(), in.first, in.first + in.second);
101
2.65k
   return out;
102
2.65k
}
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
99
1.84k
std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<const T*, L>& in) {
100
1.84k
   out.insert(out.end(), in.first, in.first + in.second);
101
1.84k
   return out;
102
1.84k
}
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
99
810
std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<const T*, L>& in) {
100
810
   out.insert(out.end(), in.first, in.first + in.second);
101
810
   return out;
102
810
}
103
104
template <typename T, typename Alloc, typename L>
105
0
std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<T*, L>& in) {
106
0
   out.insert(out.end(), in.first, in.first + in.second);
107
0
   return out;
108
0
}
109
110
/**
111
* Zeroise the values; length remains unchanged
112
* @param vec the vector to zeroise
113
*/
114
template <typename T, typename Alloc>
115
0
void zeroise(std::vector<T, Alloc>& vec) {
116
0
   std::fill(vec.begin(), vec.end(), static_cast<T>(0));
117
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> >&)
118
119
/**
120
* Zeroise the values then free the memory
121
* @param vec the vector to zeroise and free
122
*/
123
template <typename T, typename Alloc>
124
0
void zap(std::vector<T, Alloc>& vec) {
125
0
   zeroise(vec);
126
0
   vec.clear();
127
0
   vec.shrink_to_fit();
128
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> >&)
129
130
}  // namespace Botan
131
132
#endif