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) {
164 if(std::is_constant_evaluated()) {
165 std::copy(std::ranges::begin(in), std::ranges::end(in), std::ranges::begin(out));
175template <ranges::contiguous_output_range ToR, ranges::contiguous_range FromR>
176 requires std::is_trivially_copyable_v<std::ranges::range_value_t<FromR>> &&
177 std::is_trivially_copyable_v<std::ranges::range_value_t<ToR>>
187template <
typename ToT, ranges::contiguous_range FromR>
188 requires std::is_trivially_copyable_v<std::ranges::range_value_t<FromR>> && std::is_trivially_copyable_v<ToT> &&
189 (!std::ranges::range<ToT>)
198template <ranges::contiguous_output_range ToR,
typename FromT>
199 requires std::is_trivially_copyable_v<FromT> &&
200 (!std::ranges::range<FromT>) && std::is_trivially_copyable_v<std::ranges::range_value_t<ToR>>
209template <
typename ToT, ranges::contiguous_range FromR>
210 requires std::is_default_constructible_v<ToT> && std::is_trivially_copyable_v<ToT> &&
211 std::is_trivially_copyable_v<std::ranges::range_value_t<FromR>>
221 requires std::is_trivially_copyable<T>::value
224 typecast_copy(std::span<uint8_t>(out,
sizeof(
T) * N), std::span<const T>(in, N));
230 requires std::is_trivial<T>::value
233 typecast_copy(std::span<T>(out, N), std::span<const uint8_t>(in, N *
sizeof(
T)));
245 requires std::is_trivial<typename std::decay<T>::type>::value
248 typecast_copy(out, std::span<
const uint8_t,
sizeof(
T)>(in,
sizeof(
T)));
252template <
typename To>
253 requires std::is_trivial<To>::value
259#if !defined(BOTAN_IS_BEGIN_BUILT)
268 std::memset(ptr, val, n);
274 return reinterpret_cast<const uint8_t*
>(s);
278 return reinterpret_cast<const char*
>(
b);
282 return reinterpret_cast<uint8_t*
>(s);
286 return reinterpret_cast<char*
>(
b);
289#if !defined(BOTAN_IS_BEING_BUILT)
300 volatile T difference = 0;
302 for(
size_t i = 0; i != n; ++i) {
303 difference = difference | (p1[i] ^ p2[i]);
306 return difference == 0;
310#if !defined(BOTAN_IS_BEING_BUILT)
312template <
typename T,
typename Alloc>
314size_t
buffer_insert(std::vector<
T, Alloc>& buf,
size_t buf_offset, const
T input[],
size_t input_length) {
316 const size_t to_copy = std::min(input_length, buf.size() - buf_offset);
318 copy_mem(&buf[buf_offset], input, to_copy);
323template <
typename T,
typename Alloc,
typename Alloc2>
325size_t
buffer_insert(std::vector<
T, Alloc>& buf,
size_t buf_offset, const std::vector<
T, Alloc2>& input) {
327 const size_t to_copy = std::min(input.size(), buf.size() - buf_offset);
329 copy_mem(&buf[buf_offset], input.data(), to_copy);
345 std::span<uint8_t> o(out);
346 std::span<const uint8_t> i(in);
348 for(; o.size_bytes() >= 32; o = o.subspan(32), i = i.subspan(32)) {
360 for(
size_t off = 0; off != o.size_bytes(); ++off) {
380 for(; o.size_bytes() >= 32; o = o.subspan(32), i1 = i1.subspan(32), i2 = i2.subspan(32)) {
392 for(
size_t off = 0; off != o.size_bytes(); ++off) {
393 o[off] = i1[off] ^ i2[off];
403inline void xor_buf(uint8_t out[],
const uint8_t in[],
size_t length) {
405 xor_buf(std::span{out, length}, std::span{in, length});
415inline void xor_buf(uint8_t out[],
const uint8_t in[],
const uint8_t in2[],
size_t length) {
417 xor_buf(std::span{out, length}, std::span{in, length}, std::span{in2, length});
421inline void xor_buf(std::span<uint8_t> out, std::span<const uint8_t> in,
size_t n) {
424 xor_buf(out.first(n), in.first(n));
428template <
typename Alloc>
429void xor_buf(std::vector<uint8_t, Alloc>& out,
const uint8_t* in,
size_t n) {
432 xor_buf(std::span{out}.first(n), std::span{in, n});
436template <
typename Alloc,
typename Alloc2>
437void xor_buf(std::vector<uint8_t, Alloc>& out,
const uint8_t* in,
const std::vector<uint8_t, Alloc2>& in2,
size_t n) {
441 xor_buf(std::span{out}.first(n), std::span{in, n}, std::span{in2}.first(n));
444template <
typename Alloc,
typename Alloc2>
445std::vector<uint8_t, Alloc>&
operator^=(std::vector<uint8_t, Alloc>& out,
const std::vector<uint8_t, Alloc2>& in) {
446 if(out.size() < in.size()) {
447 out.resize(in.size());
450 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)