Coverage Report

Created: 2023-02-22 06:14

/src/botan/build/include/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/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
#if !defined(_ITERATOR_DEBUG_LEVEL) || _ITERATOR_DEBUG_LEVEL == 0
22
 /*
23
  * Assert exists to prevent someone from doing something that will
24
  * probably crash anyway (like secure_vector<non_POD_t> where ~non_POD_t
25
  * deletes a member pointer which was zeroed before it ran).
26
  * MSVC in debug mode uses non-integral proxy types in container types
27
  * like std::vector, thus we disable the check there.
28
 */
29
requires std::is_integral<T>::value
30
#endif
31
class secure_allocator
32
   {
33
   public:
34
      typedef T          value_type;
35
      typedef std::size_t size_type;
36
37
      secure_allocator() noexcept = default;
38
      secure_allocator(const secure_allocator&) noexcept = default;
39
      secure_allocator& operator=(const secure_allocator&) noexcept = default;
40
      ~secure_allocator() noexcept = default;
41
42
      template<typename U>
43
      secure_allocator(const secure_allocator<U>&) noexcept {}
44
45
      T* allocate(std::size_t n)
46
16.0M
         {
47
16.0M
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
48
16.0M
         }
Botan::secure_allocator<unsigned char>::allocate(unsigned long)
Line
Count
Source
46
231k
         {
47
231k
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
48
231k
         }
Botan::secure_allocator<unsigned long>::allocate(unsigned long)
Line
Count
Source
46
15.7M
         {
47
15.7M
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
48
15.7M
         }
Botan::secure_allocator<unsigned int>::allocate(unsigned long)
Line
Count
Source
46
55.6k
         {
47
55.6k
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
48
55.6k
         }
Botan::secure_allocator<unsigned short>::allocate(unsigned long)
Line
Count
Source
46
632
         {
47
632
         return static_cast<T*>(allocate_memory(n, sizeof(T)));
48
632
         }
49
50
      void deallocate(T* p, std::size_t n)
51
16.0M
         {
52
16.0M
         deallocate_memory(p, n, sizeof(T));
53
16.0M
         }
Botan::secure_allocator<unsigned long>::deallocate(unsigned long*, unsigned long)
Line
Count
Source
51
15.7M
         {
52
15.7M
         deallocate_memory(p, n, sizeof(T));
53
15.7M
         }
Botan::secure_allocator<unsigned char>::deallocate(unsigned char*, unsigned long)
Line
Count
Source
51
231k
         {
52
231k
         deallocate_memory(p, n, sizeof(T));
53
231k
         }
Botan::secure_allocator<unsigned int>::deallocate(unsigned int*, unsigned long)
Line
Count
Source
51
55.6k
         {
52
55.6k
         deallocate_memory(p, n, sizeof(T));
53
55.6k
         }
Botan::secure_allocator<unsigned short>::deallocate(unsigned short*, unsigned long)
Line
Count
Source
51
632
         {
52
632
         deallocate_memory(p, n, sizeof(T));
53
632
         }
54
   };
55
56
template<typename T, typename U> inline bool
57
operator==(const secure_allocator<T>&, const secure_allocator<U>&)
58
   { return true; }
59
60
template<typename T, typename U> inline bool
61
operator!=(const secure_allocator<T>&, const secure_allocator<U>&)
62
1.85k
   { return false; }
63
64
template<typename T> using secure_vector = std::vector<T, secure_allocator<T>>;
65
template<typename T> using secure_deque = std::deque<T, secure_allocator<T>>;
66
67
// For better compatibility with 1.10 API
68
template<typename T> using SecureVector = secure_vector<T>;
69
70
template<typename T>
71
std::vector<T> unlock(const secure_vector<T>& in)
72
484
   {
73
484
   return std::vector<T>(in.begin(), in.end());
74
484
   }
75
76
template<typename T, typename Alloc, typename Alloc2>
77
std::vector<T, Alloc>&
78
operator+=(std::vector<T, Alloc>& out,
79
           const std::vector<T, Alloc2>& in)
