Coverage Report

Created: 2020-06-30 13:58

/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
96.4M
         {
48
96.4M
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
49
96.4M
         }
Botan::secure_allocator<unsigned long>::allocate(unsigned long)
Line
Count
Source
47
92.7M
         {
48
92.7M
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
49
92.7M
         }
Botan::secure_allocator<unsigned char>::allocate(unsigned long)
Line
Count
Source
47
3.56M
         {
48
3.56M
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
49
3.56M
         }
Botan::secure_allocator<unsigned int>::allocate(unsigned long)
Line
Count
Source
47
135k
         {
48
135k
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
49
135k
         }
Unexecuted instantiation: Botan::secure_allocator<unsigned short>::allocate(unsigned long)
50
51
      void deallocate(T* p, std::size_t n)
52
96.4M
         {
53
96.4M
         deallocate_memory(p, n, sizeof(T));
54
96.4M
         }
Botan::secure_allocator<unsigned long>::deallocate(unsigned long*, unsigned long)
Line
Count
Source
52
92.7M
         {
53
92.7M
         deallocate_memory(p, n, sizeof(T));
54
92.7M
         }
Botan::secure_allocator<unsigned char>::deallocate(unsigned char*, unsigned long)
Line
Count
Source
52
3.56M
         {
53
3.56M
         deallocate_memory(p, n, sizeof(T));
54
3.56M
         }
Botan::secure_allocator<unsigned int>::deallocate(unsigned int*, unsigned long)
Line
Count
Source
52
135k
         {
53
135k
         deallocate_memory(p, n, sizeof(T));
54
135k
         }
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
495k
   { 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
495k
   { 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.57k
   {
74
7.57k
   std::vector<T> out(in.size());
75
7.57k
   copy_mem(out.data(), in.data(), in.size());
76
7.57k
   return out;
77
7.57k
   }
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
958k
   {
85
958k
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
86
958k
   const size_t to_copy = std::min(input_length, buf.size() - buf_offset);
87
958k
   if(to_copy > 0)
88
611k
      {
89
611k
      copy_mem(&buf[buf_offset], input, to_copy);
90
611k
      }
91
958k
   return to_copy;
92
958k
   }
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
955k
   {
85
955k
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
86
955k
   const size_t to_copy = std::min(input_length, buf.size() - buf_offset);
87
955k
   if(to_copy > 0)
88
609k
      {
89
609k
      copy_mem(&buf[buf_offset], input, to_copy);
90
609k
      }
91
955k
   return to_copy;
92
955k
   }
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
3.01k
   {
85
3.01k
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
86
3.01k
   const size_t to_copy = std::min(input_length, buf.size() - buf_offset);
87
3.01k
   if(to_copy > 0)
88
2.06k
      {
89
2.06k
      copy_mem(&buf[buf_offset], input, to_copy);
90
2.06k
      }
91
3.01k
   return to_copy;
92
3.01k
   }
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
3.05k
   {
99
3.05k
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
100
3.05k
   const size_t to_copy = std::min(input.size(), buf.size() - buf_offset);
101
3.05k
   if(to_copy > 0)
102
3.05k
      {
103
3.05k
      copy_mem(&buf[buf_offset], input.data(), to_copy);
104
3.05k
      }
105
3.05k
   return to_copy;
106
3.05k
   }
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
44
   {
99
44
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
100
44
   const size_t to_copy = std::min(input.size(), buf.size() - buf_offset);
101
44
   if(to_copy > 0)
102
44
      {
103
44
      copy_mem(&buf[buf_offset], input.data(), to_copy);
104
44
      }
105
44
   return to_copy;
106
44
   }
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
3.01k
   {
99
3.01k
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
100
3.01k
   const size_t to_copy = std::min(input.size(), buf.size() - buf_offset);
101
3.01k
   if(to_copy > 0)
102
3.01k
      {
103
3.01k
      copy_mem(&buf[buf_offset], input.data(), to_copy);
104
3.01k
      }
105
3.01k
   return to_copy;
106
3.01k
   }
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
361k
   {
113
361k
   const size_t copy_offset = out.size();
114
361k
   out.resize(out.size() + in.size());
115
361k
   if(in.size() > 0)
116
319k
      {
117
319k
      copy_mem(&out[copy_offset], in.data(), in.size());
118
319k
      }
119
361k
   return out;
120
361k
   }
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.54k
   {
113
7.54k
   const size_t copy_offset = out.size();
114
7.54k
   out.resize(out.size() + in.size());
115
7.54k
   if(in.size() > 0)
116
1.39k
      {
117
1.39k
      copy_mem(&out[copy_offset], in.data(), in.size());
118
1.39k
      }
119
7.54k
   return out;
120
7.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
112
1.05k
   {
113
1.05k
   const size_t copy_offset = out.size();
114
1.05k
   out.resize(out.size() + in.size());
115
1.05k
   if(in.size() > 0)
116
1.05k
      {
117
1.05k
      copy_mem(&out[copy_offset], in.data(), in.size());
118
1.05k
      }
119
1.05k
   return out;
120
1.05k
   }
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
6.71k
   {
113
6.71k
   const size_t copy_offset = out.size();
114
6.71k
   out.resize(out.size() + in.size());
115
6.71k
   if(in.size() > 0)
116
6.71k
      {
117
6.71k
      copy_mem(&out[copy_offset], in.data(), in.size());
118
6.71k
      }
119
6.71k
   return out;
120
6.71k
   }
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
346k
   {
113
346k
   const size_t copy_offset = out.size();
114
346k
   out.resize(out.size() + in.size());
115
346k
   if(in.size() > 0)
116
310k
      {
117
310k
      copy_mem(&out[copy_offset], in.data(), in.size());
118
310k
      }
119
346k
   return out;
120
346k
   }
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
267k
   {
133
267k
   const size_t copy_offset = out.size();
134
267k
   out.resize(out.size() + in.second);
135
267k
   if(in.second > 0)
136
255k
      {
137
255k
      copy_mem(&out[copy_offset], in.first, in.second);
138
255k
      }
139
267k
   return out;
140
267k
   }
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
259k
   {
133
259k
   const size_t copy_offset = out.size();
134
259k
   out.resize(out.size() + in.second);
135
259k
   if(in.second > 0)
136
247k
      {
137
247k
      copy_mem(&out[copy_offset], in.first, in.second);
138
247k
      }
139
259k
   return out;
140
259k
   }
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
7.54k
   {
133
7.54k
   const size_t copy_offset = out.size();
134
7.54k
   out.resize(out.size() + in.second);
135
7.54k
   if(in.second > 0)
136
7.54k
      {
137
7.54k
      copy_mem(&out[copy_offset], in.first, in.second);
138
7.54k
      }
139
7.54k
   return out;
140
7.54k
   }
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
235k
   {
146
235k
   const size_t copy_offset = out.size();
147
235k
   out.resize(out.size() + in.second);
148
235k
   if(in.second > 0)
149
235k
      {
150
235k
      copy_mem(&out[copy_offset], in.first, in.second);
151
235k
      }
152
235k
   return out;
153
235k
   }
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
702k
   {
162
702k
   clear_mem(vec.data(), vec.size());
163
702k
   }
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
51.0k
   {
162
51.0k
   clear_mem(vec.data(), vec.size());
163
51.0k
   }
void Botan::zeroise<unsigned char, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&)
Line
Count
Source
161
648k
   {
162
648k
   clear_mem(vec.data(), vec.size());
163
648k
   }
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
3.01k
   {
162
3.01k
   clear_mem(vec.data(), vec.size());
163
3.01k
   }
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 char, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_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> >&)
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