Botan 3.9.0
Crypto and TLS for C&
secmem.h
Go to the documentation of this file.
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/allocator.h>
12#include <botan/types.h> // IWYU pragma: export
13#include <algorithm>
14#include <span>
15#include <type_traits>
16#include <vector> // IWYU pragma: export
17
18#if !defined(BOTAN_IS_BEING_BUILT) && !defined(BOTAN_DISABLE_DEPRECATED_FEATURES)
19 // TODO(Botan4) remove this
20 #include <deque>
21#endif
22
23namespace Botan {
24
25template <typename T>
26#if !defined(_ITERATOR_DEBUG_LEVEL) || _ITERATOR_DEBUG_LEVEL == 0
27/*
28 * Check exists to prevent someone from doing something that will
29 * probably crash anyway (like secure_vector<non_POD_t> where ~non_POD_t
30 * deletes a member pointer which was zeroed before it ran).
31 * MSVC in debug mode uses non-integral proxy types in container types
32 * like std::vector, thus we disable the check there.
33 */
34 requires std::is_integral_v<T> || std::is_enum_v<T>
35#endif
37
38 public:
39 typedef T value_type;
40 typedef std::size_t size_type;
41
42 secure_allocator() noexcept = default;
43 secure_allocator(const secure_allocator&) noexcept = default;
44 secure_allocator& operator=(const secure_allocator&) noexcept = default;
45 secure_allocator(secure_allocator&&) noexcept = default;
46 secure_allocator& operator=(secure_allocator&&) noexcept = default;
47
48 ~secure_allocator() noexcept = default;
49
50 template <typename U>
51 explicit secure_allocator(const secure_allocator<U>& /*other*/) noexcept {}
52
53 T* allocate(std::size_t n) { return static_cast<T*>(allocate_memory(n, sizeof(T))); }
54
55 void deallocate(T* p, std::size_t n) { deallocate_memory(p, n, sizeof(T)); }
56};
57
58template <typename T, typename U>
59inline bool operator==(const secure_allocator<T>& /*a*/, const secure_allocator<U>& /*b*/) {
60 return true;
61}
62
63template <typename T, typename U>
64inline bool operator!=(const secure_allocator<T>& /*a*/, const secure_allocator<U>& /*b*/) {
65 return false;
66}
67
68template <typename T>
69using secure_vector = std::vector<T, secure_allocator<T>>;
70
71#if !defined(BOTAN_IS_BEING_BUILT) && !defined(BOTAN_DISABLE_DEPRECATED_FEATURES)
72template <typename T>
73using secure_deque = std::deque<T, secure_allocator<T>>;
74#endif
75
76// For better compatibility with 1.10 API
77template <typename T>
79
80template <typename T>
81secure_vector<T> lock(const std::vector<T>& in) {
82 return secure_vector<T>(in.begin(), in.end());
83}
84
85template <typename T>
86std::vector<T> unlock(const secure_vector<T>& in) {
87 return std::vector<T>(in.begin(), in.end());
88}
89
90template <typename T, typename Alloc, typename Alloc2>
91std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::vector<T, Alloc2>& in) {
92 out.insert(out.end(), in.begin(), in.end());
93 return out;
94}
95
96template <typename T, typename Alloc>
97std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, std::span<const T> in) {
98 out.insert(out.end(), in.begin(), in.end());
99 return out;
100}
101
102template <typename T, typename Alloc>
103std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, T in) {
104 out.push_back(in);
105 return out;
106}
107
108template <typename T, typename Alloc, typename L>
109std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<const T*, L>& in) {
110 out.insert(out.end(), in.first, in.first + in.second);
111 return out;
112}
113
114template <typename T, typename Alloc, typename L>
115std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<T*, L>& in) {
116 out.insert(out.end(), in.first, in.first + in.second);
117 return out;
118}
119
120/**
121* Zeroise the values; length remains unchanged
122* @param vec the vector to zeroise
123*/
124template <typename T, typename Alloc>
125void zeroise(std::vector<T, Alloc>& vec) {
126 std::fill(vec.begin(), vec.end(), static_cast<T>(0));
127}
128
129/**
130* Zeroise the values then free the memory
131* @param vec the vector to zeroise and free
132*/
133template <typename T, typename Alloc>
134void zap(std::vector<T, Alloc>& vec) {
135 zeroise(vec);
136 vec.clear();
137 vec.shrink_to_fit();
138}
139
140} // namespace Botan
141
142#endif
T * allocate(std::size_t n)
Definition secmem.h:53
void deallocate(T *p, std::size_t n)
Definition secmem.h:55
secure_allocator() noexcept=default
std::size_t size_type
Definition secmem.h:40
void zeroise(std::vector< T, Alloc > &vec)
Definition secmem.h:125
void zap(std::vector< T, Alloc > &vec)
Definition secmem.h:134
void deallocate_memory(void *p, size_t elems, size_t elem_size)
Definition allocator.cpp:48
secure_vector< T > lock(const std::vector< T > &in)
Definition secmem.h:81
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition alg_id.cpp:68
std::vector< T > unlock(const secure_vector< T > &in)
Definition secmem.h:86
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition alg_id.cpp:53
std::deque< T, secure_allocator< T > > secure_deque
Definition secmem.h:73
std::vector< T, Alloc > & operator+=(std::vector< T, Alloc > &out, const std::vector< T, Alloc2 > &in)
Definition secmem.h:91
BOTAN_MALLOC_FN void * allocate_memory(size_t elems, size_t elem_size)
Definition allocator.cpp:21
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:69
secure_vector< T > SecureVector
Definition secmem.h:78