8#ifndef BOTAN_MEMORY_OPS_H_
9#define BOTAN_MEMORY_OPS_H_
11#include <botan/concepts.h>
12#include <botan/types.h>
27#if !defined(BOTAN_IS_BEING_BUILT)
28 #include <botan/allocator.h>
61#if !defined(BOTAN_IS_BEGIN_BUILT)
105 std::memset(ptr, 0, bytes);
131template <ranges::contiguous_output_range R>
133 requires std::is_trivially_copyable_v<std::ranges::range_value_t<R>>
145 requires std::is_trivial<typename std::decay<T>::type>::value
146inline constexpr void copy_mem(
T* out,
const T* in,
size_t n) {
149 if(in !=
nullptr && out !=
nullptr && n > 0) {
150 std::memmove(out, in,
sizeof(
T) * n);
159template <ranges::contiguous_output_range OutR, ranges::contiguous_range InR>
160 requires std::is_same_v<std::ranges::range_value_t<OutR>, std::ranges::range_value_t<InR>> &&
161 std::is_trivially_copyable_v<std::ranges::range_value_t<InR>>
162inline constexpr void copy_mem(OutR&& out, InR&& in) {
173template <ranges::contiguous_output_range ToR, ranges::contiguous_range FromR>
174 requires std::is_trivially_copyable_v<std::ranges::range_value_t<FromR>> &&
175 std::is_trivially_copyable_v<std::ranges::range_value_t<ToR>>
185template <
typename ToT, ranges::contiguous_range FromR>
186 requires std::is_trivially_copyable_v<std::ranges::range_value_t<FromR>> && std::is_trivially_copyable_v<ToT> &&
187 (!std::ranges::range<ToT>)
196template <ranges::contiguous_output_range ToR,
typename FromT>
197 requires std::is_trivially_copyable_v<FromT> &&
198 (!std::ranges::range<FromT>) && std::is_trivially_copyable_v<std::ranges::range_value_t<ToR>>
207template <
typename ToT, ranges::contiguous_range FromR>
208 requires std::is_trivial_v<ToT> && std::is_trivially_copyable_v<std::ranges::range_value_t<FromR>>
218 requires std::is_trivially_copyable<T>::value
221 typecast_copy(std::span<uint8_t>(out,
sizeof(
T) * N), std::span<const T>(in, N));
227 requires std::is_trivial<T>::value
230 typecast_copy(std::span<T>(out, N), std::span<const uint8_t>(in, N *
sizeof(
T)));
242 requires std::is_trivial<typename std::decay<T>::type>::value
245 typecast_copy(out, std::span<
const uint8_t,
sizeof(
T)>(in,
sizeof(
T)));
249template <
typename To>
250 requires std::is_trivial<To>::value
253 return typecast_copy<To>(std::span<
const uint8_t,
sizeof(To)>(src,
sizeof(To)));
256#if !defined(BOTAN_IS_BEGIN_BUILT)
265inline constexpr
void set_mem(uint8_t* ptr,
size_t n, uint8_t val) {
267 std::memset(ptr, val, n);
273 return reinterpret_cast<const uint8_t*
>(s);
277 return reinterpret_cast<const char*
>(b);
281 return reinterpret_cast<uint8_t*
>(s);
285 return reinterpret_cast<char*
>(b);
288#if !defined(BOTAN_IS_BEING_BUILT)
299 volatile T difference = 0;
301 for(
size_t i = 0; i != n; ++i) {
302 difference = difference | (p1[i] ^ p2[i]);
305 return difference == 0;
309#if !defined(BOTAN_IS_BEING_BUILT)
311template <
typename T,
typename Alloc>
313size_t
buffer_insert(std::vector<
T, Alloc>& buf,
size_t buf_offset, const
T input[],
size_t input_length) {
315 const size_t to_copy = std::min(input_length, buf.size() - buf_offset);
317 copy_mem(&buf[buf_offset], input, to_copy);
322template <
typename T,
typename Alloc,
typename Alloc2>
324size_t
buffer_insert(std::vector<
T, Alloc>& buf,
size_t buf_offset, const std::vector<
T, Alloc2>& input) {
326 const size_t to_copy = std::min(input.size(), buf.size() - buf_offset);
328 copy_mem(&buf[buf_offset], input.data(), to_copy);
347 for(; o.size_bytes() >= 32; o = o.subspan(32), i = i.subspan(32)) {
348 auto x = typecast_copy<std::array<uint64_t, 4>>(o.template first<32>());
349 const auto y = typecast_copy<std::array<uint64_t, 4>>(i.template first<32>());
359 for(
size_t off = 0; off != o.size_bytes(); ++off) {
379 for(; o.size_bytes() >= 32; o = o.subspan(32), i1 = i1.subspan(32), i2 = i2.subspan(32)) {
380 auto x = typecast_copy<std::array<uint64_t, 4>>(i1.template first<32>());
381 const auto y = typecast_copy<std::array<uint64_t, 4>>(i2.template first<32>());
391 for(
size_t off = 0; off != o.size_bytes(); ++off) {
392 o[off] = i1[off] ^ i2[off];
402inline void xor_buf(uint8_t out[],
const uint8_t in[],
size_t length) {
404 xor_buf(std::span{out, length}, std::span{in, length});
414inline void xor_buf(uint8_t out[],
const uint8_t in[],
const uint8_t in2[],
size_t length) {
416 xor_buf(std::span{out, length}, std::span{in, length}, std::span{in2, length});
420inline void xor_buf(std::span<uint8_t> out, std::span<const uint8_t> in,
size_t n) {
423 xor_buf(out.first(n), in.first(n));
427template <
typename Alloc>
428void xor_buf(std::vector<uint8_t, Alloc>& out,
const uint8_t* in,
size_t n) {
431 xor_buf(std::span{out}.first(n), std::span{in, n});
435template <
typename Alloc,
typename Alloc2>
436void xor_buf(std::vector<uint8_t, Alloc>& out,
const uint8_t* in,
const std::vector<uint8_t, Alloc2>& in2,
size_t n) {
440 xor_buf(std::span{out}.first(n), std::span{in, n}, std::span{in2}.first(n));
443template <
typename Alloc,
typename Alloc2>
444std::vector<uint8_t, Alloc>&
operator^=(std::vector<uint8_t, Alloc>& out,
const std::vector<uint8_t, Alloc2>& in) {
445 if(out.size() < in.size()) {
446 out.resize(in.size());
449 xor_buf(std::span{out}.first(in.size()), in);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg)
#define BOTAN_ARG_CHECK(expr, msg)
#define BOTAN_PUBLIC_API(maj, min)
#define BOTAN_DEPRECATED(msg)
constexpr size_t size_bytes(spanable_range auto &&r)
constexpr void assert_equal_byte_lengths(R0 &&r0, Rs &&... rs)
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
constexpr void typecast_copy(ToR &&out, FromR &&in)
constexpr void set_mem(uint8_t *ptr, size_t n, uint8_t val)
constexpr void clear_bytes(void *ptr, size_t bytes)
void secure_scrub_memory(void *ptr, size_t n)
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)
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
bool same_mem(const T *p1, const T *p2, size_t n)
const char * cast_uint8_ptr_to_char(const uint8_t *b)
bool constant_time_compare(std::span< const uint8_t > x, std::span< const uint8_t > y)
constexpr void copy_mem(T *out, const T *in, size_t n)
constexpr void clear_mem(T *ptr, size_t n)
const uint8_t * cast_char_ptr_to_uint8(const char *s)