Coverage Report

Created: 2020-02-14 15:38

/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
100M
         {
48
100M
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
49
100M
         }
Botan::secure_allocator<unsigned char>::allocate(unsigned long)
Line
Count
Source
47
3.57M
         {
48
3.57M
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
49
3.57M
         }
Botan::secure_allocator<unsigned long>::allocate(unsigned long)
Line
Count
Source
47
96.5M
         {
48
96.5M
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
49
96.5M
         }
Botan::secure_allocator<unsigned int>::allocate(unsigned long)
Line
Count
Source
47
131k
         {
48
131k
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
49
131k
         }
Unexecuted instantiation: Botan::secure_allocator<unsigned short>::allocate(unsigned long)
50
51
      void deallocate(T* p, std::size_t n)
52
100M
         {
53
100M
         deallocate_memory(p, n, sizeof(T));
54
100M
         }
Botan::secure_allocator<unsigned long>::deallocate(unsigned long*, unsigned long)
Line
Count
Source
52
96.5M
         {
53
96.5M
         deallocate_memory(p, n, sizeof(T));
54
96.5M
         }
Botan::secure_allocator<unsigned char>::deallocate(unsigned char*, unsigned long)
Line
Count
Source
52
3.57M
         {
53
3.57M
         deallocate_memory(p, n, sizeof(T));
54
3.57M
         }
Botan::secure_allocator<unsigned int>::deallocate(unsigned int*, unsigned long)
Line
Count
Source
52
131k
         {
53
131k
         deallocate_memory(p, n, sizeof(T));
54
131k
         }
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
496k
   { 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
496k
   { 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
8.45k
   {
74
8.45k
   std::vector<T> out(in.size());
75
8.45k
   copy_mem(out.data(), in.data(), in.size());
76
8.45k
   return out;
77
8.45k
   }
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
928k
   {
85
928k
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
86
928k
   const size_t to_copy = std::min(input_length, buf.size() - buf_offset);
87
928k
   if(to_copy > 0)
88
591k
      {
89
591k
      copy_mem(&buf[buf_offset], input, to_copy);
90
591k
      }
91
928k
   return to_copy;
92
928k
   }
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
925k
   {
85
925k
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
86
925k
   const size_t to_copy = std::min(input_length, buf.size() - buf_offset);
87
925k
   if(to_copy > 0)
88
589k
      {
89
589k
      copy_mem(&buf[buf_offset], input, to_copy);
90
589k
      }
91
925k
   return to_copy;
92
925k
   }
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.05k
   {
85
3.05k
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
86
3.05k
   const size_t to_copy = std::min(input_length, buf.size() - buf_offset);
87
3.05k
   if(to_copy > 0)
88
2.10k
      {
89
2.10k
      copy_mem(&buf[buf_offset], input, to_copy);
90
2.10k
      }
91
3.05k
   return to_copy;
92
3.05k
   }
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.08k
   {
99
3.08k
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
100
3.08k
   const size_t to_copy = std::min(input.size(), buf.size() - buf_offset);
101
3.08k
   if(to_copy > 0)
102
3.08k
      {
103
3.08k
      copy_mem(&buf[buf_offset], input.data(), to_copy);
104
3.08k
      }
105
3.08k
   return to_copy;
106
3.08k
   }
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.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
33
   {
99
33
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
100
33
   const size_t to_copy = std::min(input.size(), buf.size() - buf_offset);
101
33
   if(to_copy > 0)
102
33
      {
103
33
      copy_mem(&buf[buf_offset], input.data(), to_copy);
104
33
      }
105
33
   return to_copy;
106
33
   }
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
354k
   {
113
354k
   const size_t copy_offset = out.size();
114
354k
   out.resize(out.size() + in.size());
115
354k
   if(in.size() > 0)
116
308k
      {
117
308k
      copy_mem(&out[copy_offset], in.data(), in.size());
118
308k
      }
119
354k
   return out;
120
354k
   }
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
8.52k
   {
113
8.52k
   const size_t copy_offset = out.size();
114
8.52k
   out.resize(out.size() + in.size());
115
8.52k
   if(in.size() > 0)
116
1.42k
      {
117
1.42k
      copy_mem(&out[copy_offset], in.data(), in.size());
118
1.42k
      }
119
8.52k
   return out;
120
8.52k
   }
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.00k
   {
113
1.00k
   const size_t copy_offset = out.size();
114
1.00k
   out.resize(out.size() + in.size());
115
1.00k
   if(in.size() > 0)
116
1.00k
      {
117
1.00k
      copy_mem(&out[copy_offset], in.data(), in.size());
118
1.00k
      }
119
1.00k
   return out;
120
1.00k
   }
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
340k
   {
113
340k
   const size_t copy_offset = out.size();
114
340k
   out.resize(out.size() + in.size());
115
340k
   if(in.size() > 0)
116
302k
      {
117
302k
      copy_mem(&out[copy_offset], in.data(), in.size());
118
302k
      }
119
340k
   return out;
120
340k
   }
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
4.52k
   {
113
4.52k
   const size_t copy_offset = out.size();
114
4.52k
   out.resize(out.size() + in.size());
115
4.52k
   if(in.size() > 0)
116
4.52k
      {
117
4.52k
      copy_mem(&out[copy_offset], in.data(), in.size());
118
4.52k
      }
119
4.52k
   return out;
120
4.52k
   }
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
264k
   {
133
264k
   const size_t copy_offset = out.size();
134
264k
   out.resize(out.size() + in.second);
135
264k
   if(in.second > 0)
136
253k
      {
137
253k
      copy_mem(&out[copy_offset], in.first, in.second);
138
253k
      }
139
264k
   return out;
140
264k
   }
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
257k
   {
133
257k
   const size_t copy_offset = out.size();
134
257k
   out.resize(out.size() + in.second);
135
257k
   if(in.second > 0)
136
245k
      {
137
245k
      copy_mem(&out[copy_offset], in.first, in.second);
138
245k
      }
139
257k
   return out;
140
257k
   }
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.64k
   {
133
7.64k
   const size_t copy_offset = out.size();
134
7.64k
   out.resize(out.size() + in.second);
135
7.64k
   if(in.second > 0)
136
7.64k
      {
137
7.64k
      copy_mem(&out[copy_offset], in.first, in.second);
138
7.64k
      }
139
7.64k
   return out;
140
7.64k
   }
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
240k
   {
146
240k
   const size_t copy_offset = out.size();
147
240k
   out.resize(out.size() + in.second);
148
240k
   if(in.second > 0)
149
240k
      {
150
240k
      copy_mem(&out[copy_offset], in.first, in.second);
151
240k
      }
152
240k
   return out;
153
240k
   }
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
682k
   {
162
682k
   clear_mem(vec.data(), vec.size());
163
682k
   }
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.2k
   {
162
51.2k
   clear_mem(vec.data(), vec.size());
163
51.2k
   }
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
161
628k
   {
162
628k
   clear_mem(vec.data(), vec.size());
163
628k
   }
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.05k
   {
162
3.05k
   clear_mem(vec.data(), vec.size());
163
3.05k
   }
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 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> >&)
176
177
}
178
179
#endif