14#ifndef BOTAN_CT_UTILS_H_
15#define BOTAN_CT_UTILS_H_
17#include <botan/concepts.h>
18#include <botan/secmem.h>
19#include <botan/internal/bit_ops.h>
20#include <botan/internal/stl_util.h>
26#if defined(BOTAN_HAS_VALGRIND)
27 #include <valgrind/memcheck.h>
53constexpr inline void poison(
const T* p,
size_t n) {
54#if defined(BOTAN_HAS_VALGRIND)
55 if(!std::is_constant_evaluated()) {
56 VALGRIND_MAKE_MEM_UNDEFINED(p, n *
sizeof(
T));
64constexpr inline void unpoison(
const T* p,
size_t n) {
65#if defined(BOTAN_HAS_VALGRIND)
66 if(!std::is_constant_evaluated()) {
67 VALGRIND_MAKE_MEM_DEFINED(p, n *
sizeof(
T));
83#if defined(BOTAN_HAS_VALGRIND)
84 return RUNNING_ON_VALGRIND;
98template <std::
integral T>
103template <std::
integral T>
111template <ranges::spanable_range R>
112 requires std::is_trivially_copyable_v<std::ranges::range_value_t<R>>
115 poison(s.data(), s.size());
118template <ranges::spanable_range R>
119 requires std::is_trivially_copyable_v<std::ranges::range_value_t<R>>
130 requires requires(
const T& x) { x._const_time_poison(); }
132 x._const_time_poison();
136 requires requires(
const T& x) { x._const_time_unpoison(); }
138 x._const_time_unpoison();
146constexpr void poison(
const std::optional<T>& x) {
154constexpr void unpoison(
const std::optional<T>& x) {
173template <std::ranges::range R>
176 for(
const auto& v : r) {
181template <std::ranges::range R>
182 requires unpoisonable<std::ranges::range_value_t<R>>
184 for(
const auto& v : r) {
193template <poisonable... Ts>
194 requires(
sizeof...(Ts) > 0)
199template <unpoisonable... Ts>
200 requires(
sizeof...(Ts) > 0)
214template <
typename... Ts>
215 requires(
sizeof...(Ts) > 0) && (poisonable<Ts> && ...) && (unpoisonable<Ts> && ...)
225template <poisonable T>
227 requires(std::is_rvalue_reference_v<
decltype(v)>)
230 return std::forward<T>(v);
236template <unpoisonable T>
238 requires(std::is_rvalue_reference_v<
decltype(v)>)
241 return std::forward<T>(v);
253 if(!std::is_constant_evaluated()) {
269#if defined(BOTAN_USE_GCC_INLINE_ASM) && !defined(BOTAN_HAS_SANITIZER_MEMORY)
270 asm(
"" :
"+r"(x) : );
290 template <
typename T>
291 requires std::unsigned_integral<T> && (!std::same_as<bool, T>)
330 constexpr bool as_bool()
const {
return m_value != 0; }
341 constexpr explicit Choice(uint32_t v) : m_value(v) {}
362 static_assert(std::is_unsigned<T>::value && !std::is_same<bool, T>::value,
363 "Only unsigned integer types are supported by CT::Mask");
371 template <
typename U>
373 static_assert(
sizeof(U) >
sizeof(
T),
"sizes ok");
395 if constexpr(
sizeof(
T) <=
sizeof(uint32_t)) {
420 template <
typename U>
422 static_assert(
sizeof(U) <
sizeof(
T),
"sizes ok");
443 T u = x ^ ((x ^ y) | ((x - y) ^ x));
465 const T v_lt_l = v ^ ((v ^ l) | ((v - l) ^ v));
466 const T v_gt_u = u ^ ((u ^ v) | ((u - v) ^ u));
474 for(
auto a : accepted) {
475 const T diff = a ^ v;
557 constexpr void select_n(
T output[],
const T x[],
const T y[],
size_t len)
const {
559 for(
size_t i = 0; i != len; ++i) {
560 output[i] =
choose(mask, x[i], y[i]);
568 for(
size_t i = 0; i != elems; ++i) {
609 constexpr Mask(
T m) : m_mask(m) {}
625 constexpr Option(
T v,
Choice valid) : m_has_value(valid), m_value(std::move(v)) {}
632 requires std::default_initializable<
T>
645 template <std::invocable<const T&> F>
647 return {f(m_value), m_has_value};
663 other.conditional_assign(m_has_value, m_value);
672 requires std::unsigned_integral<T>
675 return mask.select(m_value, other);
700 mask.
select_n(to, from0, from1, elems);
713 mask.select_n(sink, src, sink, elems);
720 mask.select_n(sink, src, sink, elems);
728 T t0 = swap.select(y, x);
729 T t1 = swap.select(x, y);
736 uintptr_t xp =
reinterpret_cast<uintptr_t
>(x);
737 uintptr_t yp =
reinterpret_cast<uintptr_t
>(y);
741 x =
reinterpret_cast<T>(xp);
742 y =
reinterpret_cast<T>(yp);
748 for(
size_t i = 0; i != len; ++i) {
760 if(std::is_constant_evaluated()) {
763 for(
size_t i = 0; i != len; ++i) {
764 difference = difference | (x[i] ^ y[i]);
769 volatile T difference = 0;
771 for(
size_t i = 0; i != len; ++i) {
772 difference = difference | (x[i] ^ y[i]);
809 std::span<uint8_t> output,
810 std::span<const uint8_t> input,
#define BOTAN_STATE_CHECK(expr)
constexpr Choice operator==(const Choice &other) const
static constexpr Choice yes()
static constexpr Choice from_int(T v)
constexpr Choice & operator=(const Choice &other) noexcept=default
constexpr Choice(const Choice &other)=default
constexpr Choice operator!=(const Choice &other) const
constexpr Choice operator||(const Choice &other) const
constexpr Choice operator!() const
static constexpr Choice no()
constexpr bool as_bool() const
constexpr Choice(Choice &&other)=default
constexpr Choice operator&&(const Choice &other) const
constexpr uint32_t value() const
Return the masked value.
constexpr Choice & operator=(Choice &&other) noexcept=default
static constexpr Mask< T > expand_bit(T v, size_t bit)
Mask< T > select_mask(Mask< T > x, Mask< T > y) const
constexpr T value() const
constexpr void if_set_zero_out(T buf[], size_t elems)
static constexpr Mask< T > is_lte(T x, T y)
static constexpr Mask< T > is_gte(T x, T y)
constexpr T if_not_set_return(T x) const
constexpr Mask(Mask< U > o)
static constexpr Mask< T > set()
friend Mask< T > operator|(Mask< T > x, Mask< T > y)
Mask< T > & operator^=(Mask< T > o)
constexpr T unpoisoned_value() const
constexpr void select_n(T output[], const T x[], const T y[], size_t len) const
friend Mask< T > operator^(Mask< T > x, Mask< T > y)
constexpr T if_set_return(T x) const
Mask< T > & operator=(const Mask< T > &other)=default
constexpr void _const_time_poison() const
Mask(const Mask< T > &other)=default
static constexpr Mask< T > expand(Mask< U > m)
Mask< T > & operator&=(Mask< T > o)
constexpr T select_and_unpoison(T x, T y) const
static constexpr Mask< T > expand(T v)
constexpr T select(T x, T y) const
static constexpr Mask< T > expand_top_bit(T v)
static constexpr Mask< T > is_within_range(T v, T l, T u)
constexpr CT::Choice as_choice() const
static constexpr Mask< T > from_choice(Choice c)
static constexpr Mask< T > is_equal(T x, T y)
Mask< T > & operator|=(Mask< T > o)
constexpr Mask< T > operator~() const
static constexpr Mask< T > is_gt(T x, T y)
constexpr bool as_bool() const
static constexpr Mask< T > is_lt(T x, T y)
friend Mask< T > operator&(Mask< T > x, Mask< T > y)
constexpr void _const_time_unpoison() const
static constexpr Mask< T > is_any_of(T v, std::initializer_list< T > accepted)
static constexpr Mask< T > is_zero(T x)
static constexpr Mask< T > cleared()
constexpr Option(T v)
Construct a set option with the provided value.
constexpr Option(T v, Choice valid)
Construct an Option which contains the specified value, and is set or not.
constexpr auto transform(F f) const -> Option< std::remove_cvref_t< std::invoke_result_t< F, const T & > > >
constexpr Choice has_value() const
Return true if this Option contains a value.
constexpr CT::Option< T > operator&&(CT::Choice also)
Return a new CT::Option that is set if also is set as well.
constexpr const T & value() const
Either returns the value or throws an exception.
constexpr Option()
Construct an unset option with a default inner value.
constexpr std::optional< T > as_optional_vartime() const
constexpr T value_or(T other) const
constexpr T value_or(T other) const
Helper class to create a RAII-style cleanup callback.
int(* final)(unsigned char *, CTX *)
constexpr T value_barrier(T x)
secure_vector< uint8_t > strip_leading_zeros(std::span< const uint8_t > input)
constexpr void conditional_swap_ptr(bool cnd, T &x, T &y)
decltype(auto) driveby_unpoison(T &&v)
constexpr void conditional_swap(bool cnd, T &x, T &y)
decltype(auto) driveby_poison(T &&v)
constexpr void unpoison_all(Ts &&... ts)
constexpr Mask< T > conditional_assign_mem(T cnd, T *sink, const T *src, size_t elems)
constexpr auto scoped_poison(const Ts &... xs)
constexpr Mask< T > conditional_copy_mem(Mask< T > mask, T *to, const T *from0, const T *from1, size_t elems)
constexpr void unpoison_range(R &&r)
constexpr void poison_range(R &&r)
constexpr CT::Mask< T > is_not_equal(const T x[], const T y[], size_t len)
constexpr void poison_all(Ts &&... ts)
BOTAN_TEST_API CT::Option< size_t > copy_output(CT::Choice accept, std::span< uint8_t > output, std::span< const uint8_t > input, size_t offset)
size_t count_leading_zero_bytes(std::span< const uint8_t > input)
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
constexpr void unpoison(const T *p, size_t n)
constexpr CT::Mask< T > all_zeros(const T elem[], size_t len)
constexpr void poison(const T *p, size_t n)
constexpr T choose(T mask, T a, T b)
std::vector< T, secure_allocator< T > > secure_vector
constexpr T ct_is_zero(T x)
constexpr T expand_top_bit(T a)