80
998
   {
81
998
   out.insert(out.end(), in.begin(), in.end());
82
998
   return out;
83
998
   }
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
80
998
   {
81
998
   out.insert(out.end(), in.begin(), in.end());
82
998
   return out;
83
998
   }
Unexecuted instantiation: 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&)
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&)
84
85
template<typename T, typename Alloc>
86
std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, T in)
87
   {
88
   out.push_back(in);
89
   return out;
90
   }
91
92
template<typename T, typename Alloc, typename L>
93
std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out,
94
                                  const std::pair<const T*, L>& in)
95
1.44k
   {
96
1.44k
   out.insert(out.end(), in.first, in.first + in.second);
97
1.44k
   return out;
98
1.44k
   }
99
100
template<typename T, typename Alloc, typename L>
101
std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out,
102
                                  const std::pair<T*, L>& in)
103
834
   {
104
834
   out.insert(out.end(), in.first, in.first + in.second);
105
834
   return out;
106
834
   }
107
108
/**
109
* Zeroise the values; length remains unchanged
110
* @param vec the vector to zeroise
111
*/
112
template<typename T, typename Alloc>
113
void zeroise(std::vector<T, Alloc>& vec)
114
1.03M
   {
115
1.03M
   clear_mem(vec.data(), vec.size());
116
1.03M
   }
void Botan::zeroise<unsigned char, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&)
Line
Count
Source
114
579k
   {
115
579k
   clear_mem(vec.data(), vec.size());
116
579k
   }
void Botan::zeroise<unsigned long, Botan::secure_allocator<unsigned long> >(std::__1::vector<unsigned long, Botan::secure_allocator<unsigned long> >&)
Line
Count
Source
114
262k
   {
115
262k
   clear_mem(vec.data(), vec.size());
116
262k
   }
void Botan::zeroise<unsigned int, Botan::secure_allocator<unsigned int> >(std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> >&)
Line
Count
Source
114
193k
   {
115
193k
   clear_mem(vec.data(), vec.size());
116
193k
   }
void Botan::zeroise<unsigned char, std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&)
Line
Count
Source
114
3.27k
   {
115
3.27k
   clear_mem(vec.data(), vec.size());
116
3.27k
   }
void Botan::zeroise<unsigned short, Botan::secure_allocator<unsigned short> >(std::__1::vector<unsigned short, Botan::secure_allocator<unsigned short> >&)
Line
Count
Source
114
270
   {
115
270
   clear_mem(vec.data(), vec.size());
116
270
   }
117
118
/**
119
* Zeroise the values then free the memory
120
* @param vec the vector to zeroise and free
121
*/
122
template<typename T, typename Alloc>
123
void zap(std::vector<T, Alloc>& vec)
124
39.2k
   {
125
39.2k
   zeroise(vec);
126
39.2k
   vec.clear();
127
39.2k
   vec.shrink_to_fit();
128
39.2k
   }
void Botan::zap<unsigned char, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&)
Line
Count
Source
124
75
   {
125
75
   zeroise(vec);
126
75
   vec.clear();
127
75
   vec.shrink_to_fit();
128
75
   }
void Botan::zap<unsigned long, Botan::secure_allocator<unsigned long> >(std::__1::vector<unsigned long, Botan::secure_allocator<unsigned long> >&)
Line
Count
Source
124
272
   {
125
272
   zeroise(vec);
126
272
   vec.clear();
127
272
   vec.shrink_to_fit();
128
272
   }
Unexecuted instantiation: void Botan::zap<unsigned char, std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&)
void Botan::zap<unsigned int, Botan::secure_allocator<unsigned int> >(std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> >&)
Line
Count
Source
124
38.6k
   {
125
38.6k
   zeroise(vec);
126
38.6k
   vec.clear();
127
38.6k
   vec.shrink_to_fit();
128
38.6k
   }
void Botan::zap<unsigned short, Botan::secure_allocator<unsigned short> >(std::__1::vector<unsigned short, Botan::secure_allocator<unsigned short> >&)
Line
Count
Source
124
270
   {
125
270
   zeroise(vec);
126
270
   vec.clear();
127
270
   vec.shrink_to_fit();
128
270
   }
129
130
}
131
132
#endif