8#ifndef BOTAN_MEMORY_OPS_H_
9#define BOTAN_MEMORY_OPS_H_
11#include <botan/types.h>
101 std::memset(ptr, 0, bytes);
121#if (BOTAN_GCC_VERSION > 0 && BOTAN_GCC_VERSION < 500)
122#define BOTAN_IS_TRIVIALLY_COPYABLE(T) true
124#define BOTAN_IS_TRIVIALLY_COPYABLE(T) std::is_trivially_copyable<T>::value
133template<
typename T>
inline void copy_mem(
T* out,
const T* in,
size_t n)
135 static_assert(std::is_trivial<typename std::decay<T>::type>::value,
"");
137 "If n > 0 then args are not null");
139 if(in !=
nullptr && out !=
nullptr && n > 0)
141 std::memmove(out, in,
sizeof(
T)*n);
148 std::memcpy(out, in,
sizeof(
T)*N);
151template<
typename T>
inline void typecast_copy(
T out[],
const uint8_t in[],
size_t N)
153 static_assert(std::is_trivial<T>::value,
"");
154 std::memcpy(out, in,
sizeof(
T)*N);
164 static_assert(std::is_trivial<typename std::decay<T>::type>::value,
"");
168template <
class To,
class From>
inline To
typecast_copy(
const From *src)
noexcept
172 std::memcpy(&dst, src,
sizeof(To));
182inline void set_mem(uint8_t* ptr,
size_t n, uint8_t val)
186 std::memset(ptr, val, n);
192 return reinterpret_cast<const uint8_t*
>(s);
197 return reinterpret_cast<const char*
>(b);
202 return reinterpret_cast<uint8_t*
>(s);
207 return reinterpret_cast<char*
>(b);
217template<
typename T>
inline bool same_mem(
const T* p1,
const T* p2,
size_t n)
219 volatile T difference = 0;
221 for(
size_t i = 0; i != n; ++i)
222 difference |= (p1[i] ^ p2[i]);
224 return difference == 0;
227template<
typename T,
typename Alloc>
234 const size_t to_copy = std::min(input_length, buf.size() - buf_offset);
237 copy_mem(&buf[buf_offset], input, to_copy);
242template<
typename T,
typename Alloc,
typename Alloc2>
245 const std::vector<T, Alloc2>& input)
248 const size_t to_copy = std::min(input.size(), buf.size() - buf_offset);
251 copy_mem(&buf[buf_offset], input.data(), to_copy);
266 const size_t blocks = length - (length % 32);
268 for(
size_t i = 0; i != blocks; i += 32)
284 for(
size_t i = blocks; i != length; ++i)
302 const size_t blocks = length - (length % 32);
304 for(
size_t i = 0; i != blocks; i += 32)
320 for(
size_t i = blocks; i != length; ++i)
322 out[i] = in[i] ^ in2[i];
326template<
typename Alloc,
typename Alloc2>
328 const std::vector<uint8_t, Alloc2>& in,
331 xor_buf(out.data(), in.data(), n);
334template<
typename Alloc>
342template<
typename Alloc,
typename Alloc2>
345 const std::vector<uint8_t, Alloc2>& in2,
348 xor_buf(out.data(), in, in2.data(), n);
351template<
typename Alloc,
typename Alloc2>
352std::vector<uint8_t, Alloc>&
354 const std::vector<uint8_t, Alloc2>& in)
356 if(out.size() < in.size())
357 out.resize(in.size());
359 xor_buf(out.data(), in.data(), in.size());
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg)
#define BOTAN_PUBLIC_API(maj, min)
#define BOTAN_UNSTABLE_API
#define BOTAN_IS_TRIVIALLY_COPYABLE(T)
void set_mem(uint8_t *ptr, size_t n, uint8_t val)
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
BOTAN_MALLOC_FN void * allocate_memory(size_t elems, size_t elem_size)
void deallocate_memory(void *p, size_t elems, size_t elem_size)
void secure_scrub_memory(void *ptr, size_t n)
void clear_bytes(void *ptr, size_t bytes)
void copy_mem(T *out, const T *in, size_t n)
bool constant_time_compare(const uint8_t x[], const uint8_t y[], size_t len)
uint8_t ct_compare_u8(const uint8_t x[], const uint8_t y[], size_t len)
std::vector< uint8_t, Alloc > & operator^=(std::vector< uint8_t, Alloc > &out, const std::vector< uint8_t, Alloc2 > &in)
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
void typecast_copy(uint8_t out[], T in[], size_t N)
void initialize_allocator()
bool same_mem(const T *p1, const T *p2, size_t n)
const char * cast_uint8_ptr_to_char(const uint8_t *b)
void clear_mem(T *ptr, size_t n)
const uint8_t * cast_char_ptr_to_uint8(const char *s)