25 #ifndef CRYPTO3_MEMORY_OPERATIONS_HPP
26 #define CRYPTO3_MEMORY_OPERATIONS_HPP
31 #ifdef CRYPTO3_HAS_LOCKING_ALLOCATOR
48 BOOST_ATTRIBUTE_MALLOC_FUNCTION
void *
allocate_memory(
size_t elems,
size_t elem_size) {
49 #if defined(CRYPTO3_HAS_LOCKING_ALLOCATOR)
55 void *ptr = std::calloc(elems, elem_size);
57 throw std::bad_alloc();
78 #if defined(CRYPTO3_TARGET_OS_HAS_RTLSECUREZEROMEMORY)
79 ::RtlSecureZeroMemory(ptr, n);
81 #elif defined(CRYPTO3_TARGET_OS_HAS_EXPLICIT_BZERO)
82 ::explicit_bzero(ptr, n);
84 #elif defined(CRYPTO3_USE_VOLATILE_MEMSET_FOR_ZERO) && (CRYPTO3_USE_VOLATILE_MEMSET_FOR_ZERO == 1)
92 static void *(*
const volatile memset_ptr)(
void *,
int,
size_t) = std::memset;
93 (memset_ptr)(ptr, 0, n);
96 volatile uint8_t *p =
reinterpret_cast<volatile uint8_t *
>(ptr);
98 for (
size_t i = 0; i != n; ++i) {
117 #if defined(CRYPTO3_HAS_LOCKING_ALLOCATOR)
130 #if defined(CRYPTO3_HAS_LOCKING_ALLOCATOR)
144 volatile uint8_t difference = 0;
146 for (
size_t i = 0; i != len; ++i) {
147 difference |= (x[i] ^ y[i]);
150 return difference == 0;
160 std::memset(ptr, 0, bytes);
186 inline void copy_mem(T *out,
const T *in,
size_t n) {
188 std::memmove(out, in,
sizeof(T) * n);
199 inline void set_mem(T *ptr,
size_t n, uint8_t val) {
201 std::memset(ptr, val,
sizeof(T) * n);
206 return reinterpret_cast<const uint8_t *
>(s);
210 return reinterpret_cast<const char *
>(b);
214 return reinterpret_cast<uint8_t *
>(s);
218 return reinterpret_cast<char *
>(b);
229 inline bool same_mem(
const T *p1,
const T *p2,
size_t n) {
230 volatile T difference = 0;
232 for (
size_t i = 0; i != n; ++i) {
233 difference |= (p1[i] ^ p2[i]);
236 return difference == 0;
245 inline void xor_buf(uint8_t out[],
const uint8_t in[],
size_t length) {
246 while (length >= 16) {
247 uint64_t x0, x1, y0, y1;
248 std::memcpy(&x0, in, 8);
249 std::memcpy(&x1, in + 8, 8);
250 std::memcpy(&y0, out, 8);
251 std::memcpy(&y1, out + 8, 8);
255 std::memcpy(out, &y0, 8);
256 std::memcpy(out + 8, &y1, 8);
277 inline void xor_buf(uint8_t out[],
const uint8_t in[],
const uint8_t in2[],
size_t length) {
278 while (length >= 16) {
279 uint64_t x0, x1, y0, y1;
280 std::memcpy(&x0, in, 8);
281 std::memcpy(&x1, in + 8, 8);
282 std::memcpy(&y0, in2, 8);
283 std::memcpy(&y1, in2 + 8, 8);
287 std::memcpy(out, &x0, 8);
288 std::memcpy(out + 8, &x1, 8);
295 for (
size_t i = 0; i != length; ++i) {
296 out[i] = in[i] ^ in2[i];
300 template<
typename Alloc,
typename Alloc2>
301 void xor_buf(std::vector<uint8_t, Alloc> &out,
const std::vector<uint8_t, Alloc2> &in,
size_t n) {
302 xor_buf(out.data(), in.data(), n);
305 template<
typename Alloc>
306 void xor_buf(std::vector<uint8_t, Alloc> &out,
const uint8_t *in,
size_t n) {
310 template<
typename Alloc,
typename Alloc2>
311 void xor_buf(std::vector<uint8_t, Alloc> &out,
const uint8_t *in,
const std::vector<uint8_t, Alloc2> &in2,
313 xor_buf(out.data(), in, in2.data(), n);
316 template<
typename Alloc,
typename Alloc2>
317 std::vector<uint8_t, Alloc> &
operator^=(std::vector<uint8_t, Alloc> &out,
318 const std::vector<uint8_t, Alloc2> &in) {
319 if (out.size() < in.size()) {
320 out.resize(in.size());
323 xor_buf(out.data(), in.data(), in.size());
static mlock_allocator & instance()
Definition: locking_allocator.hpp:17
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: memory_operations.hpp:205
void set_mem(T *ptr, size_t n, uint8_t val)
Definition: memory_operations.hpp:199
void secure_scrub_memory(void *ptr, size_t n)
Definition: memory_operations.hpp:77
BOOST_ATTRIBUTE_MALLOC_FUNCTION void * allocate_memory(size_t elems, size_t elem_size)
Definition: memory_operations.hpp:48
bool same_mem(const T *p1, const T *p2, size_t n)
Definition: memory_operations.hpp:229
void deallocate_memory(void *p, size_t elems, size_t elem_size)
Definition: memory_operations.hpp:110
void initialize_allocator()
Definition: memory_operations.hpp:129
void clear_bytes(void *ptr, size_t bytes)
Definition: memory_operations.hpp:158
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: memory_operations.hpp:245
bool constant_time_compare(const uint8_t x[], const uint8_t y[], size_t len)
Definition: memory_operations.hpp:143
std::vector< uint8_t, Alloc > & operator^=(std::vector< uint8_t, Alloc > &out, const std::vector< uint8_t, Alloc2 > &in)
Definition: memory_operations.hpp:317
void copy_mem(T *out, const T *in, size_t n)
Definition: memory_operations.hpp:186
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: memory_operations.hpp:209
void clear_mem(T *ptr, size_t n)
Definition: memory_operations.hpp:175