1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
/**
* Copyright (C) 2021-2024 Saturneric <[email protected]>
*
* This file is part of GpgFrontend.
*
* GpgFrontend is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GpgFrontend is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
*
* The initial version of the source code is inherited from
* the gpg4usb project, which is under GPL-3.0-or-later.
*
* All the source code of GpgFrontend was modified and released by
* Saturneric <[email protected]> starting on May 12, 2021.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#pragma once
#include "core/GpgFrontendCoreExport.h"
#include "core/function/SecureMemoryAllocator.h"
/* To avoid that a compiler optimizes certain memset calls away, these
macros may be used instead. */
#define wipememory2(_ptr, _set, _len) \
do { \
volatile char *_vptr = (volatile char *)(_ptr); \
size_t _vlen = (_len); \
while (_vlen) { \
*_vptr = (_set); \
_vptr++; \
_vlen--; \
} \
} while (0)
#define wipememory(_ptr, _len) wipememory2(_ptr, 0, _len)
#define wipe(_ptr, _len) wipememory2(_ptr, 0, _len)
#define xtoi_1(p) \
(*(p) <= '9' ? (*(p) - '0') \
: *(p) <= 'F' ? (*(p) - 'A' + 10) \
: (*(p) - 'a' + 10))
#define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p) + 1))
namespace GpgFrontend {
template <typename T>
class PointerConverter {
public:
explicit PointerConverter(void *ptr) : ptr_(ptr) {}
auto AsType() const -> T * { return static_cast<T *>(ptr_); }
private:
void *ptr_;
};
/**
* @brief
*
* @return void*
*/
auto GPGFRONTEND_CORE_EXPORT SecureMalloc(std::size_t) -> void *;
/**
* @brief
*
* @return void*
*/
auto GPGFRONTEND_CORE_EXPORT SecureRealloc(void *, std::size_t) -> void *;
/**
* @brief
*
* @tparam T
* @return T*
*/
template <typename T>
auto SecureMallocAsType(std::size_t size) -> T * {
return PointerConverter<T>(SecureMemoryAllocator::Allocate(size)).AsType();
}
/**
* @brief
*
* @return void*
*/
template <typename T>
auto SecureReallocAsType(T *ptr, std::size_t size) -> T * {
return PointerConverter<T>(SecureMemoryAllocator::Reallocate(ptr, size))
.AsType();
}
/**
* @brief
*
*/
void GPGFRONTEND_CORE_EXPORT SecureFree(void *);
template <typename T, typename... Args>
static auto SecureCreateObject(Args &&...args) -> T * {
void *mem = SecureMemoryAllocator::Allocate(sizeof(T));
if (!mem) return nullptr;
try {
return new (mem) T(std::forward<Args>(args)...);
} catch (...) {
SecureMemoryAllocator::Deallocate(mem);
throw;
}
}
template <typename T>
static void SecureDestroyObject(T *obj) {
if (!obj) return;
obj->~T();
SecureMemoryAllocator::Deallocate(obj);
}
template <typename T, typename... Args>
static auto SecureCreateUniqueObject(Args &&...args)
-> std::unique_ptr<T, SecureObjectDeleter<T>> {
void *mem = SecureMemoryAllocator::Allocate(sizeof(T));
if (!mem) throw std::bad_alloc();
try {
return std::unique_ptr<T, SecureObjectDeleter<T>>(
new (mem) T(std::forward<Args>(args)...));
} catch (...) {
SecureMemoryAllocator::Deallocate(mem);
throw;
}
}
template <typename T, typename... Args>
auto SecureCreateSharedObject(Args &&...args) -> std::shared_ptr<T> {
void *mem = SecureMemoryAllocator::Allocate(sizeof(T));
if (!mem) throw std::bad_alloc();
try {
T *obj = new (mem) T(std::forward<Args>(args)...);
return std::shared_ptr<T>(obj, [](T *ptr) {
ptr->~T();
SecureMemoryAllocator::Deallocate(ptr);
});
} catch (...) {
SecureMemoryAllocator::Deallocate(mem);
throw;
}
}
template <typename T, typename... Args>
auto SecureCreateQSharedObject(Args &&...args) -> QSharedPointer<T> {
void *mem = SecureMemoryAllocator::Allocate(sizeof(T));
if (!mem) throw std::bad_alloc();
try {
T *obj = new (mem) T(std::forward<Args>(args)...);
return QSharedPointer<T>(obj, [](T *ptr) {
ptr->~T();
SecureMemoryAllocator::Deallocate(ptr);
});
} catch (...) {
SecureMemoryAllocator::Deallocate(mem);
throw;
}
}
}; // namespace GpgFrontend
|