Coverage Report

Created: 2020-05-23 13:54

/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
84.9M
         {
48
84.9M
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
49
84.9M
         }
Botan::secure_allocator<unsigned char>::allocate(unsigned long)
Line
Count
Source
47
3.24M
         {
48
3.24M
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
49
3.24M
         }
Botan::secure_allocator<unsigned long>::allocate(unsigned long)
Line
Count
Source
47
81.6M
         {
48
81.6M
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
49
81.6M
         }
Botan::secure_allocator<unsigned int>::allocate(unsigned long)
Line
Count
Source
47
122k
         {
48
122k
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
49
122k
         }
Unexecuted instantiation: Botan::secure_allocator<unsigned short>::allocate(unsigned long)
50
51
      void deallocate(T* p, std::size_t n)
52
84.9M
         {
53
84.9M
         deallocate_memory(p, n, sizeof(T));
54
84.9M
         }
Botan::secure_allocator<unsigned char>::deallocate(unsigned char*, unsigned long)
Line
Count
Source
52
3.24M
         {
53
3.24M
         deallocate_memory(p, n, sizeof(T));
54
3.24M
         }
Botan::secure_allocator<unsigned long>::deallocate(unsigned long*, unsigned long)
Line
Count
Source
52
81.6M
         {
53
81.6M
         deallocate_memory(p, n, sizeof(T));
54
81.6M
         }
Botan::secure_allocator<unsigned int>::deallocate(unsigned int*, unsigned long)
Line
Count
Source
52
122k
         {
53
122k
         deallocate_memory(p, n, sizeof(T));
54
122k
         }
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
449k
   { 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
449k
   { 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
7.63k
   {
74
7.63k
   std::vector<T> out(in.size());
75
7.63k
   copy_mem(out.data(), in.data(), in.size());
76
7.63k
   return out;
77
7.63k
   }
78
79
template<typename T, typename Alloc>
80
size_t buffer_insert(std::vector<T, Alloc>& buf,
81
                     size_t buf_offset,
82
                     const T input[],
83
                     size_t input_length)
84
880k
   {
85
880k
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
86
880k
   const size_t to_copy = std::min(input_length, buf.size() - buf_offset);
87
880k
   if(to_copy > 0)
88
559k
      {
89
559k
      copy_mem(&buf[buf_offset], input, to_copy);
90
559k
      }
91
880k
   return to_copy;
92
880k
   }
unsigned long Botan::buffer_insert<unsigned char, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&, unsigned long, unsigned char const*, unsigned long)
Line
Count
Source
84
877k
   {
85
877k
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
86
877k
   const size_t to_copy = std::min(input_length, buf.size() - buf_offset);
87
877k
   if(to_copy > 0)
88
557k
      {
89
557k
      copy_mem(&buf[buf_offset], input, to_copy);
90
557k
      }
91
877k
   return to_copy;
92
877k
   }
unsigned long Botan::buffer_insert<unsigned char, std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&, unsigned long, unsigned char const*, unsigned long)
Line
Count
Source
84
2.88k
   {
85
2.88k
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
86
2.88k
   const size_t to_copy = std::min(input_length, buf.size() - buf_offset);
87
2.88k
   if(to_copy > 0)
88
1.98k
      {
89
1.98k
      copy_mem(&buf[buf_offset], input, to_copy);
90
1.98k
      }
91
2.88k
   return to_copy;
92
2.88k
   }
93
94
template<typename T, typename Alloc, typename Alloc2>
95
size_t buffer_insert(std::vector<T, Alloc>& buf,
96
                     size_t buf_offset,
97
                     const std::vector<T, Alloc2>& input)
98
2.91k
   {
99
2.91k
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
100
2.91k
   const size_t to_copy = std::min(input.size(), buf.size() - buf_offset);
101
2.91k
   if(to_copy > 0)
102
2.91k
      {
103
2.91k
      copy_mem(&buf[buf_offset], input.data(), to_copy);
104
2.91k
      }
105
2.91k
   return to_copy;
106
2.91k
   }
unsigned long Botan::buffer_insert<unsigned char, Botan::secure_allocator<unsigned char>, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&, unsigned long, std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > const&)
Line
Count
Source
98
28
   {
99
28
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
100
28
   const size_t to_copy = std::min(input.size(), buf.size() - buf_offset);
101
28
   if(to_copy > 0)
102
28
      {
103
28
      copy_mem(&buf[buf_offset], input.data(), to_copy);
104
28
      }
105
28
   return to_copy;
106
28
   }
unsigned long Botan::buffer_insert<unsigned char, Botan::secure_allocator<unsigned char>, std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&, unsigned long, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&)
Line
Count
Source
98
2.88k
   {
99
2.88k
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
100
2.88k
   const size_t to_copy = std::min(input.size(), buf.size() - buf_offset);
101
2.88k
   if(to_copy > 0)
102
2.88k
      {
103
2.88k
      copy_mem(&buf[buf_offset], input.data(), to_copy);
104
2.88k
      }
105
2.88k
   return to_copy;
106
2.88k
   }
