8 #ifndef BOTAN_MEMORY_OPS_H_
9 #define BOTAN_MEMORY_OPS_H_
11 #include <botan/types.h>
13 #include <type_traits>
98 std::memset(ptr, 0, bytes);
120 #if !__clang__ && __GNUG__ && __GNUC__ < 5
121 #define BOTAN_IS_TRIVIALLY_COPYABLE(T) true
123 #define BOTAN_IS_TRIVIALLY_COPYABLE(T) std::is_trivially_copyable<T>::value
132 template<
typename T>
inline void copy_mem(
T* out,
const T* in,
size_t n)
136 "If n > 0 then args are not null");
138 if(in !=
nullptr && out !=
nullptr && n > 0)
140 std::memmove(out, in,
sizeof(
T)*n);
147 std::memcpy(out, in,
sizeof(
T)*N);
150 template<
typename T>
inline void typecast_copy(
T out[],
const uint8_t in[],
size_t N)
152 static_assert(std::is_trivial<T>::value,
"");
153 std::memcpy(out, in,
sizeof(
T)*N);
167 template <
class To,
class From>
inline To
typecast_copy(
const From *src) noexcept
171 std::memcpy(&dst, src,
sizeof(To));
181 inline void set_mem(uint8_t* ptr,
size_t n, uint8_t val)
185 std::memset(ptr, val, n);
191 return reinterpret_cast<const uint8_t*
>(s);
196 return reinterpret_cast<const char*
>(b);
201 return reinterpret_cast<uint8_t*
>(s);
206 return reinterpret_cast<char*
>(b);
216 template<
typename T>
inline bool same_mem(
const T* p1,
const T* p2,
size_t n)
218 volatile T difference = 0;
220 for(
size_t i = 0; i != n; ++i)
221 difference |= (p1[i] ^ p2[i]);
223 return difference == 0;
236 const size_t blocks = length - (length % 32);
238 for(
size_t i = 0; i != blocks; i += 32)
254 for(
size_t i = blocks; i != length; ++i)
272 const size_t blocks = length - (length % 32);
274 for(
size_t i = 0; i != blocks; i += 32)
290 for(
size_t i = blocks; i != length; ++i)
292 out[i] = in[i] ^ in2[i];
296 template<
typename Alloc,
typename Alloc2>
297 void xor_buf(std::vector<uint8_t, Alloc>& out,
298 const std::vector<uint8_t, Alloc2>& in,
301 xor_buf(out.data(), in.data(), n);
304 template<
typename Alloc>
305 void xor_buf(std::vector<uint8_t, Alloc>& out,
312 template<
typename Alloc,
typename Alloc2>
313 void xor_buf(std::vector<uint8_t, Alloc>& out,
315 const std::vector<uint8_t, Alloc2>& in2,
318 xor_buf(out.data(), in, in2.data(), n);
321 template<
typename Alloc,
typename Alloc2>
322 std::vector<uint8_t, Alloc>&
324 const std::vector<uint8_t, Alloc2>& in)
326 if(out.size() < in.size())
327 out.resize(in.size());
329 xor_buf(out.data(), in.data(), in.size());
void clear_bytes(void *ptr, size_t bytes)
bool same_mem(const T *p1, const T *p2, size_t n)
BOTAN_MALLOC_FN void * allocate_memory(size_t elems, size_t elem_size)
void clear_mem(T *ptr, size_t n)
void set_mem(uint8_t *ptr, size_t n, uint8_t val)
#define BOTAN_PUBLIC_API(maj, min)
const uint8_t * cast_char_ptr_to_uint8(const char *s)
bool constant_time_compare(const uint8_t x[], const uint8_t y[], size_t len)
void typecast_copy(uint8_t out[], T in[], size_t N)
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
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 deallocate_memory(void *p, size_t elems, size_t elem_size)
void copy_mem(T *out, const T *in, size_t n)
#define BOTAN_IS_TRIVIALLY_COPYABLE(T)
void secure_scrub_memory(void *ptr, size_t n)
const char * cast_uint8_ptr_to_char(const uint8_t *b)
#define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg)
void initialize_allocator()