Coverage Report

Created: 2021-05-04 09:02

/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
85.5M
         {
48
85.5M
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
49
85.5M
         }
Botan::secure_allocator<unsigned long>::allocate(unsigned long)
Line
Count
Source
47
82.1M
         {
48
82.1M
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
49
82.1M
         }
Botan::secure_allocator<unsigned char>::allocate(unsigned long)
Line
Count
Source
47
3.29M
         {
48
3.29M
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
49
3.29M
         }
Botan::secure_allocator<unsigned int>::allocate(unsigned long)
Line
Count
Source
47
124k
         {
48
124k
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
49
124k
         }
Unexecuted instantiation: Botan::secure_allocator<unsigned short>::allocate(unsigned long)
50
51
      void deallocate(T* p, std::size_t n)
52
85.5M
         {
53
85.5M
         deallocate_memory(p, n, sizeof(T));
54
85.5M
         }
Botan::secure_allocator<unsigned long>::deallocate(unsigned long*, unsigned long)
Line
Count
Source
52
82.1M
         {
53
82.1M
         deallocate_memory(p, n, sizeof(T));
54
82.1M
         }
Botan::secure_allocator<unsigned char>::deallocate(unsigned char*, unsigned long)
Line
Count
Source
52
3.29M
         {
53
3.29M
         deallocate_memory(p, n, sizeof(T));
54
3.29M
         }
Botan::secure_allocator<unsigned int>::deallocate(unsigned int*, unsigned long)
Line
Count
Source
52
124k
         {
53
124k
         deallocate_memory(p, n, sizeof(T));
54
124k
         }
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
435k
   { 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
435k
   { 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.10k
   {
74
1.10k
   return std::vector<T>(in.begin(), in.end());
75
1.10k
   }
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
284k
   {
82
284k
   out.reserve(out.size() + in.size());
83
284k
   out.insert(out.end(), in.begin(), in.end());
84
284k
   return out;
85
284k
   }
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
1.54k
   {
82
1.54k
   out.reserve(out.size() + in.size());
83
1.54k
   out.insert(out.end(), in.begin(), in.end());
84
1.54k
   return out;
85
1.54k
   }
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
719
   {
82
719
   out.reserve(out.size() + in.size());
83
719
   out.insert(out.end(), in.begin(), in.end());
84
719
   return out;
85
719
   }
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
278k
   {
82
278k
   out.reserve(out.size() + in.size());
83
278k
   out.insert(out.end(), in.begin(), in.end());
84
278k
   return out;
85
278k
   }
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
3.83k
   {
82
3.83k
   out.reserve(out.size() + in.size());
83
3.83k
   out.insert(out.end(), in.begin(), in.end());
84
3.83k
   return out;
85
3.83k
   }
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
197k
   {
98
197k
   out.reserve(out.size() + in.second);
99
197k
   out.insert(out.end(), in.first, in.first + in.second);
100
197k
   return out;
101
197k
   }
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
193k
   {
98
193k
   out.reserve(out.size() + in.second);
99
193k
   out.insert(out.end(), in.first, in.first + in.second);
100
193k
   return out;
101
193k
   }
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
3.16k
   {
98
3.16k
   out.reserve(out.size() + in.second);
99
3.16k
   out.insert(out.end(), in.first, in.first + in.second);
100
3.16k
   return out;
101
3.16k
   }
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
287k
   {
107
287k
   out.reserve(out.size() + in.second);
108
287k
   out.insert(out.end(), in.first, in.first + in.second);
109
287k
   return out;
110
287k
   }
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
587k
   {
119
587k
   clear_mem(vec.data(), vec.size());
120
587k
   }
void Botan::zeroise<unsigned int, Botan::secure_allocator<unsigned int> >(std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> >&)
Line
Count
Source
118
36.5k
   {
119
36.5k
   clear_mem(vec.data(), vec.size());
120
36.5k
   }
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
549k
   {
119
549k
   clear_mem(vec.data(), vec.size());
120
549k
   }
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
1.52k
   {
119
1.52k
   clear_mem(vec.data(), vec.size());
120
1.52k
   }
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