107
108
template<typename T, typename Alloc, typename Alloc2>
109
std::vector<T, Alloc>&
110
operator+=(std::vector<T, Alloc>& out,
111
           const std::vector<T, Alloc2>& in)
112
335k
   {
113
335k
   const size_t copy_offset = out.size();
114
335k
   out.resize(out.size() + in.size());
115
335k
   if(in.size() > 0)
116
295k
      {
117
295k
      copy_mem(&out[copy_offset], in.data(), in.size());
118
295k
      }
119
335k
   return out;
120
335k
   }
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
112
322k
   {
113
322k
   const size_t copy_offset = out.size();
114
322k
   out.resize(out.size() + in.size());
115
322k
   if(in.size() > 0)
116
288k
      {
117
288k
      copy_mem(&out[copy_offset], in.data(), in.size());
118
288k
      }
119
322k
   return out;
120
322k
   }
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
112
7.51k
   {
113
7.51k
   const size_t copy_offset = out.size();
114
7.51k
   out.resize(out.size() + in.size());
115
7.51k
   if(in.size() > 0)
116
1.21k
      {
117
1.21k
      copy_mem(&out[copy_offset], in.data(), in.size());
118
1.21k
      }
119
7.51k
   return out;
120
7.51k
   }
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
112
962
   {
113
962
   const size_t copy_offset = out.size();
114
962
   out.resize(out.size() + in.size());
115
962
   if(in.size() > 0)
116
962
      {
117
962
      copy_mem(&out[copy_offset], in.data(), in.size());
118
962
      }
119
962
   return out;
120
962
   }
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
112
5.11k
   {
113
5.11k
   const size_t copy_offset = out.size();
114
5.11k
   out.resize(out.size() + in.size());
115
5.11k
   if(in.size() > 0)
116
5.11k
      {
117
5.11k
      copy_mem(&out[copy_offset], in.data(), in.size());
118
5.11k
      }
119
5.11k
   return out;
120
5.11k
   }
121
122
template<typename T, typename Alloc>
123
std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, T in)
124
   {
125
   out.push_back(in);
126
   return out;
127
   }
128
129
template<typename T, typename Alloc, typename L>
130
std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out,
131
                                  const std::pair<const T*, L>& in)
132
236k
   {
133
236k
   const size_t copy_offset = out.size();
134
236k
   out.resize(out.size() + in.second);
135
236k
   if(in.second > 0)
136
227k
      {
137
227k
      copy_mem(&out[copy_offset], in.first, in.second);
138
227k
      }
139
236k
   return out;
140
236k
   }
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
132
6.90k
   {
133
6.90k
   const size_t copy_offset = out.size();
134
6.90k
   out.resize(out.size() + in.second);
135
6.90k
   if(in.second > 0)
136
6.90k
      {
137
6.90k
      copy_mem(&out[copy_offset], in.first, in.second);
138
6.90k
      }
139
6.90k
   return out;
140
6.90k
   }
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
132
229k
   {
133
229k
   const size_t copy_offset = out.size();
134
229k
   out.resize(out.size() + in.second);
135
229k
   if(in.second > 0)
136
220k
      {
137
220k
      copy_mem(&out[copy_offset], in.first, in.second);
138
220k
      }
139
229k
   return out;
140
229k
   }
141
142
template<typename T, typename Alloc, typename L>
143
std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out,
144
                                  const std::pair<T*, L>& in)
145
228k
   {
146
228k
   const size_t copy_offset = out.size();
147
228k
   out.resize(out.size() + in.second);
148
228k
   if(in.second > 0)
149
228k
      {
150
228k
      copy_mem(&out[copy_offset], in.first, in.second);
151
228k
      }
152
228k
   return out;
153
228k
   }
154
155
/**
156
* Zeroise the values; length remains unchanged
157
* @param vec the vector to zeroise
158
*/
159
template<typename T, typename Alloc>
160
void zeroise(std::vector<T, Alloc>& vec)
161
639k
   {
162
639k
   clear_mem(vec.data(), vec.size());
163
639k
   }
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 int, Botan::secure_allocator<unsigned int> >(std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> >&)
Line
Count
Source
161
44.5k
   {
162
44.5k
   clear_mem(vec.data(), vec.size());
163
44.5k
   }
void Botan::zeroise<unsigned char, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&)
Line
Count
Source
161
592k
   {
162
592k
   clear_mem(vec.data(), vec.size());
163
592k
   }
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
161
2.88k
   {
162
2.88k
   clear_mem(vec.data(), vec.size());
163
2.88k
   }
164
165
/**
166
* Zeroise the values then free the memory
167
* @param vec the vector to zeroise and free
168
*/
169
template<typename T, typename Alloc>
170
void zap(std::vector<T, Alloc>& vec)
171
0
   {
172
0
   zeroise(vec);
173
0
   vec.clear();
174
0
   vec.shrink_to_fit();
175
0
   }
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 int, Botan::secure_allocator<unsigned int> >(std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> >&)
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> >&)
176
177
}
178
179
#endif