Botan 3.3.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 <deque>
15#include <type_traits>
16#include <vector> // IWYU pragma: export
17
18namespace Botan {
19
20template <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
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) { return static_cast<T*>(allocate_memory(n, sizeof(T))); }
46
47 void deallocate(T* p, std::size_t n) { deallocate_memory(p, n, sizeof(T)); }
48};
49
50template <typename T, typename U>
52 return true;
53}
54
55template <typename T, typename U>
57 return false;
58}
59
60template <typename T>
61using secure_vector = std::vector<T, secure_allocator<T>>;
62template <typename T>
63using secure_deque = std::deque<T, secure_allocator<T>>;
64
65// For better compatibility with 1.10 API
66template <typename T>
68
69template <typename T>
70secure_vector<T> lock(const std::vector<T>& in) {
71 return secure_vector<T>(in.begin(), in.end());
72}
73
74template <typename T>
75std::vector<T> unlock(const secure_vector<T>& in) {
76 return std::vector<T>(in.begin(), in.end());
77}
78
79template <typename T, typename Alloc, typename Alloc2>
80std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::vector<T, Alloc2>& in) {
81 out.insert(out.end(), in.begin(), in.end());
82 return out;
83}
84
85template <typename T, typename Alloc>
86std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, T in) {
87 out.push_back(in);
88 return out;
89}
90
91template <typename T, typename Alloc, typename L>
92std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<const T*, L>& in) {
93 out.insert(out.end(), in.first, in.first + in.second);
94 return out;
95}
96
97template <typename T, typename Alloc, typename L>
98std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<T*, L>& in) {
99 out.insert(out.end(), in.first, in.first + in.second);
100 return out;
101}
102
103/**
104* Zeroise the values; length remains unchanged
105* @param vec the vector to zeroise
106*/
107template <typename T, typename Alloc>
108void zeroise(std::vector<T, Alloc>& vec) {
109 std::fill(vec.begin(), vec.end(), static_cast<T>(0));
110}
111
112/**
113* Zeroise the values then free the memory
114* @param vec the vector to zeroise and free
115*/
116template <typename T, typename Alloc>
117void zap(std::vector<T, Alloc>& vec) {
118 zeroise(vec);
119 vec.clear();
120 vec.shrink_to_fit();
121}
122
123} // namespace Botan
124
125#endif
T * allocate(std::size_t n)
Definition secmem.h:45
void deallocate(T *p, std::size_t n)
Definition secmem.h:47
secure_allocator() noexcept=default
std::size_t size_type
Definition secmem.h:35
FE_25519 T
Definition ge.cpp:34
void zeroise(std::vector< T, Alloc > &vec)
Definition secmem.h:108
BOTAN_MALLOC_FN void * allocate_memory(size_t elems, size_t elem_size)
Definition allocator.cpp:20
void zap(std::vector< T, Alloc > &vec)
Definition secmem.h:117
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:70
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition alg_id.cpp:69
std::vector< T > unlock(const secure_vector< T > &in)
Definition secmem.h:75
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition alg_id.cpp:54
std::deque< T, secure_allocator< T > > secure_deque
Definition secmem.h:63
std::vector< T, Alloc > & operator+=(std::vector< T, Alloc > &out, const std::vector< T, Alloc2 > &in)
Definition secmem.h:80
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
secure_vector< T > SecureVector
Definition secmem.h:67